-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHumanPlayer.java
More file actions
150 lines (129 loc) · 4.94 KB
/
HumanPlayer.java
File metadata and controls
150 lines (129 loc) · 4.94 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
142
143
144
145
146
147
148
149
150
/*
* Project 3 for CS 1181.
* This program defines the HumanPlayer class and all of its data fields and methods using the NetBeans IDE.
* This program will implement the Player class's abstract methods to prompt the user for input
* and manipulate their hand, the deck, and the discard pile accordingly.
*/
package project3_meyer;
import java.util.ArrayDeque;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* @author Elijah Meyer
* CS1181L-C07
* Dr. Cheatham
* Project 3
*/
public class HumanPlayer extends Player
{
public HumanPlayer()
{
}
/**
* Prompts the user for input concerning whether to draw from the deck or the discard pile.
* Precondition: discard has been initialized
* Postcondition: an integer reflecting the user's decision about where to draw their next Card
* from will have been returned
* @param discard - the ArrayDeque where the Players have been storing their discarded Cards
* @return - the user's choice of where to draw their next Card
*/
@Override
public int promptForDrawInput(ArrayDeque<Card> discard)
{
// Initialize variables
Scanner input = new Scanner(System.in);
boolean notDone = true;
int choice = 0;
do
{
// Output user's Cards
System.out.println("Your cards are:");
for (int i = 0; i < this.getHand().size(); i++)
{
System.out.println("\t" + this.getHand().get(i));
}
// Force user to draw from deck if discard pile is empty
if (discard.isEmpty())
{
System.out.println("The discard pile is currently empty -- you must draw a card");
return 2;
}
// If not, show Card on top of discard pile and obtain input
else
{
System.out.println("The top card in the discard pile is the " + discard.peek());
}
System.out.print("Do you want to pick up the " + discard.peek() + " (1) or draw a card " +
"(2)? ");
try
{
choice = input.nextInt();
// Exit loop if input is 1 or 2
if (choice == 1 || choice == 2)
{
notDone = false;
}
// If not, tell user to enter 1 or 2
else
{
System.out.println("Please enter either 1 or 2.");
}
// Catch if user entered something other than integer
} catch (InputMismatchException ex)
{
System.out.println("Error: invalid input.");
input.next();
}
// Continue loop until input is satisfactory, then return input
} while (notDone);
return choice;
}
/**
* Prompts the user for input concerning which Card to discard.
* Precondition: discard has been initialized
* Postcondition: an integer reflecting which Card the user wants to discard
* will have been returned
* @param discard - the ArrayDeque where the Players have been storing their discarded Cards
* @return the user's choice of which card to discard
*/
@Override
public int promptForDiscardInput(ArrayDeque<Card> discard)
{
// Initialize variables
Scanner input = new Scanner(System.in);
boolean notDone = true;
int choice = 0;
// Output user's Cards
do
{
System.out.println("Now your cards are: ");
for (int i = 0; i < this.getHand().size(); i++)
{
System.out.println("\t" + (i + 1) + ". " + this.getHand().get(i));
}
// Obtain user input
System.out.print("Which one do you want to discard? ");
try
{
choice = input.nextInt();
// If input is an integer between one and five, exit loop
if (choice >= 1 && choice <= 5)
{
notDone = false;
}
// Inform user if input was unacceptable
else
{
System.out.println("Please enter an integer between 1 and 5.");
}
// Catch if user entered something other than integer
} catch (InputMismatchException ex)
{
System.out.println("Error: invalid input.");
input.next();
}
// Repeat until input is acceptable, then return input
} while (notDone);
return choice;
}
}