简体   繁体   中英

C++ line repeats 8 times outside of a loop

I am writing a connect 4 program to reinforce my understanding of pointers, and decided to write it all in a class (I began learning programming in Java), but a certain line of code repeats over and over when it is reached and I cannot understand why.

I am using XCode. Here is the code I am running:

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


}

This is the offending code:

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

Whenever a player wins, the line "Player x is the winner!" is repeated 8 times in the console like so:

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!

Can anyone help me shed light on what I am doing wrong?

Your turn() method is clearly recursive, so once the game completes, it will print out "Player X has won!" For the number of turns your game ran.

The best way to fix the problem would be to refactor the code so it's using a loop that is broken by a win condition, rather than going recursive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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