-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
79 lines (74 loc) · 1.95 KB
/
Copy pathPlayer.java
File metadata and controls
79 lines (74 loc) · 1.95 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
/*This Player Class uses the Singleton design, only one instantiated
object is created and this object can be accessed globally using a particular
method. The Player Class of the guessing game can do the following
*scan user input
The scanned user input is then stored in the private attribute number
this attribute is retrieved everytime main compares it to the random number generated*/
import java.util.*;//this is for the Scanner class
public class Player
{
Scanner scan = new Scanner(System.in);
private int number;
private String name;
private static Player player;
/*The constructor is private to make sure instantiation outside the class
impossible, this makes sure that only one instance of this class is created.*/
private Player()
{
this.name = "Human Player";
}
/*this particular method created the singleton instance, it checks first if
is there already an instantiated object, otherwise it creates one, and then
returns the instance.*/
public static Player getPlayer()
{
if(player == null)
{
player = new Player();
}
return player;
}
/*This method sets the private attribute number through user input
it also makes sure that the input is valid(within the range of 1 to 5) otherwise
user must input a number again until input is valid.*/
public void setNumber()
{
boolean check = false;
while(!check)
{
System.out.print("Human Player: ");
this.number = scan.nextInt();
if(this.number>=1 && this.number<=5)
{
check = true;
}
else
{
System.out.println("Range: 1-5, Try Again.\n");
}
}
}
//self-explanatory
public int getNumber()
{
return this.number;
}
//self-explanatory
public String getName()
{
return this.name;
}
/*This method compares the randomnumber and userInput, and prints
the result like so.*/
public void checkGuess(int x)
{
if(this.number<x)
{
System.out.print("Guess is too low.\n");
}
else if(this.number>x)
{
System.out.print("Guess is too high.\n");
}
}
}