connect four crashed...

below is my code
it compiles whitout errors, but when i run it, it crashes at wincheck ()

first prog btw, so be nice :)

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
#include <iostream>
using namespace std;


int field[7][7], p = 2, a, b, c;
int place, place2;

void clear ()
{   for (int a=0 ; a != 8 ; a++)
    {   for (int b=0 ; b != 9 ; b++)
        field[a][b] = 0;
    }
}

void draw ()
{   for (int a = 1 ; a != 9 ; a++ )
    cout << a << " , ";
    cout << endl;
    for (int a = 1 ; a != 32 ; a++ )
    cout << "-";
    cout << endl;
    
    
    for (int b = 0 ; b != 8 ; b++ )
    { 
      for (int a = 0 ; a != 8 ; a++ )
      cout << field[a][b] << " , ";
      cout << endl;
    }
}

void ask ()
{         cout << "it is player " << p << "turn" << endl << "pick numer 1-8 to drop " <<
     "a piece in that row" << endl;
     cin >> place;
     place--;
}

void drop ()
{
     for (int a=0 ; a != 8 ; a++)
     {
     if (field[place][a] == 0)
        place2 = a;
     }
     field[place][place2] = p;             
}

int horizontal ()
{   c = 0;
    for (int y = 0 ; y != 8 ; y++)
    {   for (int x = 0 ; x != 6 ; x++)
        {   c = field[x][y] + field[x+1][y] + field[x+2][y] + field[x+3][y];
            c==(4*p) ? p:c;
        }
    }
return (c);
}

int vertical ()
{   c = 0;
    for (int y = 0 ; y != 6 ; y++)
    {   for (int x = 0 ; x != 8 ; x++)
        {   c = field[x][y] + field[x][y+1] + field[x][y+2] + field[x][y+3];
            c==(4*p) ? p:c;
        }
    }
return (c);
}

int diagonal1 () //positive slope
{   c = 0;
    for (int y = 0 ; y != 6 ; y++)
    {   for (int x = 0 ; x != 6 ; x++)
        {   c = field[x][y+3] + field[x++][y+2] + field[x+2][y++] + field[x+3][y];
            c==(4*p) ? p:c;
        }    
    }
return (c);    
}

int diagonal2 () //negative slope
{   c = 0;
    for (int y = 0 ; y != 6 ; y++)
    {   for (int x = 0 ; x != 6 ; x++)
        {c = field[x][y] + field[x++][y+1] + field[x+2][y+2] + field[x+3][y+3];
        c==(4*p) ? p:c;
        }
    }
return (c);
}
    
int wincheck (int p)
{
int w = horizontal () + vertical () + diagonal1 () + diagonal2 ();
return (w);
}     
         

int main ()
{
clear ();   
do {
p = p==1 ? 2:1;
draw ();
ask ();
drop ();
} while (wincheck (p) == 0);
cout << "player " << p << ", you won!!!";

system ("pause");
return (0);
}
closed account (z05DSL3A)
I think the problem is in line 75.
{ c = field[x][y+3] + field[x++][y+2] + field[x+2][y++] + field[x+3][y];

The y++, in this line will take the value of y passed the guard y != 6 and then the for loop will spiral until it crashes.
so i should change all != into < sings?
closed account (z05DSL3A)
I think you would probably want to change line 75, try something like;

{ c = field[x][y+3] + field[x++][y+2] + field[x+2][y+1] + field[x+3][y];
Last edited on
it wont crash now, but when i enter stuff,
int place2 apears at field[7][4] (or [8][5], depending on how you look at it)
but i dont modify that spot (am i accidently using pointers? lol)

edit : it apears my comp is real buggy, sometimes the program "forgets" to draw "----" or comma's " , "

lol
Last edited on
Grey Wolf identified a serious problem. Change all those
field[x++][...]
and
field[...][y++]
lines to get rid of the increment operation. Just say:
field[x+1][...]
and
field[...][y+1]
and save variable increments for the right place.

Chances are near zero that it is your computer that is buggy. Never blame the tool; always assume first that you made an error. Such is a programmer's life.

When you compile, turn on all warnings that you can. Take them one at a time and get your code to compile without them. For example, if you are using the GCC, compile with

g++ -Wall -pedantic connect4.cpp -o connect4

One of the first things it will tell you is that on lines 50, 61, 72, and 83 you have declared c without a type --which is not legal C++. (It is a kludge to support old and poorly-written C code.)

------------------------------------

Unfortunately, most C++ compilers that I know of are not very good at compile-time range-checking, so I'll just tell you where it is a problem. (Grey Wolf also noticed this to you.)

Your fields[][] gameboard array is declared as a (7,7) matrix. However, in many of your loops (for example, lines 9 and 10) you exceed the valid subscript range, which is (0..6,0..6).

It would be worth your while to simply not hardcode subscript ranges into your program. (This is a very good rule of thumb. If you are ever breaking it you should immediately suspect it as a cause for errors.) Instead, use constant values where you can define the subscript ranges once, in a single place.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

// This is the size of my gameboard.
// (BTW, a standard Connect-4 gameboard is 6 rows by 7 columns.)
const int ROWS = 7;
const int COLS = 7;

// This is my gameboard.
int fields[ROWS][COLS];

This does two things. It allows you to change the gameboard size without breaking any code. And it is wonderful documentation by itself: you know exactly how many rows and columns there are and you know that your gameboard is organized with the major dimension as rows and the minor dimension as columns.

Thereafter it is convenient to use it without caring what the actual value of ROWS or COLS is:
1
2
3
4
5
6
void clear ()
{   for (int a=0 ; a < ROWS ; a++)
    {   for (int b=0 ; b < COLS ; b++)
        field[a][b] = 0;
    }
}

The "for (i=0; i<N; i++)" is a fairly standard idiom in C++. Again, whenever you see a for statement that doesn't quite match that pattern, suspect it as a potential error.

------------------------------------

The very last major error you are making is in the conditionals of your four-in-a-row routines:

Suppose that the gameboard contains [0][2][0][2] and you are checking to see if player 1 has won. 1*4 == 0+2+0+2. Player 1 wins.

It is nice to see that you are thinking of ways to compact information, but don't be too tricky at the outset. There are two simple ways to fix this error, but I'll leave that to you. ;-)


Nice organization and use of subroutines.

Once you get the above fixed, if you really want to wow your prof, consider the following: horizontal(), vertical(), diagonal1(), and diagonal2() all look nearly identical. Whenever you find yourself repeating code except for minor variations in variable or constant values, consider ways to combine it. You can combine all these by creating a routine with the following prototype:
int four( int rowcount, int colcount, int rowincr, int colincr )
where rowcount and colcount are upper bounds for the for loops (remember the idiom I showed you above), and rowincr, colincr are the amount to add to the loop variables when checking the fields (this indicates yet another loop where now you just have c = fields[...][...] + fields[...).

You will still need to call the routine four times though.

Hope this helps.
Last edited on
thanks

however, when i drop someting in row 0, the field looks someting like this:

1
2
3
4
5
6
7
8
0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 
1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 


notice the 7

then, i try to drop someting in the last row, the field looks like this:
1
2
3
4
5
6
7
8
0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 
1 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 


this is really weird, as i only mod the field once per loop ( drop () )

edit: i think i found it...
field[7][7] creates an array of 7*7, not 8*8...
Last edited on
Topic archived. No new replies allowed.