Connect Four Help

So I have this program that uses a C4Col class that will represent the columns of a Connect 4 board. It also has the C4Board class which creates an array of C4Col class objects. The problem I'm running into is that when I add a piece after the second player's turn, if the piece happens to be next to the previous player's move, the program adds 1 to the location the previous player entered. I'm using 'X' and 'O' to represent the pieces so to clarify, the program would go on to change the X piece to a Y or the O piece to an O, any help would be greatly appreciated! (I know the code below doesn't have a way for verifying a winner yet!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifndef ConnectFour_C4Col_h
#define ConnectFour_C4Col_h

class C4Col{
public:
    C4Col();
    int isFull();
    int getMaxDisc();
    char getDisc(int);
    void addDisc(char);
    
private:
    int numDiscs;
    int maxDiscs;
    char Discs[];
};

#endif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include <iostream>
#include "C4Col.h"

C4Col::C4Col(){
    numDiscs = 0;
    maxDiscs = 6;
    for (int i = 0; i < maxDiscs ; i++){
        Discs[i] = ' ';
    }

}

int C4Col::isFull(){
    if (numDiscs == maxDiscs){
        return 1;
    }
    else return 0;
}

int C4Col::getMaxDisc(){
    return maxDiscs;
}

char C4Col::getDisc(int a){
    if (a >= maxDiscs){
        std::cout << "INVALID SELECTION.";
    }
    return Discs[a];
    
}

void C4Col::addDisc(char piece){
    Discs[numDiscs++] = piece;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


#ifndef ConnectFour_C4Board_h
#define ConnectFour_C4Board_h

#include "C4Col.h"

class C4Board{
public:
    C4Board();
    void display();
    void play();
private:
    int columns;
    C4Col Board[100] ;
};


#endif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include <iostream>
#include "C4Col.h"
#include "C4Board.h"
using namespace std;

C4Board::C4Board(){
    columns = 7;
    
}

void C4Board::display(){
    for (int c = 1 ; c <= columns ; c++){
        cout << " " << c;
    }
    cout << "\n";
    for (int i = Board[0].getMaxDisc() - 1 ; i >= 0 ; i--){
        cout << "|";
        for (int j =0 ; j < columns ; j++){
            cout << Board[j].getDisc(i) << "|";
        }
        cout << endl;
    }
}

void C4Board::play(){
    int move;
    int turn = 1;
    const char player1 = 'X';
    const char player2 = 'O';
    char playChoice;
    bool GameOver = false;
    cout << "Welcome to Connect 4!" << endl;
    while(!GameOver){
        if (turn%2 == 1){
                 display();
                cout << "Player one enter your move: ";
                cin >> move;
                if (Board[--move].isFull()){
                    cout << "Invalid Selection. Please try again." << endl;
                    turn = turn;
                }
                else{
                Board[move].addDisc(player1);
                turn++;
                }
        }
        
        if (turn %2 == 0){
                    display();
                cout << "Player two enter your move: ";
                cin >> move;
                if (Board[--move].isFull()){
                    cout << "Invalid Selection. Please try again." << endl;
                    turn = turn;
                }
                else {
                    Board[move].addDisc(player2);
                    turn++;
                }
        }
        if (turn > 42){
            GameOver = true;
        }
        if (GameOver){
            cout << "Game Over!\nWould you like to play again? (y or n)" << endl;
            cin >> playChoice;
            if (playChoice != 'y' || playChoice != 'n'){
                cout << "Invalid Selection. Try again." << endl;
                GameOver = true;
            }
            if (playChoice == 'y'){
                GameOver = false;
            }
            if (playChoice == 'n'){
                // Display score ie wins for each player
                cout << "Player Scores: " << endl;
                break;
            }
        }
        
    }
}
Topic archived. No new replies allowed.