现在我的数组打印如下:
0 | 1 | 2
-----------
3 | 4 | 5
-----------
6 | 7 | 8
我希望是空的,但不确定如何从我的代码中提取出来。我希望董事会看起来像这样:
| |
-----------
| |
-----------
| |
我不太清楚为什么我不知道如何让它发挥作用。有什么快速帮助吗?
在最初打印索引的情况下,您不必这样做,而且您的sizeof也不起作用。。。所以它变成了:-
void displayBoard(char board[]){
for(int i=0;i<9;i++){
printf(" %c ",board[i]);
if(i != 2 && i != 5 && i != 8) printf("|");
if(i == 2 || i == 5) printf("n------------n");
}
printf("n");
}
对于您的原件,sizeof(board)
等于4
因为它是函数的一个参数,并且是一个指针。
奖励答案:提供键盘映射
int keyboard_mapping[9] = {6,7,8,3,4,5,0,1,2};
int from_entry(char* s)
{
int v = atoi(s);
if(v < 1 || v > 9) return 0; // we have a problem...not handled
return keyboard_mapping[v-1];
}
然后类似于:-
board[atoi(move)] = 'X';
成为
board[from_entry(move)] = 'X';
更多奖金:
将first设置为1或2,具体取决于您希望玩家先上还是后上。
char move[] = "";
int turn;
int first;
//TODO ask the user whether to do go first or second
printf("Tic-Tac-ToenCreated by nYou are first! What's your move going to be?n");
while(checkForWin(board) == ' ' && boardFull(board) == 0){
printf("n");
displayBoard(board);
for(turn=1; turn <= 2; turn++;)
{
if(turn == first)
{
printf("nSelection a position to place your piece: ");
scanf("%s",move);
if(board[atoi(move)] == ' ')
{
board[atoi(move)] = 'X';
}
}
else
{
int compmove = chooseMove(board, 1).move;
board[compmove] = 'O';
}
}
else {
printf("n----------------------------------------nnPlease choose another location.nThis one has already been selected.nn----------------------------------------nn");
}
}
在displayBoard函数中,如果板阵列在插槽中有空间,则打印出板上的数字。如果你真的想总是在板上什么都不打印:把它改成这个:
void displayBoard(char board[]){
for(int i=0;i<=(sizeof(board)/sizeof(board[0]));i++){
//if(board[i] == ' ') printf(" %i ",i);
//else printf(" %c ",board[i]);
if(i != 2 && i != 5 && i != 8) printf("|");
if(i == 2 || i == 5) printf("n------------n");
}
printf("n");
}
真正的问题是,你试图用这段代码(我认为你没有写?)完成什么?