繁体   English   中英

C ++行在循环外重复8次

[英]C++ line repeats 8 times outside of a loop

我正在编写一个connect 4程序,以加强对指针的理解,并决定将其全部写在一个类中(我开始学习Java编程),但是当到达该行时,某行代码会不断重复,我无法理解为什么。

我正在使用XCode。 这是我正在运行的代码:

class Connect_4{

public:

char **p_p_board = NULL;
int w; //Width stores the arrays that make up the vertical columns of the connect 4 board
int h; //Height is how large the arrays that make up the vertical colums are
Player *p_player_1 = NULL;
Player *p_player_2 = NULL;
bool player_two_turn = false;

Connect_4(){

    w = 7;
    h = 6;

    p_player_1 = new Player('x');
    p_player_2 = new Player('+');

    init_board();

}

Connect_4(int width, int height){

    w = width;
    h = height;

    p_player_1 = new Player('x');
    p_player_2 = new Player('+');

    init_board();

}

Connect_4(int width, int height, Player* one, Player* two){

    w = width;
    h = height;

    p_player_1 = one;
    p_player_2 = two;

    init_board();

}

void init_board(){

    p_p_board = new char*[w];

    for(int i = 0; i < w; i++){

        p_p_board[i] = new char[h];

        for(int j = 0; j < h; j++){

            p_p_board[i][j] = '_';

        }

    }

}

void draw(){

    cout<<"\n";

    for(int i = h; i >= 0; i--){
        for(int j = 0; j < w; j++){

            if(i != h){

                cout<<p_p_board[j][i]<<" ";

            }else{

                cout<<j<<" ";

            }

        }
        cout<<"\n";
    }

    cout<<"\n";

}

bool insert_piece(int index, char piece){

    bool inserted = false;

    for(int i = 0; i < h; i++){

        if(p_p_board[index][i] == '_'){

            p_p_board[index][i] = piece;
            inserted = true;
            break;

        }

    }

    return inserted;

}

void check_board(){

    char p1 = p_player_1->p;
    char **p = p_p_board;

    for(int i = 0; i < w; i++){
        for(int j = 0; j < h-3; j++){

            if(p[i][j] == '_') break;

            if(p[i][j] == p[i][j+1] && p[i][j] == p[i][j+2] && p[i][j] == p[i][j+3]){
                if(p[i][j] == p1){
                    p_player_1->won = true;
                    break;
                }else{
                    p_player_2->won = true;
                    break;
                }
            }

        }
    }

}

void turn(){

    draw();

    int input;

    cout<<"Player "<<(player_two_turn+1)<<", it is your turn. In which column will you drop a piece?  ";
    cin>>input;

    if(input >= w || input < 0){
        cout<<"ERROR: INVALID INPUT";
        cout<<"\n";
        sleep(1);
        turn();
    }

    if(!player_two_turn){
        if(!insert_piece(input, p_player_1->p)){
            cout<<"ERROR: COULD NOT INSERT PIECE";
            cout<<"\n";
            sleep(1);
            turn();
        }
    }else{
        if(!insert_piece(input, p_player_2->p)){
            cout<<"ERROR: COULD NOT INSERT PIECE";
            cout<<"\n";
            sleep(1);
            turn();
        }
    }

    check_board();

    if(!p_player_1->won && !p_player_2->won){
        player_two_turn = !player_two_turn;
        turn();
    }

    if(p_player_1->won){
        cout<<"Player one is the winner!\n";
    }else{
        cout<<"Player two is the winner!\n";
    }

}

};

int main()
{

    Connect_4 game = Connect_4();

    game.turn();

    cin.get();


}

这是令人反感的代码:

    if(p_player_1->won){
        cout<<"Player one is the winner!\n";
    }else{
        cout<<"Player two is the winner!\n";
    }

每当玩家获胜时,“ Player x是获胜者!”这一行! 在控制台中重复8次,如下所示:

Player one is the winner!
Player one is the winner!
Player one is the winner!
Player one is the winner!
Player one is the winner!
Player one is the winner!
Player one is the winner!
Player one is the winner!

谁能帮我弄清楚我做错了什么?

您的turn()方法显然是递归的,因此一旦游戏完成,它将打印出“ Player X获胜!”。 对于您的游戏回合数。

解决该问题的最佳方法是重构代码,以便它使用被获胜条件破坏的循环,而不是递归的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM