Question

In: Computer Science

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 :(

Solutions

Expert Solution

CODE

if nargin == 0
X = zeros(5,5);
game = 1;
B = buttons;
job = '';
else
[X,game,B] = getgame;
end
switch job
case 'green'
[i,j] = find(gcbo == B);
if winner(X) || X(i,j)
return
end
X(i,j) = 1;
savegame(X,game,B)
tictactoe('blue')
return
case 'blue'
if winner(X)
return
end
[i,j] = strategy(X,-1);
X(i,j) = -1;
case 'game'
game = mod(game,3)+1;
case 'start'
X = zeros(5,5);
case 'exit'
close(gcf)
return
end
savegame(X,game,B)
% ------------------------
function p = winner(X)
% p = winner(X) returns
% p = 0, no winner yet,
% p = -1, blue has won,
% p = 1, green has won,
% p = 2, game is a draw.
for p = [-1 1]
s = 5*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 [i,j] = winningmove(X,p);
% [i,j] = winningmove(X,p) finds any winning move for player p.
s = 4*p;
if any(sum(X) == s)
j = find(sum(X) == s);
i = find(X(:,j) == 0);
elseif any(sum(X') == s)
i = find(sum(X') == s);
j = find(X(i,:) == 0);
elseif sum(diag(X)) == s
i = find(diag(X) == 0);
j = i;
elseif sum(diag(fliplr(X))) == s
i = find(diag(fliplr(X)) == 0);
j = 4 - i;
else
i = [];
j = [];
end
% ------------------------
function [i,j] = strategy(X,p);
% [i,j] = strategy(X,p) is a good, but not perfect, move for player p.
% Appear to think.
pause(0.5)
% If possible, make a winning move.
[i,j] = winningmove(X,p);
% Block any winning move by opponent.
if isempty(i)
[i,j] = winningmove(X,-p);
end
% Otherwise, make a random move.
if isempty(i)
[i,j] = find(X == 0);
m = ceil(rand*length(i));
i = i(m);
j = j(m);
end
% ------------------------
function B = buttons
% Initialize push buttons and text.
clf
shg
set(gcf,'menubar','none','numbertitle','off','name','Tictactoe')
B = zeros(5,5);
M = magic(5);
for k = 1:25
[i,j] = find(k == M);
B(i,j) = uicontrol('style','pushbutton','units','normal', ...
'fontsize',16,'callback','tictactoe(''green'')');
end
uicontrol('style','text','units','normal','pos',[0.10 0.82 0.40 0.10], ...
'fontsize',20,'background',get(gcf,'color'),'tag','toptext');
uicontrol('style','text','units','normal','pos',[0.20 0.72 0.60 0.10], ...
'fontsize',10,'background',get(gcf,'color'),'tag','toptext','string', ...
['Pick single digit numbers. Each digit can be chosen only once. ' ...
'Generate a total of 15 using exactly three digits.'])
uicontrol('style','pushbutton','units','normal','string','Game', ...
'fontsize',12,'position',[.13 .12 .15 .07], ...
'callback','tictactoe(''game'')');
uicontrol('style','pushbutton','units','normal','string','Start', ...
'fontsize',12,'position',[.43 .12 .15 .07], ...
'callback','tictactoe(''start'')');
uicontrol('style','pushbutton','units','normal','string','Exit', ...
'fontsize',12,'position',[.83 .12 .15 .07], ...
'callback','tictactoe(''exit'')');
% ------------------------
function [X,game,B] = getgame
% Restore the state of the game.
ud = get(gcf,'userdata');
X = ud{1};
game = ud{2};
B = ud{3};
% ------------------------
function savegame(X,game,B)
% Update the display and save the state of the game.
M = magic(5);
for k = 1:25
[i,j] = find(k == M);
switch game
case 1 % Pick15
pos = [0.10*k-0.055 0.50 0.10 0.1333];
str = int2str(k);
case 2 % TicTacToe
pos = [0.10*j+0.25 0.1333*(6-i)+0.17 0.10 0.1333];
str = '';
case 3 % Magic3
pos = [0.10*j+0.25 0.1333*(6-i)+0.17 0.10 0.1333];
str = int2str(k);
end
switch X(i,j)
case -1
bg = 'blue';
case 0
bg = 'white';
case 1
bg = 'green';
end
set(B(i,j),'position',pos,'string',str,'background',bg)
end
T = flipud(findobj(gcf,'tag','toptext'));
set(T(2),'visible','off')
switch winner(X)
case 0,
switch game
case 1, set(T(1),'string','Pick15')
set(T(2),'visible','on')
case 2, set(T(1),'string','TicTacToe')
case 3, set(T(1),'string','Magic3')
end
case -1, set(T(1),'string','Blue wins')
case 1, set(T(1),'string','Green wins')
case 2, set(T(1),'string','Draw')
end
set(gcf,'userdata',{X,game,B})


Related Solutions

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...
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
If you were to write a game of tic-tac-toe, you may store the representation of the...
If you were to write a game of tic-tac-toe, you may store the representation of the game board as a two dimensional list such as   [['X', '', 'X'], ['O', 'X', ''], ['', 'O', 'X']] where each sublist is a row in the board.   Empty strings ('') denote a space that does not yet have a value. Assuming this representation, write functions (contained in the same file) that do the following: a) Create a new empty tic-tac-toe board. This function should...
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
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...
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...
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?
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: 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...
PLEASE READ VERY CAREFULLY write a client.py and server.py file for tic-tac-toe IN PYTHON with the...
PLEASE READ VERY CAREFULLY write a client.py and server.py file for tic-tac-toe IN PYTHON with the following restrictions (SO WRITE TWO FILES THAT PLAY PYTHON THROUGH A SOCKET) Use a 5 x 5 grid (dimensions are subject to change, so use constants for NUM_ROWS and NUM_COLS) Use 'X' for player 1 and 'O' for player 2 (symbols and the number of players is subject to change, so use constants) Each player can make 1 move per turn before having to...
Game of Tic Tac Toe with the following conditions A point system where a  move that leads...
Game of Tic Tac Toe with the following conditions A point system where a  move that leads to a winning game is given 1 point, a move that leads to a tie is given 0 point, and a  lead to a losing game will get -1 point. Grid size of 5x5 A timer that can be set for how long a game can be played 5 symbols in a row to get a point Connected lines cannot be crossed (No diagonal lines)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT