# four in a row

Printable View

• 04-30-2011
bluebell
four in a row
Firstly I would like to say Hi and sorry if I post in the wrong sections or get anything wrong. Anyway I have been looking at the four in a row game or connect four and was hoping somebody could send me in the right direction for help with the playing of the game for now I managed to do the board using 2 for loops but then ran into the problem of actually playing the pieces.

Thanks and Hi
• 04-30-2011
threahdead
Hi, could you be a bit more specific about your problem? Posting your source code might be a good idea too.
• 04-30-2011
bluebell
sorry I built a menu system that would call the game then built the board then got stuck with the actual placeing of the pieces for a 2 player game I was following a tutorial but it was difficult so tried to go with my own code and had a nice board but no weay of filling the spaces for the placement.

Code:

```int i,j;         cout << "\n";         cout << "\t\t"<<"_____________________________";         cout << "\n\t\t"<<"|  |  |  |  |  |  |  |  ";         for ( i=0; i <7; i++)         {                 cout << "\n" <<"\t\t" <<"|";                                 for (j=7; j > 0; j--)                                                 cout <<"___|" ;                         cout <<"\n\t        |  |  |  |  |  |  |  |";                         }                                         cout << "\n\t\t"<<"| 1 | 2 | 3 | 4 | 5 | 6 | 7 |";                 cout << "\n\n";```
that was my board and I know i need to have something to make spaces for the pieces and then be able to drop them to the bottom counting upwards.
• 04-30-2011
threahdead
I remember i had to program a backgammon game once with ASCII output.
You should have a data structure set up, that represents the placement of the stones. If it were to be a 2D Matrix you would go through your 2 for loops and check for every row and column, if there exists a stone already. Based on that, you can either print the symbol for a stone, or not.

How to store the positions of the stones in your program?
• 04-30-2011
iMalc
I'd probably format it more like this:
Code:

```+---+---+---+---+---+---+---+ |  |  |  |  |  |  |  | +---+---+---+---+---+---+---+ |  |  |  |  |  |  |  | +---+---+---+---+---+---+---+ |  |  |  |  |  |  |  | +---+---+---+---+---+---+---+ |  |  |  |  |  |  |  | +---+---+---+---+---+---+---+ |  |  |  | O |  |  |  | +---+---+---+---+---+---+---+ |  |  |  | X |  |  |  | +---+---+---+---+---+---+---+ |  |  |  | X | O |  |  | +---+---+---+---+---+---+---+   1  2  3  4  5  6  7```
Solving this is as easy as using an if or switch statement. Instead of always outputting "___|" you do an if-test and either output "___|" or "_O_|" or "_X_|".
• 04-30-2011
bluebell
Quote:

Originally Posted by iMalc
I'd probably format it more like this:
Code:

```+---+---+---+---+---+---+---+ |  |  |  |  |  |  |  | +---+---+---+---+---+---+---+ |  |  |  |  |  |  |  | +---+---+---+---+---+---+---+ |  |  |  |  |  |  |  | +---+---+---+---+---+---+---+ |  |  |  |  |  |  |  | +---+---+---+---+---+---+---+ |  |  |  | O |  |  |  | +---+---+---+---+---+---+---+ |  |  |  | X |  |  |  | +---+---+---+---+---+---+---+ |  |  |  | X | O |  |  | +---+---+---+---+---+---+---+   1  2  3  4  5  6  7```
Solving this is as easy as using an if or switch statement. Instead of always outputting "___|" you do an if-test and either output "___|" or "_O_|" or "_X_|".

that board looks better than mine and similiar to the program I saw on here, i have a solution and i kind of understand it but considering it is supposed to easy I am lost I did see a tutorial where the place function was added into the loop for the board and "_" spaces added but again your board looks better in that thwe pieces are easy to identify. I used a switch statement in my menu but was told using for loops and incrementing was the way to go. I need to go away and bang my head a little :) so sort of if user chooses 2 place x if user chooses 3 place x and if so how would i ensure it went to the bottom and counted up in increments until full.
• 04-30-2011
bluebell
Quote:

Originally Posted by bluebell
that board looks better than mine and similiar to the program I saw on here, i have a solution and i kind of understand it but considering it is supposed to easy I am lost I did see a tutorial where the place function was added into the loop for the board and "_" spaces added but again your board looks better in that thwe pieces are easy to identify. I used a switch statement in my menu but was told using for loops and incrementing was the way to go. I need to go away and bang my head a little :) so sort of if user chooses 2 place x if user chooses 3 place x and if so how would i ensure it went to the bottom and counted up in increments until full.

iMalc if i send you my code so far can you take a look for me.