// Handles when the user touches a square on the board. public async void HandleSquareTouched(Object obj) { // Get the col, row from the CommandParameter of the Command string parameter = obj as string; int value = int.Parse(parameter); int col = value / 10; int row = value % 10; // If the game is over, don't do anything if (game.State != Game.GameState.Playing) { return; } // Interact with the Game object // Is the square empty? if (game.canMakeMove(col, row)) { images[col, row].Source = game.CurrentPlayer == Game.PlayerX ? "x.png" : "o.png"; dingPlayer.Play(); Game.GameState state = game.makeMove(col, row); switch (state) { // We're still playing case Game.GameState.Playing: setMessage(game.CurrentPlayer == Game.PlayerX ? xsturn : osturn); break; // This move won the game case Game.GameState.WinCol0: case Game.GameState.WinCol1: case Game.GameState.WinCol2: case Game.GameState.WinRow0: case Game.GameState.WinRow1: case Game.GameState.WinRow2: case Game.GameState.WinDiagLTR: case Game.GameState.WinDiagRTL: setMessage(game.CurrentPlayer == Game.PlayerX ? xwins : owins); getImagesForWinner(state); SoundService.Instance.PlaySound(Constants.winsound); await animateWinningSquares(); break; case Game.GameState.Draw: setMessage(draw); // Play a sound to let them know something happened SoundService.Instance.PlaySound(Constants.drawsound); break; } } else { // The square wasn't empty. Play an annoying sound. SoundService.Instance.PlaySound(Constants.badsound); } }
// Given the winning row, column. or diagonal returned by the // Game object, fill an array with the images for those squares. // Note; squares is an instance object that keep getting reused. Image[] getImagesForWinner(Game.GameState state) { switch (state) { case Game.GameState.WinCol0: winningsquares[0] = image00; winningsquares[1] = image01; winningsquares[2] = image02; break; case Game.GameState.WinCol1: winningsquares[0] = image10; winningsquares[1] = image11; winningsquares[2] = image12; break; case Game.GameState.WinCol2: winningsquares[0] = image20; winningsquares[1] = image21; winningsquares[2] = image22; break; case Game.GameState.WinRow0: winningsquares[0] = image00; winningsquares[1] = image10; winningsquares[2] = image20; break; case Game.GameState.WinRow1: winningsquares[0] = image01; winningsquares[1] = image11; winningsquares[2] = image21; break; case Game.GameState.WinRow2: winningsquares[0] = image02; winningsquares[1] = image12; winningsquares[2] = image22; break; case Game.GameState.WinDiagLTR: winningsquares[0] = image00; winningsquares[1] = image11; winningsquares[2] = image22; break; case Game.GameState.WinDiagRTL: winningsquares[0] = image02; winningsquares[1] = image11; winningsquares[2] = image20; break; } return(winningsquares); }