|
|
@ -11,7 +11,7 @@ |
|
|
|
#define BOLDRED "\033[1m\033[31m" /* Bold RED */ |
|
|
|
#define BOLDRED "\033[1m\033[31m" /* Bold RED */ |
|
|
|
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */ |
|
|
|
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */ |
|
|
|
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */ |
|
|
|
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */ |
|
|
|
#define RESET "\033[0m" |
|
|
|
#define RESET "\033[0m" /* Reset colour */ |
|
|
|
|
|
|
|
|
|
|
|
char board[ROWS][COLUMNS]; |
|
|
|
char board[ROWS][COLUMNS]; |
|
|
|
|
|
|
|
|
|
|
@ -25,13 +25,15 @@ int free_spaces(); |
|
|
|
int main(int argc, char *argv[]) { |
|
|
|
int main(int argc, char *argv[]) { |
|
|
|
char flag = ' '; |
|
|
|
char flag = ' '; |
|
|
|
int players; |
|
|
|
int players; |
|
|
|
|
|
|
|
unsigned int sleeptime = 1; // seconds to sleep
|
|
|
|
clear_board(); |
|
|
|
clear_board(); |
|
|
|
|
|
|
|
|
|
|
|
printf("How many players? (1/2)\n> "); |
|
|
|
printf("How many players? (1/2)\n> "); |
|
|
|
scanf("%d", &players); |
|
|
|
scanf("%d", &players); // something better than scanf("%d")
|
|
|
|
|
|
|
|
|
|
|
|
// TODO Tidy this up!
|
|
|
|
// TODO Tidy this up!
|
|
|
|
if(players == 1){ |
|
|
|
switch (players) { |
|
|
|
|
|
|
|
case 1: |
|
|
|
do { |
|
|
|
do { |
|
|
|
flag = check_win(); |
|
|
|
flag = check_win(); |
|
|
|
if (free_spaces() == 0) { |
|
|
|
if (free_spaces() == 0) { |
|
|
@ -41,17 +43,18 @@ int main(int argc, char *argv[]){ |
|
|
|
print_board(); |
|
|
|
print_board(); |
|
|
|
|
|
|
|
|
|
|
|
player_choice('X'); |
|
|
|
player_choice('X'); |
|
|
|
|
|
|
|
flag = check_win(); |
|
|
|
print_board(); |
|
|
|
print_board(); |
|
|
|
sleep(2); |
|
|
|
sleep(sleeptime); |
|
|
|
|
|
|
|
|
|
|
|
cpu_choice('O'); |
|
|
|
cpu_choice('O'); |
|
|
|
flag = check_win(); |
|
|
|
flag = check_win(); |
|
|
|
print_board(); |
|
|
|
print_board(); |
|
|
|
sleep(2); |
|
|
|
sleep(sleeptime); |
|
|
|
|
|
|
|
|
|
|
|
} while (flag == ' '); |
|
|
|
} while (flag == ' '); |
|
|
|
|
|
|
|
|
|
|
|
} else if(players == 2){ |
|
|
|
case 2: |
|
|
|
do { |
|
|
|
do { |
|
|
|
flag = check_win(); |
|
|
|
flag = check_win(); |
|
|
|
if (free_spaces() == 0) { |
|
|
|
if (free_spaces() == 0) { |
|
|
@ -61,6 +64,7 @@ int main(int argc, char *argv[]){ |
|
|
|
print_board(); |
|
|
|
print_board(); |
|
|
|
|
|
|
|
|
|
|
|
player_choice('X'); |
|
|
|
player_choice('X'); |
|
|
|
|
|
|
|
flag = check_win(); |
|
|
|
print_board(); |
|
|
|
print_board(); |
|
|
|
|
|
|
|
|
|
|
|
player_choice('O'); |
|
|
|
player_choice('O'); |
|
|
@ -110,7 +114,8 @@ int free_spaces(){ |
|
|
|
s = 0; |
|
|
|
s = 0; |
|
|
|
for (r = 0; r < ROWS; ++r) { |
|
|
|
for (r = 0; r < ROWS; ++r) { |
|
|
|
for (c = 0; c < COLUMNS; ++c) { |
|
|
|
for (c = 0; c < COLUMNS; ++c) { |
|
|
|
if(board[r][c] == ' ') ++s; |
|
|
|
if (board[r][c] == ' ') |
|
|
|
|
|
|
|
++s; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return s; |
|
|
|
return s; |
|
|
@ -129,7 +134,7 @@ void player_choice(char player){ |
|
|
|
|
|
|
|
|
|
|
|
if (board[pr][pc] != ' ') { |
|
|
|
if (board[pr][pc] != ' ') { |
|
|
|
printf(BOLDRED "\n[%c] Cannot play here, skipping...\n" RESET, player); |
|
|
|
printf(BOLDRED "\n[%c] Cannot play here, skipping...\n" RESET, player); |
|
|
|
sleep(2); |
|
|
|
sleep(1); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
board[pr][pc] = player; |
|
|
|
board[pr][pc] = player; |
|
|
|
} |
|
|
|
} |
|
|
|