-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewBot.js
134 lines (108 loc) · 4.26 KB
/
newBot.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class Bot {
//Dynamite functionaality to be separate / added later
whatBeats(myChar){
if (myChar==="R"){
return "P";
}
else if (myChar==="P"){
return "S";
}
else if (myChar==="S"){
return "R";
}
}
numberOfConsecutiveDraws(gamestate){
let i=1;
while (i<=gamestate.rounds.length && gamestate.rounds[gamestate.rounds.length-i].p2===gamestate.rounds[gamestate.rounds.length-i].p1){
i++;
}
return i-1;
}
stringOfLastNMoves(n, p2PreviousOutput){
let outputString = "";
for( let i=1; i<=Math.min(n, p2PreviousOutput.length); i++){
outputString += p2PreviousOutput[p2PreviousOutput.length-i];
}
return outputString;
}
makeMove(gamestate) {
// weight previous moves more heavily but take into account all previous moves
// after that, think about how and when to deploy the dynamite (after a certain number of draws, and taking into account the max number of dynamites useable)
let output;
let p2RockPercent;
let p2ScissorsPercent;
let p2PaperPercent;
let rand;
//if it's the first round
if (gamestate.rounds.length === 0) {
this.dynamiteCount = 0;
this.roundCount = 0;
this.p1PreviousOutput = [];
this.p2PreviousOutput = [];
output = ['P','R','S'][Math.floor(Math.random() * 3)];
}
//else if it's not the first round:
else{
let drawThreshold = 3;
if(this.stringOfLastNMoves(4,this.p2PreviousOutput)==="DDDD"){
output = "W";
}
//if there have been enough consecutive draws - play dynamite
else if (this.dynamiteCount<100 && this.numberOfConsecutiveDraws(gamestate)>=drawThreshold){
rand = Math.random()*100
if (this.numberOfConsecutiveDraws(gamestate)===4){
output = "D";
this.dynamiteCount++;
}
else if (rand > this.dynamiteCount){
output = "D";
this.dynamiteCount++;
}
}
//if there are enough consecutive draws, but no dynamite left
else if(this.dynamiteCount==0 && this.numberOfConsecutiveDraws(gamestate)>=drawThreshold){
output = "W";
}
if (!output){
let p2LastMove = gamestate.rounds[this.roundCount-1].p2;
this.p2PreviousOutput.push(p2LastMove);
p2RockPercent = this.p2PreviousOutput.filter(i=>i==="R").length/this.roundCount;
p2ScissorsPercent = this.p2PreviousOutput.filter(i=>i==="S").length/this.roundCount;
p2PaperPercent = this.p2PreviousOutput.filter(i=>i==="P").length/this.roundCount;
rand = Math.random() * (p2RockPercent + p2ScissorsPercent + p2PaperPercent);
let toBeat;
if (rand<p2RockPercent)
{
toBeat = "R";
}
else if (rand < p2RockPercent + p2ScissorsPercent)
{
toBeat = "S";
}
else
{
toBeat = "P";
}
if (Math.random()<0.25){
toBeat = this.whatBeats(toBeat);
}
output = this.whatBeats(toBeat);
//if we're getting close to the end, more likely to override dynamite
if (this.dynamiteCount<100 && this.roundCount>1500)
{
if (Math.random()<0.20){
output = "D";
this.dynamiteCount++;
}
}
}
}
// if (gamestate.rounds[gamestate.rounds.length-1].p1===this.whatBeats(gamestate.rounds[gamestate.rounds.length-1].p2)){
// this.p1Score += this.numberOfConsecutiveDraws(gamestate);
// }
this.roundCount++;
this.p1PreviousOutput.push(output);
return output;
}
}
module.exports = new Bot();