4 Replies - 5086 Views - Last Post: 10 March 2015 - 02:08 AM Rate Topic: -----

#1 Promisek3u   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-January 15

Connect Four in C Help!

Posted 09 March 2015 - 03:08 PM

Hello, I'm trying to make a Connect Four game in C.

Here is a link to the lab if anyone would like to read over what exactly I need to do: http://ece15.ucsd.edu/Labs/lab4.pdf

I have a few problems with my code so far (my code is still in the beginning stages and I'm just testing some stuff).

1. For some odd reason, when I do a player move to the first column (which in an array board[row][column] of size 6 x 7 would be board[5][0]) I successfully get an 'X' in the first column, however the space in board[4][6] also gets an 'X'

Here is what I mean: http://scr.hu/0c1l/l7uhi

It works for any other column that is not the first one: http://scr.hu/0c1l/oq3o6

2. I'm having a little problem with making sure the user only enters an integer. In my function player_move could someone tell me why the while(1) loop does not work for s_ret != 1? I get an infinite loop, but the (m < 0) and (m > BOARD_SIZE_HORIZ) work fine, that is they display the error message, then prompt the user to re-enter a number.

Here is what I mean: http://scr.hu/0c1l/wgg9f

Here is what is wrong with while(1): http://scr.hu/0c1l/rhs0h

3. Also, could someone check to see if I am correctly erasing the input buffer? With the while(getchar() != '\n');

Sorry for the super long text! Thanks again for any help.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

#define BOARD_SIZE_HORIZ 7
#define BOARD_SIZE_VERT 6

int print_welcome(void)
{
	char input;

	printf("*** Welcome to the Connect Four game!!! ***\n");

	printf("Would you like to make the first move [y/n]: ");
	scanf("%c", &input);

	while(getchar() != '\n'); //am i using this right?

	if(input == 'n') //if user enters n or N then the computer goes first
		return 2;

	if(input == 'N')
		return 2;
	else //literally every other input makes the user go first
		return 1;
}

void display_board(int board[][BOARD_SIZE_VERT])
{
	int i = 0, j;
	int x, y, z;
	char character;

	for(x = 0; x < BOARD_SIZE_VERT; x++, i++)
	{
		for(y = 0; y < BOARD_SIZE_HORIZ; y++)
			printf("+---");

		printf("+\n");

		for(z = 0, j = 0; z < BOARD_SIZE_HORIZ; z++, j++)
		{
			if(board[i][j] == 0) //could there be something wrong with this? causing the column issue
				character = ' ';
			else if(board[i][j] == 1)
				character = 'X';
			else if(board[i][j] == 2)
				character = 'O';

			printf("| %c ", character);
		}

		printf("|\n");
	}

	for(x = 0; x < BOARD_SIZE_HORIZ; x++) //these stuffs are for formatting the board etc.
		printf("+---");

	printf("+\n");

	for(x = 1; x <= BOARD_SIZE_HORIZ; x++)
		printf("  %d ", x);

	printf("\n");
}

void update_board(int board[][BOARD_SIZE_VERT], int m, int player_num)
{
	board[5][m - 1] = player_num; //i know i will have to edit this in the future, but it's basic to just test the program so far
}

int player_move(int board[][BOARD_SIZE_VERT], int player_num)
{
	int m;
	int s_ret; //scanf return value

	while(1)
	{
		printf("Please enter your move: ");
			s_ret = scanf("%d", &m);
		
		if(s_ret != 1) //infinite loop! :(/>/>
		{
			printf("Not a valid move. Enter a column number!\n");
			
			continue;
		}

		else if(m < 0) //works
		{
			printf("Not a valid move. Enter a column number!\n");
			
			continue;
		}

		else if(m > BOARD_SIZE_HORIZ) //works
		{
			printf("Not a valid move. Enter a column number!\n");
			
			continue;
		}
		else
			break;
	}

	while(getchar() != '\n');
			
	update_board(board, m, player_num);

	return m;		
}

int main()
{
	int last_move;
	int player_num, computer_num;
	int board[BOARD_SIZE_HORIZ][BOARD_SIZE_VERT] = { {0} };

	player_num = print_welcome();

	if(player_num == 1)
		computer_num = 2;
	else 
		computer_num = 1;

	if(player_num == 1)
	{
		display_board(board);

		last_move = player_move(board, player_num);

		display_board(board);
	}

	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Connect Four in C Help!

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Connect Four in C Help!

Posted 09 March 2015 - 08:57 PM

Why are you not following the assignment directions? You need to read and follow the directions for the first bullet point in the first link you gave.

From connect4.c

Quote

This program plays a game of Connect-4 between the user (herein called Alice)
and the computer. The declarations of all the functions used in this program
are in connect4_functions.h. The definitions of these functions are expected
in connect4_functions.c.


Reading your directions it seems that you can't modify the include file or connect4.c all of your modifications must be done in connect4_functions.c and nowhere else.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Promisek3u   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-January 15

Re: Connect Four in C Help!

Posted 09 March 2015 - 10:39 PM

Ah, no that's a bit misleading, they did not just hand me the codes that are already written. They gave us the names of the requires functions to implement. The files they are referring to are shown here: http://ece15.ucsd.ed...ct4_functions.c
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Connect Four in C Help!

Posted 09 March 2015 - 10:55 PM

No it is not misleading, it is very clear. You can't modify anything except connect4_functions.c which is:
Spoiler


The other two files connect4.c
Spoiler

And connect4_functions.h
Spoiler

Can not be modified.

Re-read your instructions.


Jim
Was This Post Helpful? 1
  • +
  • -

#5 Promisek3u   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-January 15

Re: Connect Four in C Help!

Posted 10 March 2015 - 02:08 AM

Oh, man, wow I spent all that time coding haha. Thanks a lot then!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1