A connect 4 program problem

This is a connect 4 program that is not completed.
It prints a 9 by 9 board for 2 players to play on. The players type the column number for their piece. The piece is represented by 1 or 2 according to their player number. The players enter the column alternately.

I have encountered no error when running the compiler. However, when the players enter the column number, nothing changed on the 9 by 9 board. I suspect the problem lies in the enterNum function. I appreciate anyone that help me. Thank you!

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

using namespace std;

int coor[9][9];
int column = 0;

// This prints the 9 by 9 board
void display(){
    cout << "1 2 3 4 5 6 7 8 9\n\n";

    for(int y = 0; y < 9; y++){
        for(int x = 0; x< 9; x++){
            cout << coor[x][y] << " ";
        }
        cout << endl;
        }
}

// This asks the players to enter the column number
void enterNum(int player){
    cout << "This is player " << player << endl;
    cout << "Enter column number:" << endl;
    cin >> column;

    for(int y = 9; y < 1; y--){
        if(coor[column][y] > 0){
            coor[column][y+1] = player;
            } else if(coor[column][0] == 0&& y == 0){
                    coor[column][y] = player;
                }
                }
}

int main()
{
// This sets all the board values to 0
    for(int y = 0; y < 9; y++){
        for(int x = 0; x< 9; x++){
            coor[x][y] = 0;
        }
    }

// This runs all the functions
    while(1){
    display();

    enterNum(1);

    display();

    enterNum(2);

    }

    return 0;

}
You are correct.

You have designed enterNum() to place the user's piece ABOVE any EXISTING piece in the column.

However, your board starts out without any pieces at all. So enterNum() never finds one and... does nothing.

Hope this helps.
I understand that this would happen. Therefore, I have written an else if statement at the end the solve this problem. But why didn't it work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void enterNum(int player){
    cout << "This is player " << player << endl;
    
    while (cin)
    {
      cout << "Enter column number (1-9):" << endl;
      cin >> column;
      if (column < 1 || column > 9) continue;
      column -= 1;
      
      if (coor[column][0])
        cout << "Column is full. Try again.\n";
      else
        break;
    }

By the way, it is difficult to follow your board.

Standard Connect4 is 7 columns by 6 rows.

Users may have an easier time if you print { '-', 'X', 'O' } for { 0, 1, 2 }, for example. (Pick anything you like, though.)

Also, don't forget to check for winner or stalemate after every move.
You can, BTW, reduce that loop in main() by tracking whose turn it is.

I would move the column variable out of global scope, too.

Hope this helps.
Sorry for these mistakes, this is my first program that I have written on my own. I would improve on the messy code once I have learned the basics. But still the same question, why didn't the else if statement in the enterNum function solve the previous problem? Thanks for your advice by the way.
Topic archived. No new replies allowed.