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.