Compare commits
6
Commits
a84b5918b9
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5dae8fab87 | ||
|
|
62e5515472 | ||
|
|
9a4963f474 | ||
|
|
bf7a214788 | ||
|
|
df7db80888 | ||
|
|
e9a3fb0217 |
@@ -0,0 +1 @@
|
||||
ttt
|
||||
@@ -4,22 +4,11 @@ A simple TicTacToe game written in C in under 2 hours!
|
||||
|
||||
## Usage
|
||||
```bash
|
||||
./build
|
||||
./build.sh
|
||||
```
|
||||
then
|
||||
```bash
|
||||
./ttt
|
||||
```
|
||||
|
||||
## CPU
|
||||
There is infact a CPU programmed into this application!
|
||||
You can play against it if you swap out:
|
||||
```c
|
||||
41 player_choice('O');
|
||||
```
|
||||
with
|
||||
```c
|
||||
41 cpu_choice('O');
|
||||
```
|
||||
|
||||
Enjoy!
|
||||
|
||||
@@ -8,10 +8,10 @@
|
||||
#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 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 RESET "\033[0m" /* Reset colour */
|
||||
|
||||
char board[ROWS][COLUMNS];
|
||||
|
||||
@@ -25,13 +25,15 @@ int free_spaces();
|
||||
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);
|
||||
scanf("%d", &players); // something better than scanf("%d")
|
||||
|
||||
//this could be tidier i think
|
||||
if(players == 1){
|
||||
// TODO Tidy this up!
|
||||
switch (players) {
|
||||
case 1:
|
||||
do {
|
||||
flag = check_win();
|
||||
if (free_spaces() == 0) {
|
||||
@@ -41,15 +43,18 @@ int main(int argc, char *argv[]){
|
||||
print_board();
|
||||
|
||||
player_choice('X');
|
||||
flag = check_win();
|
||||
print_board();
|
||||
sleep(sleeptime);
|
||||
|
||||
cpu_choice('O');
|
||||
flag = check_win();
|
||||
print_board();
|
||||
sleep(sleeptime);
|
||||
|
||||
} while (flag == ' ');
|
||||
|
||||
} else if(players == 2){
|
||||
case 2:
|
||||
do {
|
||||
flag = check_win();
|
||||
if (free_spaces() == 0) {
|
||||
@@ -59,6 +64,7 @@ int main(int argc, char *argv[]){
|
||||
print_board();
|
||||
|
||||
player_choice('X');
|
||||
flag = check_win();
|
||||
print_board();
|
||||
|
||||
player_choice('O');
|
||||
@@ -108,7 +114,8 @@ int free_spaces(){
|
||||
s = 0;
|
||||
for (r = 0; r < ROWS; ++r) {
|
||||
for (c = 0; c < COLUMNS; ++c) {
|
||||
if(board[r][c] == ' ') ++s;
|
||||
if (board[r][c] == ' ')
|
||||
++s;
|
||||
}
|
||||
}
|
||||
return s;
|
||||
@@ -127,7 +134,7 @@ void player_choice(char player){
|
||||
|
||||
if (board[pr][pc] != ' ') {
|
||||
printf(BOLDRED "\n[%c] Cannot play here, skipping...\n" RESET, player);
|
||||
sleep(2);
|
||||
sleep(1);
|
||||
} else {
|
||||
board[pr][pc] = player;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user