-
Notifications
You must be signed in to change notification settings - Fork 1
/
WhackAMole.java
138 lines (119 loc) · 2.88 KB
/
WhackAMole.java
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
package whackamole;
public class WhackAMole
{
// ------------------ Instance variables ----------------------------- //
private int score;
private int molesLeft;
private int attemptsLeft;
private int gridDimension;
boolean isConsecutive;
private char[][] moleGrid;
int multiplier;
// --------------------------- Constructor ------------------------ //
public WhackAMole (int gridDimension)
{
this.gridDimension = gridDimension;
attemptsLeft = (int)((gridDimension * gridDimension)/2);
moleGrid = new char[gridDimension][gridDimension];
for (int i = 0; i < gridDimension; i++)
for (int j=0; j < gridDimension; j++)
moleGrid[i][j] = '*';
molesLeft = 0;
score = 0;
isConsecutive = false;
multiplier = 1;
}
// ------------------------------ Methods --------------------------- //
// to place the moles in the correct coordinates
public boolean place (int x, int y)
{
if ((x > gridDimension) || (y > gridDimension) || (moleGrid[x][y] == 'M'))
return false;
else
{
moleGrid[x][y] = 'M';
molesLeft++;
return true;
}
}
// to whack the mole at the coordinate entered by the user
public boolean whack (int x, int y)
{
if ((x >= gridDimension) || (y >= gridDimension) || (x < 0) || (y < 0))
{
System.out.println("Invalid Co-ordinates, Please enter x and y between 0-" + gridDimension);
return false;
}
else if ((moleGrid[x][y] == 'W'))
{
System.out.println("Already WHACKED, please enter a different Co-ordinate");
return false;
}
else
{
if (moleGrid[x][y] == 'M')
{
System.out.println("You WHACKED a mole");
moleGrid[x][y] = 'W';
molesLeft --;
if(isConsecutive)
{
multiplier++;
score = score + (10 * multiplier);
}
else
score += 10;
isConsecutive = true;
}
else
{
System.out.println("You MISSED the mole");
moleGrid[x][y] = 'W';
attemptsLeft--;
score--;
isConsecutive = false;
}
return true;
}
}
//to print grid, hiding the locations of the moles
public void printGridToUser ()
{
for (int i = 0; i < moleGrid[0].length; i++)
{
for(int j=0; j < moleGrid[0].length; j++)
{
if (moleGrid[i][j] == 'M')
System.out.print("* ");
else
System.out.print(moleGrid[i][j] + " ");
}
System.out.println();
}
System.out.println("Your SCORE is "+score);
}
// to print grid, showing the location of the moles
public void printGrid()
{
for (int i = 0; i < moleGrid[0].length; i++)
{
for(int j=0; j < moleGrid[0].length; j++)
System.out.print(moleGrid[i][j] + " ");
System.out.println();
}
System.out.println("Your FINAL SCORE is "+score);
}
// --------------------------- Getters ------------------------------ //
public int getMolesLeft()
{
return molesLeft;
}
public int getAttemptsLeft()
{
return attemptsLeft;
}
public int getGridDimension()
{
return gridDimension;
}
}