Skip to content

Commit

Permalink
Merge pull request #1 from BrinzaBezrukoff/dev
Browse files Browse the repository at this point in the history
Game modes
  • Loading branch information
brinza888 authored Oct 22, 2021
2 parents 87c66ad + 30ed0fb commit 4afeb0d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 97,057 deletions.
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,30 @@ git clone https://github.com/BrinzaBezrukoff/tictactoe.git
```
cd tictactoe
```
3. Compile source:
```
gcc main.c tictactoe.c minimax.c -o tictactoe
```
3. Compile source with make:
```
make
```
or with gcc:
```
gcc main.c tictactoe.c minimax.c -o tictactoe
```
4. Run binary:
```
./tictactoe
```
## Play
X is human, O is AI.


1. Choose who will be make turn first. Type X or O and press Enter key.
2. When it is your time to make turn, type 2 numbers: row and column indices of cell, where you want to place X.
![image](https://user-images.githubusercontent.com/29017599/138512021-00aa33b2-c18c-4c30-8938-d2b4b989de8e.png)
* Human playing for X
* AI playing for O
2. Choose level mode for AI (this feature in develoment, some modes work incorrect):
![image](https://user-images.githubusercontent.com/29017599/138511830-b7aa34fc-f540-44e7-8246-3402b464d4f4.png)
* Easy (E)
* Medium (M)
* Hard (H)
* eXpert (X)
3. When it is your time to make turn, type 2 numbers: row and column indices of cell, where you want to place X.
![image](https://user-images.githubusercontent.com/29017599/138511117-e12dba88-b23c-44d6-87a8-3afce50d9b97.png)
19 changes: 19 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "tictactoe.h"
#include "minimax.h"


const char loadingS[] = {'-', '\\', '|', '/'};

void print_turn(enum field map[SIZE][SIZE], int turn, char ch) {
Expand All @@ -28,6 +29,24 @@ int main(int argc, char** argv){
printf("Who is the first? [X/O]: ");
scanf("%c", &current_ch);
enum field current = char2field(current_ch);

char ai_mode_ch;
int ai_mode;
printf("AI mode?\n");
printf("Easy Medium Hard Expert [E/M/H/X]: ");
scanf("%*c%c", &ai_mode_ch);
switch (ai_mode_ch) {
case 'E': ai_mode = MODE_EASY; break;
case 'M': ai_mode = MODE_MEDIUM; break;
case 'H': ai_mode = MODE_HARD; break;
case 'X': ai_mode = MODE_EXPERT; break;
default:
printf("Incorrect mode specified!\n");
return 1;
break;
}

setMode(ai_mode);

if (current == EMPTY) {
printf("You may choose only X or O\n");
Expand Down
14 changes: 12 additions & 2 deletions minimax.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
#include "minimax.h"


static int depthMode = MODE_EXPERT;

int getMode() {
return depthMode;
}

void setMode(int value) {
depthMode = value;
}


struct cell new_cell(int row, int col) {
struct cell new;
new.row = row;
Expand Down Expand Up @@ -33,7 +44,6 @@ int get_moves(enum field map[SIZE][SIZE], struct cell **moves) {
return count;
}


int minimax(enum field (*map)[SIZE][SIZE], int* row, int* col, enum field player, int depth) {
*row = -1;
*col = -1;
Expand All @@ -42,7 +52,7 @@ int minimax(enum field (*map)[SIZE][SIZE], int* row, int* col, enum field player
if (winner != EMPTY) {
return (winner == ZERO) ? 10 : -10;
}
if (is_draw(*map)) {
if (is_draw(*map) || (depthMode != 0 && depth > depthMode)) {
return 0;
}

Expand Down
9 changes: 9 additions & 0 deletions minimax.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@

#include "tictactoe.h"

#define MODE_EASY 1
#define MODE_MEDIUM 2
#define MODE_HARD 3
#define MODE_EXPERT 0


struct cell {
int row;
int col;
};

struct cell new_cell(int row, int col);

int getMode();
void setMode(int value);

int get_moves(enum field map[SIZE][SIZE], struct cell** moves);
int minimax(enum field (*map)[SIZE][SIZE], int* row, int* cell, enum field player, int depth);

Expand Down
Loading

0 comments on commit 4afeb0d

Please sign in to comment.