0

我在使用 C 语言创建的井字游戏时遇到了一些问题。

我有一个名为 int cats 的变量(猫游戏是平局游戏),用于帮助计算和显示平局游戏的文本。

我几乎整个星期都在尝试弄清楚如何让比赛打成平局。即使你们能指出我正确的方向,那也太好了!

-弗伦丁

(PS开头的空if语句是针对玩家与电脑游戏的。如果我能弄清楚如何让游戏打成平局,我可以编写那部分没问题!再次感谢大家!)

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

void printTheBoard(char boardPieces[3][3]);
int playerOne(char boardPieces[3][3], int cats);
int playerTwo(char boardPieces[3][3], int cats);
void checkBoard(char boardPieces[3][3], int cats);

int main(void)
{
    char boardPieces[3][3] = {'\0'};
    int cats = 0;  //A tie game in Tic-Tac-Toe is called a Cats Game.
    int choice;

    printf("Would you like to play against the computer or another player?\n");
    printf("Press 1 to play against another player.\n");
    printf("Press 2 to play against the computer.\n");
    scanf("%d", &choice);

    if(choice == 1)
    {
        for(int i = 0; i < 5; ++i)
        {
            printTheBoard(boardPieces);
            playerOne(boardPieces, cats);
            checkBoard(boardPieces, cats);

            printTheBoard(boardPieces);
            playerTwo(boardPieces, cats);
            checkBoard(boardPieces, cats);
        }   
    }

    if(choice == 2)
    {

    }

    return 0;
}

void printTheBoard(char boardPieces[3][3])
{
    printf("\n %c | %c | %c\n", boardPieces[0][0], boardPieces[0][1], boardPieces[0][2]);
    printf("---|---|---\n");
    printf(" %c | %c | %c\n", boardPieces[1][0], boardPieces[1][1], boardPieces[1][2]);
    printf("---|---|---\n");
    printf(" %c | %c | %c\n\n", boardPieces[2][0], boardPieces[2][1], boardPieces[2][2]);
}

int playerOne(char boardPieces[3][3], int cats)
{
    int choice;

    printf("It is your turn. You are player O. Please enter in the space you would like to  take. The first space is 1, continuing left to right, top to bottom\n");
    scanf("%d", &choice);

    if(choice == 1)
    {
        boardPieces[0][0] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 2)
    {
        boardPieces[0][1] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 3)
    {
        boardPieces[0][2] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 4)
    {
        boardPieces[1][0] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 5)
    {
        boardPieces[1][1] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 6)
    {
        boardPieces[1][2] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 7)
    {
        boardPieces[2][0] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 8)
    {
        boardPieces[2][1] = 'O';
        ++cats;
        return cats;
    }
    else if(choice == 9)
    {
        boardPieces[2][2] = 'O';
        ++cats;
        return cats;
    }
}

int playerTwo(char boardPieces[3][3], int cats)
{
    int choice;

    printf("It is your turn. You are player X. Please enter in the space you would like to  take. The first space is 1, continuing left to right, top to bottom\n");
    scanf("%d", &choice);

    if(choice == 1)
    {
        boardPieces[0][0] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 2)
    {
        boardPieces[0][1] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 3)
    {
        boardPieces[0][2] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 4)
    {
        boardPieces[1][0] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 5)
    {
        boardPieces[1][1] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 6)
    {
        boardPieces[1][2] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 7)
    {
        boardPieces[2][0] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 8)
    {
        boardPieces[2][1] = 'X';
        ++cats;
        return cats;
    }
    else if(choice == 9)
    {
        boardPieces[2][2] = 'X';
        ++cats;
        return cats;
    }
}

void checkBoard(char boardPieces[3][3], int cats)
{
    if(boardPieces[0][0] == 'O')
    {
        if(boardPieces[0][1] == 'O')
        {
            if(boardPieces[0][2] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
        else if(boardPieces[1][1] == 'O')
        {
            if(boardPieces[2][2] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
        else if(boardPieces[1][0] == 'O')
        {
            if(boardPieces[2][0] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
    }
    else if(boardPieces[0][2] == 'O')
    {
        if(boardPieces[1][1] == 'O')
        {
            if(boardPieces[2][0] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
        else if(boardPieces[1][2] == 'O')
        {
            if(boardPieces[2][2] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
    }
    else if(boardPieces[1][1] == 'O')
    {
        if(boardPieces[0][1] == 'O')
        {
            if(boardPieces[2][1] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
        else if(boardPieces[1][0] == 'O')
        {
            if(boardPieces[1][2] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
    }
    else if(boardPieces[2][0] == 'O')
    {
        if(boardPieces[2][1] == 'O')
        {
            if(boardPieces[2][2] == 'O')
            {
                printf("Player O has won the game!\n");
            }
        }
    }




    if(boardPieces[0][0] == 'X')
    {
        if(boardPieces[0][1] == 'X')
        {
            if(boardPieces[0][2] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
        else if(boardPieces[1][1] == 'X')
        {
            if(boardPieces[2][2] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
        else if(boardPieces[1][0] == 'X')
        {
            if(boardPieces[2][0] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
    }
    else if(boardPieces[0][2] == 'X')
    {
        if(boardPieces[1][1] == 'X')
        {
            if(boardPieces[2][0] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
        else if(boardPieces[1][2] == 'X')
        {
            if(boardPieces[2][2] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
    }
    else if(boardPieces[1][1] == 'X')
    {
        if(boardPieces[0][1] == 'X')
        {
            if(boardPieces[2][1] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
        else if(boardPieces[1][0] == 'X')
        {
            if(boardPieces[1][2] == 'X')
            {
                printf("Player X has won the game!\n");
            }
        }
    }
    else if(boardPieces[2][0] == 'X')
    {
        if(boardPieces[2][1] == 'X')
        {
            if(boardPieces[2][2] == 'X')
            {
                printf("Player O has won the game!\n");
            }
        }
    }



    else if(cats == 9)
    {
        printf("There was a tie game!\n");
    }
}
4

2 回答 2

1

在函数中playerOne()playerTwo()您递增cats声明为函数参数的变量。您应该将值存储在cats您在 start of 中声明的变量中main,例如:

for(int i = 0; i < 5; ++i)
{
    printTheBoard(boardPieces);
    cats = playerOne(boardPieces, cats);
    checkBoard(boardPieces, cats);

    printTheBoard(boardPieces);
    cats = playerTwo(boardPieces, cats);
    checkBoard(boardPieces, cats);
}

更详细的解释如下。考虑以下代码:

void func(int var)
{
    var = 2;
}

int main(void) 
{
    int var;
    var = 1;
    func(var);
}

这里有两个不同的变量名称 var 一个在 的开头声明main,它的作用域或生命周期在main函数内。

另一个被声明为 的参数func,它的作用域是func函数的本地。这意味着当函数返回时,变量及其值不再存在。 func

现在,如果我们想func在函数返回后使用 's 局部变量的值,我们可以:

  • 返回由 修改的值func

    int func(int var)
    {
        var++;
        return var;
    }
    ...
    new_var = func(old_var);
    
  • func改为通过指针就地修改变量:

    void func(int *var)
    {
        (*var)++;
    }
    ...
    func(&var);
    

此外,在您当前的代码中永远不会运行else if最后打印平局消息的条件:checkBoard()

...
else if(boardPieces[2][0] == 'X')
{
    if(boardPieces[2][1] == 'X')
    {
        if(boardPieces[2][2] == 'X')
        {
            printf("Player O has won the game!\n");
        }
    }
}

else if(cats == 9)
{
    printf("There was a tie game!\n");
}

checkBoard()考虑在打印游戏结果后从函数返回。也许考虑返回一个值来指示游戏是否已经结束,这样你就可以在发生这种情况时结束程序。

于 2013-02-23T00:03:25.700 回答
0

我也是一个初学者程序员。我只是想用 C++ 分享我的井字游戏。我只是想分享它,因为我认为如果您使用 for 循环将值放入表中,它可以以更有效的方式创建。

这是主文件

#include <iostream>
#include <vector>
#include "tic_tac.hpp"
#include <bits/stdc++.h>


int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    printf("-------------------------Welcome to the Tic Tac Toe game-------------------------\n");
    printf("Now steps to play this game is simple:- \n-> Enter the position at which you want to mark your sign.\n\n");
    printf("Just in case for position reference of the table\n\n");
    printf("  1  |  2  |  3  \n");
    printf("------------------\n");
    printf("  4  |  5  |  6  \n");
    printf("------------------\n");
    printf("  7  |  8  |  9  \n");
    printf("\n Ready to play the game(1 for YES and 0 for no): ");
    int start;
    scanf("%d", &start);
    if (start == 1) {
        game_run();
    }
    else {    
        return 0;
    }
}

这是函数文件“tic_f.cpp”

#include <vector>
#include <iostream>
#include "tic_tac.hpp"
#include <algorithm>

//I am using 0 for player a and X for player b

int game_run() {
    //This the vector to which we pass input.
    std::vector<std::string> game_p = {" ", " ", " ", " ", " ", " ", " ", " ", " "};
    bool game_on = true;
    std::string user_t = "A";
    
    while (game_on) {
        int user_in;

        for (int i = 0; i < 9; i += 3) {
            std::cout << std::endl << " " + game_p[i] + "  |  " + game_p[i+1] + "  |  " + game_p[i+2] << std::endl;
            
            if (i < 6) {
                std::cout << "--------------\n";
            }    
        }
        //Check if any user has won
        //Thus variable check if the game has tie
        bool it = false;
        for (int i = 0; i < 9; i++) {
            if (game_p[i] == " ") {
                it = true;
            }
        }
        bool win_check = win(game_p);

        if (win_check) {
            std::cout << "\n\nPlayer " + user_t +  " won the game. Congrats!\n";
            game_on = false;
            return 0;
        }
        else if (!it) {
            std::cout << "Ohh, that's a tie. Nice game fellas.\n";
            game_on = false;
            return 0;
        }
        ;

        //This checks on which user turn it is
        if (user_t == "A") {
            user_t = "B";
        }
        else if (user_t == "B") {
            user_t = "A";
        }
        printf("\nEnter the position you want to make the sign: ");
        scanf("%1d", &user_in);

        if (user_t == "A" && user_in <= 9) {
            game_p[user_in - 1] = "0";
        }
        else if (user_t == "B" && user_in <= 9) {
            game_p[user_in -1] = "X";
        }
        
    }



}

bool win(std::vector<std::string> game_l) {
    bool what_r = false;
    for (int i = 0; i< 9; i += 3) {
        if (game_l[i] == game_l[i+1] && game_l[i] == game_l[i+2] && game_l[i] != " ") {
            what_r = true;
        }
    }
    for (int i = 0; i < 3; i++) {
        if (game_l[i] == game_l[i+3] && game_l[i] == game_l[i+6] && game_l[i] != " ") {
            what_r = true;
        }
    }    
    for (int i = 0; i < 3; i+= 2) {
        if (i == 0 && (game_l[i] == game_l[i+4] && game_l[i] == game_l[i+8])&& game_l[i] != " ") {
            what_r = true;
        }
        else if (i==2 && (game_l[i] == game_l[i+4] && game_l[i] == game_l[i+6]) && game_l[i] != " ") {
            what_r = true;
        }
    }
    return what_r;
}

这是头文件“tic_tac.hpp”

#include <vector>
#include <string>
#include <stack>

bool win(std::vector<std::string> game_l);
int game_run();

于 2021-05-01T09:13:54.763 回答