gpt4 book ai didi

c - 用 C 语言编写井字游戏并指定规范

转载 作者:行者123 更新时间:2023-11-30 15:21:47 26 4
gpt4 key购买 nike

我正在为我的 C 编程入门类(class)做一个项目,我已经能够完成其中的大部分内容,但只是在寻找一些关于如何完成最后几个步骤的建议,因为我遇到了困难。1) 这是一个井字游戏程序,专为两个用户使用他们的名字手动玩而设计。2)此代码必须模块化(至少对于游戏板和检查获胜者功能)3)一旦宣布获胜者,应提示用户是否想再次玩4) 如果再次玩,必须从新的游戏板开始,并记录所玩的游戏并计算每个玩家获胜的次数。

到目前为止,我的代码可以接收并使用玩家姓名、玩游戏并检查获胜者。我所坚持的部分是弄清楚如何提示再次玩游戏,并在计算和显示玩过的游戏以及每个玩家获胜的次数的同时执行此操作。

任何帮助和建议都会很棒!谢谢!

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// Prototypes
int check_winner(char game_score[9], char player1[10], char player2[10]);
char game_board(char game_score[9]);

// Begin main
int main(void)
{
// Initialize ad declare variables
char player1[10], player2[10];
char game_score[9] = {'1','2','3','4','5','6','7','8','9'};
int go, game_count, player1_wins, player2_wins;
int i, turn, play_again;

printf("\nWelcome to Tic-Tac-Toe!!\n\nWhat is Player 1's name? You will be X's: ");
fgets(player1, 10, stdin);

printf("\nWhat is Player 2's name? You will be O's: ");
fgets(player2, 10, stdin);

// Begin game that last 9 turns
for (turn = 1 ; turn <= 9 ; turn++)
{
// Display the initial game board
game_board(game_score);

// Prompt the first player to select where to go
printf("\n\n%s\nit is your turn! Please select the number where\n", player1);
printf("you want to place your X: ");
scanf("%d", &go);

// Place X in the selected spot on the board
game_score[go-1] = 'X';

// Check for a winner
check_winner(game_score, player1, player2);

// Display the game board updated with Player 1's move
game_board(game_score);

// Prompt Player 2 to select where to go
printf("\n\n%s\nit is your turn! Please select the number where\n", player2);
printf("you want to place your O: ");
scanf("%d", &go);

// Place O in selected spot on the board
game_score[go-1] = 'O';

// Check for a winner
check_winner(game_score, player1, player2);
}



return 0;
}

// Check winner function
int check_winner(char game_score[9], char player1[10], char player2[10])
{
int i;

if (game_score[0] == 'X' && game_score[3] == 'X' && game_score[6] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);
else if (game_score[1] == 'X' && game_score[4] == 'X' && game_score[7] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);
else if (game_score[2] == 'X' && game_score[5] == 'X' && game_score[8] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);
else if (game_score[0] == 'X' && game_score[1] == 'X' && game_score[2] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);
else if (game_score[3] == 'X' && game_score[4] == 'X' && game_score[5] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);
else if (game_score[6] == 'X' && game_score[7] == 'X' && game_score[8] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);
else if (game_score[0] == 'X' && game_score[4] == 'X' && game_score[8] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);
else if (game_score[2] == 'X' && game_score[4] == 'X' && game_score[6] == 'X')
printf("\n\n%syou are the winner! Would you like to play again?: ", player1);

else if (game_score[0] == 'O' && game_score[3] == 'O' && game_score[6] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);
else if (game_score[1] == 'O' && game_score[4] == 'O' && game_score[7] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);
else if (game_score[2] == 'O' && game_score[5] == 'O' && game_score[8] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);
else if (game_score[0] == 'O' && game_score[1] == 'O' && game_score[2] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);
else if (game_score[3] == 'O' && game_score[4] == 'O' && game_score[5] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);
else if (game_score[6] == 'O' && game_score[7] == 'O' && game_score[8] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);
else if (game_score[0] == 'O' && game_score[4] == 'O' && game_score[8] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);
else if (game_score[2] == 'O' && game_score[4] == 'O' && game_score[6] == 'O')
printf("\n\n%syou are the winner! Would you like to play again?: ", player2);

}

// Game board display function
char game_board(char game_score[9])
{

printf("\n\n %c | %c | %c \n---+---+---\n %c | %c | %c \n---+---+---\n %c | %c | %c \n",
game_score[0], game_score[1], game_score[2], game_score[3], game_score[4],
game_score[5], game_score[6], game_score[7], game_score[8]);
return 0;
}

最佳答案

main 函数的内容放入另一个函数中,我将其命名为 play_game。在新的 main 中,循环运行 play_game

关于c - 用 C 语言编写井字游戏并指定规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29482391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com