From a6c3d5ddca420cfa5c0182306e3366c9ef7c05ea Mon Sep 17 00:00:00 2001 From: Mathieu GROUSSEAU Date: Wed, 11 Jun 2025 08:38:06 +0200 Subject: [PATCH] Checked number bounds & localized default player name --- App/App/Localizable.xcstrings | 54 +++++++++++++++++++++++++++++++ App/App/View/NewGameView.swift | 25 +++++++++----- App/App/ViewModel/NewGameVM.swift | 10 +++--- 3 files changed, 77 insertions(+), 12 deletions(-) diff --git a/App/App/Localizable.xcstrings b/App/App/Localizable.xcstrings index 32f2ae7..3a6936c 100644 --- a/App/App/Localizable.xcstrings +++ b/App/App/Localizable.xcstrings @@ -71,6 +71,23 @@ } } }, + "generic.player.type.aiFinnishHim.name" : { + "extractionState" : "manual", + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "Finish Him" + } + }, + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Finish Him" + } + } + } + }, "generic.player.type.aiRandom" : { "extractionState" : "manual", "localizations" : { @@ -88,6 +105,23 @@ } } }, + "generic.player.type.aiRandom.name" : { + "extractionState" : "manual", + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "Random" + } + }, + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Aléatoire" + } + } + } + }, "generic.player.type.aiSimpleNegaMax" : { "extractionState" : "manual", "localizations" : { @@ -105,6 +139,23 @@ } } }, + "generic.player.type.aiSimpleNegaMax.name" : { + "extractionState" : "manual", + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "NegaMax" + } + }, + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "NegaMax" + } + } + } + }, "generic.player.type.human" : { "extractionState" : "manual", "localizations" : { @@ -188,6 +239,9 @@ } } } + }, + "inGame.title" : { + }, "mainMenu.button.newGame" : { "extractionState" : "manual", diff --git a/App/App/View/NewGameView.swift b/App/App/View/NewGameView.swift index b171e24..159af77 100644 --- a/App/App/View/NewGameView.swift +++ b/App/App/View/NewGameView.swift @@ -49,8 +49,8 @@ struct NewGameView: View { } - Button("newGame.play", systemImage: "play") { - // TODO: yes + NavigationLink(destination: IngameView(settings: vm, player1: p1, player2: p2)/*.navigationTitle("inGame.title")*/) { + Label("newGame.play", systemImage: "play") } } } @@ -66,17 +66,15 @@ private struct PlayerSectionView: View { var body: some View { Picker("newGame.player.type", selection: $settings.type) { - Text("generic.player.type.human").tag(PlayerType.Human) - Text("generic.player.type.aiRandom").tag(PlayerType.AIRandom) - Text("generic.player.type.aiFinnishHim").tag(PlayerType.AIFinnishHim) - Text("generic.player.type.aiSimpleNegaMax").tag(PlayerType.AISimpleNegaMax) + ForEach(PlayerType.allCases) { + Text(LocalizedStringKey($0.baseTranslationKey)).tag($0) + } } let binding: Binding = if (settings.type == .Human) { $name } else { - // FIXME: make text field readonly - .constant("TODO constant name") + .constant(String(localized: String.LocalizationValue("\(settings.type.baseTranslationKey).name" as String))) } TextField("newGame.player.name", text: binding) // TODO: MacOS @@ -97,6 +95,17 @@ private struct PlayerSectionView: View { } } +extension PlayerType { + var baseTranslationKey: String { + return switch (self) { + case .Human: "generic.player.type.human" + case .AIRandom: "generic.player.type.aiRandom" + case .AIFinnishHim: "generic.player.type.aiFinnishHim" + case .AISimpleNegaMax: "generic.player.type.aiSimpleNegaMax" + } + } +} + #Preview { NewGameView() } diff --git a/App/App/ViewModel/NewGameVM.swift b/App/App/ViewModel/NewGameVM.swift index a8d2b5c..11ead28 100644 --- a/App/App/ViewModel/NewGameVM.swift +++ b/App/App/ViewModel/NewGameVM.swift @@ -2,9 +2,9 @@ import Foundation class NewGameVM : ObservableObject { @Published var rulesType: RulesType = .Classic - @Published var width: UInt = 7 - @Published var height: UInt = 7 - @Published var alignedTokens: UInt = 4 + @Published var width: UInt = 7 { didSet { if width < 1 { width = 1 } } } + @Published var height: UInt = 7 { didSet { if height < 1 { height = 1 } } } + @Published var alignedTokens: UInt = 4 { didSet { if alignedTokens < 2 { alignedTokens = 2 } } } } // Didn't manage to nest player settings under NewGameVM @@ -17,7 +17,9 @@ class PlayerSettingsVM : ObservableObject, Identifiable { } } -enum PlayerType { +enum PlayerType: CaseIterable, Identifiable { + var id: Self { self } + case Human, AIRandom, AIFinnishHim, AISimpleNegaMax }