Quasi connect 4 help

Hi there. Totally new to this forum, and am sort of picking up programming again after not doing it for quite a while. I was just messing around, trying to make a game of connect 4 (though not really, since I think I have the wrong number of slots).

Anyway, I have a partially working program just trying to draw a board, take an input, and place a piece, but something is going wrong. Here is my code:

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
#include <iostream>

using namespace std;

void drawBoard(); // Draws the game board
void updateBoard();
void getMove(); // Gets player's move

char spaces[7][7]; // Creates the playing array


int main()
{
    int i;
    drawBoard();
    for(i=0; i<5; i++){
        getMove();
        updateBoard();
    }
    return 0;
}

//********************************************************************************************
void drawBoard()
{
    int i, j;
    
    cout << " 1  2  3  4  5  6  7  8" << endl;
    for(j=0; j<8; j++){
        cout << endl;
        for(i=0; i<8; i++){
            spaces[i][j]= '-';
            cout << "[" << spaces[i][j] << "]";
        }
    }
}
//********************************************************************************************
void getMove()
{
    int y = 0;
    int move = 0;
    
    cout << "Enter move by selecting number" << endl;
    cin >> move;
    
    for(y=7; y>=0; y--){
        if(spaces[move-1][y] == '-'){
            spaces[move-1][y] = 'x';
            break;
            
        }
    }
    
}
//********************************************************************************************
void updateBoard()
{
    int i, j;
    
    cout << " 1  2  3  4  5  6  7  8" << endl;
    for(j=0; j<8; j++){
        cout << endl;
        for(i=0; i<8; i++){
            cout << "[" << spaces[i][j] << "]";
        }
    }
}


And then here is the output:
 
 1  2  3  4  5  6  7  8

[-][-][-][-][-][-][-][-]
[-][-][-][-][-][-][-][-]
[-][-][-][-][-][-][-][-]
[-][-][-][-][-][-][-][-]
[-][-][-][-][-][-][-][-]
[-][-][-][-][-][-][-][-]
[-][-][-][-][-][-][-][-]
[-][-][-][-][-][-][-][-]Enter move by selecting number
2
 1  2  3  4  5  6  7  8

[-][-][x][-][-][-][-][-]
[-][-][-][-][-][-][-][-]
[-][-][-][-][-][-][-][-]
[-][-][-][-][-][-][-][-]
[-][-][-][-][-][-][-][-]
[-][-][-][-][-][-][-][-]
[-][-][-][-][-][-][-][-]
[-][x][-][-][-][-][-][-]Enter move by selecting number


I have a loop in the main() just so I could see if I got the stacking of pieces code right. But why is it placing an X in both the bottom of column 2 AND the top of column 3? I have tried messing with the getMove() function, as I feel that is where the error is but nothing I try changes it. I feel like it's some obvious thing I'm overlooking.

Any help would be greatly appreciated.
1
2
3
4
for(y=7; y>=0; y--){
        if(spaces[move-1][y] == '-'){
            spaces[move-1][y] = 'x';
            break;


As I'm sure you're aware, mainly from the move-1, is that arrays start at 0 and go to n-1, in your case 0-6. However, in the above loop, you start y at 7. I'm honestly surprised that doesn't crash your program. Changing that to 6 may fix your issue. I'll have to look a little more into the program to see if that's exactly what's causing the issue, but that's a start at least.

Again:
 
char spaces[7][7]; // Creates the playing array 


You declare your board as 7x7 array, but here at update board:
1
2
3
4
5
6
7
8
9
10
11
12
void updateBoard()
{
    int i, j;
    
    cout << " 1  2  3  4  5  6  7  8" << endl;
    for(j=0; j<8; j++){
        cout << endl;
        for(i=0; i<8; i++){
            cout << "[" << spaces[i][j] << "]";
        }
    }
}


You have j going to 7, which is again out of bounds. Same here:
1
2
3
4
5
6
7
8
9
10
11
12
13
void drawBoard()
{
    int i, j;
    
    cout << " 1  2  3  4  5  6  7  8" << endl;
    for(j=0; j<8; j++){
        cout << endl;
        for(i=0; i<8; i++){
            spaces[i][j]= '-';
            cout << "[" << spaces[i][j] << "]";
        }
    }
}


It looks like your code is based around an 8x8 array, but you're using a smaller array. I'd also look into that.

Edit: Also notice that your update board and draw board functions are identical, with the exception that you assign values in draw board. You could combine the functions into one, and just make an initialize board function that clears it out to '-'.
Last edited on
Thank you for the quick reply! Hah, I have my code so confused that your explanation confused me, and that led to me finding my error. Your very first line cleared everything up for me. I thought declaring it as [7][7] would result in an array numbered 0-7, not 0-6. I should have declared it as [8][8], which with that change my code appears to be functioning as it should, stacking and all.

I agree my drawBoard and updateBoard functions are somewhat redundant. My first thought was to combine them but just getting started again I wasn't sure of the best way to do it. I'll need to call updateBoard repeatedly until there is a winner. Hopefully I won't have too many more hiccups and can build a check in there so the functions can be combined.

Thank you again!

A real Connect 4 board is 7 by 7, if I'm not mistaken. I'm not positive on the height, but definitely the width. You can only have one possible winner width wise, but I believe the same can be true vertically as well. I suggest sticking with the 7x7 array, just simply changing those functions around. I'd also suggest, instead of using "magic" numbers, creating a global const int named HEIGHT and WIDTH for your array. You can then use these variables in your functions and to define your array. This way, if you decide you want to make a 20x20 board, you simply change those two values and tada, brand new game. Just a thought ;)
Yeah I think you're right on the real board size. It makes more sense that way. Hah to be honest I never really played that game, it just seemed easy enough to make starting out. Good call on the constants too, I think I'll do that. Thanks again!
Topic archived. No new replies allowed.