public void Cell_Click(object sender, RoutedEventArgs e) { Button btn = sender as Button; --gameCounter; if (isPlayerOneGame) { // player one vs AI // player 1 will always be human player // player 2 will be AI if (gamePlayers[0].getPlayerTurn()) { // human player goes first int row = (int)btn.GetValue(Grid.RowProperty); int col = (int)btn.GetValue(Grid.ColumnProperty); ImageSource ImgSrc = playerOneGameStone; ImageBrush imgBrush = new ImageBrush(ImgSrc); btn.Background = imgBrush; btn.IsEnabled = false; gameBoard.updateBoard(row, col, 1); // update score board for player one Label_PlayerOneScore.Content = gameBoard.getPlayerOneScore(); gamePlayers[0].setScore(gameBoard.getPlayerOneScore()); gamePlayers[0].setPlayerTurn(false); gamePlayers[1].setPlayerTurn(true); if (gameCounter == 0) { if (gamePlayers[0].getScore() > gamePlayers[1].getScore()) { gameWinner = gamePlayers[0].getPlayerID(); gamePlayers[0].setWinner(true); gamePlayers[1].setWinner(false); } else if (gamePlayers[0].getScore() < gamePlayers[1].getScore()) { gameWinner = gamePlayers[1].getPlayerID(); gamePlayers[0].setWinner(false); gamePlayers[1].setWinner(true); } else { gameWinner = "Draw"; gamePlayers[0].setWinner(false); gamePlayers[1].setWinner(false); } GameOver = true; isGameOver(); } else { btn.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); } } else { // AI goes second AImove(); } } else { // two player game // player 1 will always be human player // player 2 will be SECOND polayer int row = (int)btn.GetValue(Grid.RowProperty); int col = (int)btn.GetValue(Grid.ColumnProperty); if (gamePlayers[0].getPlayerTurn()) { // human player goes first ImageSource ImgSrc = playerOneGameStone; ImageBrush imgBrush = new ImageBrush(ImgSrc); btn.Background = imgBrush; btn.IsEnabled = false; gameBoard.updateBoard(row, col, 1); // update score board for player one Label_PlayerOneScore.Content = gameBoard.getPlayerOneScore(); gamePlayers[0].setScore(gameBoard.getPlayerOneScore()); gamePlayers[0].setPlayerTurn(false); gamePlayers[1].setPlayerTurn(true); if (gameCounter == 0) { if (gamePlayers[0].getScore() > gamePlayers[1].getScore()) { gameWinner = gamePlayers[0].getPlayerID(); gamePlayers[0].setWinner(true); gamePlayers[1].setWinner(false); } else if (gamePlayers[0].getScore() < gamePlayers[1].getScore()) { gameWinner = gamePlayers[1].getPlayerID(); gamePlayers[0].setWinner(false); gamePlayers[1].setWinner(true); } else { gameWinner = "Draw"; gamePlayers[0].setWinner(false); gamePlayers[1].setWinner(false); } GameOver = true; isGameOver(); } } else { ImageSource ImgSrc = playerTwoGameStone; ImageBrush imgBrush = new ImageBrush(ImgSrc); btn.Background = imgBrush; btn.IsEnabled = false; gameBoard.updateBoard(row, col, 2); // update score board for player two Label_PlayerTwoScore.Content = gameBoard.getPlayerTwoScore(); gamePlayers[1].setScore(gameBoard.getPlayerTwoScore()); gamePlayers[1].setPlayerTurn(false); gamePlayers[0].setPlayerTurn(true); if (gameCounter == 0) { if (gamePlayers[0].getScore() > gamePlayers[1].getScore()) { gameWinner = gamePlayers[0].getPlayerID(); gamePlayers[0].setWinner(true); gamePlayers[1].setWinner(false); } else if (gamePlayers[0].getScore() < gamePlayers[1].getScore()) { gameWinner = gamePlayers[1].getPlayerID(); gamePlayers[0].setWinner(false); gamePlayers[1].setWinner(true); } else { gameWinner = "Draw"; gamePlayers[0].setWinner(false); gamePlayers[1].setWinner(false); } GameOver = true; isGameOver(); } } } }
/* *This module checks the board and returns the cell number for computer player to click. 1. Check the current status of the game board. 2. Calculate the change in score for offensive by checking computer player score. 3. Calculate the change in score for defensive by checking human player score. 4. Return the location if offensive or defensive is better. 5. If both score are same then use look ahead strategy. 6. Circular to check any score can be made with priority assigned. 7. Second block to stop any consecutive third moves. 8. Use down right, up right, down left, up left to block ahead moves. */ private int[] mediumMove() { int[] cell = { -1, -1 }; int next_x_offensive = -1; int next_y_offensive = -1; int next_x_defensive = -1; int next_y_defensive = -1; Board testBoard = new Board(); testBoard.setGameMatrix(gameBoard.getGameMatrix()); // offensive testBoard.setPlayerTwoScore(gameBoard.getPlayerTwoScore()); int trialScore = testBoard.getPlayerTwoScore(); int oldScore = testBoard.getPlayerTwoScore(); for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { if (testBoard.isFree(i, j)) { testBoard.updateBoard(i, j, 2); trialScore = testBoard.getPlayerTwoScore(); if (trialScore > oldScore) { next_x_offensive = i; next_y_offensive = j; j = 7; i = 7; Console.WriteLine("this is - OFFENSIVE "); Console.WriteLine("--------------------"); } else { testBoard.deleteLastMove(i, j); } } } } // defensive testBoard.setPlayerOneScore(gameBoard.getPlayerOneScore()); trialScore = testBoard.getPlayerOneScore(); oldScore = testBoard.getPlayerOneScore(); for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { if (testBoard.isFree(i, j)) { testBoard.updateBoard(i, j, 1); trialScore = testBoard.getPlayerOneScore(); if (trialScore > oldScore) { next_x_defensive = i; next_y_defensive = j; j = 7; i = 7; Console.WriteLine("this is - DEFENSIVE "); Console.WriteLine("--------------------"); } else { testBoard.deleteLastMove(i, j); } } } } if (next_x_offensive == -1 && next_x_defensive != -1) { cell[0] = next_x_defensive; cell[1] = next_y_defensive; } else if (next_x_offensive != -1 && next_x_defensive == -1) { cell[0] = next_x_offensive; cell[1] = next_y_offensive; } else if (next_x_offensive != -1 && next_x_defensive != -1) { cell[0] = next_x_offensive; cell[1] = next_y_offensive; } else if (next_x_offensive == -1 && next_x_defensive == -1) { int[] lastMove = gameBoard.getLastMove(); int lastMove_x = lastMove[0]; int lastMove_y = lastMove[1]; bool cellChanged = false; if (lastMove_x == -1 && lastMove_y == -1) { cell = easyMove(); } else { for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { if (lastMove_x == i && lastMove_y == j) { Console.WriteLine("this is - TACKLE @ " + i + j); Console.WriteLine("--------------------"); // circle if (j <= 4 && j >= 1 && i <= 4 && i >= 1) { // two consecutive move block if(gameBoard.getGameMatrix()[i,j]==1 && gameBoard.getGameMatrix()[i-1, j] == 1) { if (gameBoard.isFree(i + 1, j)) { cell[0] = i + 1; cell[1] = j; cellChanged = true; } } else if (gameBoard.getGameMatrix()[i, j] == 1 && gameBoard.getGameMatrix()[i + 1, j] == 1) { if (gameBoard.isFree(i - 1, j)) { cell[0] = i - 1; cell[1] = j; cellChanged = true; } } else if (gameBoard.getGameMatrix()[i, j] == 1 && gameBoard.getGameMatrix()[i , j-1] == 1) { if (gameBoard.isFree(i, j+1)) { cell[0] = i ; cell[1] = j+1; cellChanged = true; } } else if (gameBoard.getGameMatrix()[i, j] == 1 && gameBoard.getGameMatrix()[i , j+1] == 1) { if (gameBoard.isFree(i, j - 1)) { cell[0] = i; cell[1] = j - 1; cellChanged = true; } } else { cell = easyMove(); } if (!cellChanged) { if (gameBoard.isFree(i - 1, j)) { cell[0] = i - 1; cell[1] = j; } else if (gameBoard.isFree(i + 1, j)) { cell[0] = i + 1; cell[1] = j; } else if (gameBoard.isFree(i, j - 1)) { cell[0] = i; cell[1] = j - 1; } else if (gameBoard.isFree(i, j + 1)) { cell[0] = i; cell[1] = j + 1; } else if (gameBoard.isFree(i - 1, j - 1)) { cell[0] = i - 1; cell[1] = j - 1; } else if (gameBoard.isFree(i + 1, j - 1)) { cell[0] = i + 1; cell[1] = j - 1; } else if (gameBoard.isFree(i + 1, j + 1)) { cell[0] = i + 1; cell[1] = j + 1; } else if (gameBoard.isFree(i - 1, j + 1)) { cell[0] = i - 1; cell[1] = j + 1; } else { cell = easyMove(); } } } // down right else if (i >= 0 && i <= 4 && j >= 0 && j <= 4) { if (gameBoard.isFree(i, j + 1)) { cell[0] = i; cell[1] = j + 1; } else if (gameBoard.isFree(i + 1, j + 1)) { cell[0] = i + 1; cell[1] = j + 1; } else if (gameBoard.isFree(i + 1, j)) { cell[0] = i + 1; cell[1] = j; } else { cell = easyMove(); } } //up right else if (i <= 5 && i >= 1 && j >= 0 && j <= 4) { if (gameBoard.isFree(i, j + 1)) { cell[0] = i; cell[1] = j + 1; } else if (gameBoard.isFree(i - 1, j + 1)) { cell[0] = i - 1; cell[1] = j + 1; } else if (gameBoard.isFree(i - 1, j)) { cell[0] = i - 1; cell[1] = j; } else { cell = easyMove(); } } //down left else if (j <= 5 && j >= 1 && i >= 0 && i <= 4) { if (gameBoard.isFree(i, j - 1)) { cell[0] = i; cell[1] = j - 1; } else if (gameBoard.isFree(i + 1, j - 1)) { cell[0] = i + 1; cell[1] = j - 1; } else if (gameBoard.isFree(i + 1, j)) { cell[0] = i + 1; cell[1] = j; } else { cell = easyMove(); } } // up left else if (j <= 5 && j >= 1 && i <= 5 && i >= 1) { if (gameBoard.isFree(i, j - 1)) { cell[0] = i; cell[1] = j - 1; } else if (gameBoard.isFree(i - 1, j - 1)) { cell[0] = i - 1; cell[1] = j - 1; } else if (gameBoard.isFree(i - 1, j)) { cell[0] = i - 1; cell[1] = j; } else { cell = easyMove(); } } } } } } } return cell; }
/* * This module checks the board and returns the cell number for computer player to click. * 1. Check the current status of the game board. * 2. Calculate the change in score for offensive by checking computer player score. * 3. Calculate the change in score for defensive by checking human player score. * 4. Return the location if offensive or defensive is better. * 5. If both score are same then use look ahead strategy. * 6. Circular to check any score can be made with priority assigned. * 7. Second block to stop any consecutive third moves. * 8. Use down right, up right, down left, up left to block ahead moves. * */ private int[] mediumMove() { int[] cell = { -1, -1 }; int next_x_offensive = -1; int next_y_offensive = -1; int next_x_defensive = -1; int next_y_defensive = -1; Board testBoard = new Board(); testBoard.setGameMatrix(gameBoard.getGameMatrix()); // offensive testBoard.setPlayerTwoScore(gameBoard.getPlayerTwoScore()); int trialScore = testBoard.getPlayerTwoScore(); int oldScore = testBoard.getPlayerTwoScore(); for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { if (testBoard.isFree(i, j)) { testBoard.updateBoard(i, j, 2); trialScore = testBoard.getPlayerTwoScore(); if (trialScore > oldScore) { next_x_offensive = i; next_y_offensive = j; j = 7; i = 7; Console.WriteLine("this is - OFFENSIVE "); Console.WriteLine("--------------------"); } else { testBoard.deleteLastMove(i, j); } } } } // defensive testBoard.setPlayerOneScore(gameBoard.getPlayerOneScore()); trialScore = testBoard.getPlayerOneScore(); oldScore = testBoard.getPlayerOneScore(); for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { if (testBoard.isFree(i, j)) { testBoard.updateBoard(i, j, 1); trialScore = testBoard.getPlayerOneScore(); if (trialScore > oldScore) { next_x_defensive = i; next_y_defensive = j; j = 7; i = 7; Console.WriteLine("this is - DEFENSIVE "); Console.WriteLine("--------------------"); } else { testBoard.deleteLastMove(i, j); } } } } if (next_x_offensive == -1 && next_x_defensive != -1) { cell[0] = next_x_defensive; cell[1] = next_y_defensive; } else if (next_x_offensive != -1 && next_x_defensive == -1) { cell[0] = next_x_offensive; cell[1] = next_y_offensive; } else if (next_x_offensive != -1 && next_x_defensive != -1) { cell[0] = next_x_offensive; cell[1] = next_y_offensive; } else if (next_x_offensive == -1 && next_x_defensive == -1) { int[] lastMove = gameBoard.getLastMove(); int lastMove_x = lastMove[0]; int lastMove_y = lastMove[1]; bool cellChanged = false; if (lastMove_x == -1 && lastMove_y == -1) { cell = easyMove(); } else { for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { if (lastMove_x == i && lastMove_y == j) { Console.WriteLine("this is - TACKLE @ " + i + j); Console.WriteLine("--------------------"); // circle if (j <= 4 && j >= 1 && i <= 4 && i >= 1) { // two consecutive move block if (gameBoard.getGameMatrix()[i, j] == 1 && gameBoard.getGameMatrix()[i - 1, j] == 1) { if (gameBoard.isFree(i + 1, j)) { cell[0] = i + 1; cell[1] = j; cellChanged = true; } } else if (gameBoard.getGameMatrix()[i, j] == 1 && gameBoard.getGameMatrix()[i + 1, j] == 1) { if (gameBoard.isFree(i - 1, j)) { cell[0] = i - 1; cell[1] = j; cellChanged = true; } } else if (gameBoard.getGameMatrix()[i, j] == 1 && gameBoard.getGameMatrix()[i, j - 1] == 1) { if (gameBoard.isFree(i, j + 1)) { cell[0] = i; cell[1] = j + 1; cellChanged = true; } } else if (gameBoard.getGameMatrix()[i, j] == 1 && gameBoard.getGameMatrix()[i, j + 1] == 1) { if (gameBoard.isFree(i, j - 1)) { cell[0] = i; cell[1] = j - 1; cellChanged = true; } } else { cell = easyMove(); } if (!cellChanged) { if (gameBoard.isFree(i - 1, j)) { cell[0] = i - 1; cell[1] = j; } else if (gameBoard.isFree(i + 1, j)) { cell[0] = i + 1; cell[1] = j; } else if (gameBoard.isFree(i, j - 1)) { cell[0] = i; cell[1] = j - 1; } else if (gameBoard.isFree(i, j + 1)) { cell[0] = i; cell[1] = j + 1; } else if (gameBoard.isFree(i - 1, j - 1)) { cell[0] = i - 1; cell[1] = j - 1; } else if (gameBoard.isFree(i + 1, j - 1)) { cell[0] = i + 1; cell[1] = j - 1; } else if (gameBoard.isFree(i + 1, j + 1)) { cell[0] = i + 1; cell[1] = j + 1; } else if (gameBoard.isFree(i - 1, j + 1)) { cell[0] = i - 1; cell[1] = j + 1; } else { cell = easyMove(); } } } // down right else if (i >= 0 && i <= 4 && j >= 0 && j <= 4) { if (gameBoard.isFree(i, j + 1)) { cell[0] = i; cell[1] = j + 1; } else if (gameBoard.isFree(i + 1, j + 1)) { cell[0] = i + 1; cell[1] = j + 1; } else if (gameBoard.isFree(i + 1, j)) { cell[0] = i + 1; cell[1] = j; } else { cell = easyMove(); } } //up right else if (i <= 5 && i >= 1 && j >= 0 && j <= 4) { if (gameBoard.isFree(i, j + 1)) { cell[0] = i; cell[1] = j + 1; } else if (gameBoard.isFree(i - 1, j + 1)) { cell[0] = i - 1; cell[1] = j + 1; } else if (gameBoard.isFree(i - 1, j)) { cell[0] = i - 1; cell[1] = j; } else { cell = easyMove(); } } //down left else if (j <= 5 && j >= 1 && i >= 0 && i <= 4) { if (gameBoard.isFree(i, j - 1)) { cell[0] = i; cell[1] = j - 1; } else if (gameBoard.isFree(i + 1, j - 1)) { cell[0] = i + 1; cell[1] = j - 1; } else if (gameBoard.isFree(i + 1, j)) { cell[0] = i + 1; cell[1] = j; } else { cell = easyMove(); } } // up left else if (j <= 5 && j >= 1 && i <= 5 && i >= 1) { if (gameBoard.isFree(i, j - 1)) { cell[0] = i; cell[1] = j - 1; } else if (gameBoard.isFree(i - 1, j - 1)) { cell[0] = i - 1; cell[1] = j - 1; } else if (gameBoard.isFree(i - 1, j)) { cell[0] = i - 1; cell[1] = j; } else { cell = easyMove(); } } } } } } } return(cell); }