I know scanf
is bad and all, but we won't be testing for invalid input on this (assume always valid). However, when I do a call to promptPlayer()
which has the scanf
call, output to the console stops (which makes no sense because all the output should be in the console before the calls to scanf
are even made. Input should be a set of coordinates separated by whitespace (as shown in output).
Here is the code (the important stuff):
othello.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "othello_engine.h"
int main(int numArgs, const char *args[]) {
// -------------------------- Initialize the Othello routine --------------------------- //
// Create the board
int size = atoi(args[2]);
char board[size][size];
// Display game start message
if (args[4][0] == 'B' || args[4][0] == 'b')
printf("Othello game with a %dx%d board - Player %d with black disc to start\n\n", size, size, atoi(args[3]));
else
printf("Othello game with a %dx%d board - Player %d with white disc to start\n\n", size, size, atoi(args[3]));
// Initially display the game board
displayBoard(size, size, board);
// --------------------------- Start the game through user input --------------------------- //
Coordinate placed = promptPlayer(1);
printf("Place at [%d,%d].\n", placed.x, placed.y);
Coordinate placed2 = promptPlayer(1);
printf("Place at [%d,%d].\n", placed2.x, placed2.y);
return EXIT_SUCCESS;
}
othello_engine.c
#include "othello_engine.h"
#include <stdio.h>
void displayBoard(int size, int size2, char board[size][size2]) {
// Print the column numbers
printf(" ");
for (int i = 0; i < size; i ++) {
printf(" %d", (i+1));
}
printf("\n");
// Print the rest of the matrix, including row numbers
for (int i = 0; i < size; i ++) {
printf("%d", (i+1));
for (int j = 0; j < size2; j ++) {
if (board[i][j] != 'B' && board[i][j] != 'W') printf(" -");
else printf(" %c", board[i][j]);
}
printf("\n");
}
printf("\n\n");
}
Coordinate promptPlayer(int player) {
Coordinate desiredLocation;
printf("Player %d's turn (enter row and column): ", player);
scanf("%d %d", &desiredLocation.x, &desiredLocation.y);
//scanf("%*[^\n]%*c");
return desiredLocation;
}
Lastly, othello_engine.h
#ifndef OTHELLO_ENGINE_H_
#define OTHELLO_ENGINE_H_
typedef struct {
int x;
int y;
} Coordinate;
#endif /* OTHELLO_ENGINE_H_ */
/*
* Displays the Othello board and row/column numbers.
*/
void displayBoard(int size, int size2, char board[size][size2]);
Coordinate promptPlayer(int player);
Here is the output: Basically the output stream is halted for the entire program until after all scanf
s have been evaluated. I have no idea what is causing this.
1 2 // This is the first scanf input
3 4 // This is the second set
Othello game with a 6x6 board - Player 1 with black disc to start
1 2 3 4 5 6
1 - - - - - -
2 - - - - - -
3 - - B W - -
4 - - W B - -
5 - - - - - -
6 - - - - - -
Player 1's turn (enter row and column): Place at [1,2]. // Here is where the first scanf's values are output
Player 1's turn (enter row and column): Place at [3,4]. // Here is the second set
It really doesn't make any sense. Everything prints out perfectly until the promptPlayer
method is called - the values are being received correctly, so I don't really see the problem... Seems like scanf
stops console output (magically?) until the values are input - then it prints everything to the console all at once. I just know it's something really obvious.