-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicStrategy.java
More file actions
161 lines (136 loc) · 5.06 KB
/
BasicStrategy.java
File metadata and controls
161 lines (136 loc) · 5.06 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*BasicStrategy is a computer-controlled strategy which has the lowest level
of complexity to its decisions and for this reason is a superclass to all the other strategies.
it contains 2 protected methods which perform tasks that are required for any strategy
*/
import java.util.*;
public class BasicStrategy implements Strategy {
//overridden to always return false as BasicStrategy never cheats
@Override
public boolean cheat(Bid b, Hand h)
{
Card.Rank bidRank = b.getRank();
Iterator<Card> it = h.iterator();
while(it.hasNext())
{
Card curCard = it.next();
//if a card exists in the player's hand whose rank is required by the bid, no need to cheat
if(curCard.getRank() == bidRank || curCard.getRank() == bidRank.next())
{
return false;
}
}
//if the player has no cards that match the bid rank, it must cheat
return true;
}
@Override
public Bid chooseBid(Bid b, Hand h, boolean cheat)
{
//card rank must match one of these ranks to be eligible to be played
Card.Rank posRankLower = b.getRank();
Card.Rank posRankHigher = b.getRank().next();
//Hand and Rank used to create Bid instance to be returned
Hand playHand = new Hand();
Card.Rank playRank;
if(cheat)
{
//adding a random card from the hand to the playing hand
playHand.addSingleCard(h.getRandomCard());
//decide on which potential rank to bid as
playRank = getCheatRank(posRankLower, posRankHigher);
return new Bid(playHand, playRank);
}
else
{
//holds the cards eligible to be played for either possible rank
ArrayList<Card> toPlayLower = new ArrayList<>();
ArrayList<Card> toPlayHigher = new ArrayList<>();
//generate ArrayList's of cards at each potential rank
toPlayLower = generateToPlay(h, b, 0);
toPlayHigher = generateToPlay(h, b, 1);
//if toPlayLower is empty play toPlayHigher, else play toPlayLower
if(toPlayLower.isEmpty())
{
playHand.addCardCollection(toPlayHigher);
playRank = posRankHigher;
}
else
{
playHand.addCardCollection(toPlayLower);
playRank = posRankLower;
}
return new Bid(playHand, playRank);
}
}
@Override
public boolean callCheat(Hand h, Bid b)
{
int playedCount = b.getHand().size();
int ownCount = countCurHand(h, b);
//if you have over 2 cards of the bid rank the previous bid must be a cheat
if(ownCount + playedCount > 4)
{
return true;
}
else
{
return false;
}
}
//randomly decides whether to bid with the lower or higher rank when cheating
protected Card.Rank getCheatRank(Card.Rank posRankLower, Card.Rank posRankHigher)
{
//creating arraylist from possible ranks to randomly choose one
ArrayList<Card.Rank> ranks = new ArrayList<Card.Rank>();
ranks.add(posRankHigher);
ranks.add(posRankLower);
//randomly selecting whether to cheat as the lower or higher rank
Random rand = new Random();
int index = rand.nextInt(ranks.size());
return ranks.get(index);
}
//returns an ArrayList of cards in the hand that can be played at the given bid rank
//highOrLow != 1 finds cards at the bid rank
//highOrLow == 1 finds cards at one above the bid rank
protected ArrayList<Card> generateToPlay(Hand h, Bid b, int highOrLow)
{
//holds the cards eligible to be played at bid rank
ArrayList<Card> toPlay = new ArrayList<>();
//card rank must match one of the rank given by highOrLow to be eligible to be played
Card.Rank posRank = b.getRank();
if(highOrLow == 1)
{
posRank = b.getRank().next();
}
//iterate over hand and add cards that match posRank to toPlay
Iterator<Card> it = h.iterator();
while(it.hasNext())
{
Card curCard = it.next();
if(curCard.getRank() == posRank)
{
toPlay.add(curCard);
}
}
return toPlay;
}
/*counting matching cards to bid in current hand
embedded in seperate method for code reuse*/
protected int countCurHand(Hand h, Bid b)
{
//get details about the played hand
Card.Rank playedRank = b.getRank();
//iterate through own hand and count how many cards of the played rank there are
Iterator<Card> it = h.iterator();
int ownCount = 0;
while(it.hasNext())
{
Card curCard = it.next();
//if a card in your hand has the same rank add to running total
if(curCard.getRank().equals(playedRank))
{
ownCount++;
}
}
return ownCount;
}
}