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 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 RED */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define RESET "\033[0m"
#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 RED */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define RESET "\033[0m" /* Reset colour */
char board[ROWS][COLUMNS];
@ -22,170 +22,175 @@ 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);
//TODO Tidy this up!
if(players == 1){
do{
flag = check_win();
if(free_spaces() == 0){
break;
}
print_board();
player_choice('X');
print_board();
sleep(2);
cpu_choice('O');
flag = check_win();
print_board();
sleep(2);
} 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;
int main(int argc, char *argv[]) {
char flag = ' ';
int players;
unsigned int sleeptime = 1; // seconds to sleep
clear_board();
printf("How many players? (1/2)\n> ");
scanf("%d", &players); // something better than scanf("%d")
// TODO Tidy this up!
switch (players) {
case 1:
do {
flag = check_win();
if (free_spaces() == 0) {
break;
}
print_board();
player_choice('X');
flag = check_win();
print_board();
sleep(sleeptime);
cpu_choice('O');
flag = check_win();
print_board();
sleep(sleeptime);
} while (flag == ' ');
case 2:
do {
flag = check_win();
if (free_spaces() == 0) {
break;
}
print_board();
player_choice('X');
flag = check_win();
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);
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 "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 "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(BOLDWHITE "3 " RESET);
printf("%c | %c | %c ", board[2][0], board[2][1], board[2][2]);
printf("\n");
printf("\n");
}
void clear_board(){
int r, c;
for(r = 0; r < ROWS; ++r){
for(c = 0; c < COLUMNS; ++c){
board[r][c] = ' ';
}
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;
}
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;
}
return s;
}
void player_choice(char player){
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;
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;
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;
}
if (board[pr][pc] != ' ') {
printf(BOLDRED "\n[%c] Cannot play here, skipping...\n" RESET, player);
sleep(1);
} 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;
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;
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;
}

Loading…
Cancel
Save