-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputerPlayer.java
More file actions
70 lines (58 loc) · 1.92 KB
/
ComputerPlayer.java
File metadata and controls
70 lines (58 loc) · 1.92 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
/*
* ComputerPlayer.java
*
* A class for a program that plays a card game called CardMatch.
* Serves as a blueprint class for a ComputerPlayer object, which represents
* a single CardMatch computer player
*
*/
import java.util.*;
public class ComputerPlayer extends Player {
/*
* constructor - takes the the name of the player and calls the super's constructor
*/
public ComputerPlayer(String name) {
super(name);
}
/*
* displayHand - an accessor method that overrides the inherited version and prints
* the number of cards in the ComputerPlayer's hand
*/
public void displayHand() {
System.out.println(this.getName() + "'s hand:");
if (this.getNumCards() == 1) {
System.out.println(" 1 card");
} else {
System.out.println(" " + this.getNumCards() + " cards");
}
}
/*
* getPlay - an accessor method that overrides the inherited version
*/
public int getPlay(Scanner console, Card top) {
int[] possiblePlays = new int[CardMatch.MAX_CARDS];
int numPossiblePlays = 0;
for (int i = 0; i < this.getNumCards(); i++) {
Card card = this.getCard(i);
if (card.getValue() == top.getValue() || card.getColor() == top.getColor()) {
possiblePlays[numPossiblePlays] = i;
numPossiblePlays++;
}
}
if (numPossiblePlays == 0) {
return -1;
} else {
int best = 0;
int bestIndex = 0;
for (int i = 0; i < numPossiblePlays; i++) {
int index = possiblePlays[i];
Card card = this.getCard(index);
if (card.getValue() > best) {
best = card.getValue();
bestIndex = index;
}
}
return bestIndex;
}
}
}