Question

Can you create a 2-player Tic Tac Toe game (also known as noughts and crosses) on...

Can you create a 2-player Tic Tac Toe game (also known as noughts and crosses) on MATLAB, using nested loops (for, if, else, while loops) arrays, conditional execution and functions, as well as displaying the game board of X and O on a plot.

Homework Answers

Answer #1

function [] = tictac

n = 1;

cla;

hold on;

c = zeros(2,5);

d = zeros(2,5);

for n = 1:6

m = (-1)^n;

if m == 1;

x1 = input('player 1 move:');

y1 = ('');

move(x1,y1)

c(1, (n+1)/2) = x1;

c(2, (n+1)/2) = y1;

else

x2 = input('player 2 move:');

y2 = ('');

move(x2,y2)

d(1, (n+1)/2) = x2;

d(2, (n+1)/2) = y2;

end

n = n+1;

end

f=sum(c,2);

g=sum(d,2);

if c(1,1)==c(1,2)==c(1,3) || c(2,1)==c(2,2)==c(2,3);

disp ('PLAYER 1 WINS')

elseif d(1,1)==d(1,2)==d(1,3) || d(2,1)==d(2,2)==d(2,3);

disp ('PLAYER 2 WINS')  

elseif f(1,1) == 6 && f(2,1) == 6;

disp ('PLAYER 1 WINS')

elseif g(1,1) == 6 && f(2,1) == 6;

disp ('PLAYER 2 WINS')  

else

x3 = input('player 1 move:');

y3 = input('');

move(x3,y3);

c(1,4) = x1;

c(2,4) = y1;

end

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
JAVA Programming * create a 3 dimensional tic tac toe game. make 1 player the computer.
JAVA Programming * create a 3 dimensional tic tac toe game. make 1 player the computer.
Prove that the first player in a game of (the usual 3 × 3) tic-tac-toe can...
Prove that the first player in a game of (the usual 3 × 3) tic-tac-toe can always (at least) force a draw. Note that one way to do this is to draw the full game tree, but there is an easier proof.
A Tic-tac-toe board has 9 spaces. The first player fills one of the spaces with an...
A Tic-tac-toe board has 9 spaces. The first player fills one of the spaces with an X, then the second player fills a space with an O. The players continue to alternate filling a space with their respective symbol until no empty spaces remain on the board. How many different arrangements of X’s and O’s are possible at the end of the game? You must fully justify your answer.
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional char array with three rows and three columns as the game board. Each element in the array should be initialized with an asterisk (*). The program should run a loop that: • Displays the contents of the board array. • Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and...
Part 1: Create the grid tic-tac-toe gameboard using buttons and iteration. Part 2: Human user gets...
Part 1: Create the grid tic-tac-toe gameboard using buttons and iteration. Part 2: Human user gets to select an open cell on the grid - place an X on that button selected Part 3: Check for a win using DOM iteration - new game option if row or column matching X pattern Part 4: Computer gets to select an open cell on the grid - place an O on that button selected Part 5: Check for a win using DOM...
in c++ : Problem Domain: The classic TicTacToe game alternatively place Xs and Os on a...
in c++ : Problem Domain: The classic TicTacToe game alternatively place Xs and Os on a 3x3 grid. The winner is the first player to place 3 consecutive marks in a horizontal, vertical or diagonal row. Understanding the Problem: In this assignment, you will: 1) Create a tic tac toe board data structure. 2) Check for game over. 3) Clear the tic tac toe game for each game. 4) Display the tic tac toe board to screen. 5) Simulate playing...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT