-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameState.java
More file actions
141 lines (118 loc) · 4.83 KB
/
GameState.java
File metadata and controls
141 lines (118 loc) · 4.83 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
import java.util.List;
import java.util.Random;
public class GameState {
private static final int CARDS_PER_COLOR = 7;
private static final int TOTAL_COLORS = 3;
private static final int TOTAL_CARDS = CARDS_PER_COLOR * TOTAL_COLORS;
private static final int INITIAL_HAND_SIZE = 4;
private boolean[] redsInDeck;
private boolean[] yellowsInDeck;
private boolean[] violetsInDeck;
private String canvasColor;
private Player playerA;
private Player playerB;
private int currentPlayerIndex;
private Random randGen;
private final String[] playerNames = {"A", "B"};
public GameState() {
this.randGen = new Random();
initializeGame();
}
private void initializeGame() {
initializeDecks();
initializePlayers();
setInitialPlayer();
}
private boolean[] createFullDeck() {
boolean[] deck = new boolean[CARDS_PER_COLOR];
for (int i = 0; i < CARDS_PER_COLOR; i++) {
deck[i] = true;
}
return deck;
}
private void initializeDecks() {
this.redsInDeck = createFullDeck();
this.yellowsInDeck = createFullDeck();
this.violetsInDeck = createFullDeck();
}
private void setInitialPlayer() {
this.currentPlayerIndex = GameLogic.playerWinning(canvasColor, playerA.getPalette(), playerB.getPalette()) ? 1 : 0;
}
private void initializePlayers() {
this.canvasColor = "Red";
this.playerA = new Player();
this.playerB = new Player();
setupPlayer(playerA);
setupPlayer(playerB);
}
private void setupPlayer(Player player) {
dealCardToPalette(player);
dealCardsToHand(player, INITIAL_HAND_SIZE);
}
private void dealCardToPalette(Player player) {
dealCard(redsInDeck, yellowsInDeck, violetsInDeck, player.getPalette());
}
private void dealCardsToHand(Player player, int count) {
for (int i = 0; i < count; i++) {
dealCard(redsInDeck, yellowsInDeck, violetsInDeck, player.getHand());
}
}
public void dealCard(boolean[] redsInDeck, boolean[] yellowsInDeck, boolean[] violetsInDeck, List<Card> targetList) {
Card newCard = generateRandomCard();
targetList.add(newCard);
}
private Card generateRandomCard() {
while (true) {
int cardIndex = randGen.nextInt(TOTAL_CARDS);
CardLocation location = getCardLocation(cardIndex);
if (isCardAvailable(location)) {
markCardAsUsed(location);
return createCard(location);
}
}
}
private CardLocation getCardLocation(int cardIndex) {
int colorIndex = cardIndex / CARDS_PER_COLOR;
int numberIndex = cardIndex % CARDS_PER_COLOR;
return new CardLocation(colorIndex, numberIndex);
}
private boolean isCardAvailable(CardLocation location) {
boolean[] deck = getDeckByColorIndex(location.colorIndex);
return deck[location.numberIndex];
}
private void markCardAsUsed(CardLocation location) {
boolean[] deck = getDeckByColorIndex(location.colorIndex);
deck[location.numberIndex] = false;
}
private Card createCard(CardLocation location) {
String color = getColorByIndex(location.colorIndex);
int number = location.numberIndex + 1;
return new Card(color, number);
}
private boolean[] getDeckByColorIndex(int colorIndex) {
switch (colorIndex) {
case 0: return redsInDeck;
case 1: return yellowsInDeck;
case 2: return violetsInDeck;
default: throw new IllegalArgumentException("Invalid color index: " + colorIndex);
}
}
private String getColorByIndex(int colorIndex) {
switch (colorIndex) {
case 0: return "Red";
case 1: return "Yellow";
case 2: return "Violet";
default: throw new IllegalArgumentException("Invalid color index: " + colorIndex);
}
}
public String getCanvasColor() { return canvasColor; }
public Player getPlayerA() { return playerA; }
public Player getPlayerB() { return playerB; }
public int getCurrentPlayerIndex() { return currentPlayerIndex; }
public Player getCurrentPlayer() { return (currentPlayerIndex == 0) ? playerA : playerB; }
public Player getOpponentPlayer() { return (currentPlayerIndex == 0) ? playerB : playerA; }
public String getCurrentPlayerName() { return playerNames[currentPlayerIndex]; }
public String getOpponentPlayerName() { return playerNames[(currentPlayerIndex + 1) % 2]; }
public void setCanvasColor(String canvasColor) { this.canvasColor = canvasColor; }
public void switchPlayer() { this.currentPlayerIndex = (this.currentPlayerIndex + 1) % 2; }
}