public MainWindow() { InitializeComponent(); // track the squres squares = new Rectangle[AcquireConstants.BoardHeight, AcquireConstants.BoardWidth]; for (int dim0 = 0; dim0 < AcquireConstants.BoardHeight; dim0++) { for (int dim1 = 0; dim1 < AcquireConstants.BoardWidth; dim1++) { squares[dim0, dim1] = (Rectangle)this.FindName("rectangle" + ((dim1 + 1) + (dim0 * 12)).ToString()); if (null == squares[dim0, dim1]) { throw new NullReferenceException("Square " + dim0 + " x " + dim1 + " is null!"); } } } // init data computers = new Dictionary <int, IComputer>(); lastComputer = null; lastState = AcquireGameStates.Done; cycleQueue = new List <string>(); oneThread = 0; playerNames = new Dictionary <string, Oppenents>(); playerNames.Add("Human", Oppenents.Human); playerNames.Add("Computer Easy", Oppenents.Random); playerNames.Add("Computer Hard", Oppenents.Computer2); playerNames.Add("Computer Very Hard", Oppenents.Computer3); game = null; GetItem <Label>(Laddmsg).Content = ""; // fix up the bad looking red block repeatAgain = false; lifetime = new Dictionary <int, WinStats>(); // initialize the startup screen for (int i = 1; i <= AcquireConstants.MaxPlayers; i++) { string basename = Lstartbase + i; GetItem <ComboBox>(basename, Lstartlist).Items.Clear(); foreach (string name in playerNames.Keys) { GetItem <ComboBox>(basename, Lstartlist).Items.Add(name); } // set the defaults if (i == 1) { GetItem <ComboBox>(basename, Lstartlist).SelectedIndex = 0; } if (i == 2) { GetItem <ComboBox>(basename, Lstartlist).SelectedIndex = 1; } } // start the AI thread aiTimer = new DispatcherTimer(); aiTimer.Interval = new TimeSpan(0, 0, 0, 0, 100); aiTimer.Tick += new EventHandler(AdvanceAI_Callback); aiTimer.Start(); }
// manipulation private void StartGame() { int id; List <string[]> players; // grab the player names and algorithms for each player players = new List <string[]>(); for (int i = 1; i <= AcquireConstants.MaxPlayers; i++) { string basename = Lstartbase + i; if (null != GetItem <ComboBox>(basename, Lstartlist).SelectedItem) { string[] npair = new string[2]; npair[0] = (string)GetItem <ComboBox>(basename, Lstartlist).SelectedItem; npair[1] = (string)GetItem <TextBox>(basename, Lstarttext).Text; players.Add(npair); } } if (players.Count < 2) { GetItem <TextBlock>(Lstartstatus).Text = (string)GetItem <Label>(Lstarterr).Content; return; } // start Acquire game engine game = new AcquireGame(); // init game state computers.Clear(); cycleQueue.Clear(); // add the players foreach (string[] npairs in players) { if (npairs.Length != 2) { throw new ArgumentException("Wrong size of the internal data structure"); } if (!playerNames.ContainsKey(npairs[0])) { throw new ArgumentException("Missing a player implementation"); } id = game.AddPlayer(npairs[1], playerNames[npairs[0]] != Oppenents.Human); switch (playerNames[npairs[0]]) { case Oppenents.Random: computers.Add(id, new Computer1()); break; case Oppenents.Computer2: computers.Add(id, new Computer2()); break; case Oppenents.Computer3: computers.Add(id, new Computer3()); break; } } // hide the UI StartGrid.Visibility = System.Windows.Visibility.Collapsed; FinalDisplayGrid.Visibility = System.Windows.Visibility.Collapsed; if (lifetime.Count > 0 && (computers.Count == game.Players.Count)) { // only display if there are computer players DisplayLifetime(); } // start the game game.StartGame(); // update the UI Refresh(game.CurrentPlayer); }