Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function newBoard = make_move(board, player)
- %MAKE_MOVE Determine the next move of a player for tic tac toe
- % Given a board setup and a player, determine the "best" move for the
- % current player and update and return the board setup after this move
- % has been made.
- %
- % The process is :
- % 1. Find a winning position. If found, make the move and exit
- % the function;
- % 2. Find a position to block the other player from winning. If
- % found, make the move and exit the function;
- % 3. Otherwise, make a move to a random free location
- %
- % board: The tic tac toe matricx
- % player: 1 for player one, -1 for player two
- %
- % the nested function switches the player, and will return either a -1
- % for player two, or 1 for player one
- function player = getCurrentPlayer(oldPlayer)
- if (oldPlayer == 1)
- player = -1;
- elseif (oldPlayer == -1)
- player = 1;
- else
- fprintf(1, 'The current player %d is not valid!', oldPlayer);
- end
- end
- % Generetaes a random position which will be returned as a 1 by 2
- % column matrix.
- function randomPosition = generateRandomSpot()
- randomPosition = zeros(1, 2);
- randomPosition(1, 1) = randi([1 3]);
- randomPosition(1, 2) = randi([1 3]);
- end
- % If a winning move is unable to be found, we will now find an empty,
- % randomposition and set the value of empty random position to be
- % equal to the player's value.
- function updatedBoard = moveRandomFreeSpot(oldBoard, player)
- hasFoundEmptySpot = 0;
- updatedBoard = oldBoard;
- while (hasFoundEmptySpot == 0)
- pos = generateRandomSpot();
- if (oldBoard(pos(1), pos(2)) == 0)
- hasFoundEmptySpot = 1;
- oldBoard(pos(1), pos(2))
- updatedBoard(pos(1), pos(2)) = player;
- end
- end
- end
- % A winning move for either of the players will be found. The function
- % takes the absolute of the row/column/diagonal sums and checks which
- % ones (if any) are equal to 2, which will enable us to make the
- % winning move.
- function updatedBoard = findWinningMove(oldBoard, player)
- winnerFound = 0;
- updatedBoard = oldBoard;
- for (currentValue = 1 : 3)
- rowSum = sum(oldBoard(currentValue, :));
- if (rowSum == 2)
- winnerFound = 1;
- index = getIndex(oldBoard(currentValue, :));
- updatedBoard(currentValue, index) = player;
- return;
- end
- if (winnerFound == 0)
- columnSum = sum(oldBoard(:, currentValue));
- if (columnSum == 2)
- winnerFound = 1;
- index = getIndex(oldBoard(:, currentValue));
- updatedBoard(index, currentValue) = player;
- return;
- end
- end
- end
- % If there was no winning move beforehand, we will check the
- % diagonals of the board, and see if there is a viable move that
- % will allow us to make a winning/block a winning move.
- if (winnerFound == 0)
- diagSum = sum(diag(oldBoard));
- if (diagSum == 2)
- winnerFound = 1;
- index = getDiagonalIndex(oldBoard, 1);
- updatedBoard(index(1, 1), index(2, 1));
- return;
- end
- end
- end
- % Returns a matrix (2 by 1) that will give us the coordinares of the
- % potential winning move position in the matrix
- function result = getDia
- % Finds the index of the element in the vector
- function result = getIndex(theVector)
- for (i = 1 : length(theVector))
- if (theVector(i) == 0)
- result = i;
- end
- end
- end
- % We will try and find a winning move, and if there are no winning
- % moves, then we will provide a random position.
- updatedBoard = findWinningMove(board, player);
- if (updatedBoard == board)
- updatedBoard = moveRandomFreeSpot(board, player);
- end
- newBoard = updatedBoard;
- end
Add Comment
Please, Sign In to add comment