Thread: Why is connect 4 game saying a winner? C programming

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    33

    Why is connect 4 game saying a winner? C programming

    I cant get my code to say the winner after 4 spaces have been occupied?
    Code:
    #include "ConnectFour.h"
    
    
    void initialize_game_board (Cell game_board [6][7])
    {
    	int row_index = 0, col_index = 0;
    
    	for (row_index = 0; row_index < 6; row_index++)
    	{
    		for (col_index = 0; col_index < 7; col_index++)
    		{
    			game_board[row_index][col_index].color = NONE;
    			game_board[row_index][col_index].occupied = FALSE;
    			game_board[row_index][col_index].location.row = row_index;
    			game_board[row_index][col_index].location.column = col_index;
    		}
    	}
    }
    
    void print_game_board (Cell game_board [6][7])
    {
    	int row_index = 0, col_index = 0;
    
    	printf ("0 1 2 3 4 5 6\n");
    
    	for (row_index = 0; row_index < 6; row_index++)
    	{
    		for (col_index = 0; col_index < 7; col_index++)
    		{
    			switch (game_board[row_index][col_index].color)
    			{
    				case NONE: printf ("- ");
    					       break;
    				case RED: printf ("r ");
    					      break;
    				case BLACK: printf ("b ");
    					        break;
    			}
    		}
    		putchar ('\n');
    	}
    }
    
    void place_token (Cell game_board [6][7], Token_color new_color, int column)
    {
    	int row_index = 0;
    
    	for (row_index = 0; (row_index <= 5) && (game_board[row_index][column].occupied == FALSE); row_index++)
    	{
    		// No need for a body here
    	}
    
    	switch (new_color)
    	{
    		case RED: game_board[row_index - 1][column].color = RED;
    			      game_board[row_index - 1][column].occupied = TRUE;
    				  break;
    		case BLACK: game_board[row_index - 1][column].color = BLACK;
    			        game_board[row_index - 1][column].occupied = TRUE;
    				    break;
    	}	
    }
    int isWin(int player, Cell game_board[6][7], int size)
    {
    	char symbol;
    	int i, j, number, win = 0;
    
    	if (player == 0)
    		symbol = 'b';
    	else if (player == 1)
    		symbol = 'r';
    
    	//horizontally
    	for (i = 0; i < size && win == 0; i++)
    	{
    		number = 0;
    		for (j = 0; j < size; j++)
    			if (game_board[i][j].color == symbol)
    				number++;
    		if (number == size)
    			win = 1;
    	}
    
    	if(win == 0)
    	{
    		//vertically
    		for (i = 0; i < size && win == 0; i++)
    		{
    			number = 0;
    			for (j = 0; j < size; j++)
    				if (game_board[j][i].color == symbol)
    					number++;
    			if (number == size)
    				win = 1;
    		}
    	}
    
    	if (win == 0)
    	{
    		//diagonally
    		number = 0;
    		for (i = 0; i < size; i++)
    			if (game_board[i][i].color == symbol)
    				number++;
    		if (number == size)
    			win = 1;
    
    		if(win == 0)
    		{
    			number = 0;
    			for (i = 0; i < size; i++)
    				if (game_board[i][size - 1 - i].color == symbol)
    					number++;
    			if (number == size)
    				win = 1;
    		}		
    	}
    
    	return win;
    }

    Code:
    #include "ConnectFour.h"
    
    int main (void)
    {
    	int col_slot = 0, flag = 0, player1, player2, column = 0;
    	Cell game_board[50][50];
    	Token_color new_color = RED;
    	srand (time(NULL));
    	initialize_game_board (game_board);
    
    
    	do
    	{
    		player2 = rand() % 6 + 1;
    		print_game_board (game_board);
    		new_color = RED;
    		printf ("PLAYER1:What column would you want to place your token in?");
    		scanf (" %d", &column);
    		place_token (game_board, new_color, column);
    		system("pause");
    		system("cls");
    		new_color = BLACK;
    		place_token (game_board, new_color, player2);
    
    		if(isWin(0, game_board, 4) == 1)
    		{
    			printf("You win!");
    			break;
    		}
    		else if(isWin(1, game_board, 4) == 1)
    		{
    			printf("You lose!");
    			break;
    		}
    
    	} while (flag < 13);
    	return 0;
    
    }

    Code:
    #ifndef CONNECT_FOUR_H
    #define CONNECT_FOUR_H
    
    #include <stdio.h>
    
    typedef enum boolean
    {
    	FALSE, TRUE
    } Boolean;
    
    typedef enum token_color
    {
    	NONE, RED, BLACK
    } Token_color;
    
    typedef struct position
    {
    	int row;
    	int column;
    } Position;
    
    typedef struct cell
    {
    	Boolean occupied;
    	Token_color color;
    	Position location;
    } Cell;
    
    void initialize_game_board (Cell game_board [6][7]);
    void print_game_board (Cell game_board [6][7]);
    void place_token (Cell game_board [6][7], Token_color new_color, int column);
    int isWin(int player, Cell game_board[6][7], int size);
    
    #endif

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    Please indicate what part of the code is supposed to do that, because I don't see where that is, please make sure you add comments to your code in the future...

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    33
    The isWin function is supposed to tell whether a player won yet. Which i do not know why it isnt working? Ok i will

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    I looked at your code again, in "int isWin(int player, Cell game_board[6][7], int size);" & "if(isWin(0, game_board, 4) == 1)". I don't see the point of int player, it prevents the "if" statement from ever being true, that is only at first glance though.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    33
    sorry i dont understand what you mean. can u explain it a little more?

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    What is the purpose of int player in int isWin?

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    33
    so i can make the function check if the player has 4 in a row or the computer has 4 in a row. I put a 0 in for player it will mean that i am checking if the user has 4 in a row and is to check for the computer. Thats what i thought anyways haha

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Hi lilbo4231

    there may be a better way, but one thing that I sometimes do to diagnose problems with my code is to use printf statements in strategic places in the code. put printf statements (which will display the values contained by key variables at key times) throughout your isWin function to find out why the function is not returning a value of 1 when somebody has four in a row. doing this and seeing exactly where the results deviate from expectation will help you gain a lot of insight into why things are working the way you want them to.

    hope this helps,

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    Alright, the only time you use int player is here:
    Code:
    if (player == 0)
    		symbol = 'b';
    	else if (player == 1)
    		symbol = 'r';
    There is no way int player can ever equal 1 because you never refer to player anywhere else. What are symbol='b' and symbol='r' supposed to do.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    I use the same technique all the time... Very helpful.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    33
    ya ha do you know whats wrong? lol

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    Please tell me you're kidding.

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    33
    nope your advice isnt correct?

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    You should put a printf in every place leading up to the "You Win!". You will see outputs for each step, and you can see where things go wrong...

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. declaring a winner for this game
    By joeman in forum C++ Programming
    Replies: 9
    Last Post: 02-08-2010, 11:00 PM
  2. Replies: 12
    Last Post: 06-06-2005, 05:45 AM
  3. New Connect Four Game
    By PJYelton in forum Game Programming
    Replies: 4
    Last Post: 01-17-2003, 10:13 AM
  4. Connect 4 game
    By sundeeptuteja in forum Game Programming
    Replies: 6
    Last Post: 08-12-2002, 11:09 PM
  5. Connect Four game...need help
    By Ion Blade in forum C++ Programming
    Replies: 2
    Last Post: 06-18-2002, 06:18 PM