Hello, so I have been working on a program that will eventually allow you to play connect four. As of now I am still worknig on the basic functions: playing a move, testing if the move wins and etc. my current code is this:
Code:
#include <iostream>
#include <vector>
using namespace std;
const short BOARDX = 7;
const short BOARDY = 6;
void output_board();
short play_move(short x, short player); //0 = fail 1 = succeed 2 = game won
bool test_win(short x, short y);
bool test_v(short x, short y);
bool test_h(short x, short y);
bool test_d(short x, short y);
vector<vector<char> > board(BOARDX, vector<char>(BOARDY));
int main()
{
cout << "Hello, this is a bare bones rendering of the connect four game." << endl;
for(short a = BOARDY; a >= 0; a--) //for testing
{
cout << play_move(a,1) << endl;
output_board();
}
return 0;
}
void output_board()
{
for(short y = BOARDY - 1; y >= 0; y--)
{
for(short x = 0; x < BOARDX; x++)
{
cout << "|";
switch (board[x][y])
{
case 0:
cout << " ";
break;
case 1:
cout << "X";
break;
case 2:
cout << "O";
break;
default:
cout << "E";
break;
}
}
cout << "|" << endl;
}
return;
}
short play_move(short x, short player)
{
//starting from the bottom, selects the bottom unoccupied space and puts the player there
//returns 2 if the game is won, 1 if it is successful, and 0 if the column is filled
for(short y = 0; y < BOARDY; y++)
{
if(!board[x][y])
{
board[x][y] = player;
if(test_win(x, y))
return 2;
return 1;
}
}
return 0;
}
bool test_win(short x, short y)
{
//test the entered location for a win
if(board[x][y])
{
if(test_h(x, y))
return 1;
if(test_v(x, y))
return 1;
if(test_d(x, y))
return 1;
}
return 0;
}
bool test_v(short x, short y)
{
//test verticals
short line_length = 1;
for(short x_down = x - 1; x_down >= 0 && board[x][y] == board[x_down][y]; x_down--)
line_length++;
if(line_length >= 4)
return 1;
return 0;
}
bool test_h(short x, short y)
{
//test horizontals, right is currently not working
short line_length = 1;
for(short y_right = y + 1; y_right < BOARDY && board[x][y] == board[x][y_right]; y_right++)
cout << line_length++ << endl;// cout for testing
for(short y_left = y - 1; y_left >= 0 && board[x][y] == board[x][y_left]; y_left--)
line_length++;
if(line_length >= 4)
return 1;
return 0;
}
bool test_d(short x, short y)
{
//test for diagonals with a positive slope
short line_length = 1;
short x_positive = x + 1;
short x_negative = x - 1;
short y_positive = y + 1;
short y_negative = y - 1;
for(; x_positive < BOARDX && y_positive < BOARDY && board[x][y] == board[x_positive][y_positive]; x_positive++ * y_positive++)
line_length++;
for(; x_negative >= 0 && y_negative >= 0 && board[x][y] == board[x_negative][y_negative]; x_negative-- * y_negative--)
line_length++;
if(line_length >= 4)
return 1;
// now test for diagonals with a negative slope
line_length = 1;
x_positive = x + 1;
x_negative = x - 1;
y_positive = y + 1;
y_negative = y - 1;
for(; x_positive < BOARDX && y_negative >= 0 && board[x][y] == board[x_positive][y_negative]; x_positive++ * y_negative--)
line_length++;
for(; x_negative >= 0 && y_positive < BOARDY && board[x][y] == board[x_negative][y_positive]; x_negative-- * y_positive++)
line_length++;
if(line_length >= 4)
return 1;
return 0;
}
When run,Ii get this output:
Code:
Hello, this is a bare bones rendering of the connect four game.
1
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | |X|
1
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | |X|X|
1
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | |X|X|X|
1
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |X|X|X|X|
1
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |X|X|X|X|X|
1
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| |X|X|X|X|X|X|
1
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
|X|X|X|X|X|X|X|
Process returned 0 (0x0) execution time : 0.109 s
Press any key to continue.
The problem I am having is, test_h does not appear to check to the right. which is done by this line:
Code:
for(short y_right = y + 1; y_right < BOARDY && board[x][y] == board[x][y_right]; y_right++)
cout << line_length++ << endl;
For whatever reason, this does not add to line_length, but the other half of test_h works fine. I have asked my teacher about this, rewritten the line 3 times and for the life of me I cannot figure out what is wrong.