-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputer4.cc
More file actions
108 lines (94 loc) · 3.31 KB
/
computer4.cc
File metadata and controls
108 lines (94 loc) · 3.31 KB
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
95
96
97
98
99
100
101
102
103
//
// computer4.cpp
// PawnPusher9000
//
// Created by Lavi on 2015-11-28.
// Copyright © 2015 Lavi. All rights reserved.
//
#include "computer4.h"
#include <ctype.h>
#include <cstdlib>
#include <time.h>
#include <iostream>
int ComputerAI_4::evaluatePosition(ChessPosition & cp, float ply){
if (ply<=0){
return evaluatePosition(cp);
}
vector <int> choice= cp.getAllLegalMoves();
if (choice.size()==0){
if (cp.isCheck()){
if (cp.colourToMove()==WHITE) return -10000;
else return 10000;
}
else return 0;
}
int best=(cp.colourToMove()==WHITE)?-11000:11000;
for (int choiceInd=0;choiceInd<choice.size();choiceInd++){
string begPos=cp.ChessSquareConverti(choice[choiceInd]/64);
string endPos=cp.ChessSquareConverti(choice[choiceInd]%64);
MoveFootprint &fp=cp.makeMove(begPos, endPos);
int ev;
//extra calculation for interesting variations
if (fp.pieceOnEndPos!=' ' || cp.isCheck()) ev=evaluatePosition(cp, ply-0.9);
else ev=evaluatePosition(cp,ply-1);
cp.undoMove(fp);
delete &fp;
//faster mates are better/worse
if (ev>9000){
ev--;
}
if (ev<-9000){
ev++;
}
if ((cp.colourToMove()==WHITE && best< ev) || (cp.colourToMove()==BLACK && best> ev)) best=ev;
}
return best;
}
//heuristical evaluation, without looking ahead
int ComputerAI_4::evaluatePosition(ChessPosition & cp){
int eval=0;
//pure material evaluation
for (int i=0;i<cp.pieceTypes.length();i++){
eval+=pieceValue(toupper(cp.pieceTypes[i]))*cp.numPiecesOfType(cp.pieceTypes[i])*(i<6?1:-1);
}
return eval;
}
void ComputerAI_4::makeMove(){
srand(time(NULL));
ChessPosition cp=game.getCurrentPosition();
vector<int> choice =cp.getAllLegalMoves();
/*for (int i=0;i<choice.size();i++){
//cout << choice[i]/64 << " " << choice[i]%64 << endl;
cout << cp.ChessSquareConverti(choice[i]/64) << " " << cp.ChessSquareConverti(choice[i]%64) << endl;
}*/
vector<float> score(choice.size());
int maxScore=(cp.colourToMove()==WHITE)?-11000:11000;
int maxScoreCounter=0;
for (int choiceInd=0;choiceInd<choice.size();choiceInd++){
string begPos=cp.ChessSquareConverti(choice[choiceInd]/64);
string endPos=cp.ChessSquareConverti(choice[choiceInd]%64);
MoveFootprint & fp=cp.makeMove(begPos,endPos);
score[choiceInd]=evaluatePosition(cp, 2);
cp.undoMove(fp);
delete &fp;
if ((cp.colourToMove()==WHITE && score[choiceInd]>maxScore) || (cp.colourToMove()==BLACK && score[choiceInd]<maxScore)) {
maxScore=score[choiceInd];
maxScoreCounter=1;
}
else if(score[choiceInd]==maxScore){
maxScoreCounter++;
}
}
int finalChoice=rand()%maxScoreCounter;
for (int choiceInd=0;choiceInd<choice.size();choiceInd++){
if (score[choiceInd]==maxScore){
if (finalChoice==0){
string begPos=cp.ChessSquareConverti(choice[choiceInd]/64);
string endPos=cp.ChessSquareConverti(choice[choiceInd]%64);
game.makeMove(begPos, endPos);
break;
}
else finalChoice--;
}
}
}