照书抄的井字棋
c吧
全部回复
仅看楼主
level 3
刚日坂川 楼主
有两段的,第一段画了三个格子,想把他们放一起然后报错。
不知道怎么弄就单独第二段,反复找,它一直对着“?”报错
大致如下:
//program 5.8 tic_tac_toe
#include <stdio.h>
int main(void)
{
int player = 0;//current player number -1 or 2
int winner = 0;// the winning player number
int choice = 0;
unsigned int row = 0;
unsigned int column = 0;
char board[3][3] = {//the board
{'1','2','3'},// initial values are charact'1'to'9'
{'4','5','6'},//used to select a vacant square
{'7','8','9'} //for a player's turn.
};
//the main game loop. the game continues for up to 9 turns
// as long as there is no winner
for (unsigned int i = 0;i < 9 && winner == 0;++i)
{
//display the board
player = i % 2 + 1;
//游戏循环和显示方格板
//program 5.8 tic -tac-toe 不可反复定义
// get valid play er square selection
do
{
printf("player %d , please enter a valid square number"
"for where you want to place your %c: ",
player, (player == 1) ? 'x' : 'o');
scanf(" %d", &choice);
row = --choice / 3;
column = choice % 3;
}while (choice < 0 || choice >8 || board[row][column] > '9');
//insert player symbol
board[row][column] = { player == 1 } ? 'x' : 'o';
}
return 0;
}
2022年11月06日 04点11分 1
level 10
这句话 board[row][column] = { player == 1 } ? 'x' : 'o';
你把花括号换成 ()就好了。
或者改成if-else写法。试过了没什么大问题
2022年11月07日 02点11分 2
[呵呵]
2022年11月07日 03点11分
level 10
后续开发,这种三目运算符,尽量不用,其他语言像go都不支持这种写法。这种设计用if-else代替更清晰。[黑线]
2022年11月07日 02点11分 3
level 10
2022年11月07日 04点11分 4
1