Guest User

Untitled

a guest
Nov 17th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. function newBoard = make_move(board, player)
  2. %MAKE_MOVE Determine the next move of a player for tic tac toe
  3. % Given a board setup and a player, determine the "best" move for the
  4. % current player and update and return the board setup after this move
  5. % has been made.
  6. %
  7. % The process is :
  8. % 1. Find a winning position. If found, make the move and exit
  9. % the function;
  10. % 2. Find a position to block the other player from winning. If
  11. % found, make the move and exit the function;
  12. % 3. Otherwise, make a move to a random free location
  13. %
  14. % board: The tic tac toe matricx
  15. % player: 1 for player one, -1 for player two
  16. %
  17.  
  18.  
  19. % the nested function switches the player, and will return either a -1
  20. % for player two, or 1 for player one
  21. function player = getCurrentPlayer(oldPlayer)
  22. if (oldPlayer == 1)
  23. player = -1;
  24. elseif (oldPlayer == -1)
  25. player = 1;
  26. else
  27. fprintf(1, 'The current player %d is not valid!', oldPlayer);
  28. end
  29. end
  30.  
  31. % Generetaes a random position which will be returned as a 1 by 2
  32. % column matrix.
  33. function randomPosition = generateRandomSpot()
  34. randomPosition = zeros(1, 2);
  35. randomPosition(1, 1) = randi([1 3]);
  36. randomPosition(1, 2) = randi([1 3]);
  37. end
  38.  
  39. % If a winning move is unable to be found, we will now find an empty,
  40. % randomposition and set the value of empty random position to be
  41. % equal to the player's value.
  42. function updatedBoard = moveRandomFreeSpot(oldBoard, player)
  43. hasFoundEmptySpot = 0;
  44. updatedBoard = oldBoard;
  45. while (hasFoundEmptySpot == 0)
  46. pos = generateRandomSpot();
  47. if (oldBoard(pos(1), pos(2)) == 0)
  48. hasFoundEmptySpot = 1;
  49. oldBoard(pos(1), pos(2))
  50. updatedBoard(pos(1), pos(2)) = player;
  51. end
  52. end
  53. end
  54.  
  55. % A winning move for either of the players will be found. The function
  56. % takes the absolute of the row/column/diagonal sums and checks which
  57. % ones (if any) are equal to 2, which will enable us to make the
  58. % winning move.
  59. function updatedBoard = findWinningMove(oldBoard, player)
  60. winnerFound = 0;
  61. updatedBoard = oldBoard;
  62. for (currentValue = 1 : 3)
  63. rowSum = sum(oldBoard(currentValue, :));
  64. if (rowSum == 2)
  65. winnerFound = 1;
  66. index = getIndex(oldBoard(currentValue, :));
  67. updatedBoard(currentValue, index) = player;
  68. return;
  69. end
  70. if (winnerFound == 0)
  71. columnSum = sum(oldBoard(:, currentValue));
  72. if (columnSum == 2)
  73. winnerFound = 1;
  74. index = getIndex(oldBoard(:, currentValue));
  75. updatedBoard(index, currentValue) = player;
  76. return;
  77. end
  78. end
  79. end
  80.  
  81. % If there was no winning move beforehand, we will check the
  82. % diagonals of the board, and see if there is a viable move that
  83. % will allow us to make a winning/block a winning move.
  84. if (winnerFound == 0)
  85. diagSum = sum(diag(oldBoard));
  86. if (diagSum == 2)
  87. winnerFound = 1;
  88. index = getDiagonalIndex(oldBoard, 1);
  89. updatedBoard(index(1, 1), index(2, 1));
  90. return;
  91. end
  92. end
  93. end
  94.  
  95. % Returns a matrix (2 by 1) that will give us the coordinares of the
  96. % potential winning move position in the matrix
  97. function result = getDia
  98.  
  99. % Finds the index of the element in the vector
  100. function result = getIndex(theVector)
  101. for (i = 1 : length(theVector))
  102. if (theVector(i) == 0)
  103. result = i;
  104. end
  105. end
  106. end
  107.  
  108. % We will try and find a winning move, and if there are no winning
  109. % moves, then we will provide a random position.
  110. updatedBoard = findWinningMove(board, player);
  111. if (updatedBoard == board)
  112. updatedBoard = moveRandomFreeSpot(board, player);
  113. end
  114. newBoard = updatedBoard;
  115. end
Add Comment
Please, Sign In to add comment