-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicPlayer.java
95 lines (81 loc) · 2.41 KB
/
BasicPlayer.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
/*a BasicPlayer represents the player entity who will be playing the game. A player's hand and strategy
are all specific to BasicPlayer, meaning most of what BasicPlayer does concerns their hand or strategy,
the majority of what a player does is dictated by their strategy
*/
public class BasicPlayer implements Player {
//player-specific attributes
private Hand playerHand;
private Strategy playerStrategy;
private CardGame playerGame;
/*----------CONSTRUCTOR METHODS-----------*/
public BasicPlayer(String strategy, CardGame game)
{
playerHand = new Hand();
//try to set strategy using strategy factory
try
{
new StrategyFactory(this, strategy);
}
//if invalid arguments, set strategy to BasicPlayer by default
catch(InvalidArgumentsException ex)
{
this.setStrategy(new BasicStrategy());
}
this.setGame(game);
}
/*----------CLASS METHODS-------------*/
//adds a card to the player's hand
@Override
public void addCard(Card c)
{
playerHand.addSingleCard(c);
}
//adds a hand to the player's deck
@Override
public void addHand(Hand h)
{
playerHand.addHand(h);
}
//determines what the player will do on their turn
@Override
public Bid playHand(Bid b)
{
//evaluate whether to cheat or not
boolean cheat = playerStrategy.cheat(b, playerHand);
Bid chosenBid = playerStrategy.chooseBid(b, playerHand, cheat);
//remove cards about to be played from the players hand
playerHand.removeHand(chosenBid.getHand());
//plays hand accordingly
return chosenBid;
}
//uses strategy to decide whether to call cheat or not
@Override
public boolean callCheat(Bid b)
{
return playerStrategy.callCheat(playerHand, b);
}
/*----------ATTRIBUTE ACCESSOR & MUTATOR METHODS--------*/
//returns the size of the players hand
@Override
public int cardsLeft()
{
return playerHand.size();
}
//sets the game this player belongs to
@Override
public void setGame(CardGame g)
{
this.playerGame = g;
}
//sets the strategy this player is following
@Override
public void setStrategy(Strategy s)
{
this.playerStrategy = s;
}
@Override
public Hand getHand()
{
return playerHand;
}
}