Originally Posted by
Matticus
This might suit your needs:
Code:
numScan = scanf("%d", &position);
if(numScan != 1) // if "scanf" failed,
while(getchar() != '\n') // eat up all characters in the
; // input buffer up to the newline
Review that carefully and make sure you understand how it works. If you're not certain, just ask for clarification.
This is not the most elegant approach, and will not cover all circumstances (e.g. if the user just presses enter without typing anything), but should do you well for now.
I think I see what it's doing. It's going to be consuming each character in the input buffer including the new line character, but breaks from the while loop when that happens so it stops waiting for more chars by that point.
Originally Posted by
Matticus
In the future, you might consider making a separate function just for reading a number from stdin. This can use the "fgets()" and "sscanf()" combination to read the input without having to worry about the issues usually encountered with "scanf()" alone.
Could I get some help with this please? I'm willing to try and do it, but I'm rusty with each of those functions.
Originally Posted by
hk_mp5kpdw
Looks like you need to increment round in the loop and also reset player so that it switches back and forth.
I wasn't quite done at that point. I now have
Code:
// 4 in a row game
#include <stdio.h>
#include <stdlib.h>
#define NUM_ROWS 6
#define NUM_COLS 7
#define NO_PLAYER 0
#define PLAYER_1 1
#define PLAYER_2 2
#define TRUE 1
#define FALSE 0
void printBoard(int board[NUM_ROWS][NUM_COLS]);
void prompt(int player);
int input(int board[NUM_ROWS][NUM_COLS], int player, int *play);
int gameOver(int board[NUM_ROWS][NUM_COLS], int play);
int win(int board[NUM_ROWS][NUM_COLS], int play);
int main(int argc, char **argv) {
int board[NUM_ROWS][NUM_COLS] = {{NO_PLAYER}}; //(a,b) = (row,col)
int round = 1;
int player;
int play = 0;
int *playP = &play;
printBoard(board);
while (gameOver(board, play) == FALSE) {
player = (round-1)%2 + 1;
prompt(player);
while (input(board, player, playP) == FALSE) {
printf("(!) Invalid input\n\n");
prompt(player);
}
printBoard(board);
round++;
}
return EXIT_SUCCESS;
}
void printBoard(int board[NUM_ROWS][NUM_COLS]) {
int i,j;
printf("\n");
for (i=0; i<NUM_ROWS; i++) {
printf("\t\t");
for (j=0; j<NUM_COLS; j++) {
if (board[i][j] == NO_PLAYER)
printf(".");
else if (board[i][j] == PLAYER_1)
printf("X");
else if (board[i][j] == PLAYER_2)
printf("O");
else
printf("E");
printf(" ");
}
printf("\n");
}
printf("\n\t\t");
for (i=0; i<NUM_COLS; i++) {
printf("%d ", i+1);
}
printf("\n\n");
return;
}
void prompt(int player) {
printf("\t\tPlayer %d: ", player);
return;
}
int input(int board[NUM_ROWS][NUM_COLS], int player, int *play) {
int position;
int numScan;
int i;
numScan = scanf("%d", &position);
if (numScan != 1 || position < 1 || position > NUM_COLS
|| board[0][position] != NO_PLAYER)
return FALSE;
*play = position;
for (i=NUM_ROWS-1; i>=0; i--) {
if (board[i][position-1] == NO_PLAYER) {
board[i][position-1] = player;
break;
}
}
return TRUE;
}
int gameOver(int board[NUM_ROWS][NUM_COLS], int play) {
int i;
for (i=0; i<NUM_COLS; i++)
if (board[0][i] == NO_PLAYER) break;
if (i == NUM_COLS && board[0][i-1] != NO_PLAYER) return TRUE;
if (win(board, play) == TRUE) return TRUE;
return FALSE;
}
int win(int board[NUM_ROWS][NUM_COLS], int play) {
return FALSE;
}