You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
189 lines
4.3 KiB
189 lines
4.3 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#define ROWS 3
|
|
#define COLUMNS 3
|
|
#define RED "\033[31m" /* Red */
|
|
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
|
|
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
|
|
#define BOLDRED "\033[1m\033[31m" /* Bold Yellow */
|
|
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
|
|
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
|
|
#define RESET "\033[0m"
|
|
|
|
char board[ROWS][COLUMNS];
|
|
|
|
void print_board();
|
|
void clear_board();
|
|
void player_choice(char player);
|
|
void cpu_choice(char cpu);
|
|
char check_win();
|
|
int free_spaces();
|
|
|
|
int main(int argc, char *argv[]){
|
|
char flag = ' ';
|
|
int players;
|
|
clear_board();
|
|
|
|
printf("How many players? (1/2)\n> ");
|
|
scanf("%d", &players);
|
|
|
|
//this could be tidier i think
|
|
if(players == 1){
|
|
do{
|
|
flag = check_win();
|
|
if(free_spaces() == 0){
|
|
break;
|
|
}
|
|
|
|
print_board();
|
|
|
|
player_choice('X');
|
|
print_board();
|
|
|
|
cpu_choice('O');
|
|
flag = check_win();
|
|
print_board();
|
|
|
|
} while(flag == ' ');
|
|
|
|
} else if(players == 2){
|
|
do{
|
|
flag = check_win();
|
|
if(free_spaces() == 0){
|
|
break;
|
|
}
|
|
|
|
print_board();
|
|
|
|
player_choice('X');
|
|
print_board();
|
|
|
|
player_choice('O');
|
|
flag = check_win();
|
|
print_board();
|
|
|
|
} while(flag == ' ');
|
|
}
|
|
if(flag == ' ')
|
|
printf(BOLDMAGENTA "\nNo-one won!" RESET);
|
|
else
|
|
printf(BOLDMAGENTA "\n%c won!" RESET, flag );
|
|
|
|
printf(BOLDWHITE "\nGame Over!\n" RESET);
|
|
return 0;
|
|
}
|
|
|
|
void print_board(){
|
|
system("clear");
|
|
printf(BOLDWHITE " 1 2 3 \n" RESET);
|
|
|
|
printf(BOLDWHITE "1 " RESET);
|
|
printf("%c | %c | %c ", board[0][0], board[0][1], board[0][2]);
|
|
printf("\n ---|---|---\n");
|
|
|
|
printf(BOLDWHITE "2 " RESET);
|
|
printf("%c | %c | %c ", board[1][0], board[1][1], board[1][2]);
|
|
printf("\n ---|---|---\n");
|
|
|
|
printf(BOLDWHITE "3 " RESET);
|
|
printf("%c | %c | %c ", board[2][0], board[2][1], board[2][2]);
|
|
|
|
printf("\n");
|
|
}
|
|
|
|
void clear_board(){
|
|
int r, c;
|
|
for(r = 0; r < ROWS; ++r){
|
|
for(c = 0; c < COLUMNS; ++c){
|
|
board[r][c] = ' ';
|
|
}
|
|
}
|
|
}
|
|
|
|
int free_spaces(){
|
|
int r, c, s;
|
|
s = 0;
|
|
for(r = 0; r < ROWS; ++r){
|
|
for(c = 0; c < COLUMNS; ++c){
|
|
if(board[r][c] == ' ') ++s;
|
|
}
|
|
}
|
|
return s;
|
|
}
|
|
|
|
void player_choice(char player){
|
|
|
|
int pr, pc;
|
|
printf(BOLDGREEN "\n[%c] Enter row (1, 2 or 3) <---->\n> " RESET, player);
|
|
scanf("%d", &pr);
|
|
--pr;
|
|
|
|
printf(BOLDYELLOW "\n[%c] Enter column (1, 2 or 3) ^ v\n> " RESET, player);
|
|
scanf("%d", &pc);
|
|
--pc;
|
|
|
|
if(board[pr][pc] != ' ' ){
|
|
printf(BOLDRED "\n[%c] Cannot play here, skipping...\n" RESET, player);
|
|
sleep(2);
|
|
} else {
|
|
board[pr][pc] = player;
|
|
}
|
|
}
|
|
|
|
void cpu_choice(char cpu){
|
|
time_t t;
|
|
srand((unsigned)time(&t));
|
|
|
|
int cr, cc;
|
|
cr = cc = 0;
|
|
while(board[cr][cc] != ' '){
|
|
cr = rand() % 3;
|
|
cc = rand() % 3;
|
|
}
|
|
board[cr][cc] = cpu;
|
|
}
|
|
|
|
char check_win(){
|
|
char flag = ' ';
|
|
/*
|
|
00 |01 |02
|
|
---|---|---
|
|
10 |11 |12
|
|
---|---|---
|
|
20 |21 |22
|
|
*/
|
|
//horizontal checks l to r
|
|
if(board[0][0] == board[0][1] && board[0][1] == board[0][2]){
|
|
flag = board[0][0];
|
|
}
|
|
if(board[1][0] == board[1][1] && board[1][1] == board[1][2]){
|
|
flag = board[1][0];
|
|
}
|
|
if(board[2][0] == board[2][1] && board[2][1] == board[2][2]){
|
|
flag = board[2][0];
|
|
}
|
|
|
|
//horizontal check up n down
|
|
if(board[0][0] == board[1][0] && board[1][0] == board[2][0]){
|
|
flag = board[0][0];
|
|
}
|
|
if(board[0][1] == board[1][1] && board[1][1] == board[2][1]){
|
|
flag = board[0][1];
|
|
}
|
|
if(board[0][2] == board[1][2] && board[1][2] == board[2][2]){
|
|
flag = board[0][2];
|
|
}
|
|
|
|
//diagonal checks
|
|
if(board[0][0] == board[1][1] && board[1][1] == board[2][2]){
|
|
flag = board[0][0];
|
|
}
|
|
if(board[0][2] == board[1][1] && board[1][1] == board[2][0]){
|
|
flag = board[0][2];
|
|
}
|
|
|
|
return flag;
|
|
}
|
|
|