-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
135 lines (116 loc) · 4.56 KB
/
Player.java
File metadata and controls
135 lines (116 loc) · 4.56 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
/*
* Project 3 for CS 1181.
* This program defines the Player class and all of its data fields and methods using the NetBeans IDE.
* This program will represent and keep track of a player in the card game and store the
* methods necessary to monitor and manage the player's hand.
*/
package project3_meyer;
import java.util.ArrayDeque;
import java.util.ArrayList;
/**
* @author Elijah Meyer
* CS1181L-C07
* Dr. Cheatham
* Project 3
*/
public abstract class Player
{
private ArrayList<Card> hand = new ArrayList<>();
public Player()
{
}
/**
* Returns the hand of the calling Player.
* Precondition: none
* Postcondition: the hand of the Player will have been returned
* @return the Player's hand
*/
public ArrayList<Card> getHand()
{
return hand;
}
public abstract int promptForDrawInput(ArrayDeque<Card> discard);
public abstract int promptForDiscardInput(ArrayDeque<Card> discard);
/**
* Draws from the submitted discard pile or deck, depending on the value of the index submitted
* as a parameter.
* Precondition: index is 1 or 2; deck and discard have been initialized
* Postcondition: a Card will have been added to the calling Player's hand from either the
* discard pile or deck
* @param index - a number indicating whether the Player wants to draw from the discard pile or deck
* @param deck - the DrawPile the Players have been drawing from
* @param discard - the ArrayDeque where the Players have been storing their discarded Cards
*/
public void draw(int index, DrawPile deck, ArrayDeque<Card> discard)
{
// Draw Card from where index indicates: if 1, from discard pile, if 2, from deck
if (index == 1)
{
// If caller is ComputerPlayer, output Card it picked up
if (this instanceof ComputerPlayer)
{
System.out.println("I will pick up the " + discard.peek());
}
this.getHand().add(discard.pop());
}
// If caller drew from deck, inform them about what Card they drew
else if (index == 2)
{
// Retrieve Card from deck
Card c = deck.getDeck().poll();
// If caller is ComputerPlayer, inform user of ComputerPlayer's move
if (this instanceof ComputerPlayer)
{
System.out.println("I will draw a new card.");
}
// If caller is HumanPlayer, output what Card they drew
else if (this instanceof HumanPlayer)
{
System.out.println("You drew the " + c);
}
this.getHand().add(c);
}
}
/**
* Removes the Card specified by the index from the calling Player's hand and pushes it to
* the submitted discard pile.
* Precondition: the discard pile has been initialized; the first counting number is 1
* Postcondition: a Card will have been removed from the Player's hand and pushed
* onto the discard pile
* @param index - the index in the Player's hand of the card to be removed (before adjusting
* for ArrayLists starting at 0)
* @param discard - the ArrayDeque to push the discarded Card onto
*/
public void discard(int index, ArrayDeque<Card> discard)
{
// Adjust index count to reflect counting beginning with 0, remove Card at that index
// from Player's hand, and push it onto discard pile
discard.push(hand.remove(index - 1));
}
/**
* Checks whether or not the Player has won (whether every Card in their hand has the
* same value)
* Precondition: hand size is 4
* Postcondition: whether or not the Player has won the game will have been returned
* @return whether or not the Player is victorious
*/
public boolean isVictorious()
{
// Initialize counter variable
int matchCount = 0;
// Compare each Card with next Card in hand
for (int i = 0; i < hand.size() - 1; i++)
{
if (hand.get(i).equals(hand.get(i + 1)))
{
matchCount++;
}
}
// Return whether or not all Cards in hand had the same value
if (matchCount == 3)
{
return true;
}
return false;
}
}