Issue with Placement in Connect Four

Hi,

I am new to C++ and am trying my hand at programming a Connect Four game.

I've been trying my best to find what's gone wrong but after a good look I've come up result less. There are 42 spots where the "markers" go and each spot has a variable declared for it. One's represent the markers of Player 1 and two's represent the markers of Player 2.

http://i49.tinypic.com/de5qg1.jpg


I input Player 1's first turn, it goes in correctly and displays.


http://tinypic.com/r/1zfsbj6/6

Then I input Player 2's first turn, it also goes in without a problem.

http://tinypic.com/r/fx9vs3/6

Then comes Player 1's second turn; despite choosing only the fifth column, a 1 is place in the desired location and an extra 1 is place over the previous column I selected for Player 2's turn.

This happens over and over again with player 1 placing a marker in his desired column and one in the column that player 2 previously chose giving poor player 2 not a chance to win.


This is the main display for the game which I use for both players, changing a few things of course to differentiate from P1 and P2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout << endl;
cout << "Turn" << " "<< TurnCounter << endl;
cout << endl<< "Player 1, place your marker!";
 cout << endl;
 cout << endl;
 cout << "1 2 3 4 5 6 7" << endl;
 cout << "_____________" << endl;
 cout << endl;
 cout << Column1Row1 << " " << Column2Row1 << " " << Column3Row1 << " " << Column4Row1 << " " << Column5Row1 << " " << Column6Row1 << " " << Column7Row1 << endl;
 cout << Column1Row2 << " " << Column2Row2 << " " << Column3Row2 << " " << Column4Row2 << " " << Column5Row2 << " " << Column6Row2 << " " << Column7Row2 << endl;
 cout << Column1Row3 << " " << Column2Row3 << " " << Column3Row3 << " " << Column4Row3 << " " << Column5Row3 << " " << Column6Row3 << " " << Column7Row3 << endl;
 cout << Column1Row4 << " " << Column2Row4 << " " << Column3Row4 << " " << Column4Row4 << " " << Column5Row4 << " " << Column6Row4 << " " << Column7Row4 << endl;
 cout << Column1Row5 << " " << Column2Row5 << " " << Column3Row5 << " " << Column4Row5 << " " << Column5Row5 << " " << Column6Row5 << " " << Column7Row5 << endl;
 cout << Column1Row6 << " " << Column2Row6 << " " << Column3Row6 << " " << Column4Row6 << " " << Column5Row6 << " " << Column6Row6 << " " << Column7Row6 << endl;
 cout << "_____________" << endl;
int Player1Turn = 0;
cin >> Player1Turn;




There's a total of 7 integers declared to keep track of the columns and I add markers into columns using the following

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
//Input Player 1 markers for column 7

if (Player1Turn == 7 && Column7Counter == 6)
{cout << endl << "This column is full; select another column!" << endl;
goto main;}

if (Player1Turn == 7 && Column7Row2 == 1 && Column7Counter == 5 || Column7Row2 == 2 && Column7Counter == 5)
{Column7Row1 = 1;
Column7Counter++;}

if (Player1Turn == 7 && Column7Row3 == 1 && Column7Counter == 4 || Column7Row3 == 2 && Column7Counter == 4)
{Column7Row2 = 1;
Column7Counter++;}

if (Player1Turn == 7 && Column7Row4 == 1 && Column7Counter == 3 || Column7Row4 == 2 && Column7Counter == 3)
{Column7Row3 = 1;
Column7Counter++;}

if (Player1Turn == 7 && Column7Row5 == 1 && Column7Counter == 2 || Column7Row5 == 2 && Column7Counter == 2)
{Column7Row4 = 1;
Column7Counter++;}

if (Player1Turn == 7 && Column7Row6 == 1 && Column7Counter == 1 || Column7Row6 == 2 && Column7Counter == 1)
{Column7Row5 = 1;
Column7Counter++;}

if (Player1Turn == 7 && Column7Row6 == 0 && Column7Counter == 0)
{Column7Row6 = 1;
Column7Counter++;}

	}


All I do is change the Player # and Column/Row # and the Column Counter # and that pretty much is the bulk of my code.

Those two chunks are just changed a bit and looped using a goto to make my game.

Any tips, hints, etc. would be greatly appreciated.
Last edited on
1.) Don't use goto when you could just use a loop, it makes your code harder to follow
2.) When you have variables called Column1Row1 you should think about using an array (or std::vector)
Thanks for the response.

I'm not that familiar with arrays and am not sure how I would go about checking for a winner or placing markers with them hence why I am using quite sloppy code. Would there be anyway I would be able to fix the code I currently have? The project is due very soon so I have to use whatever I currently have.
Last edited on
Hrm, I'd need to see the entire section that you are looping...I would suggest putting it up on pastebin.com or something instead of here. Although I do hope that after you finish this assignment you will try to go back and improve your style, since it will only help you to learn how to do it in a more efficient way.
http://pastebin.com/m13aafc44

I really appreciate your assistance, after this assignment I will definitely go back and see where I could improve.
aren't u using way to many variables... use a damn array.

this seems like a good program to try... I'm going to try to make it(using a class) and re-post it.
Last edited on
Hi, I'm trying t his code out....

Will someone tell me why it isn't working?
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
#include <iostream>
#include <string>
using namespace std;

int knock = 6;
class makeGame
{
private:
int gameGrid [42];
int choice1;
int choice2;
public:
void setChoice(int n, int p){if(p > 0) {choice2 = n;} else {choice1 = n;}}
void setGame();
};




int main()
{
makeGame o;
o.setGame();
return 0;
}
void makeGame::setGame()
{
cout << "1234567";
cout << endl;
for(int b = 0; b < 42; b++)
{
gameGrid[b] = 0;
cout << gameGrid[b];
if(knock == b)
{
cout << endl;
knock += knock + 1;
}
}
}


this is the output

1234567
0000000
0000000
00000000000000
00000000000000

Last edited on
Topic archived. No new replies allowed.