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;
}

New Topic/Question
Reply


MultiQuote



|