Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "dseif.h"
- #include "c5utils.h"
- #include <math.h>
- #define NULL 0
- #define MAXSCORE 1000
- #include "Configuration.h"
- #include <iostream>
- using namespace std;
- int turn = 0;
- int total = 0;
- int lBound = 0;
- int rBound = 0;
- int bBound = 0;
- int tBound = 0;
- int alpha = 1000000;
- int beta = -1000000;
- dseif::Node::Node(){
- for(int i=0;i<NUMSQUARES;i++){
- for(int j=0;j<NUMSQUARES;j++){
- child_[i][j]=NULL;
- }
- }
- }
- /*board is the board right now. lvl is the lvl of the node we are creating,
- row and col is the position of the most recently added piece. row and col are
- not relevant when lvl is 0 and are invalid*/
- dseif::Node* dseif::GameTree::CreateTree(int board[][NUMSQUARES],int lvl,int row,int col, int &alpha, int &beta){
- Node* nn=new Node();
- int whowon;
- int whomoves=(lvl%2)?-player_:player_;
- if(lvl > 0)
- whowon=fastCheckWin(board,row,col);
- else
- whowon=checkWin(board);
- //Base case 1: someone has won the game, game is over score is 100
- if(whowon) {
- nn->score_=whowon * player_* MAXSCORE; //if player_ is 1 and they won the whowon would be +ve so
- //we have score being +1, otherwise if player_ is -1 and
- //player 2 won, then whowon would be -1 so score will still
- //be +1.
- }
- else if(boardFull(board)){ //tie game
- nn->score_=0;
- }
- else if(lvl==height_-1){ //game is tied.
- nn->score_=Eval(board,player_, 0, 0); //always evaluate for player at root!
- }
- else{
- if (turn == 0)
- {
- bestrow_=7;
- bestcol_=7;
- turn += 2;
- lBound = 7;
- rBound = 7;
- bBound = 7;
- tBound = 7;
- }
- else
- {
- int tmp = 0;
- for(int i=tBound-1;i <= bBound+1;i++){
- for(int j=lBound-1;j <= rBound+1;j++){
- if(board[i][j]==0){
- board[i][j]=whomoves; //make the move onto the board//undo the move...this is important
- nn->child_[i][j]=CreateTree(board,lvl+1,i,j, alpha, beta); //recursively create the //subtree
- nn->child_[i][j]->threat_ = Eval(board, -whomoves, i, j);
- nn->child_[i][j]->score_ = Eval(board, whomoves, i, j);
- board[i][j]=0;
- if(nn->child_[i][j]->score_ > tmp)
- {
- if(i < tBound)
- tBound = i;
- if(i > bBound)
- bBound = i;
- if(j > rBound)
- rBound = j;
- if(j < lBound)
- lBound = j;
- }
- /*if(nn->child_[i][j]->score_ > beta)
- {
- beta = nn->score_;
- break;
- }
- if(nn->child_[i][j]->score_ < alpha && nn->child_[i][j]->score_ > beta)
- alpha = nn->score_;
- */
- }
- }
- }/*for j*/
- }/*for i*/
- }
- bool setgood=false;
- for(int i=tBound-1;i <= bBound+1;i++){
- for(int j=lBound-1;j <= rBound+1;j++){
- if(nn->child_[i][j]){
- if(!setgood || nn->child_[i][j]->score_ > nn->score_ && nn->child_[i][j]->score_ > nn->child_[i][j]->threat_){
- nn->score_=nn->child_[i][j]->score_;
- setgood=true;
- if(lvl==0){
- bestrow_= i;
- bestcol_= j;
- }
- }
- else if(!setgood || nn->child_[i][j]->threat_ > nn->score_ && nn->child_[i][j]->threat_ > nn->child_[i][j]->score_){
- nn->score_=nn->child_[i][j]->threat_;
- setgood=true;
- if(lvl==0){
- bestrow_= i;
- bestcol_= j;
- }
- }
- }
- }//for
- }//for
- return nn;
- }
- /*PLEASE PLEASE PLEASE REMEMBER YOUR DESTRUCTORS! with the size of the trees
- we are creating, a lack of one will be a major issue*/
- dseif::GameTree::~GameTree(){
- DestroyTree(root_);
- }
- void dseif::GameTree::DestroyTree(Node* root){
- if (root){
- for(int i=0;i<NUMSQUARES;i++){
- for(int j=0;j<NUMSQUARES;j++){
- DestroyTree(root->child_[i][j]);
- }
- }
- delete root;
- }/*if*/
- }
- /*this function returns a board score for the given board for the player
- indicated. well.. actually it just randomly generates a number
- and returns that right now. You will need to make it better.*/
- int dseif::Eval(int board[][NUMSQUARES],int player, int row, int col){
- int score = 0;
- double mult = 0;
- if(player == 1)
- mult = 0.5;
- board[row][col] = player;
- if(numUp(board,row,col) > 0)
- {
- if(board[row-numUp(board,row,col)-1][col] == 0)
- mult += 0.25;
- score += pow(10.0, numUp(board,row,col)+(mult+numDown(board,row,col)));
- mult = 0;
- }
- if(numDown(board,row,col) > 0)
- {
- if(board[row+numDown(board,row,col)+1][col] == 0)
- mult += 0.25;
- score += pow(10.0, numDown(board,row,col)+(mult+numUp(board,row,col)));
- mult = 0;
- }
- if(numLeft(board,row,col) > 0)
- {
- if(board[row][col-numLeft(board,row,col)-1] == 0)
- mult += 0.25;
- score += pow(10.0, numLeft(board,row,col)+ (mult+numRight(board,row,col)));
- mult = 0;
- }
- if(numRight(board,row,col) > 0)
- {
- if(board[row][col+numRight(board,row,col)+1] == 0)
- mult += 0.25;
- score += pow(10.0, numRight(board,row,col)+ (mult+numLeft(board,row,col)));
- mult = 0;
- }
- if(numUpLeft(board,row,col) > 0)
- {
- if(board[row-numUpLeft(board,row,col)-1][col-numUpLeft(board,row,col)-1] == 0)
- mult += 0.25;
- score += pow(10.0, numUpLeft(board,row,col)+ (mult+numDownRight(board,row,col)));
- mult = 0;
- }
- if(numUpRight(board,row,col) > 0)
- {
- if(board[row-numUpRight(board,row,col)-1][col+numUpRight(board,row,col)+1] == 0)
- mult += 0.25;
- score += pow(10.0, numUpRight(board,row,col)+ (mult+numDownLeft(board,row,col)));
- mult = 0;
- }
- if(numDownLeft(board,row,col) > 0)
- {
- if(board[row+numDownLeft(board,row,col)+1][col-numDownLeft(board,row,col)-1] == 0)
- mult += 0.25;
- score += pow(10.0, numDownLeft(board,row,col)+ (mult+numUpRight(board,row,col)));
- mult = 0;
- }
- if(numDownRight(board,row,col) > 0)
- {
- if(board[row+numDownRight(board,row,col)+1][col+numDownRight(board,row,col)+1] == 0)
- mult += 0.25;
- score += pow(10.0, numDownRight(board,row,col)+ (mult+numUpLeft(board,row,col)));
- mult = 0;
- }
- board[row][col] = 0;
- return score;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement