gistlib
Here's a one-player tic-tac-toe game in MATLAB with simple artificial intelligence. The AI will randomly select a cell to make a move.
main.m% Clear the command window and the variables clc; clear; % Initialize the variables board = ['1' '2' '3'; '4' '5' '6'; '7' '8' '9']; player = 'X'; % Display the initial board disp(board); % Loop until there is a winner or tie while ~check_win(board, player) && ~check_tie(board) % Player's turn if player == 'X' % Get the player's move move = input('Your move (1-9): '); % Update the board board = update_board(board, move, player); % AI's turn else % Get a random move move = randi(9); % Update the board board = update_board(board, move, player); end % Display the updated board disp(board); % Switch player player = switch_player(player); end % Display the winner or tie message if check_win(board, player) disp(['Player ' player ' wins!']); elseif check_tie(board) disp('Tie game!'); end function board = update_board(board, move, player) % Update the board with the player's move row = ceil(move / 3); col = mod(move - 1, 3) + 1; if board(row, col) ~= 'X' && board(row, col) ~= 'O' board(row, col) = player; else disp('Invalid move! Try again.'); move = input('Your move (1-9): '); board = update_board(board, move, player); end end function player = switch_player(player) % Switch the player if player == 'X' player = 'O'; else player = 'X'; end end function win = check_win(board, player) % Check if the player has won win = false; for i = 1:3 % Check rows if board(i,1) == player && board(i,2) == player && board(i,3) == player win = true; end % Check columns if board(1,i) == player && board(2,i) == player && board(3,i) == player win = true; end end % Check diagonals if board(1,1) == player && board(2,2) == player && board(3,3) == player win = true; end if board(1,3) == player && board(2,2) == player && board(3,1) == player win = true; end end function tie = check_tie(board) % Check if the game is a tie tie = true; for i = 1:3 for j = 1:3 if board(i,j) ~= 'X' && board(i,j) ~= 'O' tie = false; end end end end 2150 chars95 lines
% Clear the command window and the variables clc; clear; % Initialize the variables board = ['1' '2' '3'; '4' '5' '6'; '7' '8' '9']; player = 'X'; % Display the initial board disp(board); % Loop until there is a winner or tie while ~check_win(board, player) && ~check_tie(board) % Player's turn if player == 'X' % Get the player's move move = input('Your move (1-9): '); % Update the board board = update_board(board, move, player); % AI's turn else % Get a random move move = randi(9); % Update the board board = update_board(board, move, player); end % Display the updated board disp(board); % Switch player player = switch_player(player); end % Display the winner or tie message if check_win(board, player) disp(['Player ' player ' wins!']); elseif check_tie(board) disp('Tie game!'); end function board = update_board(board, move, player) % Update the board with the player's move row = ceil(move / 3); col = mod(move - 1, 3) + 1; if board(row, col) ~= 'X' && board(row, col) ~= 'O' board(row, col) = player; else disp('Invalid move! Try again.'); move = input('Your move (1-9): '); board = update_board(board, move, player); end end function player = switch_player(player) % Switch the player if player == 'X' player = 'O'; else player = 'X'; end end function win = check_win(board, player) % Check if the player has won win = false; for i = 1:3 % Check rows if board(i,1) == player && board(i,2) == player && board(i,3) == player win = true; end % Check columns if board(1,i) == player && board(2,i) == player && board(3,i) == player win = true; end end % Check diagonals if board(1,1) == player && board(2,2) == player && board(3,3) == player win = true; end if board(1,3) == player && board(2,2) == player && board(3,1) == player win = true; end end function tie = check_tie(board) % Check if the game is a tie tie = true; for i = 1:3 for j = 1:3 if board(i,j) ~= 'X' && board(i,j) ~= 'O' tie = false; end end end end
gistlibby LogSnag