如何让玩家选择X或O以及再次玩游戏的选项?
Posted
技术标签:
【中文标题】如何让玩家选择X或O以及再次玩游戏的选项?【英文标题】:How to let player chose to be X or O as well as an option to play the game again? 【发布时间】:2022-01-17 10:36:29 【问题描述】:所以我在这个井字游戏中需要一些帮助。我几乎所有东西都能正常工作,我只需要添加一些功能。这些功能包括:1.) 允许玩家选择他们想在游戏中扮演 X 还是 O,以及 2.) 如果玩家愿意,可以选择重玩游戏。我已经很好地完成了游戏的基本机制,但我只是不知道如何将这些功能添加到代码中。我已经开始为第一个功能“奠定基础”(让玩家可以选择他们的标记)。 (老实说,过去 48 小时的期末考试让我真的厌倦了,我真的可以在这里伸出援助之手)。这是我的完整代码:
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#include <stdio.h>
#include <conio.h>
int checkwin(char gridspace[]);
void gameboard(char gridspace[]);
int gameLoop(int choice, int player, char gridspace[], char mark);
void main(void)
char grid[10] = '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ;
int player = 1, victory, choice;
char mark, player1, player2;
int markchoice;
do // game loop start
gameboard(grid); // draws the game board
if (player % 2) // player turn conditional
player = 1;
else
player = 2;
printf("Player 1, choose which mark you would like to play as (1=X or 2=O)\n"); // asks the player to choose a mark
scanf("%c", &markchoice);
if (markchoice == 1) // mark choice conditional
mark = 'X';
else
mark = 'O';
printf("Player %d, enter the number of the place you wish to place your mark: ", player); // asks the player to place their mark on a space
scanf("%d", &choice);
if (player == 1) // mark type (X or O) placement conditional
mark;
else
mark;
gameLoop(choice, player, grid, mark); // calls the main game function that houses the conditionals
victory = checkwin(grid); // checks the state of the game (if its in progress, a draw, or a player has achieved victory.
player++;
while (victory == -1); //end of game loop
gameboard(grid); // redraws the game board
if (victory == 1) // conditional that breaks the game loop and gives a result (victory or draw)
printf("==>\aPlayer %d win ", --player);
else
printf("==>\aGame draw");
getch();
return 0;
// end of main
int gameLoop(int choice,int player, char gridspace[], char mark) // function for game loop
decisionals
if (choice == 1 && gridspace[1] == '1')
gridspace[1] = mark;
else if (choice == 2 && gridspace[2] == '2')
gridspace[2] = mark;
else if (choice == 3 && gridspace[3] == '3')
gridspace[3] = mark;
else if (choice == 4 && gridspace[4] == '4')
gridspace[4] = mark;
else if (choice == 5 && gridspace[5] == '5')
gridspace[5] = mark;
else if (choice == 6 && gridspace[6] == '6')
gridspace[6] = mark;
else if (choice == 7 && gridspace[7] == '7')
gridspace[7] = mark;
else if (choice == 8 && gridspace[8] == '8')
gridspace[8] = mark;
else if (choice == 9 && gridspace[9] == '9')
gridspace[9] = mark;
else
printf("Invalid move, input a different number.");
player--;
getch();
return 1;
// end of game loop function
int checkwin(char gridspace[]) // check win function
if (gridspace[1] == gridspace[2] && gridspace[2] == gridspace[3])
return 1;
else if (gridspace[4] == gridspace[5] && gridspace[5] == gridspace[6])
return 1;
else if (gridspace[7] == gridspace[8] && gridspace[8] == gridspace[9])
return 1;
else if (gridspace[1] == gridspace[4] && gridspace[4] == gridspace[7])
return 1;
else if (gridspace[2] == gridspace[5] && gridspace[5] == gridspace[8])
return 1;
else if (gridspace[3] == gridspace[6] && gridspace[6] == gridspace[9])
return 1;
else if (gridspace[1] == gridspace[5] && gridspace[5] == gridspace[9])
return 1;
else if (gridspace[3] == gridspace[5] && gridspace[5] == gridspace[7])
return 1;
else if (gridspace[1] != '1' && gridspace[2] != '2' && gridspace[3] != '3' &&
gridspace[4] != '4' && gridspace[5] != '5' && gridspace[6] != '6' && gridspace[7]
!= '7' && gridspace[8] != '8' && gridspace[9] != '9') // if all spaces are filled but no
three match then the game is a draw
return 0;
else // if no three spaces match and empty spaces still exist then the game is still in
progress.
return -1;
// end of check win function
void gameboard(char gridspace[]) // game board function.
system("cls");
printf("\n\n\tTic Tac Toe\n\n");
printf("Enter a number for where you wish to place your mark");
printf("Player 1 (X) - Player 2 (O)\n\n\n");
printf(" | | \n");
printf(" %c | %c | %c \n", gridspace[1], gridspace[2], gridspace[3]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", gridspace[4], gridspace[5], gridspace[6]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", gridspace[7], gridspace[8], gridspace[9]);
printf(" | | \n\n");
// end of board drawing funciton.
我们将不胜感激任何和所有的帮助。所以提前谢谢你!
【问题讨论】:
【参考方案1】:要允许多轮循环,您可以在游戏之外添加另一个 while 循环,以重置所有统计数据并重新开始游戏,直到玩家决定停止(您可以使用 scanf 来检查 y/n)。
【讨论】:
您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。以上是关于如何让玩家选择X或O以及再次玩游戏的选项?的主要内容,如果未能解决你的问题,请参考以下文章