One of the coding exercises I've used to interview people is to implement a TicTacToe in 30 minutes. The idea is to implement as much as possible from the simple game in the language of their choice, being able to search whatever they need to on the internet.
Tonight I was in the mood to do some C programming (haven't done that in a long time) so I decided to implement the TicTacToe game in C. In total I spent 25 minutes doing the implementation. I had to look a couple of things like how to get a random int, and how to initialize an array.
I liked my resulting code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ERROR_INVALID_POSITION -1
#define ERROR_POSITION_NOT_EMPTY -2
#define ERROR_INVALID_TOKEN -3
#define NO_WINNER '-'
#define WINNER_TIE 'T'
void printBoard();
int setToken(int n, char token);
void userMove();
void computerMove();
char checkWinner();
char board[] = {'-', '-', '-', '-', '-', '-', '-', '-','-' };
int main() {
srand(time(NULL));
char winner = '-';
while(1) {
printBoard();
userMove();
winner = checkWinner();
if (winner != NO_WINNER) break;
printf("%c\n", winner);
computerMove();
winner = checkWinner();
printf("%c\n", winner);
if (winner != NO_WINNER) break;
}
printBoard();
if (winner == WINNER_TIE) {
puts("A boring tie.... how boring");
}
else
{
printf("Winner is: %c. Congrats!\n", winner);
}
return 0;
}
void printBoard() {
for (int row=0; row < 3; row++) {
printf("%c|%c|%c\n",board[3*row+0], board[3*row+1], board[3*row+2]);
}
puts("");
}
int setToken(int n, char token) {
n--; // To start positions from 1
if (n < 0 || n> 8) {
return ERROR_INVALID_POSITION;
}
if (board[n] != '-') {
return ERROR_POSITION_NOT_EMPTY;
}
if (token != 'O' && token !='X') {
return ERROR_INVALID_TOKEN;
}
board[n] = token;
return 0;
}
void userMove() {
int position;
while (1) {
puts("Enter a number [1-9] to set an O:");
scanf("%d",&position);
int error = setToken( position, 'O');
if (error == ERROR_INVALID_POSITION) {
puts("Invalid Position Entered, try again\n");
} else
if (error == ERROR_POSITION_NOT_EMPTY) {
puts("That position is already taken, try again\n");
} else
{
break;
}
}
}
void computerMove() {
int n = 0;
while (1) {
n = rand() % 8 + 1;
if (setToken(n,'X') == 0) break;
}
}
char checkWinner() {
if (board[0] == board[1] && board[1] == board[2]) { return board[0]; }
if (board[3] == board[4] && board[4] == board[5]) { return board[3]; }
if (board[6] == board[7] && board[7] == board[8]) { return board[6]; }
if (board[0] == board[3] && board[3] == board[6]) { return board[0]; }
if (board[1] == board[4] && board[4] == board[7]) { return board[1]; }
if (board[2] == board[5] && board[5] == board[8]) { return board[2]; }
if (board[0] == board[4] && board[4] == board[8]) { return board[0]; }
if (board[2] == board[4] && board[4] == board[6]) { return board[2]; }
int empty = 0;
for (int i = 0; i < 9; i++ ) {
if (board[i] == '-') { empty++; }
}
if (empty == 0) { return WINNER_TIE; }
return NO_WINNER;
}