-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess_AI.c
92 lines (78 loc) · 2.09 KB
/
chess_AI.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Created by William on 3/15/2019.
// Contains functions for running the AI
//
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "min_max.h"
#include "chess_alpha_beta.h"
#include "chess_board.h"
#include "chess_moves_save.h"
#include "chess_moves_saves_black.h"
#include "chess_moves_saves_white.h"
#include "chess_alpha_beta.h"
#include "chess_AI.h"
#include "chess.h"
#define SEARCH_DEPTH 5
/**
* gets the chess move for an AI to move
* @param board The board to use for move calculation
* @return The move to make
*/
chessMove * get_move_to_move(chessBoard board, char player_turn){
if (player_turn == WHITE_PLAYERS_TURN){
chessMove * move = malloc(sizeof(chessMove));
int8_t out = treeless_chess_alpha_beta(MAX, board, SEARCH_DEPTH, move);
#ifdef DEBUG
printf("FINAL MAX SCORE: %d\n", out);
print_move(*move);
#endif
if (move->taken == 15){
return NULL;
}
return move;
}else{
chessMove * move = malloc(sizeof(chessMove));
int8_t out = treeless_chess_alpha_beta(MIN, board, SEARCH_DEPTH, move);
#ifdef DEBUG
printf("FINAL MIN SCORE: %d\n", out);
print_move(*move);
#endif
if (move->taken == 15){
return NULL;
}
return move;
}
}
/**
* Returns the turn after this turn
* @param player_turn current turn
* @return the next turn
*/
char get_next_turn(char player_turn){
if (player_turn == WHITE_PLAYERS_TURN){
return BLACK_PLAYERS_TURN;
}else{
return WHITE_PLAYERS_TURN;
}
}
/**
* Runs the AI for a turn
* @param board The game board
* @param player_turn The current turn
* @return True if the game is not done
*/
uint8_t run_AI_turn(chessBoard board, char * player_turn){
chessMove * move = get_move_to_move(board, *player_turn);
if (move == NULL){
return 0;
}else{
printf("%c AI MOVES: ", *player_turn);
print_move(*move);
run_chess_move(board, *move);
free(move);
*player_turn = get_next_turn(*player_turn);
return validate_board(board);
}
}