Determining a winner in Tic Tac Toe using if statements

13 views (last 30 days)
I'm writing a program to play tic-tac-toe in Matlab.
I'm having trouble in coding how to determine a winner using if statements.
So far, this is my function:
function won = Winner(board, player)
if board(1,1) == 'X' &&
Any help will be much appreciated

Answers (2)

Adam
Adam on 21 Nov 2014
Edited: Adam on 21 Nov 2014
Your function doesn't give any validation of 'board' and what format it is supposed to be in.
If this is just the background representation of the game rather than what is presented to the user a numeric 3*3 matrix with 1s and 0s representing the two players and NaNs for unused squares would make the task trivial.
Dealing with strings adds an un-necessary complexity unless you are forced to do that, in which case my first step would probably be to convert that into 1s and 0s.
Then you can just sum the columns, rows and leading and reverse-leading diagonals - if the answer to any is 0 or 3 then you declare the winner to be player 0 or 1.
Of course your function may get given an illegal board position, but that is a whole other matter that you haven't gone into so I am assuming that only legal board positions will be given to the function.
  1 Comment
Adam
Adam on 21 Nov 2014
Edited: Adam on 21 Nov 2014
board = [ ['X', ' ', 'O']; ['X', 'O', 'O']; ['X', 'X', ' '] ];
myBoard = nan(3);
myBoard( board == 'X' ) = 1;
myBoard( board == 'O' ) = 0;
for example, is what I would do with a board of chars!
Then both determining the winner and whether or not the game is over are trivial.
It can be done working with chars of course, but I hate working with chars if I don't have to.

Sign in to comment.


Kyle
Kyle on 21 Nov 2014
this is the whole program:
%% %% MATLAB function to play a game of Tic-Tac-Toe, %% where the human plays against the computer. %% The computer is not very smart and by default %% selects the first open move. %%
function TicTacToe() board = [ ... [' ', ' ', ' ']; ... [' ', ' ', ' ']; ... [' ', ' ', ' '] ... ];
turn = 1; %%human:
while ~GameOver(board)
Draw(board);
if turn == 1 %%human's turn:
board = HumanMove(board);
turn = 2;
else %%computer's turn:
board = ComputerMove(board);
turn = 1;
end
end
fprintf('\n');
fprintf('##########################\n');
fprintf('\n');
fprintf('Game over! Final board:\n');
Draw(board);
if Winner(board, 'X')
fprintf('Human wins!\n');
elseif Winner(board, 'O')
fprintf('Computer wins!\n');
else
fprintf('Game was a draw...\n');
end
fprintf('\n');
end
%% %% Draws the board on the screen: %% function Draw(board) fprintf('\n'); for r=1:3 fprintf(' '); for c=1:3 fprintf(' %c ', board(r, c)); if c < 3 fprintf('|'); end end
fprintf('\n');
if r < 3
fprintf(' ---|---|---\n');
end
end
fprintf('\n');
end
%% %% Allows the human to enter their move, %% and updates the board: %% function board = HumanMove(board) row = input('Your turn, row? '); col = input('Your turn, col? ');
board(row, col) = 'X';
end
%% %% Allows the computer to make a move: %% function board = ComputerMove(board) %% %% find first open spot -- we know there %% is one since game is not over: %% [rows, cols] = find(board == ' ');
%%put an O in that spot:
row = rows(1);
col = cols(1);
board(row, col) = 'O';
end
%% %% Returns true if the given player has won, where player %% is either 'X' or 'O': %% function won = Winner(board, player) %% %% %% TODO! You need to write this function... %%
won = false;
end
%% %% Returns true if the board is full (i.e. there are %% no open spaces), false if not. %% function full = BoardFull(board) %% %% %% TODO! You need to write this function... %% %% full = false; end
%% %% Returns true if the game is over, false if not: %% function over = GameOver(board) %% %% %% TODO! You need to write this function... %% %% over = false; end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!