我的任务不需要我制作一个完整的井字游戏程序。我只需要能够将 X 和 O 打印到板上即可。
我现在需要在板上打印“X”和“O”,这是最后一个阶段。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
//Declare the variables
//Create the board
//Initialize the board with all blanks
//Print to screen
//Prompt user for letter 'X', 'O' or 'q' to quit
//if the input is q, then quit.
//if the input is X or O then select positions.
//prompt user to choose rows & columns to mark X O positions
//declare the variables
char board[3][4], input; //input is for the X O q
char selectRows, selectColumns; //these are for choosing position to mark X or O
int rows, columns;
//create the basic empty board
for ( rows = 0 ; rows <3 ; rows++ )
{
for ( columns = 0 ; columns < 4 ; columns++ )
{
//Initialize array to blanks (' ')
board[rows][columns] = '|';
//print to screen
printf( "%c ", board[rows][columns] );
}
printf("\n\n\n");
}
//prompt the user to input X or O
printf( "\nHit X or O. 'q' to quit\n" );
scanf("%c", &input);
while ( input != 'q' )
{
//if the input is 'X' or 'O'
if ( input == 'X' || input == 'O' )
{
//select rows
printf("Choose 1 - 3 For Rows ");
scanf( "\n%c", &selectRows );
//select columns
printf("Choose 1 - 3 For Columns ");
scanf( "\n%c", &selectColumns );
//Print X or O on the board
if ( selectRows == 1 && selectColumns == 1 )
//prompt user to hit in X or O, q to quit again
printf( "\nHit X or O. 'q' to quit\n" );
scanf("%c", &input);
}
} //end while
}//end main
所以我能够打印空棋盘并要求用户输入 X 或 O 或 q 以退出游戏。
但是我不知道如何将 X 和 O 打印到板上。
如何将input
包含“X”或“O”的内容放置在正确的位置?我相信这些陈述应该在下面if ( selectRows == 1 && selectColumns == 1 )
如果我能做if ( selectRows == 1 && selectColumns == 1 )
对,我应该能够为其他selectRows
and做对selectColumns
。