-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputer3.cc
More file actions
82 lines (67 loc) · 2.18 KB
/
computer3.cc
File metadata and controls
82 lines (67 loc) · 2.18 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
//
// computer3.cpp
// PawnPusher9000
//
// Created by Lavi on 2015-11-27.
// Copyright © 2015 Lavi. All rights reserved.
//
#include "computer3.h"
#include <ctype.h>
#include <cstdlib>
#include <time.h>
void ComputerAI_3::makeMove(){
srand(time(NULL));
ChessPosition cp=game.getCurrentPosition();
vector<int> choice =cp.getAllLegalMoves();
vector<float> score(choice.size());
int maxScore=-100;
int maxScoreCounter=0;
for (int choiceInd=0;choiceInd<choice.size();choiceInd++){
int begPos=choice[choiceInd]/64;
int endPos=choice[choiceInd]%64;
score[choiceInd]=pieceValue(toupper(cp.getSquare(endPos)));
MoveFootprint & fp=cp.makeMove(begPos,endPos);
int loss=0;
for (int i=(cp.colourToMove()==WHITE?6:0);i<(cp.colourToMove()==WHITE?12:6);i++){
for (int j=0;j<cp.numPiecesOfType(cp.pieceTypes[i]);j++){
if (cp.getLegalMovesTo(cp.getSquareOfPiece(cp.pieceTypes[i], j)).size()>0){
loss=pieceValue(toupper(cp.pieceTypes[i]));
break;
}
}
if (loss>0) break;
}
score[choiceInd]-=loss;
switch (cp.flag()){
case CHECK:
score[choiceInd]+=0.2;
break;
case CHECKMATE:
score[choiceInd]=10000;
break;
default:
break;
}
cp.undoMove(fp);
delete &fp;
if (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--;
}
}
}