This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayerList.pde
144 lines (126 loc) · 4.57 KB
/
playerList.pde
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
//===============================================
//
// Learning About Blackjack - Baseline Code
// Professor Marty Altman
// SCAD-Atlanta
// ITGM 220
//
// This file contains the definition for the PlayerList class, which represents
// the collection of players. This class contains and manages a list that contains
// all the players, including the dealer. The dealer is deliberately added last,
// because there are times when the game needs to know when the dealer is done.
//
//===============================================
class PlayerList {
ArrayList<Player> players; // all players, including the dealer
Dealer dealer; // remember the dealer
int current; // the index of the active player
PlayerList( Dealer d ) {
players = new ArrayList<Player>();
nPlayerSetup(5);
dealer = d;
players.add(dealer); // add the dealer last
}
Player firstPlayer( boolean act ) {
return( getPlayer( 0, act ) );
}
Player nextPlayer( boolean act ) {
return( getPlayer( (current+1) % players.size(), act ) );
}
Player getPlayer( int index, boolean act ) {
if ( game.player != null ) {
game.player.active = false;
}
current = index;
Player p = players.get(current);
p.active = act;
return(p);
}
boolean everybodyOut() {
for ( Player p : players ) {
if ( (p != dealer) && !p.hand.busted ) { // if any regular player is still in
return(false); // then return false
}
}
return(true); // only return true if all the players are out
}
void display() {
for ( Player p : players ) {
p.display();
}
}
void displayResults() {
for ( Player p : players ) {
p.displayResults();
}
}
void reset() {
for ( Player p : players ) {
p.reset();
}
}
void determineResults() {
for ( Player p : players ) {
if ( p != dealer ) {
if ( p.hand.value > 21 ) {
p.result = Result.PLAYER_LOSES;
} else if ( dealer.hand.value > 21 ) {
p.result = Result.PLAYER_WINS;
} else if ( p.hand.blackjack && !dealer.hand.blackjack ) {
p.result = Result.PLAYER_WINS;
} else if ( dealer.hand.blackjack && !p.hand.blackjack ) {
p.result = Result.PLAYER_LOSES;
} else if ( p.hand.value > dealer.hand.value ) {
p.result = Result.PLAYER_WINS;
} else if ( dealer.hand.value > p.hand.value ) {
p.result = Result.PLAYER_LOSES;
} else {
p.result = Result.PUSH;
}
}
}
}
void onePlayerSetup() {
players.add( new Player(0,-185,0) );
}
void nPlayerSetup(int numberOfPlayers){
if(numberOfPlayers == 1){
onePlayerSetup();
}
else{
//layout the table for n players
float arcAngle = 73; //maybe later add a function that starts arc angle at 0 for a one player setup, and eases up to a clamp for an n player setup?
float arcYOffset = -100; //offset the arc from the dealer
float radius = getRadius(numberOfPlayers, arcAngle);
float arcStart = -1*arcAngle/2;
float radiusYOffset = getRadiusYOffset(arcStart, radius);
for(int i=0; i<numberOfPlayers; i++){
float rotation = getRotation(arcStart, arcAngle, i, numberOfPlayers);
players.add(new Player(getXPosition(rotation, radius), getYPosition(rotation, radius, radiusYOffset, arcYOffset), radians(rotation)));
}
}
}
float getRadius(int numberOfPlayers, float arcAngle){
//get radius, use numberOfPlayers arg bc it's in scope and only needs to be run once
//measure card dims, and figure out how far apart they need to be spaced
float cardWidth = loadImage("card_back.png").width;
float cardHeight = loadImage("card_back.png").height;
float arcLength = numberOfPlayers*cardWidth;
float radius = arcLength/radians(arcAngle);
radius = radius+cardHeight/2;
return radius;
}
float getRotation(float arcStart, float arcAngle, int playerIndex, int numberOfPlayers){
return arcStart+arcAngle*(numberOfPlayers-playerIndex-1)/(numberOfPlayers-1);
}
float getRadiusYOffset(float arcStart, float radius){
return cos(radians(arcStart))*radius;
}
float getYPosition(float rotation, float radius, float radiusYOffset, float arcYOffset){
//use yoffset to compensate for radius
return -1*cos(radians(rotation))*radius+radiusYOffset+arcYOffset;
}
float getXPosition(float rotation, float radius){
return sin(radians(rotation))*radius;
}
}