-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI.h
94 lines (86 loc) · 2.89 KB
/
AI.h
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
93
94
//
// AI.hpp
// connect4
//
// Created by Tyler Small on 4/10/16.
// Copyright © 2016 Tyler Small. All rights reserved.
//
#ifndef AI_h
#define AI_h
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include "Board.h"
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define LOOK_AHEAD 5 //set depth of tree
#define TABLE_SIZE 32000
#define TABLE_BIN_SIZE 10
//stating this class exists
class Board;
//TranspositionTable
typedef struct {
Board*** bins;
} TranspositionTable;
//GameTreeNode
typedef struct {
Board* board;
int player;
int other_player;
int turn;
int alpha;
int beta;
int best_move;
TranspositionTable* ht;
} GameTreeNode;
class AI {
public:
//constructor
AI();
//Evaluation function passes coordinates, board, player, returns int (score)
int static countAt(Board* board, int x, int y, int player);
//finds the amount of connected pieces for a given player in an array, used in countAt
int static getIncrementForArray(int* arr, int player);
//performs the evaluation function given the board and the players for the current move
int static getHeuristic(Board board, int player, int other_player);
//copies the board and returns said board after a given move
Board* stateForMove(Board *board, int column, int player);
//creates a hash value for the board
unsigned long long hashBoard(Board board);
//checks to see if two boards are equal
int isBoardEqual(Board *board1, Board *board2);
//create new hash table
TranspositionTable* newTable();
//find a board in the table
Board* lookupInTable(TranspositionTable* t, Board* k);
//add a board to the table
void addToTable(TranspositionTable* t, Board* k);
//deallocate hash table
void freeTranspositionTable(TranspositionTable* t);
//create tree node
GameTreeNode* newGameTreeNode(Board* gs, int player, int other, int turn, int alpha, int beta, TranspositionTable* ht);
//return the score for the current player
int static heuristicForBoard(Board* gs, int player, int other);
//ascending comparison
int static ascComp(const void* a, const void* b);
//descending comparison
int static desComp(const void* a, const void* b);
//main alpha-beta algorithm
int getWeight(GameTreeNode* node, int movesLeft);
//runs get-weight
int getBestMove(GameTreeNode* node, int movesLeft);
//runs newTable newGameTreeNode and getBestMove, frees the node and runs freeTranspositionTable, returns the best move
int bestMove(Board* gs, int player, int other_player, int look_ahead);
//runs bestMove
int computerMove(int look_ahead, Board board);
//keep track of the depth
void retainGameState(Board* board);
//go back one level or if the root node, free the board
void freeGameState(Board* board);
private:
int CPU;
int HUMAN;
};
#endif /* AI_h */