Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Static Protected Attributes

AIOpponent Class Reference

AI Opponent class. More...

#include <AIOpponent.h>

List of all members.

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.

Detailed Description

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.


Constructor & Destructor Documentation

AIOpponent::AIOpponent ( int  difficulty  ) 

Constructor.

Create a new AI Opponent.

Parameters:
[in] difficulty Difficulty level (1 = easy, 2 = medium, 3 = hard).
AIOpponent::~AIOpponent (  ) 

Destructor.

Destroy the AI Opponent and free all associated memory.


Member Function Documentation

CIwFVec2 AIOpponent::CalculateShot ( char  arr[6][7]  ) 

Calculate a shot.

Uses NegaMax to calculate the best move.

Returns:
Vector2 shot velocity.
bool AIOpponent::CanMakeMove ( char  g[6][7],
int  col 
) [inline, protected]

Can make move.

Can a disc be dropped into a certain column.

Parameters:
[in] g The grid.
[in] col The column to drop a disc in.
Returns:
bool representing whether a move can be made.
char AIOpponent::CheckDiagonalOne ( char  g[6][7]  )  [inline, protected]

Check diagonals.

Check whether there are four discs in a row diagonally downwards.

Parameters:
[in] g The grid.
Returns:
char E if none, R if red win, Y if yellow win.
char AIOpponent::CheckDiagonalTwo ( char  g[6][7]  )  [inline, protected]

Check diagonals.

Check whether there are four discs in a diagonally upwards.

Parameters:
[in] g The grid.
Returns:
char E if none, R if red win, Y if yellow win.
bool AIOpponent::CheckDraw ( char  g[6][7]  )  [inline, protected]

Check draw.

Check whether there has been a draw.

Parameters:
[in] g The grid.
Returns:
bool representing whether the game is a tie.
char AIOpponent::CheckForWinner ( char  g[6][7]  )  [inline, protected]

Check winner.

Check whether the game has been won.

Parameters:
[in] g The grid.
Returns:
char E if none, R if red win, Y if yellow win.
char AIOpponent::CheckHorizontal ( char  g[6][7]  )  [inline, protected]

Check horizontals.

Check whether there are four discs in a row horizontally.

Parameters:
[in] g The grid.
Returns:
char E if none, R if red win, Y if yellow win.
char AIOpponent::CheckVertical ( char  g[6][7]  )  [inline, protected]

Check verticals.

Check whether there are four discs in a row vertically.

Parameters:
[in] g The grid.
Returns:
char E if none, R if red win, Y if yellow win.
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.

Parameters:
[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.

Parameters:
[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.

Parameters:
[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.

Parameters:
[in] g The grid.
[in] turn The player who took the last turn (true for ai, false for player).
Returns:
int score of the grid.
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.

Parameters:
[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.

Parameters:
[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.

Parameters:
[in] g The grid.
[out] score Score of the move.
Returns:
int column to move in.
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.

Parameters:
[in] g The grid.
[in] col The column to drop a disc in.
Returns:
int the next empty row.
void AIOpponent::MakeMove ( char  g[6][7],
int  col 
) [inline, protected]

Make a move.

Add a disc to a grid.

Parameters:
[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.

Parameters:
[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?.
Returns:
int best score.
int AIOpponent::UpdateGrid ( char  g[6][7],
int  col,
char  colour 
) [inline, protected]

Update grid.

Add a disc to a grid.

Parameters:
[in] g The grid.
[in] col Column to add the disc to.
[in] colour Colour of the disc to be added.
Returns:
int row that the disc was placed into.

The documentation for this class was generated from the following files:
 All Classes Functions Variables Enumerations