Question
I have set up a tic tac toe game, i am having trouble displaying player two wins when the O's line up in the game.
I have set up a tic tac toe game, i am having trouble displaying player two wins when the O's line up in the game. I need help figuring out what to change up so player two winning can work properly. Can you help me out?
% Setup the figure/windows for the game. It is a three by three plot, % setting the x and y values of the table function [] = tictactoe() close all figure('Name','Tic Tac Toe Game'); axis([0 3 0 3]) set(gca,'xTick',0:3) set(gca,'yTick',0:3) set(gca,'xTickLabel','') set(gca,'yTickLabel','') %Eliminate the numbers of the grid so it looks like a tic tac toe board %not a graph xlabel('Player: 1') %The axis label will display the current player who is selecting a move grid on %the game needs to keep track of the moves for the current player. The %state of the game will help determine the winner. 0 will represent %an empty box, 1 will show a move for player 1 and -1 for player 2. player = 1; game = [[0 0 0] [0 0 0] [0 0 0]]; winner = 0; % Start main while loop for the game. The game will continue until the game ends % with a winner or lack of open spaces. If a player selects a filled spot, it % will tell the player to make a new selection while winner == 0 next = play(player, game); if next == 0 xlabel('Move cannot be completed, please make a new selection'); %the x-axis label will change to alert the player the move is %unavailable else game = next; %The game will need to select the next player to continue the %game and display the current player on the x-axis player = mod(player + 1,2); if player == 1 xlabel('Player 1'); else xlabel('Player 2'); end winner = won(game); % This uses the won function to check if the game is in a winning scenario end end %If statement used to display which player wins the game, if the O's %win it is player 2 and if the X's win, is it player 1 if winner == 0 warndlg('Player 2 has won the game'); elseif winner == 1 warndlg('Player 1 has won the game'); else%Niether,its a tie warndlg('It is a Tie'); end end
% The game function acknowledges the current player and the previous state the % game is in and simulates the new round. It positions the mouse with % repsect to the plot, it instructs the program to select the row by % subratcting the players selected row from 2 to locate the selected box. % This also tells the program when to draw the symbols during thr game. function game = play(player, game) [x,y] = ginput(1); [col,row] = position(x, y); row = 2 - row; %The bottom row starts at zero if game(col+1, row+1) ~= 0 game = 0; else %Set the state of the game equal to the box selected by the player and instruct %the program to draw an X or O for the respective player game(col+1, row+1) = player; if player drawX(col, 2 - row); else drawO(col, 2 - row); end end end %These next two functions tell the game when to draw an X and when to draw %an O. Giving the program instructions on how and where to draw the x and %o. % Locate and return the postion of mouse on grid, set position for mouse function [col,row] = position(x,y) col = floor(x); row = floor(y); end function drawX(col, row) hold on x = 0:1; pos = 0:1; neg = 1-x; plot(x+col, pos+row) plot(x+col, neg+row) hold off end
function drawO(col, row) hold on t = 0:0.1:2*pi; x = cos(t)/2+0.5; y = sin(t)/2+0.5; plot(x+col, y+row) hold off end
% This function checks whether or not the current game state is a winning % scenario. It sets winning parameters for each possible winning % combination in the horizontal, verticle, and diagonal direction. If there % are no more available moves, the game ends as well function won = won(game) %the function is comparing the value in each box of the grid to the %boxes that would be needed to win if (game(1,1) == game(1,2) && game(1,1) == game(1,3) && game(1,1)) won = game(1,1); elseif (game(2,1) == game(2,2) && game(2,1) == game(2,3) && game(2,1)) won = game(2,1); elseif (game(3,1) == game(3,2) && game(3,1) == game(3,3) && game(3,1)) won = game(3,1); %start of vertical winning combinations elseif (game(1,1) == game(2,1) && game(1,1) == game(3,1) && game(3,1)) won = game(1,1); elseif (game(1,2) == game(2,2) && game(1,2) == game(3,2) && game(1,2)) won = game(1,2); elseif (game(1,3) == game(2,3) && game(1,3) == game(3,3) && game(1,3)) won = game(1,3); %start of diagonal winning combinations elseif (game(1,1) == game(2,2) && game(1,1) == game(3,3) && game(1,1)) won = game(1,1); elseif (game(1,3) == game(2,2) && game(1,3) == game(3,1) && game(2,2)) won = game(1,3); % If no more slots are open, it's a tie elseif ~ismember(game, 0) won = 2; else won =0; end end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started