我在matlab中编写了一个井字游戏,但它不起作用:
%// 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
我非常努力地理解我做错了什么,但是4小时后,我无能为力。有人可以帮帮我吗?我做错了什么?
编辑:播放器1正常,因此问题在于播放器2.当播放器2获胜时,我的程序不显示“播放器2赢得了游戏”并错误地要求播放器1接下来移动(游戏已经完成)。
答案 0 :(得分:4)
我发现了这个问题。
当您将字符串与==
而非strcmp
进行比较时,如果存在空字符串,则比较将返回[]
。因此TRUE | []
的逻辑运算符返回[]
,这不是真的。
在您发布的示例中([1 1],[1 2],[2 1],[2 2],[1 3],[3 2])if
的最后一行返回[]
board{1,3}==board{2,2} & board{2,2}==board{3,1}
如果您用&&
和||
替换逻辑运算符,就可以解决问题。
答案 1 :(得分:3)
我同意Steffen,我也会将比较作为一个单独的功能。这是一个实现:
%// 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
功能gameWon
:
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
答案 2 :(得分:1)
这是真实的行为。
如果第一个语句是1,则if子句会起作用。
您应该使用一些默认值来初始化您的电路板 - &#39; - &#39;。 然后使用运算符&amp;&amp;和||而不是&amp;和|。 然后你还需要检查实际玩家的正确符号。
答案 3 :(得分:0)
&安培;和|是按元素进行逻辑比较,如果在内容为空的单元格上使用它们,则结果为0x0矩阵。
然后将得到的0x0矩阵与其他东西进行比较,其他东西也可能是0x0 matrizes或逻辑TRUE或逻辑FALSE。在任何情况下,程序都不会在if条件下执行您认为的操作。
解决这个问题的最简单方法是替换|通过||和&amp; by&amp;&amp;,因为后两者总是返回逻辑FALSE或TRUE。