Here's an implementation of player vs AI Tic-tac-toe game using minimax algorithm in Matlab:
function playTicTacToe()
% initialize game board
board = zeros(3,3);
% print empty board
dispBoard(board);
% game loop
while true
% player's turn
[row, col] = getPlayerMove(board);
board(row,col) = -1;
dispBoard(board);
% check if player wins
if gameEnded(board)
fprintf('Player wins!\n');
return;
end
% AI's turn
[row, col] = getAIMove(board);
board(row,col) = 1;
dispBoard(board);
% check if AI wins
if gameEnded(board)
fprintf('AI wins!\n');
return;
end
% check if board is full
if all(board(:) ~= 0)
fprintf('Game draw!\n');
return;
end
end
end
function [row, col] = getPlayerMove(board)
% prompt user for input until valid move is entered
while true
move = input('Your move (row,col): ');
if board(move(1),move(2)) == 0
row = move(1);
col = move(2);
return;
else
fprintf('Invalid move.\n');
end
end
end
function [row, col] = getAIMove(board)
% use minimax algorithm to determine AI's move
[val, idx] = max(minimax(board, 1));
[row, col] = ind2sub(size(board), idx);
end
function [val, idx] = minimax(board, player)
% base case: check if game ended
if gameEnded(board)
val = getScore(board, player);
idx = nan;
return;
end
% recursive case: apply minimax algorithm
scores = nan(1,9);
for i = 1:numel(board)
if board(i) == 0
boardNew = board;
boardNew(i) = player;
[scores(i), ~] = minimax(boardNew, -player);
end
end
% player maximizes, AI minimizes
if player == 1
[val, idx] = max(scores);
else
[val, idx] = min(scores);
end
end
function score = getScore(board, player)
% calculate score based on which player wins or if draw
if any(all(board(1:3,:) == player,1)) || any(all(board(:,1:3) == player,2)) || all(diag(board) == player) || all(diag(flipud(board)) == player)
score = 1;
elseif any(board(:) == 0)
score = 0;
else
score = -1;
end
end
function ended = gameEnded(board)
% check if game ended based on whether someone won or board is full
ended = any(all(board(1:3,:) == 1,1)) || any(all(board(:,1:3) == 1,2)) || all(diag(board) == 1) || all(diag(flipud(board)) == 1) || ...
any(all(board(1:3,:) == -1,1)) || any(all(board(:,1:3) == -1,2)) || all(diag(board) == -1) || all(diag(flipud(board)) == -1) || ...
all(board(:) ~= 0);
end
function dispBoard(board)
% print current game board
boardStr = {' ',' ',' ';' ',' ',' ';' ',' ',' '};
boardStr(board == 1) = 'O';
boardStr(board == -1) = 'X';
fprintf('\n%s %s %s\n', boardStr{1,:});
fprintf('%s %s %s\n', boardStr{2,:});
fprintf('%s %s %s\n', boardStr{3,:});
end
The above code defines a main game loop which alternates between the player's turn and the AI's turn. On the player's turn, the function getPlayerMove()
prompts the user for input until a valid move is entered. On the AI's turn, the function getAIMove()
uses the minimax algorithm to determine the AI's move. The code for the minimax algorithm is defined in the function minimax()
, which applies the algorithm recursively to evaluate all possible moves and choose the best one. The function getScore()
is used to assign a score to the game board depending on which player wins or if the game ends in a draw. Finally, the function dispBoard()
is used to print the current game board after each turn.