Repository navigation
Expand file tree
/
Copy pathCPU.java
More file actions
133 lines (120 loc) · 5.53 KB
/
Copy pathCPU.java
File metadata and controls
133 lines (120 loc) · 5.53 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
package model.player;
import java.util.ArrayList;
import java.util.Collections;
import engine.board.BoardManager;
import exception.GameException;
import exception.CannotDiscardException;
import model.Colour;
import model.card.Card;
public class CPU extends Player {
private final BoardManager boardManager;
public CPU(String name, Colour colour, BoardManager boardManager) {
super(name, colour);
this.boardManager = boardManager;
}
@Override
public void play() throws GameException {
// Retrieve a list of actionable marbles from the board manager.
ArrayList<Marble> actionableMarbles = boardManager.getActionableMarbles();
// Retrieve the current hand of cards and shuffle them to ensure randomness.
ArrayList<Card> cards = new ArrayList<>();
cards.addAll(this.getHand());
int initialHandSize = cards.size();
Collections.shuffle(cards);
boolean hasMarbleOnTrack = !actionableMarbles.isEmpty();
boolean hasFieldCard = false;
for (Card card : cards) {
String name = card.getName().toLowerCase();
if (name.equals("king") || name.equals("ace")) {
hasFieldCard = true;
break;
}
}
// If no marbles on track and no King/Ace, discard a card
if (!hasMarbleOnTrack && !hasFieldCard) {
// Discard a random card from hand
if (!cards.isEmpty()) {
int idx = (int) (Math.random() * cards.size());
Card toDiscard = cards.get(idx);
this.getHand().remove(toDiscard);
// Optionally, add to firepit if accessible
}
return;
}
// Iterate through each card in the shuffled hand.
for (Card card : cards) {
// Select the card to be played.
this.selectCard(card);
// Prepare a list to keep track of valid marble counts for the action.
ArrayList<Integer> counts = new ArrayList<>();
for(int i = 0; i < 3; i++) { // Check for 0 or 1 or 2 marbles to act upon.
if(actionableMarbles.size() >= i) {
ArrayList<Marble> testMarbles = new ArrayList<>();
for(int j = 0; j < i; j++) {
testMarbles.add(actionableMarbles.get(j));
}
// Validate the size of the marble group against the card's rules.
if(card.validateMarbleSize(testMarbles)) {
counts.add(i);
}
}
}
// Shuffle the counts to randomize the selection process.
Collections.shuffle(counts);
for(int i = 0; i < counts.size(); i++) {
if(counts.get(i) == 0) {
try {
// Attempt to act with no marbles if the count is 0.
getSelectedCard().act(new ArrayList<>());
return; // Return after successful action.
}
catch(Exception e) {
// Ignore exceptions and continue trying other possibilities.
}
}
else if(counts.get(i) == 1) {
// Attempt to act with one marble.
ArrayList<Marble> toSend = new ArrayList<>();
Collections.shuffle(actionableMarbles); // Shuffle marbles for random selection.
for(Marble marble : actionableMarbles) {
toSend.add(marble);
if(card.validateMarbleColours(toSend)) {
try {
getSelectedCard().act(toSend);
return; // Return after successful action.
}
catch(Exception e) {
// Ignore exceptions and continue.
}
}
toSend.clear();
}
}
else {
// Attempt to act with two marbles.
ArrayList<Marble> toSend = new ArrayList<>();
Collections.shuffle(actionableMarbles);
for(int j = 0; j < actionableMarbles.size(); j++) {
for(int k = j+1; k < actionableMarbles.size(); k++) {
toSend.add(actionableMarbles.get(j));
toSend.add(actionableMarbles.get(k));
if(card.validateMarbleColours(toSend)) {
try {
getSelectedCard().act(toSend);
return; // Return after successful action.
}
catch(Exception e) {
// Ignore exceptions and continue.
}
}
toSend.clear();
}
}
}
}
}
// If no cards were played, select the first card by default.
if (cards.size() == initialHandSize)
this.selectCard(this.getHand().get(0));
}
}