My tl-tac-toe program in matlab is not working

I wrote a tic-tac-toe game in matlab, but it does not work:

%// 1) Commence the game, display empty board board=cell(3); %// 2) Select which player is ?X? and ?O? (at random) playersymbols={'X','O'}; playersymbolindex=randperm(2); for i=1:2 player(i).symbol=playersymbols{playersymbolindex(i)}; end winner=0; while winner<1 %// 3) Request player1?s move player1move=input('Player 1, please enter your move: '); %// the input is something like [1 3] where the first number is the %// row number and the second number is column number so that [1 3] will %// put X in row 1, column 3. %// Redraw updated board. board{player1move(1),player1move(2)}=player(1).symbol; board %// 5) Decision: has the game been won yet? If so, go to step 9). %// If not, go to step 6). %// The game is won when three respective X are placed in a horizontal, %// vertical, or diagonal row. if board{1,1}==board{1,2} & board {1,2}==board{1,3} | ... board{2,1}==board{2,2} & board{2,2}==board{2,3} | ... board{3,1}==board{3,2} & board{3,2}==board{3,3} | ... board{1,1}==board{2,1} & board{2,1}==board{3,1} | ... board{1,2}==board{2,2} & board{2,2}==board{3,2} | ... board{1,3}==board{2,3} & board{2,3}==board{3,3} | ... board{1,1}==board{2,2} & board{2,2}==board{3,3} | ... board{1,3}==board{2,2} & board{2,2}==board{3,1} %// 9) Display winner on the screen. disp('Winner is player 1!') winner=1; else %// 6) Request player2?s move player2move=input('Player 2, please enter your move: '); %// 7) Input Player2?s move. Redraw updated board. board{player2move(1),player2move(2)}=player(2).symbol; board %// 8) Decision: has the game been won yet? If so, go to step 9). %// If not, go back to step 3). if board{1,1}==board{1,2} & board {1,2}==board{1,3} | ... board{2,1}==board{2,2} & board{2,2}==board{2,3} | ... board{3,1}==board{3,2} & board{3,2}==board{3,3} | ... board{1,1}==board{2,1} & board{2,1}==board{3,1} | ... board{1,2}==board{2,2} & board{2,2}==board{3,2} | ... board{1,3}==board{2,3} & board{2,3}==board{3,3} | ... board{1,1}==board{2,2} & board{2,2}==board{3,3} | ... board{1,3}==board{2,2} & board{2,2}==board{3,1} %// 9) Display winner on the screen. disp('Winner is player 2!') winner=1; end end end 

I tried very hard to understand what I was doing wrong, but after 4 hours I did not know what I was doing wrong. Can someone please help me? What am I doing wrong?

EDIT: Player 1 is fine, so the problem is with player 2. When player 2 wins, my program does not show that β€œplayer 2 won the game” and incorrectly asks player 1 the next move (until the game is already finished).

+2
source share
4 answers

I agree with Steffen, and I will also make the comparison a separate function. Here is the implementation:

 %// 1) Commence the game, display empty board board=cell(3); %// 2) Select which player is ?X? and ?O? (at random) playersymbols={'X','O'}; playersymbolindex=randperm(2); for i=1:2 player(i).symbol=playersymbols{playersymbolindex(i)}; end winner=0; while winner<1 %// 3) Request player1?s move player1move=input('Player 1, please enter your move: '); %// the input is something like [1 3] where the first number is the %// row number and the second number is column number so that [1 3] will %// put X in row 1, column 3. %// Redraw updated board. board{player1move(1),player1move(2)}=player(1).symbol; board %// 5) Decision: has the game been won yet? If so, go to step 9). %// If not, go to step 6). %// The game is won when three respective X are placed in a horizontal, %// vertical, or diagonal row. if gameWon(board, player(1).symbol) %// 9) Display winner on the screen. disp('Winner is player 1!') winner=1; else %// 6) Request player2?s move player2move=input('Player 2, please enter your move: '); %// 7) Input Player2?s move. Redraw updated board. board{player2move(1),player2move(2)}=player(2).symbol; board %// 8) Decision: has the game been won yet? If so, go to step 9). %// If not, go back to step 3). if gameWon(board, player(2).symbol) %// 9) Display winner on the screen. disp('Winner is player 2!') winner=1; end end end 

And the gameWon function:

 function result = gameWon(board, playersymbol) result = (isequal(board{1,1},board{1,2},playersymbol) && isequal(board{1,1},board{1,3},playersymbol)) || ... %// repeat for all your cases end 
+3
source

I noticed a problem.

How do you compare strings with == and not strcmp when there are empty strings, the comparison returns [] . Therefore, the logical operator TRUE | [] TRUE | [] returns [] , which is not true.

In the above example ([1 1], [1 2], [2 1], [2 2], [1 3], [3 2]) the last line of if returns []

 board{1,3}==board{2,2} & board{2,2}==board{3,1} 

It looks like if you replace your logical operators with && and || , you can fix this problem.

+4
source

This is real fine behavior.

your if statement works if your first statement is 1.

You must initialize your board with some default liek '-'. And then use the operators && and || instead of and and. Then you also need to check the correct character of the actual player.

+1
source

& and | are logical comparisons on elements, if you use them in a cell whose contents are empty, the result is a 0x0 matrix.

Then you compare the resulting 0x0 matrix with another material, which can also be max. 0x0 or logical TRUE or logical FALSE. In any case, the program does not do what, in your opinion, does the if condition.

The easiest way to fix this is to disable | by || and & by & &, because the last two always return a logical FALSE or TRUE as a result.

0
source

Source: https://habr.com/ru/post/986566/


All Articles