AI Opponent class. More...
#include <AIOpponent.h>
Public Member Functions | |
AIOpponent (int difficulty) | |
~AIOpponent () | |
CIwFVec2 | CalculateShot (char arr[6][7]) |
Protected Types | |
enum | State { EASY = 1, MEDIUM = 2, HARD = 3 } |
Possible difficulties. | |
Protected Member Functions | |
bool | CanMakeMove (char g[6][7], int col) |
int | GetNextEmptyInCol (char g[6][7], int col) |
char | CheckHorizontal (char g[6][7]) |
char | CheckVertical (char g[6][7]) |
char | CheckDiagonalOne (char g[6][7]) |
char | CheckDiagonalTwo (char g[6][7]) |
bool | CheckDraw (char g[6][7]) |
char | CheckForWinner (char g[6][7]) |
void | EvaluateSegment (char s[5], int &redFour, int &redThree, int &redTwo, int &yellowFour, int &yellowThree, int &yellowTwo, int &fourBlock, int &threeBlock, int &twoBlock) |
void | EvaluateHorizontals (char g[6][7], int &redFour, int &redThree, int &redTwo, int &yellowFour, int &yellowThree, int &yellowTwo, int &fourBlock, int &threeBlock, int &twoBlock) |
void | EvaluateVerticals (char g[6][7], int &redFour, int &redThree, int &redTwo, int &yellowFour, int &yellowThree, int &yellowTwo, int &fourBlock, int &threeBlock, int &twoBlock) |
void | EvaluateDiagonalOne (char g[6][7], int &redFour, int &redThree, int &redTwo, int &yellowFour, int &yellowThree, int &yellowTwo, int &fourBlock, int &threeBlock, int &twoBlock) |
void | EvaluateDiagonalTwo (char g[6][7], int &redFour, int &redThree, int &redTwo, int &yellowFour, int &yellowThree, int &yellowTwo, int &fourBlock, int &threeBlock, int &twoBlock) |
int | EvaluatePosition (char g[6][7], bool turn) |
int | UpdateGrid (char g[6][7], int col, char colour) |
void | MakeMove (char g[6][7], int col) |
int | NegaMax (char g[6][7], int depth, int &bM, int row, int col, bool aiTurn) |
int | GetMove (char g[6][7], int &score) |
Protected Attributes | |
CIwFVec2 | m_validShots [7] |
Vector of successful shots - one for each column. | |
bool | m_turn |
Is is the ai's turn? | |
int | m_difficulty |
Int representing difficulty (1 = easy, 2 = medium, 3 = hard). | |
Static Protected Attributes | |
static const int | fourWeight = 10000 |
Weighting for four in a row. | |
static const int | threeWeight = 300 |
Weighting for three in a row. | |
static const int | twoWeight = 40 |
Weighting for two in a row. | |
static const int | fourBlockWeight = 7500 |
Weighting for blocking four. | |
static const int | threeBlockWeight = 75 |
Weighting for blocking three. | |
static const int | twoBlockWeight = 10 |
Weighting for blocking two. |
AI Opponent class.
AI Opponent for singleplayer matches. Chooses shots using NegaMax and returns a velocity vector for the disc which will fire it into the chosen column.
AIOpponent::AIOpponent | ( | int | difficulty | ) |
Constructor.
Create a new AI Opponent.
[in] | difficulty | Difficulty level (1 = easy, 2 = medium, 3 = hard). |
AIOpponent::~AIOpponent | ( | ) |
Destructor.
Destroy the AI Opponent and free all associated memory.
CIwFVec2 AIOpponent::CalculateShot | ( | char | arr[6][7] | ) |
Calculate a shot.
Uses NegaMax to calculate the best move.
bool AIOpponent::CanMakeMove | ( | char | g[6][7], | |
int | col | |||
) | [inline, protected] |
Can make move.
Can a disc be dropped into a certain column.
[in] | g | The grid. |
[in] | col | The column to drop a disc in. |
char AIOpponent::CheckDiagonalOne | ( | char | g[6][7] | ) | [inline, protected] |
Check diagonals.
Check whether there are four discs in a row diagonally downwards.
[in] | g | The grid. |
char AIOpponent::CheckDiagonalTwo | ( | char | g[6][7] | ) | [inline, protected] |
Check diagonals.
Check whether there are four discs in a diagonally upwards.
[in] | g | The grid. |
bool AIOpponent::CheckDraw | ( | char | g[6][7] | ) | [inline, protected] |
Check draw.
Check whether there has been a draw.
[in] | g | The grid. |
char AIOpponent::CheckForWinner | ( | char | g[6][7] | ) | [inline, protected] |
Check winner.
Check whether the game has been won.
[in] | g | The grid. |
char AIOpponent::CheckHorizontal | ( | char | g[6][7] | ) | [inline, protected] |
Check horizontals.
Check whether there are four discs in a row horizontally.
[in] | g | The grid. |
char AIOpponent::CheckVertical | ( | char | g[6][7] | ) | [inline, protected] |
Check verticals.
Check whether there are four discs in a row vertically.
[in] | g | The grid. |
void AIOpponent::EvaluateDiagonalOne | ( | char | g[6][7], | |
int & | redFour, | |||
int & | redThree, | |||
int & | redTwo, | |||
int & | yellowFour, | |||
int & | yellowThree, | |||
int & | yellowTwo, | |||
int & | fourBlock, | |||
int & | threeBlock, | |||
int & | twoBlock | |||
) | [inline, protected] |
Evaluate diagonals.
Evaluate all segments in the grid's down diagonals and assign them scores for NegaMax.
[in] | g | The grid. |
[out] | redFour | Number of red four in a rows. |
[out] | redThree | Number of red three in a rows. |
[out] | redTwo | Number of red two in a rows. |
[out] | yellowFour | Number of yellow four in a rows. |
[out] | yellowThree | Number of yellow three in a rows. |
[out] | yellowTwo | Number of yellow two in a rows. |
[out] | fourBlock | Number of blocked potential four in a rows. |
[out] | threeBlock | Number of blocked potential three in a rows. |
[out] | twoBlock | Number of blocked potential two in a rows. |
void AIOpponent::EvaluateDiagonalTwo | ( | char | g[6][7], | |
int & | redFour, | |||
int & | redThree, | |||
int & | redTwo, | |||
int & | yellowFour, | |||
int & | yellowThree, | |||
int & | yellowTwo, | |||
int & | fourBlock, | |||
int & | threeBlock, | |||
int & | twoBlock | |||
) | [inline, protected] |
Evaluate diagonals.
Evaluate all segments in the grid's up diagonals and assign them scores for NegaMax.
[in] | g | The grid. |
[out] | redFour | Number of red four in a rows. |
[out] | redThree | Number of red three in a rows. |
[out] | redTwo | Number of red two in a rows. |
[out] | yellowFour | Number of yellow four in a rows. |
[out] | yellowThree | Number of yellow three in a rows. |
[out] | yellowTwo | Number of yellow two in a rows. |
[out] | fourBlock | Number of blocked potential four in a rows. |
[out] | threeBlock | Number of blocked potential three in a rows. |
[out] | twoBlock | Number of blocked potential two in a rows. |
void AIOpponent::EvaluateHorizontals | ( | char | g[6][7], | |
int & | redFour, | |||
int & | redThree, | |||
int & | redTwo, | |||
int & | yellowFour, | |||
int & | yellowThree, | |||
int & | yellowTwo, | |||
int & | fourBlock, | |||
int & | threeBlock, | |||
int & | twoBlock | |||
) | [inline, protected] |
Evaluate horizontals.
Evaluate all segments in the grid's rows and assign them scores for NegaMax.
[in] | g | The grid. |
[out] | redFour | Number of red four in a rows. |
[out] | redThree | Number of red three in a rows. |
[out] | redTwo | Number of red two in a rows. |
[out] | yellowFour | Number of yellow four in a rows. |
[out] | yellowThree | Number of yellow three in a rows. |
[out] | yellowTwo | Number of yellow two in a rows. |
[out] | fourBlock | Number of blocked potential four in a rows. |
[out] | threeBlock | Number of blocked potential three in a rows. |
[out] | twoBlock | Number of blocked potential two in a rows. |
int AIOpponent::EvaluatePosition | ( | char | g[6][7], | |
bool | turn | |||
) | [inline, protected] |
Evaluate position.
Evaluate a grid and assign it a score for NegaMax.
[in] | g | The grid. |
[in] | turn | The player who took the last turn (true for ai, false for player). |
void AIOpponent::EvaluateSegment | ( | char | s[5], | |
int & | redFour, | |||
int & | redThree, | |||
int & | redTwo, | |||
int & | yellowFour, | |||
int & | yellowThree, | |||
int & | yellowTwo, | |||
int & | fourBlock, | |||
int & | threeBlock, | |||
int & | twoBlock | |||
) | [inline, protected] |
Evaluate segment.
Evaluate a 4 tile segment of the grid and assign it a score for NegaMax.
[in] | s | The segment. |
[out] | redFour | Number of red four in a rows. |
[out] | redThree | Number of red three in a rows. |
[out] | redTwo | Number of red two in a rows. |
[out] | yellowFour | Number of yellow four in a rows. |
[out] | yellowThree | Number of yellow three in a rows. |
[out] | yellowTwo | Number of yellow two in a rows. |
[out] | fourBlock | Number of blocked potential four in a rows. |
[out] | threeBlock | Number of blocked potential three in a rows. |
[out] | twoBlock | Number of blocked potential two in a rows. |
void AIOpponent::EvaluateVerticals | ( | char | g[6][7], | |
int & | redFour, | |||
int & | redThree, | |||
int & | redTwo, | |||
int & | yellowFour, | |||
int & | yellowThree, | |||
int & | yellowTwo, | |||
int & | fourBlock, | |||
int & | threeBlock, | |||
int & | twoBlock | |||
) | [inline, protected] |
Evaluate verticals.
Evaluate all segments in the grid's columns and assign them scores for NegaMax.
[in] | g | The grid. |
[out] | redFour | Number of red four in a rows. |
[out] | redThree | Number of red three in a rows. |
[out] | redTwo | Number of red two in a rows. |
[out] | yellowFour | Number of yellow four in a rows. |
[out] | yellowThree | Number of yellow three in a rows. |
[out] | yellowTwo | Number of yellow two in a rows. |
[out] | fourBlock | Number of blocked potential four in a rows. |
[out] | threeBlock | Number of blocked potential three in a rows. |
[out] | twoBlock | Number of blocked potential two in a rows. |
int AIOpponent::GetMove | ( | char | g[6][7], | |
int & | score | |||
) | [inline, protected] |
Get move.
Get the AI's next move.
[in] | g | The grid. |
[out] | score | Score of the move. |
int AIOpponent::GetNextEmptyInCol | ( | char | g[6][7], | |
int | col | |||
) | [inline, protected] |
Get next empty.
Returns the index of the next empty row in a given column.
[in] | g | The grid. |
[in] | col | The column to drop a disc in. |
void AIOpponent::MakeMove | ( | char | g[6][7], | |
int | col | |||
) | [inline, protected] |
Make a move.
Add a disc to a grid.
[in] | g | The grid. |
[in] | col | Column to add the disc to. |
int AIOpponent::NegaMax | ( | char | g[6][7], | |
int | depth, | |||
int & | bM, | |||
int | row, | |||
int | col, | |||
bool | aiTurn | |||
) | [inline, protected] |
NegaMax.
Perform the NegaMax algorithm.
[in] | g | The grid. |
[in] | depth | Depth to search to. |
[out] | bM | Best move. |
[in] | col | Column of move. |
[in] | row | Row of move. |
[in] | aiTurn | Is this an ai turn or player turn?. |
int AIOpponent::UpdateGrid | ( | char | g[6][7], | |
int | col, | |||
char | colour | |||
) | [inline, protected] |
Update grid.
Add a disc to a grid.
[in] | g | The grid. |
[in] | col | Column to add the disc to. |
[in] | colour | Colour of the disc to be added. |