-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
148 lines (140 loc) · 3.55 KB
/
Player.java
File metadata and controls
148 lines (140 loc) · 3.55 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
import java.io.Serializable;
import java.util.ArrayList;
public class Player implements Serializable{
/**
* A player object is assigned to each client. It contains all the info for him
*/
private static final long serialVersionUID = 1L;
//attributes
private ArrayList<Card> cards; //player's current hand
private String name; //player's name
private int points,bet,ID,chips;
private boolean active,dealer,hasBet,hasStand,hasBlackJack,hasLost,hasNatural21; //booleans that are used for every action in the game
//constructor
public Player(String name) { //initialization of player's attributes
this.name=name;
cards=new ArrayList<Card>();
chips=1000;
active=false;
dealer=false;
hasBet=false;
hasStand=false;
points=0;
bet=0;
hasBlackJack=false;
hasNatural21=false;
hasLost=false;
}
//a method that calculates the points a player has in his hand
public void calculatePoints() {
int sum=0;
int acesAmount=0;
for(Card card: cards) { //for every card in his deck
if(!card.getName().contains("Ace")){ //if the card is not an ace
sum+=card.getValue(); //add the value of the card to the total sum
}else { //if the card is an ace
acesAmount++; //add 1 to the amount of aces in the hand
}
}
int aces=acesAmount;
for(int i=0;i<acesAmount;i++) { //repeat for every ace
if(sum<11 && aces==1) { //if total sum is less than 11 and there is one ace
sum+=11; //add 11 points to the sum
}else { //in any other case
if(aces==0) { //if there aren't any other aces left break out of the loop
break;
}
aces--; //subtract one from the amount of aces
sum++; //add one to the total sum
}
}
points=sum; //assign sum value to points attribute of player
}
//re-initialization of the player
public void reset() { //this method is called after every new round
hasBet=false;
hasStand=false;
hasBlackJack=false;
hasNatural21=false;
bet=0;
points=0;
cards.clear();
}
//a method that receives a card object reference and adds the corresponding object to player's hand
public void addCard(Card card) {
cards.add(card);
}
//getters and setters
public int getPoints() {
return points;
}
public String getName() {
return name;
}
public int getChips() {
return chips;
}
public ArrayList<Card> getCards(){
return cards;
}
public int getBet() {
return bet;
}
public void setBet(int bet) {
this.bet=bet;
}
public void betChips(int bet) {
chips-=bet;
}
public void setChips(int chips) {
this.chips=chips;
}
public void setID(int ID) {
this.ID=ID;
}
public int getID() {
return ID;
}
public void setActive(boolean active) {
this.active=active;
}
public boolean getActive() {
return active;
}
public void setDealer(boolean dealer) {
this.dealer=dealer;
}
public boolean getDealer() {
return dealer;
}
public void setHasBet(boolean hasBet) {
this.hasBet=hasBet;
}
public boolean getHasBet() {
return hasBet;
}
public void setHasStand(boolean hasStand) {
this.hasStand=hasStand;
}
public boolean getHasStand() {
return hasStand;
}
public void setHasBlackJack(boolean hasBlackJack) {
this.hasBlackJack=hasBlackJack;
}
public boolean getHasBlackJack() {
return hasBlackJack;
}
public void setHasLost(boolean hasLost) {
this.hasLost=hasLost;
}
public boolean getHasLost() {
return hasLost;
}
public boolean getNatural21() {
return hasNatural21;
}
public void setNatural21(boolean hasNatural21) {
this.hasNatural21=hasNatural21;
}
}