Thread: Connect four game help

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    21

    Connect four game help

    hi all i know this might be a simple game for you guys but i need help with it, if anyone is willing to help then i thank u in advance.

    ive define this structure:

    struct Gamestate
    {
    char board [] [];
    char symbol;
    char nextTurn;
    };

    now i would like to the following function:
    void computerMove(Gamestate &);
    int checkWinner(Gamestate, &);
    bool saveGame (GameState, char []);
    bool loadGame (GameState &, char[]);




    Code:
    #include <iostream>
    
    using namespace std;
    
    const int ROW=7;
    
    const int COL=8;
    
    
    
    const char PLAYER1='O';
    
    const char PLAYER2='X';
    
    
    
    void drawHeader();
    
    char chooseOption();
    
    void initialiseBoard(char[row][col]);
    
    void drawBoard(char[row][col]);
    
    int playerMove(char[row][col], char, int);
    
    
    
    int main()
    
    {
    
    	int move=1;
    
    	bool quit=false, startGame=true;
    
    	char board[row][col], playAgain, option;
    
    	
    
    	drawHeader();
    
    	initialiseBoard(board); 
    
    	option = chooseOption();
    
    	if (option!='c') // quit not selected
    
    	{
    
    		while(!quit) //while game not finished 
    
    		{	
    
    			if(startGame)
    
    			{	 
    
    				initialiseBoard(board);
    
    				startGame=false;
    
    			}	  	  	  
    
    			
    
    			drawBoard(board);
    
    			move=playerMove(board, PLAYER1, 1);
    
    			if (move!=0) // player 1 has not quit current game
    
    			{
    
    				drawBoard(board);
    
    				move=playerMove(board, PLAYER2, 2);
    
    								}	 
    
    			if (move==0) // quit set by any of the players	  	  
    
    			{
    
    				cout << "Do you want to start a new game (y/n)?: ";
    
    				cin >>  playAgain;
    
    				if(playAgain == 'n' || playAgain =='N')
    
    					quit=true;
    
    				else
    
    					startGame=true;
    
    			}	 	 	 
    
    		}
    
    	}
    
    	cout << "\nThanks for playing Connect Four, the game of the clever people!\n";
    
    	   
    
    	return 0;
    
    }
    
    
    
    int playerMove(char board[row][col], char symbol, int player)
    
    {
    
    	char input;
    
    	int move;
    
    	bool legalMove=false, positionFound=false;
    
    
    
    	do{
    
    		cout << "Player " << player << " move (1-8, 0 to quit): ";
    
    		cin >>  input; //read as char to avoid cin errors
    
    		move = static_cast<int>(input) - 48; // convert to integer
    
    		if(move<0 || move >8)
    
    			cout << "Move not allowed!\n"; 
    
    		else if(move && board[0][move-1]!='.')
    
    			cout << "Sorry no more room in this column...\n";
    
    		else 
    
    			legalMove = true;	 	   	   
    
    	}while(move<0 || move >8 || !legalMove);
    
    	
    
    	if(move!=0)		
    
    	{
    
    		for(int row=ROW-2; row>=0  && !positionFound; row--)
    
    		{
    
    			if(board[row][move-1]=='.')
    
    			{
    
    				board[row][move-1]= symbol;
    
    				positionFound=true;
    
    			}
    
    		}
    
    	} 	  
    
    	return move;
    
    }
    
    
    
    
    
    void drawBoard(char board[row][col])
    
    {
    
    	for(int row=0; row < ROW; row++)
    
    	{
    
    		for(int col=0; col< COL; col++)
    
    			cout << board[row][col];
    
    			cout << endl;
    
    	}
    
    	return;
    
    }
    
    void initialiseBoard(char board[row][col])
    
    {
    
    	//create bottom line of game board: 12345678
    
    	for(int col=0; col < COL ; col++)
    
    		board[row-1][col] = col + '1';
    
    	//fill the rest of the board with .'s, that is initialise the rest of the 2D array to .'s
    
    	for(int row=0; row < ROW-1; row++)
    
    		for(int col=0; col < COL; col++)
    
    			board[row][col]='.';
    
    	
    
    	return;
    
    }	 	 	 
    
    void drawHeader()
    
    {
    
    	cout <<"***********************************************
    **********\n";
    
    	cout <<"*                   Connect Four                        *\n";
    
    	cout <<"***********************************************
    **********\n";
    
    	cout <<"Welcome to the Connect Four computer game. You can choose\n";
    
    	cout <<"to play against another player or against the computer\n";
    
    	cout <<" (not implemented in this version)\n";
    
    	cout <<"In this game the first player to position four of his/her\n";
    
    	cout <<"pieces on a line will win. The line can be horizontal, \n";
    
    	cout <<"vertical or at an angle, but the 4 pieces must be next to\n";
    
    	cout <<"each other.\n";
    
    	cout <<"***********************************************
    **********\n";
    
    	cout << endl;
    
    
    
    	return;
    
    }	 
    
    char chooseOption()
    
    {
    
    	char option;
    
    	do
    
    	{
    
    		cout <<"*************************************\n";
    
    		cout <<"*         Choose an Option          *\n";
    
    		cout <<"*************************************\n";
    
    		cout <<"* a) Play against the computer      *\n";
    
    		cout <<"* b) Play against another player    *\n";
    
    		cout <<"* c) Quit                           *\n";
    
    		cout <<"*************************************\n";
    
    		cout <<"Enter your choice: ";
    
    		
    
    		cin >> option;
    
    		if(option=='a')
    
    			cout << "\nThis option has not yet been implemented\n\n";
    
    	} while (option!='b' && option !='c');
    
    		
    
    	return option;
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Colchester, Essex, United Kingdom.
    Posts
    31
    ... and the problem is? That's a lot of code to look through. Perhaps you can point us towards a specific problem you're having.
    If it wasn't for C, we'd be using BASI, PASAL and OBOL.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    21
    im not too sure how to add these following function into the code:

    void computerMove(Gamestate &);
    int checkWinner(Gamestate, &);
    bool saveGame (GameState, char []);
    bool loadGame (GameState &, char[]);



    computer move does the random move on the game
    int checkWinner checks whos the winner of the game
    save game is i would like to save the game every time i make a move or the computer does
    and loading the game.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM
  3. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  4. u got me wrong fellas
    By clover in forum Game Programming
    Replies: 4
    Last Post: 10-18-2003, 04:23 PM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM