Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Connect 4 game in c. Please HELP!!!!! (http://www.go4expert.com/forums/connect-4-game-c-help-t9693/)

viktor 2Apr2008 07:18

Connect 4 game in c. Please HELP!!!!!
 
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!


All times are GMT +5.5. The time now is 02:23.