如何在C ++中改进Connect 4的代码?

时间:2018-07-01 19:15:18

标签: c++

我最近用C ++编写了Connect 4的代码。我想知道是否可以对代码进行任何改进。

connect4.cpp:

#include <iostream>
#include <stdlib.h>

char grid[6][7];

void setup_grid();
void display_grid();
void player_turn(char);
bool four_in_a_row(char);
bool grid_is_full();

int main()
{
    char player = '1';

    setup_grid();
    system("cls");

    while(true)
    {       
        display_grid();
        player_turn(player);

        if(four_in_a_row(player))
        {
            system("cls");
            display_grid();
            std::cout << "Player " << player << " has won!" << std::endl;
            goto end;
        }
        else if(grid_is_full())
        {
            system("cls");
            display_grid();
            std::cout << "It is a draw." << std::endl;
            goto end;
        }

        if(player == '1')
            player = '2';
        else if(player == '2')
            player = '1';

        system("cls");
    }

end:
    std::cin.get();
    std::cin.get();

    return 0;
}

void setup_grid()
{
    for(int i = 0; i < 6; i++)
        for(int j = 0; j < 7; j++)
            grid[i][j] = '0';
}

void display_grid()
{
    std::cout << std::endl;
    std::cout << "Enter a column number (1-7) to put a piece into that column." << std::endl;
    std::cout << std::endl;

    for(int i = 0; i < 6; i++)
    {
        for(int j = 0; j < 7; j++)
            std::cout << grid[i][j] << ' ';

        std::cout << std::endl;
    }

    std::cout << std::endl;
}

void player_turn(char player)
{
    int column_number;
    bool valid;

    do
    {
        valid = true;
        std::cout << "Player " << player << ": ";

        std::cin >> column_number;

        column_number--;

        if((column_number < 0) || (column_number > 6))
        {
            std::cout << "That number was not between 1 and 7" << std::endl;
            valid = false;
            continue;
        }

        if(grid[0][column_number] != '0')
        {
            std::cout << "Column is full." << std::endl;
            valid = false;
        }
    }
    while(!valid);

    for(int i = 0; i < 6; i++)
    {
        if(grid[i + 1][column_number] != '0')
        {
            grid[i][column_number] = player;
            break;
        }
        else if(i == 6)
        {
            grid[i][column_number] = player;
            break;
        }
    }
}

bool four_in_a_row(char player)
{
    // Horizontal check:

    for(int i = 0; i < 6; i++)

        for(int j = 0; j < 4; j++)

            if(grid[i][j] == player && grid[i][j+1] == player)
                if(grid[i][j+2] == player && grid[i][j+3] == player)
                    return true;

    // Vertical check:

    for(int i = 0; i < 3; i++)

        for(int j = 0; j < 7; j++)

            if(grid[i][j] == player && grid[i+1][j] == player)
                if(grid[i+2][j] == player && grid[i+3][j] == player)
                    return true;

    // Diagonal check:

    for(int y = 0; y < 3; y++)
    {
        for(int x = 0; x < 7; x++)
        {
            if(grid[y][x] == player)
            {

                // Diagonally left:
                if(grid[y+1][x-1] == player)
                {
                    if(grid[y+2][x-2] == player)
                        if(grid[y+3][x-3] == player)
                            return true;
                }

                // Diagonally right: (There is an error here)
                if(grid[y+1][x+1] == player)
                {
                    if(grid[y+2][x+2] == player)
                        if(grid[y+3][x+3] == player)
                            return true;
                }
            }
        }
    }

    return false;
}

bool grid_is_full()
{
    for(int y = 0; y < 6; y++)
    {
        for(int x = 0; x < 7; x++)
        {
            if(grid[y][x] == '0')
                return false;
        }
    }

    return true;
}

我知道goto语句(第36行)是不好的编程习惯,但似乎适合这种情况。

我应该使用camelCase还是snake_case?

在第48和49行上,为什么必须两次键入std :: cin.get()才能获得一个输入?

1 个答案:

答案 0 :(得分:0)

1)将I / J大小放在#define中,而不是多次使用6和7。

2)每次玩家放置一个条目时,将6 * 7计数变量减少1。当计数变量达到0时,grid_is_full。