我需要重新学习C,我正在学习的课程的一部分要求制作一个简单的井字棋游戏。我将代码设置为接受用户输入“输入行(0、1或2)和列(0、1或2),并用空格分隔”我想将其更改为简单的“输入1-9” “因为它只是一个简单的 3x3 网格,这将是一种更容易理解的方法。 谢谢你
type #include <stdio.h>
#include <stdlib.h>
#include <time.h>
//Initalization of the board in empty sate
void initializeBoard(char board[3][3]) {
for(int i=0; i<3;i++){
for(int j=0; j < 3;j++){
board[i][j]= ' ';
}
}
}
//Printing the empty board
void printBoard(char board[3][3]){
printf("\n");
printf(" %c | %c | %c \n",board[0][0],board[0][1],board[0][2]);
printf("---|---|---\n");
printf(" %c | %c | %c \n",board[1][0],board[1][1],board[1][2]);
printf("---|---|---\n");
printf(" %c | %c | %c \n",board[2][0],board[2][1],board[2][2]);
printf("\n");
}
//Check to see if Player wins
int checkWin(char board[3][3], char player) {
//Checks rows,columns,and diagonals for win condition
for(int i = 0; i<3;i++){
if(board[i][0]==player && board[i][1]==player && board[i][2]==player) {
return 1; //Player wins
}
if (board[0][i]==player && board[1][i] == player && board[2][i] == player) {
return 1; //Player wins
}
}
if (board[0][0]==player && board[1][1] == player && board[2][2] == player) {
return 1; //Player wins
}
if (board[0][2]==player && board[1][1] == player && board[2][0] == player) {
return 1; //Player wins
}
return 0; //No winner
}
//Checks if the board is full eqauling a tie
int isBoardFull(char board[3][3]) {
for (int i=0; i<3;i++) {
for (int j= 0; j<3;j++) {
if (board[i][j] == ' ') {
return 0; //Board is not yet full
}
}
}
return 1; //Board is full
}
// Computer's move, random placement
void computerMove(char board[3][3],char computer) {
int row, col;
do{
row =rand() % 3; //Generates a random row from 0-2
col =rand() % 3; // Generates a random column from 0-2
} while (board[row][col] !=' ');//Repeats generatiing until empty cell is found
board[row][col]=computer;
printf("Computer's move:\n");
printBoard(board);
}
int main() {
char board[3][3];
char playerName[10];
char player = 'X';
char computer = 'O';
//Random number generator
srand(time(NULL));
//Initialization of the board
initializeBoard(board);
//request and obtain players name
printf("Please enter your name here: ");
fflush(stdout);
scanf("%s", playerName);
printf(" %s you will be playing Tic Tac Toe agianst the computer, good luck.\n", playerName);
//loop for the game
while (1) {
int row, col;
// Prints the curent board config
printf("%s's move:\n", playerName);
printBoard(board);
// Get the player's move
printf("Enter row (0, 1, or 2) and column (0, 1, or 2) separated by a space: ");
scanf("%d %d", &row, &col);
// Check if the chosen cell is valid and empty
if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == ' ') {
board[row][col] = player;
// Check if the player has won
if (checkWin(board, player)) {
printf("Congratulations, %s! You win!\n", playerName);
break;
}
// Check if the board is full (tie)
if (isBoardFull(board)) {
printf("It's a tie!\n");
break;
}
// Computer's move
computerMove(board, computer);
// Check if the computer has won
if (checkWin(board, computer)) {
printf("Computer wins! Better luck next time, %s.\n", playerName);
break;
}
// Check if the board is full (tie)
if (isBoardFull(board)) {
printf("It's a tie!\n");
break;
}
} else {
printf("Invalid move. Try again.\n");
}
}
return 0;
}
here
我尝试创建一个开关盒块,但我没有任何运气。
与使用整数除法的良好注释并行工作和测试,并添加一些美观改进,以下是“while”循环中代码的重构版本。
while (1)
{
int row, col, position; /* Added a position entry for 1- 9 */
// Prints the curent board config
printf("%s's move:\n", playerName);
printBoard(board);
// Get the player's move
printf("Enter position 1 - 9: ");
scanf("%d", &position);
row = (position - 1) / 3; /* Derives the row from the entry */
col = position - row * 3 - 1; /* Derives the column from the entry */
printf("Row: %d Col: %d\n", row, col);
// Check if the chosen cell is valid and empty
if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == ' ' && board[row][col] == ' ') /* Need to insure selected spot is empty */
{
board[row][col] = player;
// Check if the player has won
if (checkWin(board, player))
{
printBoard(board); /* Added to display the final board */
printf("Congratulations, %s! You win!\n", playerName);
break;
}
// Check if the board is full (tie)
if (isBoardFull(board))
{
printBoard(board); /* Added to display the final board */
printf("It's a tie!\n");
break;
}
// Computer's move
computerMove(board, computer);
// Check if the computer has won
if (checkWin(board, computer))
{
printBoard(board); /* Added to display the final board */
printf("Computer wins! Better luck next time, %s.\n", playerName);
break;
}
// Check if the board is full (tie) /* Unless the computer starts the game, this test will never be true */
if (isBoardFull(board))
{
printf("It's a tie!\n");
break;
}
}
else
{
printf("Invalid move. Try again.\n");
}
}
以下是重点。
下面是一些示例终端输出。
Enter position 1 - 9: 9
Row: 2 Col: 2
X | O | X
---|---|---
| X |
---|---|---
O | O | X
Congratulations, Craig! You win!
Enter position 1 - 9: 5
Row: 1 Col: 1
Computer's move:
X | O | X
---|---|---
X | X |
---|---|---
O | O | O
Computer wins! Better luck next time, Craig.
Enter position 1 - 9: 8
Row: 2 Col: 1
X | O | O
---|---|---
O | X | X
---|---|---
X | X | O
It's a tie!
从中得到的收获可能是深入研究一些“C”教程,更多关于整数除法、数组索引以及输出“外观和感觉”。