Trying to make a connect 4

Alright, so I just started with c++; I'm using a book called "jumping into c++" and in chapter 14 they talk about pointers, pointers to pointers, etc. That's why I'm trying to make a connect 4 using pointers (I'm sure I'm complicating it a lot, but whatever) the problem is that I don't know how to scan the board to see if someone has won or not. I've tried to scan the whole board but the game just crashed; so... does someone know how could I check if someone has connected 4?

NOTE: The void called scan_move() it's supposed to be the function that detects if someone has won

Thanks!! Here's the 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
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
  #include <iostream>
#include <stdlib.h>

using namespace std;
 int** p_p_board;
 int y = 0;
 bool player_1 = true;
 bool moves = false;
 bool game_won = false;
 string name, player1, player2;
 void indicate_player()
 {
     if (player_1 == true)
    {
        name = player1;
    }
    else if (player_1 == false)
    {
        name = player2;
    }
 }

int create_board(int h, int w)
{
    p_p_board = new int* [h];
   for (int i = 0; i < h; i++)
   {
       p_p_board [i] = new int[w];
   }
   for (int i = 0; i < h; i++)
   {
       for (int j = 0; j < w; j++)
       {
           p_p_board [i][j] = 0;
       }
   }
}
void show_board(int h, int w)
{
    for (int i = 0; i < h; i++)
    {
        cout<< "\n";
        for (int j = 0; j < w; j++)
        {
            cout<< p_p_board [i][j]<< " ";
        }
    }
}
void player_move(int x, int h)
{
    for (int i = h; i--; i < 0)
    {
        if (moves == false)
        {
           if (p_p_board[i][x] == 0)
        {
            moves = true;
            y = i;
            if (player_1 == true)
            {
              p_p_board[i][x] = 1;
              player_1 = false;
            }
            else if (player_1 == false)
            {
                p_p_board[i][x] = 2;
                player_1 = true;
            }
        }
        }
    }
    moves = false;
}

void scan_move(int y, int x)
{

}
int main()
{
    cout << "What's your name, player 1? ";
    cin>>player1;
    cout << "\nWhat's your name, player 2? ";
    cin>>player2;
    system("CLS");

    int width, height, x;
    cout << "Please indicate the width: ";
    cin >>width;
    cout << "\nPlease indicate the height:";
    cin >>height;
    system("CLS");

   create_board(height, width);
   cout<< "THIS IS THE BOARD:\n";

   while (game_won == false)
   {
        indicate_player();
        show_board(height, width);
        cout<< "\n\n"<<name<<", in what column do you wish to put the coin?:";
        cin>>x;
        player_move(x, height);
        cout<<"\n\n\nthis is x:"<<x<<"\nThis is y:"<<y;
        scan_move(y, x);
        system("CLS");
   }
delete p_p_board;
}
Last edited on
You'd have to start from coordinate [0, 0] and check to see if four to the right or four below or four diagonally have the same "color." Something like so:
1
2
3
4
5
6
7
8
9
10
//limiting x value to board_width - 3 to not go beyond the dimension of the board
for (int x = 0; x < (board_width - 3); x++)
    for (int y = 0; y < (board_height - 3); y++)
        //checking to see if space has been played
        if ( p_p_board[x][y] != 0 )
        {
            //checking to see if the three spaces to the right of (x,y) are the same
            if ( p_p_board[x][y] == p_p_board[x+1][y] == p_p_board[x+2][y] == p_p_board[x+3][y] )
                cout << "Player " << p_p_board[x][y] << " wins the game!\n";
        }
It didn't work for me :/ could you write the whole function so it would work on my "game"? Cause if I do it the way you are saying the game just crashes
So I've been looking on the internet, and almost everybody uses the same function to see if someone won; and as my application just crashes as if there was a memory problem, I guess I'm doing something wrong related with the pointers and memory use... probably if I did the same application with arrays instead of pointers it would work :P thanks for the answer anyway! :D
Topic archived. No new replies allowed.