-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputerPlayer.java
More file actions
62 lines (56 loc) · 1.54 KB
/
Copy pathComputerPlayer.java
File metadata and controls
62 lines (56 loc) · 1.54 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
/*The ComputerPlayer class is basically just the same as the Player class
with minimal difference(as in). I couldve just make it inherit from the player
class para ioverride nalang nako ang setNumber method since ag implementation
rjud ana maoy different, pero nagpaSingletonSingleton man lage ko. type nuon kog
balikbalik. Inheriting from class that follows the Singleton design is not good
practice and by inheriting so, it breaks the rule of Signleton design, if I want to
make this class inherit from player, then might as well remove the Singlton design.
Daming hanash beh*/
import java.util.*;
public class ComputerPlayer
{
private static ComputerPlayer computerPlayer;
private int number;
private String name;
//refer to Player class for documentation
private ComputerPlayer()
{
this.name = "Computer Player";
}
//refer to Player class for documentation
public static ComputerPlayer getComputerPlayer()
{
if(computerPlayer == null)
{
computerPlayer = new ComputerPlayer();
}
return computerPlayer;
}
/*The only difference here is ComputerPlayergenerates its own randomNumber
instead of user input.*/
public void setNumber()
{
Random rand = new Random();
this.number = rand.nextInt((5-1)+1) + 1;
}
public int getNumber()
{
return this.number;
}
public String getName()
{
return this.name;
}
//refer to Player class for documentation
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");
}
}
}