在Tic-Tac-Toe游戏(初级编码器)中需要一些变量的帮助

时间:2013-02-22 23:49:58

标签: c tic-tac-toe

我在使用C创建的Tic-Tac-Toe游戏时遇到了一些问题。

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

我几周都试过去弄清楚如何让比赛变得平局。即使你们可以指出我正确的方向,那将是甜蜜的!

-Frendin

(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");
    }
}

2 个答案:

答案 0 :(得分:1)

在函数playerOne()playerTwo()中,增加声明为函数参数的变量cats。您应该将值存储在cats开头声明的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的局部变量的值,我们可以:

  • 返回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()函数返回。也许可以考虑返回一个值,表明游戏是否已经结束,这样你就可以在发生这种情况时结束程序。

答案 1 :(得分: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();