-
Notifications
You must be signed in to change notification settings - Fork 3
/
ScoreKeeperUtility.java
200 lines (153 loc) · 7.09 KB
/
ScoreKeeperUtility.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
188
189
190
191
192
193
194
195
196
197
198
199
200
import java.util.Scanner;
public class ScoreKeeperUtility {
//Declaring Class Variable
private Game game=new Game ();
//startGame method starts
public void startGame() {
System.out.println("\n\n WELCOME TO THE BOWLING GAME !!!");
Scanner in = new Scanner(System.in);
for(int i=0;i<10;i++) //for loop for 10 Frames
{
Frame f=new Frame(); //temporary frame object
int first_hit=0;//number of pins knocked down in the first roll
int second_hit=0;//number of pins knocked down in the second roll
int third_hit=0;//number if pins knocked down in the third roll
//getting the no of pins knocked in the First Roll from User/Controller
System.out.println("\n\n"+"Your "+(i+1)+ "(th) Frame has started !!");
System.out.println("Please enter the number of pins knocked down in First Roll of the "+(i+1)+"(th) Frame ");
first_hit=in.nextInt();
f.setRoll_1_pins_knocked_down(first_hit);
update(i,first_hit);
if(first_hit==10 && (i!=9))//condition for STRIKE
{
System.out.println("This is s STRIKE !! Your "+(i+1)+"(th) Frame ends.");
f.setFrame_Strike(true);
f.setTotal_score_of_this_frame(-99); // -99 stand for X(STRIKE)
}
else //If it is not a STRIKE
{
//getting the no of pins knocked in the Second Roll from User/Controller
System.out.println("Please enter the number of pins knocked down in Second Roll of the "+(i+1)+"(th) Frame ");
second_hit=in.nextInt();
f.setRoll_2_pins_knocked_down(second_hit);
update(i,second_hit);
}
if((first_hit<10)&& (second_hit<10) && ((first_hit+second_hit)==10))//Condition for SPARE
{
f.setFrame_Spare(true);
System.out.println("Your "+(i+1)+"(th) Frame is a SPARE !!");
f.setTotal_score_of_this_frame(-88); // -88 stand for / (SPARE)
}
//Considering cases for 10th Frame
if((first_hit==10) && (i==9))//condition for STRIKE in 10th Frame
{
System.out.println("You hit a STRIKE in the last frame.You get an extra roll.");
System.out.println("Please enter the number of pins knocked down in Third Roll of the 10(th) Frame ");
third_hit=in.nextInt();
f.setRoll_3_pins_knocked_down(third_hit);
//update(i,third_hit);
}
if((first_hit+second_hit==10) && (i==9))//condition for SPARE in 10th Frame
{
System.out.println("You hit a SPARE in the last frame.You get an extra roll.");
System.out.println("Please enter the number of pins knocked down in Third Roll of the 10(th) Frame ");
third_hit=in.nextInt();
f.setRoll_3_pins_knocked_down(third_hit);
//update(i,third_hit);
}
f.setAbsolute_score_of_frame((first_hit+second_hit));//setting ABSOLUTE score per frame=first hit + second hit
if((f.isFrame_Spare()==false) && (f.isFrame_Strike()==false))
{
f.setTotal_score_of_this_frame((first_hit+second_hit));//setting TOTAL score per frame=first hit + second hit
}
this.getGame().getFrames()[i]=f; //Finally setting the ith frame object in Game
if(i==9)//setting the score for frame 10
{
this.getGame().getFrames()[9].setTotal_score_of_this_frame(first_hit+second_hit+third_hit);
this.getGame().getFrames()[9].setAbsolute_score_of_frame(first_hit+second_hit+third_hit);
}
f=null; //Freeing the memory.
//printing starts
System.out.println("SCORES - ");
for(int j=0;j<=i;j++)
{
//Normal case (Neither STRIKE nor SPARE)
if((this.getGame().getFrames()[j].isFrame_Strike()==false)&& (this.getGame().getFrames()[j].isFrame_Spare()==false))
{
//System.out.println("The Individual score for "+(j+1)+" Frame is :"+this.getGame().getFrames()[j].getAbsolute_score_of_frame());
System.out.println("The Individual score for "+(j+1)+" Frame is :"+this.getGame().getFrames()[j].getTotal_score_of_this_frame());
}
//STRIKE not calculated yet
if((this.getGame().getFrames()[j].isFrame_Strike()==true)&&(this.getGame().getFrames()[j].getTotal_score_of_this_frame()==-99))
{
System.out.println("The Individual score for "+(j+1)+" Frame is : X");
}
//STRIKE Calculated
if((this.getGame().getFrames()[j].isFrame_Strike()==true)&&(this.getGame().getFrames()[j].getTotal_score_of_this_frame()!=-99))
{
System.out.println("The Individual score for "+(j+1)+" Frame is : "+this.getGame().getFrames()[j].getTotal_score_of_this_frame());
}
//SPARE not calculated yet
if((this.getGame().getFrames()[j].isFrame_Spare()==true)&&(this.getGame().getFrames()[j].getTotal_score_of_this_frame()==-88))
{
System.out.println("The Individual score for "+(j+1)+" Frame is : /");
}
//SPARE Calculated
if((this.getGame().getFrames()[j].isFrame_Spare()==true)&&(this.getGame().getFrames()[j].getTotal_score_of_this_frame()!=-88))
{
System.out.println("The Individual score for "+(j+1)+" Frame is : "+this.getGame().getFrames()[j].getTotal_score_of_this_frame());
}
System.out.println("and the Cumulative score for Frame "+(j+1)+" is :"+getCumulativeScore(j));
}
//printing ends
}//end of for
}
public int getCumulativeScore(int i)
{
int sum=0;
for(int j=0;j<=i;j++ )
{
if((this.getGame().getFrames()[j].getTotal_score_of_this_frame()==-99)||(this.getGame().getFrames()[j].getTotal_score_of_this_frame()==-88))
sum=sum+0;
else
sum=sum+this.getGame().getFrames()[j].getTotal_score_of_this_frame();
}
return sum;
}
public void update(int j,int pins_hit)//Update the score of previous frames as per the score of current frame in case of SPARE/STRIKE
{
for(int i=j-1;i>=0;i--)
{
if((this.getGame().getFrames()[i].isFrame_Strike()==true) && (this.getGame().getFrames()[i].getTotal_score_of_this_frame()==-99)&&
(this.getGame().getFrames()[i].isIs_first_updated()==false)&&(this.getGame().getFrames()[i].isIs_second_updated()==false))
{
this.getGame().getFrames()[i].setIs_first_updated(true);
this.getGame().getFrames()[i].setAbsolute_score_of_frame(10+pins_hit);
continue;
}
if((this.getGame().getFrames()[i].isFrame_Strike()==true) && (this.getGame().getFrames()[i].getTotal_score_of_this_frame()==-99)
&& (this.getGame().getFrames()[i].isIs_first_updated()==true)&&(this.getGame().getFrames()[i].isIs_second_updated()==false) )
{
this.getGame().getFrames()[i].setIs_second_updated(true);
this.getGame().getFrames()[i].setAbsolute_score_of_frame((this.getGame().getFrames()[i].getAbsolute_score_of_frame())+(pins_hit));
this.getGame().getFrames()[i].setTotal_score_of_this_frame(this.getGame().getFrames()[i].getAbsolute_score_of_frame());
continue;
}
if((this.getGame().getFrames()[i].isFrame_Spare()==true) && (this.getGame().getFrames()[i].getTotal_score_of_this_frame()==-88)&&
(this.getGame().getFrames()[i].isIs_first_updated()==false))
{
this.getGame().getFrames()[i].setIs_first_updated(true);
this.getGame().getFrames()[i].setAbsolute_score_of_frame(10+pins_hit);
this.getGame().getFrames()[i].setTotal_score_of_this_frame(this.getGame().getFrames()[i].getAbsolute_score_of_frame());
continue;
}
}//end of for
}
//Getters and Setters
public Game getGame() {
return game;
}
public void setGame(Game game) {
this.game = game;
}
}