如何让玩家选择是X还是O,并且可以选择再次玩游戏?

问题描述 投票:0回答:2

所以我在这个井字游戏中需要一些帮助。我几乎一切都正常工作,我只需要添加一些功能。这些功能包括: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.

任何和所有帮助将不胜感激。所以提前谢谢你!

c conditional-statements tic-tac-toe
2个回答
0
投票

要允许多轮,您可以在游戏外添加另一个 while 循环,以便重置所有统计数据并重新启动游戏,直到玩家决定停止(您可以使用 scanf 来检查 y/n)。


0
投票

您可以将 do while 与此结构一起使用:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//insert other libraries you need here
int main()
{
do
{
    //***insert rest of code here***
    printf("Do you want to play again? (Y/N)\n");
    char Response;
    scanf("%c", &Response);
    if(toupper(Response)==N)
    {
    printf("Thanks for playing!");
    break;
    }
}while(toupper(Response)==Y);
return 0;
}

***如果有语法错误请告诉我。我从 C 开始,但已经用基本指针编写了 Tic-Tac-Toe。这几乎就是我所做的。

© www.soinside.com 2019 - 2024. All rights reserved.