-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from BrinzaBezrukoff/dev
Rewrited some shit
- Loading branch information
Showing
9 changed files
with
226 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
binaries/ | ||
bin/ | ||
tictactoe | ||
*.o | ||
*.vim | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
all: tictactoe | ||
all: outdir tictactoe | ||
|
||
tictactoe: main.o tictactoe.o minimax.o | ||
gcc -o tictactoe main.o tictactoe.o minimax.o | ||
tictactoe: bin/main.o bin/tictactoe.o bin/ai.o | ||
gcc -o tictactoe bin/main.o bin/tictactoe.o bin/ai.o | ||
|
||
main.o: main.c | ||
gcc -c main.c | ||
bin/main.o: main.c | ||
gcc -c main.c -o bin/main.o | ||
|
||
tictactoe.o: tictactoe.c | ||
gcc -c tictactoe.c | ||
bin/tictactoe.o: tictactoe.c | ||
gcc -c tictactoe.c -o bin/tictactoe.o | ||
|
||
minimax.o: minimax.c | ||
gcc -c minimax.c | ||
bin/ai.o: ai.c | ||
gcc -c ai.c -o bin/ai.o | ||
|
||
clean: | ||
rm -rf *.o tictactoe | ||
rm -rf bin tictactoe | ||
|
||
outdir: bin | ||
|
||
bin: | ||
mkdir -p bin | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
#include "tictactoe.h" | ||
#include "ai.h" | ||
|
||
|
||
static int depthMode = MODE_EXPERT; | ||
|
||
|
||
int randrange(int a, int b) { | ||
return rand() % (b - a + 1) + a; | ||
} | ||
|
||
|
||
int getMode() { | ||
return depthMode; | ||
} | ||
|
||
void setMode(int value) { | ||
depthMode = value; | ||
} | ||
|
||
|
||
int getMoves(FieldT (*map)[SIZE][SIZE], Cell** moves, size_t* n) { | ||
if (*n < 1 || *moves == NULL) { | ||
*n = sizeof(Cell); | ||
*moves = (Cell*) malloc(*n); | ||
} | ||
int count = 0; | ||
for (int i = 0; i < SIZE; i++) { | ||
for (int j = 0; j < SIZE; j++) { | ||
if ((*map)[i][j] != EMPTY) { | ||
continue; | ||
} | ||
count++; | ||
if (*n < count * sizeof(Cell)) { | ||
*n += sizeof(Cell); | ||
*moves = (Cell*) realloc(*moves, *n); | ||
if (!moves) { | ||
fprintf(stderr, "Not enough memory!\n"); | ||
exit(1); | ||
} | ||
} | ||
(*moves)[count - 1] = Cell_create(i, j); | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
|
||
int ai_minimax(FieldT (*map)[SIZE][SIZE], Cell* turn, FieldT player, int depth) { | ||
turn->row = -1; | ||
turn->col = -1; | ||
|
||
FieldT winner = check_winner(*map); | ||
if (winner != EMPTY) { | ||
return (winner == ZERO) ? 10 : -10; | ||
} | ||
if (is_draw(*map) || (depthMode != 0 && depth > depthMode)) { | ||
return 0; | ||
} | ||
|
||
Cell* moves; | ||
int count = 0; | ||
size_t allocated = 0; | ||
count = getMoves(map, &moves, &allocated); | ||
|
||
Cell* ch_move; | ||
int mnmx = (player == ZERO) ? -20 : 20; // optimized value | ||
int res = 0; // last minimax value | ||
int rw, cl; // make move | ||
|
||
for (int i = 0; i < count; i++) { | ||
rw = moves[i].row; | ||
cl = moves[i].col; | ||
|
||
(*map)[rw][cl] = player; | ||
|
||
res = ai_minimax(map, turn, switch_player(player), depth + 1); | ||
|
||
if ((player == ZERO && res > mnmx) || (player == CROSS && res < mnmx)) { | ||
mnmx = res; | ||
ch_move = moves + i; | ||
} | ||
|
||
(*map)[rw][cl] = EMPTY; | ||
} | ||
|
||
turn->row = ch_move->row; | ||
turn->col = ch_move->col; | ||
|
||
free(moves); | ||
|
||
return mnmx; | ||
} | ||
|
||
|
||
void ai_random(FieldT (*map)[SIZE][SIZE], Cell* turn) { | ||
Cell* moves; | ||
size_t allocated = 0; | ||
int count = 0; | ||
count = getMoves(map, &moves, &allocated); | ||
int r_i = randrange(0, count - 1); | ||
*turn = moves[r_i]; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef AI_H | ||
#define AI_H | ||
|
||
#include "tictactoe.h" | ||
|
||
#define MODE_EASY 1 | ||
#define MODE_MEDIUM 2 | ||
#define MODE_HARD 3 | ||
#define MODE_EXPERT 0 | ||
|
||
|
||
// random integer from a to b | ||
int randrange(int a, int b); | ||
|
||
// minimax depth control | ||
int getMode(); | ||
void setMode(int value); | ||
|
||
// service functions | ||
int getMoves(FieldT (*map)[SIZE][SIZE], Cell** moves, size_t* n); | ||
|
||
// turns functions | ||
int ai_minimax(FieldT (*map)[SIZE][SIZE], Cell* turn, FieldT player, int depth); | ||
void ai_random(FieldT (*map)[SIZE][SIZE], Cell* turn); | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.