-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyStrategy.java
142 lines (127 loc) · 5.04 KB
/
MyStrategy.java
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
135
136
137
138
139
140
141
142
/*MyStrategy is my own custom strategy class that builds upon ThinkerStrategy, it relies heavily on random rolls
to make decisions, with the cheat and callCheat methods modified from ThinkerStrategy to have more random
elements to them
Since it is the most complex, this strategy heavily makes use of other classes' protected methods
as it inherits from ThinkerStrategy, giving it access to ThinkerStrategy's protected methods as well as it's own
superclass (BasicStrategy)
*/
import java.util.ArrayList;
public class MyStrategy extends ThinkerStrategy
implements Strategy {
/*----INITIALIZING ATTRIBUTES----*/
private int numTurns = 0;
private ArrayList<Bid> playedBids = new ArrayList<>();
private Hand previousPlays = new Hand();
//cheatChance is recalculated every time it is referenced
private double cheatChance;
{
if(cheatChance <= 0.3)
{
cheatChance = numTurns / 15.0;
}
else
{
cheatChance = 0.3;
}
}
/*MyStrategy decides whether to cheat the same way as ThinkerStrategy but
the chances of them cheating unnecessarily increase as the game progresses
the chance to cheat caps at a certain point
*/
@Override
public boolean cheat(Bid b, Hand h)
{
//if not cheating, make random decision to cheat anyway
if(!super.cheat(b, h))
{
return randomWithProbability(cheatChance);
}
return super.cheat(b, h);
}
/*MyStrategy chooses their bid depending on how many of that rank has been bid
if that rank has been bid frequently, they have a higher chance
of playing another rank when cheating
*/
@Override
public Bid chooseBid(Bid b, Hand h, boolean cheat)
{
//record current bid to use when deciding future bids
playedBids.add(b);
if(cheat)
{
Hand chosenHand = super.createCheatingPlayHand(b, h);
int count = 0;
//decide on rank to bid as
Card.Rank posRankLower = b.getRank();
Card.Rank posRankHigher = b.getRank().next();
Card.Rank chosenRank = super.getCheatRank(posRankLower, posRankHigher);
//count how many cards were bid for all the times this bid has been made before
for(int i = 0; i < playedBids.size(); i++)
{
if(playedBids.get(i).getRank() == chosenRank)
{
//add on how many cards were bid in a previous bid of that rank
int numPlayed = playedBids.get(i).getCount();
count += numPlayed;
}
}
//decide whether to redraw the hand
double p = count / 5.0;
if(randomWithProbability(p))
{
//remove cards you chosen not to play from hand and redraw
Hand newHand = new Hand(h);
newHand.removeHand(chosenHand);
//reinitialising chosenHand and putting redraw in it
chosenHand = new Hand();
chosenHand = super.createCheatingPlayHand(b, newHand);
chosenRank = super.getCheatRank(posRankLower, posRankHigher);
previousPlays.addHand(chosenHand);
return new Bid(chosenHand, chosenRank);
}
//dont redraw and play the hand as it is
else
{
chosenRank = chosenHand.getHand().get(0).getRank();
previousPlays.addHand(chosenHand);
return new Bid(chosenHand, chosenRank);
}
}
/*when not cheating simply create hand and play it, no need to analyse
number of that rank played as no risk of getting caught
*/
else
{
Hand chosenHand = super.createPlayHand(b, h);
Card.Rank chosenRank = chosenHand.getHand().get(0).getRank();
previousPlays.addHand(chosenHand);
return new Bid(chosenHand, chosenRank);
}
}
/*MyStrategy will use check for certain cheats using their own hand and
will use their previous plays, but as the game progresses MyStrategy
will play more conservatively and call random cheats less*/
@Override
public boolean callCheat(Hand h, Bid b)
{
int playedCount = b.getHand().size();
//combining count methods to count both
int ownCount = countCurHand(h, b);
ownCount += countPrevPlayed(h, b, previousPlays);
//resets previousPlays and playedRanks hand on each cheat call after it has been used
previousPlays = new Hand();
playedBids = new ArrayList<>();
//if you have over 2 cards of that rank the previous player must be cheating
if(ownCount + playedCount > 4)
{
return true;
}
else
{
//determining small probability of calling cheat despite not being certain
//p decreases as numTurns increases
double p = 1.0 / (9 + numTurns);
return randomWithProbability(p);
}
}
}