Question

In: Computer Science

**MATLAB Code only Please create a MATLAB script that is a TIC-TAC-TOE game. Please keep it...

**MATLAB Code only

Please create a MATLAB script that is a TIC-TAC-TOE game. Please keep it simple, but there is no other rules. Thank you.

Solutions

Expert Solution

  ANSWER

THIS TIC-TAC-TOE GAME CODE WRITTEN IN MATLAB CODE ONLY ::

THIS GAME INVOLVE Four.m Files Which are Below :

function tictactoe(f_user1,f_user2)

% clear all
% clc
%addpath()
user1=f_user1;
user2=f_user2;

user1wins=0;
user2wins=0;
user2loss=0;
user1loss=0;
ties=0;
user1_forfiet=0;
user2_forfiet=0;

for games=1:1000
%%
board=zeros(3,3);
turn=randi([1 2],1,1);
p=0;
while p==0
turn=turn+1;
if rem(turn,2)~=0
userboard=user1(board,-1);
else
userboard=user2(board,1);   
end
p=legalmove(board,userboard,p);
if p~=3
p=winner(userboard);
end
board=userboard;
end

%%
if p==3
if rem(turn,2)==0
user2_forfiet=user2_forfiet+1;
user2loss=user2loss+1;
user1wins=user1wins+1;
else
user1_forfiet=user1_forfiet+1;
user1loss=user1loss+1;
user2wins=user2wins+1;
end
elseif p==2
ties=ties+1;
elseif p==-1
user1wins=user1wins+1;
user2loss=user2loss+1;
else
user2wins=user2wins+1;
user1loss=user1loss+1;
end
end

user1_stats=[user1wins user1loss ties user1_forfiet]
user2_stats=[user2wins user2loss ties user2_forfiet]

--------------------------------------------------------------------------------------------------------------------------------------------------

function p=legalmove(board,userboard,p)

if any(size(userboard)~=size(board))
p=3;
elseif sum(sum(abs(board-userboard)))~=1;
p=3;
else
p=p;
end

----------------------------------------------------------------------------------------------------------------------------------------------------

function p = winner(X)
% p = winner(X) returns
% p = 0, no winner yet,
% p = -1, user 1 has won,
% p = 1, user 2 has won,
% p = 2, game is a draw.

for p = [-1 1]
s = 3*p;
win = any(sum(X) == s) || any(sum(X') == s) || ...
sum(diag(X)) == s || sum(diag(fliplr(X))) == s;
if win
return
end
end
p = 2*all(X(:) ~= 0);

-----------------------------------------------------------------------------------------------------------------------------------------------------

function B=game(B,x)

r=randi(3,1);
c=randi(3,1);

while B(r,c) ~= 0
r=randi(3,1);
c=randi(3,1);
end

if B(r,c) == 0
r=randi(1,3);
elseif B(r,c) ~= 0
c=randi(1,3);
end
  

B(r,c)=x;


Related Solutions

If anyone can please write a code for a 5x5 tic tac toe game in matlab...
If anyone can please write a code for a 5x5 tic tac toe game in matlab I would greatly appreciate it. Its extra credit. PLEASE HELP ME :(
please create a tic tac toe game with python using only graphics.py library
please create a tic tac toe game with python using only graphics.py library
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please...
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please provide code for checking diagonal win. function checkWinVertical(player, row, col) { var counter = 0; //each button var btn = document.getElementsByTagName("button"); for (counterC = 0; counterC < col; counterC++){ for (countR = 0; countR < row; countR++){ if (player != btn[counter].innerHTML){ counter += 1; break; } else if (countR + 1 == col) { for (countW = 0; countW < col; countW++){ btn[counter -...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human players who alternate entering their moves on the same computer. Create a 3-by-3 two-dimensional array. Each player indicates their moves by entering a pair of numbers representing the row and column indices of the square in which they want to place their mark, either an 'X' or an 'O'. When the first player moves, place an 'X' in the specified square. When the second...
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
In a game of tic tac toe. How do you check if all the positions in...
In a game of tic tac toe. How do you check if all the positions in a double array are taken, the double arrays are used to store the x's and o's?
Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm =...
Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm = Game.new('andy', 'mike') => #<Game:0x2e91d78 @players=[Andy, Mike]> >> gm.play_game('mike') Mike, enter your next O O-- --- --- Andy, enter your next X O-X --- --- Mike, enter your next O O-X -O- --- Andy, enter your next X move Bad move dude! You go again. O-X -O- --- Andy, enter your next X move O-X -OX --- Mike, enter your next O move O-X -OX...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT