-
Notifications
You must be signed in to change notification settings - Fork 0
/
BowlingGame.java
187 lines (136 loc) · 4.19 KB
/
BowlingGame.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import java.util.*;
/**
@author Sahil Paudel
*/
class Player {
private int id;
private String name;
private List<Shot> points;
public Player(int id, String name) {
this.id = id;
this.name = name;
points = new ArrayList<Shot>(Collections.nCopies(10, null));
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public void saveShot(Shot s, int i) throws Exception {
if (i > 10) {
throw new Exception("Invalid Shot");
}
points.set(i, s);
}
public List<Shot> getPoints() {
return points;
}
public void setName(String name) {
this.name = name;
}
public void setId(int id) {
this.id = id;
}
}
class Game {
private int laneNumber;
private List<Player> players;
private Set<Integer> busyLane;
public Game(int laneNumber, List<Player> players, Set<Integer> busyLane) throws Exception {
this.laneNumber = laneNumber;
this.players = players;
if (busyLane.contains(laneNumber)){
throw new Exception("Lane "+laneNumber+" already in use");
} else {
busyLane.add(laneNumber);
}
}
private int getScore() {
Random rand = new Random();
return rand.nextInt(11);
}
public void play(int game) throws Exception {
int n = players.size();
for (int i = 0; i < n; i++) {
Player p = players.get(i);
int first = getScore();
int second = getScore();
Shot s = new Shot(first, second);
int sum = first + second;
int oldSum = 0;
if (game > 0)
oldSum = p.getPoints().get(game - 1).getSum();
s.setSum(sum + oldSum);
p.saveShot(s, game);
}
}
public void getResult() {
int highest = 0;
String winner = "";
for (Player p: players) {
System.out.println("Name: " + p.getName());
System.out.println("Points: " + p.getPoints());
int lastPoint = p.getPoints().get(9).getSum();
if (lastPoint > highest) {
highest = lastPoint;
winner = p.getName();
}
}
System.out.println("\nLane Number: "+laneNumber+", Winner: " + winner + ", Points: " + highest);
}
}
class Shot {
private int first;
private int second;
private int sum;
public Shot(int first, int second) {
this.first = first;
this.second = second;
}
public int getFirst() {
return first;
}
private int getSecond() {
return second;
}
public int getSum(){
return sum;
}
public void setSum(int sum){
this.sum = sum;
}
public String toString() {
return "{"+first+","+second+"}";
}
}
public class BowlingGame {
public static void main(String args[]) throws Exception {
Set<Integer> busyLane = new HashSet<>();
Player p1 = new Player(1, "Sahil");
Player p2 = new Player(2, "Noble");
Player p3 = new Player(3, "Subrat");
Player p4 = new Player(4, "Puneet");
List<Player> players = new ArrayList();
players.add(p1);
players.add(p2);
players.add(p3);
players.add(p4);
Game game = new Game(1, players, busyLane);
for (int i = 0; i < 10; i++)
game.play(i);
game.getResult();
game = new Game(2, players, busyLane);
for (int i = 0; i < 10; i++)
game.play(i);
game.getResult();
game = new Game(3, players, busyLane);
for (int i = 0; i < 10; i++)
game.play(i);
game.getResult();
game = new Game(4, players, busyLane);
for (int i = 0; i < 10; i++)
game.play(i);
game.getResult();
}
}