fixed format

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

309
main.c

@ -5,13 +5,13 @@
#define ROWS 3 #define ROWS 3
#define COLUMNS 3 #define COLUMNS 3
#define RED "\033[31m" /* Red */ #define RED "\033[31m" /* Red */
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */ #define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */ #define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
#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,170 +22,175 @@ 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;
clear_board(); unsigned int sleeptime = 1; // seconds to sleep
clear_board();
printf("How many players? (1/2)\n> ");
scanf("%d", &players); printf("How many players? (1/2)\n> ");
scanf("%d", &players); // something better than scanf("%d")
//TODO Tidy this up!
if(players == 1){ // TODO Tidy this up!
do{ switch (players) {
flag = check_win(); case 1:
if(free_spaces() == 0){ do {
break; flag = check_win();
} if (free_spaces() == 0) {
break;
print_board(); }
player_choice('X'); print_board();
print_board();
sleep(2); player_choice('X');
flag = check_win();
cpu_choice('O'); print_board();
flag = check_win(); sleep(sleeptime);
print_board();
sleep(2); cpu_choice('O');
flag = check_win();
} while(flag == ' '); print_board();
sleep(sleeptime);
} else if(players == 2){
do{ } while (flag == ' ');
flag = check_win();
if(free_spaces() == 0){ case 2:
break; do {
} flag = check_win();
if (free_spaces() == 0) {
print_board(); break;
}
player_choice('X');
print_board(); print_board();
player_choice('O'); player_choice('X');
flag = check_win(); flag = check_win();
print_board(); print_board();
} while(flag == ' '); player_choice('O');
} flag = check_win();
if(flag == ' ') print_board();
printf(BOLDMAGENTA "\nNo-one won!" RESET);
else } while (flag == ' ');
printf(BOLDMAGENTA "\n%c won!" RESET, flag ); }
if (flag == ' ')
printf(BOLDWHITE "\nGame Over!\n" RESET); printf(BOLDMAGENTA "\nNo-one won!" RESET);
return 0; else
printf(BOLDMAGENTA "\n%c won!" RESET, flag);
printf(BOLDWHITE "\nGame Over!\n" RESET);
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);
printf(BOLDWHITE "1 " RESET); printf(BOLDWHITE "1 " RESET);
printf("%c | %c | %c ", board[0][0], board[0][1], board[0][2]); printf("%c | %c | %c ", board[0][0], board[0][1], board[0][2]);
printf("\n ---|---|---\n"); printf("\n ---|---|---\n");
printf(BOLDWHITE "2 " RESET); printf(BOLDWHITE "2 " RESET);
printf("%c | %c | %c ", board[1][0], board[1][1], board[1][2]); printf("%c | %c | %c ", board[1][0], board[1][1], board[1][2]);
printf("\n ---|---|---\n"); printf("\n ---|---|---\n");
printf(BOLDWHITE "3 " RESET); printf(BOLDWHITE "3 " RESET);
printf("%c | %c | %c ", board[2][0], board[2][1], board[2][2]); printf("%c | %c | %c ", board[2][0], board[2][1], board[2][2]);
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);
scanf("%d", &pr); scanf("%d", &pr);
--pr; --pr;
printf(BOLDYELLOW "\n[%c] Enter column (1, 2 or 3) ^ v\n> " RESET, player); printf(BOLDYELLOW "\n[%c] Enter column (1, 2 or 3) ^ v\n> " RESET, 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
---|---|--- ---|---|---
10 |11 |12 10 |11 |12
---|---|--- ---|---|---
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];
} }
return flag; return flag;
} }

Loading…
Cancel
Save