fixed format

master
Simon Kellet 9 months ago
parent 62e5515472
commit 5dae8fab87
  1. 87
      main.c

@ -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];
@ -22,63 +22,67 @@ void cpu_choice(char cpu);
char check_win(); char check_win();
int free_spaces(); 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) {
do{ case 1:
do {
flag = check_win(); flag = check_win();
if(free_spaces() == 0){ if (free_spaces() == 0) {
break; break;
} }
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) {
break; break;
} }
print_board(); print_board();
player_choice('X'); player_choice('X');
flag = check_win();
print_board(); print_board();
player_choice('O'); player_choice('O');
flag = check_win(); flag = check_win();
print_board(); print_board();
} while(flag == ' '); } while (flag == ' ');
} }
if(flag == ' ') if (flag == ' ')
printf(BOLDMAGENTA "\nNo-one won!" RESET); printf(BOLDMAGENTA "\nNo-one won!" RESET);
else else
printf(BOLDMAGENTA "\n%c won!" RESET, flag ); printf(BOLDMAGENTA "\n%c won!" RESET, flag);
printf(BOLDWHITE "\nGame Over!\n" RESET); printf(BOLDWHITE "\nGame Over!\n" RESET);
return 0; return 0;
} }
void print_board(){ void print_board() {
system("clear"); system("clear");
printf(BOLDWHITE " 1 2 3 \n" RESET); printf(BOLDWHITE " 1 2 3 \n" RESET);
@ -96,27 +100,28 @@ void print_board(){
printf("\n"); printf("\n");
} }
void clear_board(){ void clear_board() {
int r, c; int r, c;
for(r = 0; r < ROWS; ++r){ for (r = 0; r < ROWS; ++r) {
for(c = 0; c < COLUMNS; ++c){ for (c = 0; c < COLUMNS; ++c) {
board[r][c] = ' '; board[r][c] = ' ';
} }
} }
} }
int free_spaces(){ int free_spaces() {
int r, c, s; int r, c, s;
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;
} }
void player_choice(char player){ void player_choice(char player) {
int pr, pc; int pr, pc;
printf(BOLDGREEN "\n[%c] Enter row (1, 2 or 3) <---->\n> " RESET, player); printf(BOLDGREEN "\n[%c] Enter row (1, 2 or 3) <---->\n> " RESET, player);
@ -127,28 +132,28 @@ void player_choice(char player){
scanf("%d", &pc); scanf("%d", &pc);
--pc; --pc;
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;
} }
} }
void cpu_choice(char cpu){ void cpu_choice(char cpu) {
time_t t; time_t t;
srand((unsigned)time(&t)); srand((unsigned)time(&t));
int cr, cc; int cr, cc;
cr = cc = 0; cr = cc = 0;
while(board[cr][cc] != ' '){ while (board[cr][cc] != ' ') {
cr = rand() % 3; cr = rand() % 3;
cc = rand() % 3; cc = rand() % 3;
} }
board[cr][cc] = cpu; board[cr][cc] = cpu;
} }
char check_win(){ char check_win() {
char flag = ' '; char flag = ' ';
/* /*
00 |01 |02 00 |01 |02
@ -157,33 +162,33 @@ char check_win(){
---|---|--- ---|---|---
20 |21 |22 20 |21 |22
*/ */
//horizontal checks l to r // horizontal checks l to r
if(board[0][0] == board[0][1] && board[0][1] == board[0][2]){ if (board[0][0] == board[0][1] && board[0][1] == board[0][2]) {
flag = board[0][0]; flag = board[0][0];
} }
if(board[1][0] == board[1][1] && board[1][1] == board[1][2]){ if (board[1][0] == board[1][1] && board[1][1] == board[1][2]) {
flag = board[1][0]; flag = board[1][0];
} }
if(board[2][0] == board[2][1] && board[2][1] == board[2][2]){ if (board[2][0] == board[2][1] && board[2][1] == board[2][2]) {
flag = board[2][0]; flag = board[2][0];
} }
//horizontal check up n down // horizontal check up n down
if(board[0][0] == board[1][0] && board[1][0] == board[2][0]){ if (board[0][0] == board[1][0] && board[1][0] == board[2][0]) {
flag = board[0][0]; flag = board[0][0];
} }
if(board[0][1] == board[1][1] && board[1][1] == board[2][1]){ if (board[0][1] == board[1][1] && board[1][1] == board[2][1]) {
flag = board[0][1]; flag = board[0][1];
} }
if(board[0][2] == board[1][2] && board[1][2] == board[2][2]){ if (board[0][2] == board[1][2] && board[1][2] == board[2][2]) {
flag = board[0][2]; flag = board[0][2];
} }
//diagonal checks // diagonal checks
if(board[0][0] == board[1][1] && board[1][1] == board[2][2]){ if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
flag = board[0][0]; flag = board[0][0];
} }
if(board[0][2] == board[1][1] && board[1][1] == board[2][0]){ if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
flag = board[0][2]; flag = board[0][2];
} }

Loading…
Cancel
Save