-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputer2.cc
More file actions
64 lines (56 loc) · 1.76 KB
/
computer2.cc
File metadata and controls
64 lines (56 loc) · 1.76 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
//
// computer2.cpp
// PawnPusher9000
//
// Created by Lavi on 2015-11-28.
// Copyright © 2015 Lavi. All rights reserved.
//
#include "computer2.h"
#include <ctype.h>
#include <cstdlib>
#include <time.h>
void ComputerAI_2::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++){
string begPos=cp.ChessSquareConverti(choice[choiceInd]/64);
string endPos=cp.ChessSquareConverti(choice[choiceInd]%64);
score[choiceInd]=pieceValue(cp.getSquare(endPos));
MoveFootprint & fp=cp.makeMove(begPos,endPos);
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--;
}
}
}