private void StartGame() { TicTacToeScreen theAwesomeGame = new TicTacToeScreen(); // ScreenManager.AddScreen(theAwesomeGame); // ScreenManager.Screens[ScreenManager.Screens.Count() - 1].LoadContent(); IsExiting = true; LoadingScreen loadingScreen = new LoadingScreen(new GameScreen[] { theAwesomeGame }, true, ScreenManager); }
/// <summary> /// Allows the game component to update itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> public override void Update(bool coveredByOtherScreen, bool otherScreenHasFocus, GameTime gameTime) { if (!HideMe) { base.Update(coveredByOtherScreen, otherScreenHasFocus, gameTime); positionMapper = null; TimeSpan timeFromWin = TimeSpan.Zero; // Detech which key was pressed // and determine which box to highlight int i = 0, j = 0; KeyboardState newKeyboardState = Keyboard.GetState(); if (!gameOver) { if (keyboardState != null) { // Create the mapper only if // a key was pressed // Drawback: mapper is generated regardless if user // presses key into an already marked box. // Furthermore, create the mapper only if it's null, // and the mapper is only made null after the humanMachine has made // a move. if (keyboardState.GetPressedKeys() != null) { // The player pressed a Key. Now is the time to // generate the mapping to determine the position the user // pressed, to pass to the game's AI module, or named as // the HumanMachine! positionMapper = GenerateMapping(); // Left and right key presses if (keyboardState.IsKeyDown(Keys.Right) && newKeyboardState.IsKeyUp(Keys.Right)) { ScreenManager.PlaySound("menuMouseClick"); userIndex++; int limiter = 3; if (userIndex >= 3 && userIndex <= 5) limiter = 6; if (userIndex >= 6 && userIndex <= 8) limiter = 9; userIndex %= limiter; } else if (keyboardState.IsKeyDown(Keys.Left) && newKeyboardState.IsKeyUp(Keys.Left)) { ScreenManager.PlaySound("menuMouseClick"); userIndex--; if (userIndex < 0) userIndex += 3; } // Down Key Presses if (keyboardState.IsKeyDown(Keys.Down) && newKeyboardState.IsKeyUp(Keys.Down)) { ScreenManager.PlaySound("menuMouseClick"); userIndex += 3; userIndex %= 9; } // Up Key presses if (keyboardState.IsKeyDown(Keys.Up) && newKeyboardState.IsKeyUp(Keys.Up)) { ScreenManager.PlaySound("menuMouseClick"); userIndex -= 3; if (userIndex < 0) userIndex += 3; } else if (keyboardState.IsKeyDown(Keys.Enter) && newKeyboardState.IsKeyUp(Keys.Enter)) { ScreenManager.PlaySound("boardClick"); if (!machineIsThinking) { markPlayerPosition = true; } // characterToMark = 'O'; System.Diagnostics.Debug.WriteLine("Pressed O"); } // Additional functionality to add -PAUSE SCREEN /* else if (keyboardState.IsKeyDown(Keys.Escape)) { pauseScreen = new PauseScreen(); pauseScreen.ScreenManager = ScreenManager; ScreenManager.Screens.Add(pauseScreen); ScreenManager.Screens[ScreenManager.Screens.Count - 1].LoadContent(); HideMe = true; } */ } } checkWin = board.CheckWin(out strikeDecision, out winningCharacter); if (checkWin != -1) { // This means the player has won the game. Therefore, // do not bother to obtain the HumanMachine's decision // since he has lost! // Enable drawing a strike. Do not // check for the logic in the Draw method. gameOver = true; if (!gameOverCue.IsPlaying && !gameOverCue.IsStopped && !gameOverCue.IsStopping) { if (ScreenManager.playSound) gameOverCue.Play(); } drawStrike = true; } // Calls update for every box in the board if (markPlayerPosition && !machineIsThinking && !gameOver) { // Player marked an option. // Get the machine's move. // Only enter this block of code if the Thread // has ended. Do not enter while it is sleeping. if (machineDecisionThread.ThreadState != System.Threading.ThreadState.Running || machineDecisionThread.ThreadState != System.Threading.ThreadState.WaitSleepJoin) { machineDecisionThread.Start(); machineDecisionThread = new Thread(MachineDecisionThreadDelegate); } // Now reinitalize the thread, since it is dead, and is required // next time the user makes a move! } Debug.WriteLine("The human Machune character is: {0}, and {1}", characterToMark, humanMachine.Character); } else { // end the machineDecision Thread if it is running if (machineDecisionThread.IsAlive) { machineDecisionThread.Join(); } // now check if user pressed 'R' to restart the game! if (keyboardState.IsKeyDown(Keys.R) && newKeyboardState.IsKeyUp(Keys.R) && gameOver) { // end the game LoadingScreen screen = new LoadingScreen(new GameScreen[] { new BackgroundScreen(), new ChooseCharacterScreen() }, true, ScreenManager); } else if (keyboardState.IsKeyDown(Keys.Q) && newKeyboardState.IsKeyUp(Keys.Q) && gameOver) { // end the game LoadingScreen screen = new LoadingScreen(new GameScreen[] { new BackgroundScreen(), new StartMenuScreen() }, true, ScreenManager); } if (drawStrike == false) { // this means the game is a draw } } keyboardState = newKeyboardState; board.Update(gameTime); } else { Debug.WriteLine("do nothing, since HideMe = {0}", HideMe); } }