four in a row

This is a discussion on four in a row within the C++ Programming forums, part of the General Programming Boards category; Firstly I would like to say Hi and sorry if I post in the wrong sections or get anything wrong. ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    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

  2. #2
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Hi, could you be a bit more specific about your problem? Posting your source code might be a good idea too.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    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.

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    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?

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    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_|".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Quote Originally Posted by iMalc View Post
    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.
    Last edited by bluebell; 04-30-2011 at 04:10 PM.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Quote Originally Posted by bluebell View Post
    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.
    Last edited by bluebell; 05-01-2011 at 09:49 AM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21