Connect 4 game in c. Please HELP!!!!!

Discussion in 'C' started by viktor, Apr 2, 2008.

  1. viktor

    viktor New Member

    Joined:
    Feb 27, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I am supposed to make a connect-4 game for my homework in c. So far I have a game board initialized. The biggest problem that I have right now is to sort of make the game board and work together with the rest of the code (the game rules, etc.) Here's what I have so far: (I have a main.c, ConnectFour.c and ConnectFour.h)
    Code:
    // main.c
    #include "ConnectFour.h"
    
    int main (void)
    {
    	// Note, Board is just a struct that contains a 
    	// 2-D array of Cells 
    	Board main_board;
    
    	initialize_game_board (&main_board);
    	display_game_board (main_board);
    
    	main_board.game_board[5][0].color = 'r';
    
    	display_game_board (main_board);
    	
    	return 0;
    }
    
    Code:
    // ConnectFour.c
    
    #include "ConnectFour.h"
    
    void initialize_game_board (Board *main_board)
    {
    	int row_counter = 0, column_counter = 0;
    
    	for (row_counter = 0; row_counter < ROWS; row_counter++)
    	{
    		for (column_counter = 0; column_counter < COLUMNS; column_counter++)
    		{
    			main_board->game_board[row_counter][column_counter].color = ' ';
    			main_board->game_board[row_counter][column_counter].location.column = column_counter;
    			main_board->game_board[row_counter][column_counter].location.row = row_counter;
    			main_board->game_board[row_counter][column_counter].occupied = FALSE;
    		}
    	}
    }
    
    void display_game_board (const Board main_board)
    {
    	int row_counter = 0, column_counter = 0;
    
    	printf ("%5d%4d%4d%4d%4d%4d%4d\n", 0, 1, 2, 3, 4, 5, 6);	
    
    	for (row_counter = 0; row_counter < ROWS; row_counter++)
    	{
    		printf ("%5c%4c%4c%4c%4c%4c%4c\n", '-', '-', '-', '-', '-', '-', '-');
    		printf ("%d", row_counter);
    		for (column_counter = 0; column_counter < COLUMNS; column_counter++)
    		{
    			printf ("%2c", '|');
    			printf ("%2c", main_board.game_board[row_counter][column_counter].color);
    		}
    		printf ("%2c\n", '|');
    	}
    	printf ("%5c%4c%4c%4c%4c%4c%4c\n", '-', '-', '-', '-', '-', '-', '-');
    }
    
    Code:
    // ConnectFour.h
    
    #ifndef CONNECT_FOUR_H
    #define CONNECT_FOUR_H
    
    #include <stdio.h>
    
    #define ROWS 6
    #define COLUMNS 7
    
    // This type will be used for determining whether or not
    // a game board cell is occupied, i.e. a game piece has been
    // placed in the cell
    typedef enum boolean
    {
    	FALSE, TRUE
    } Boolean;
    
    // This represents the location of a cell on the game board
    typedef struct coordinate
    {
    	int row;
    	int column;
    } Coordinate;
    
    // A cell is the place on the game board that may be
    // occupied with a game piece
    typedef struct cell
    {
    	Boolean occupied;
    	char color;
    	Coordinate location;
    } Cell;
    
    // Use a struct to define the game board
    typedef struct board
    {
    	Cell game_board[ROWS][COLUMNS];
    } Board;
    
    void initialize_game_board (Board *main_board);
    void display_game_board (const Board main_board);
    
    #endif





    Also some pseudocode that I have:

    * Initialize the board

    - Fill the board array with ' ' characters
    * Ask user for number of players
    - prompt
    - get number from user
    - return number
    * if one player – against computer, otherwise two humans
    * Play the game
    - While nobody has won (or cats game)
    - Get a valid move
    - Human?
    - Get a move
    - Check if valid column and if column is not full
    - If bad move, ask again
    - Return move
    - Computer?
    - Get random number between 0 and 6
    - Check if column is full, if so, get a new number
    - Return number
    - Update board with move
    - For the selected column
    - Start at the bottom
    - Move up while the cell isn't empty
    - Put player's marker into the cell
    - Print board
    - Check for win
    - Diagonal
    *
    - Horizontal
    - Vertical
    - Tie game

    Since I am working on it right now I'll probably update the thread within the next few hours. Any help would be appreciated guys!
     
    Last edited by a moderator: Apr 2, 2008

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice