Here's the full code for tic-tac-toe using minmax algorithm in Matlab:
main.mfunction [score, depth] = minmax(board, player, level)
% MINMAX is a recursive function that uses the minmax algorithm to find
% the optimal move for the player given the current board state
% Initialize the score and depth
score = 0;
depth = level;
% Check if the game is over or if the maximum depth level is reached
if check_game_over(board) || level == 0
score = evaluate_board(board, player);
return
end
% If the player is X (maximizer), get the maximum possible score
if player == 'X'
for i = 1:3
for j = 1:3
if board(i,j) == ' '
tmp_board = board;
tmp_board(i,j) = 'X';
[~, tmp_depth] = minmax(tmp_board, 'O', level-1);
if tmp_depth < depth
depth = tmp_depth;
end
if score < evaluate_board(tmp_board, 'X')
score = evaluate_board(tmp_board, 'X');
end
end
end
end
% If the player is O (minimizer), get the minimum possible score
else
for i = 1:3
for j = 1:3
if board(i,j) == ' '
tmp_board = board;
tmp_board(i,j) = 'O';
[~, tmp_depth] = minmax(tmp_board, 'X', level-1);
if tmp_depth < depth
depth = tmp_depth;
end
if score > evaluate_board(tmp_board, 'O')
score = evaluate_board(tmp_board, 'O');
end
end
end
end
end
end
function [result] = check_game_over(board)
% CHECK_GAME_OVER checks if the game is over and returns the result
result = false;
% Check for three in a row
for i = 1:3
if board(i,1) == board(i,2) && board(i,2) == board(i,3) && board(i,1) ~= ' '
result = true;
return
end
end
% Check for three in a column
for i = 1:3
if board(1,i) == board(2,i) && board(2,i) == board(3,i) && board(1,i) ~= ' '
result = true;
return
end
end
% Check for three in a diagonal
if board(1,1) == board(2,2) && board(2,2) == board(3,3) && board(1,1) ~= ' '
result = true;
return
end
if board(1,3) == board(2,2) && board(2,2) == board(3,1) && board(1,3) ~= ' '
result = true;
return
end
% Check if the board is full
if sum(sum(board == ' ')) == 0
result = true;
return
end
end
function [score] = evaluate_board(board, player)
% EVALUATE_BOARD returns a score for the board given the player
% Initialize the score to 0
score = 0;
% Check for three in a row, column, or diagonal
for i = 1:3
if board(i,1) == board(i,2) && board(i,2) == board(i,3) && board(i,1) == player
score = 10;
return
end
if board(1,i) == board(2,i) && board(2,i) == board(3,i) && board(1,i) == player
score = 10;
return
end
end
if board(1,1) == board(2,2) && board(2,2) == board(3,3) && board(1,1) == player
score = 10;
return
end
if board(1,3) == board(2,2) && board(2,2) == board(3,1) && board(1,3) == player
score = 10;
return
end
% Check for two in a row, column, or diagonal
for i = 1:3
if board(i,1) == board(i,2) && board(i,1) == player && board(i,3) == ' '
score = 5;
return
end
if board(i,1) == board(i,3) && board(i,1) == player && board(i,2) == ' '
score = 5;
return
end
if board(i,2) == board(i,3) && board(i,2) == player && board(i,1) == ' '
score = 5;
return
end
if board(1,i) == board(2,i) && board(1,i) == player && board(3,i) == ' '
score = 5;
return
end
if board(1,i) == board(3,i) && board(1,i) == player && board(2,i) == ' '
score = 5;
return
end
if board(2,i) == board(3,i) && board(2,i) == player && board(1,i) == ' '
score = 5;
return
end
end
if board(1,1) == board(2,2) && board(1,1) == player && board(3,3) == ' '
score = 5;
return
end
if board(1,1) == board(3,3) && board(1,1) == player && board(2,2) == ' '
score = 5;
return
end
if board(2,2) == board(3,3) && board(2,2) == player && board(1,1) == ' '
score = 5;
return
end
if board(1,3) == board(2,2) && board(1,3) == player && board(3,1) == ' '
score = 5;
return
end
if board(1,3) == board(3,1) && board(1,3) == player && board(2,2) == ' '
score = 5;
return
end
if board(2,2) == board(3,1) && board(2,2) == player && board(1,3) == ' '
score = 5;
return
end
% If no winning or blocking move, return score of 0
end
function [] = print_board(board)
% PRINT_BOARD prints the current state of the board
fprintf('\n');
for i = 1:3
fprintf(' %s | %s | %s ', board(i,:));
if i ~= 3
fprintf('\n---+---+---\n');
else
fprintf('\n');
end
end
end
function [] = tic_tac_toe()
% TIC_TAC_TOE is the main function that controls the game play
% Initialize the board
board = repmat(' ', 3, 3);
% Prompt user for X or O
player = input('Would you like to play as X or O? ', 's');
% Initialize scores
score_x = 0;
score_o = 0;
% Start game loop
while sum(sum(board == ' ')) > 0 && check_game_over(board) == false
% Print the board
print_board(board);
% Let the user make a move (if they are playing as X)
if player == 'X'
valid_move = false;
while valid_move == false
row = input('Enter the row number (1-3): ');
col = input('Enter the column number (1-3): ');
if board(row,col) == ' '
board(row,col) = 'X';
valid_move = true;
else
fprintf('That space is already taken. Try again.\n');
end
end
end
% Use minmax algorithm to find the optimal move for O
[score, ~] = minmax(board, 'O', 5);
[row, col] = find(board == ' ' & evaluate_board(board, 'O') == score, 1);
if isempty(row) % if there is no winning move, use minimax to block X's winning move
[score, ~] = minmax(board, 'X', 5);
[row, col] = find(board == ' ' & evaluate_board(board, 'X') == score, 1);
end
board(row,col) = 'O';
% Update scores
score_x = evaluate_board(board, 'X');
score_o = evaluate_board(board, 'O');
end
% Print the final board and scores
print_board(board);
if check_game_over(board) && score_x ~= 10 && score_o ~= 10
fprintf('The game is a tie.\n');
elseif player == 'X' && score_x == 10
fprintf('You win!\n');
elseif player == 'O' && score_o == 10
fprintf('You win!\n');
else
fprintf('The computer wins!\n');
end
end
7462 chars244 lines
You can use tic_tac_toe()
to start the game. The user inputs their move by row and column number. The algorithm uses the minmax algorithm to find the optimal move for the computer.
gistlibby LogSnag