// Algorithm Referenced from: //Java Graphic Tutorial, http://www3.ntu.edu.sg/home/ehchua/programming/java/javagame_tictactoe_ai.html //PLAYING WITH GAME THEORY, https://gangsterveggies.wordpress.com/tag/alpha-beta-pruning/ //improving the prunning algorithm public static result prunning(int a, int b, char player, int alpha, int beta) { result r2 = new result(); int x1 = -1, y1 = -1; int bestValue = (player == 'X') ? -10 : 10; if (checkResult() != 0) { r2.x = a; r2.y = b; r2.val = checkResult(); return r2; } if (player == 'X') { bestValue = -10; // List<char[,]> adjList = new List<char[,]>(); // Action( ref adjList, player); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (boardJim[i, j] == ' ') {//If legal, boardJim[i, j] = 'X'; int score = prunning(i, j, 'O', alpha, beta).val; if (score > alpha) { alpha = score; // bestValue = score; r2.val = score; r2.x = i; r2.y = j; // move = i; }//Pick the one that's worst for the opponent boardJim[i, j] = ' ';// if (alpha >= beta) break; } } } return r2; } if (player == 'O') { bestValue = 10; // List<char[,]> adjList = new List<char[,]>(); // Action( ref adjList, player); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (boardJim[i, j] == ' ') {//If legal, boardJim[i, j] = 'O'; int score = prunning(i, j, 'X', alpha, beta).val; if (score < beta) { beta = score; // bestValue = score; r2.val = score; r2.x = i; r2.y = j; // move = i; }//Pick the one that's worst for the opponent boardJim[i, j] = ' ';// if (alpha >= beta) break; } } } // return r2; } // r2.x = x1; // r2.y = y1; // r2.val = bestValue; return r2; }
//the computer makes the move through this function by calling minimax //does accept any parameter, nor gives an output public static result computerMove() { result r1 = new result(); r1 = minimax(0, 0, 'X'); // r1 = prunning(0, 0, 'X', -10, 10); if (r1.val == 1 || r1.val == 0) boardJim[r1.x, r1.y] = 'X'; else boardJim[r1.x, r1.y] = 'O'; //board[r1.x, r1.y] = r1.val; // Form1 f = new Form1(); //f.computerClick(r1.x, r1.y); return r1; }