136
136
import java .util .HashMap ;
137
137
import java .util .Date ;
138
138
139
+
139
140
public class Lane extends Thread implements PinsetterObserver {
140
141
private Party party ;
141
142
private Pinsetter setter ;
142
143
private HashMap scores ;
143
144
private Vector subscribers ;
144
145
145
146
private boolean gameIsHalted ;
146
-
147
+ private scoreCalculate ScCalculate ;
148
+ private SimulateThrow smt ;
149
+
147
150
private boolean partyAssigned ;
148
151
private boolean gameFinished ;
149
152
private Iterator bowlerIterator ;
150
153
private int ball ;
151
- private int bowlIndex ;
152
- private int frameNumber ;
154
+ public int bowlIndex ;
155
+ public int frameNumber ;
153
156
private boolean tenthFrameStrike ;
154
157
155
158
private int [] curScores ;
@@ -159,7 +162,7 @@ public class Lane extends Thread implements PinsetterObserver {
159
162
private int [][] finalScores ;
160
163
private int gameNumber ;
161
164
162
- private Bowler currentThrower ; // = the thrower who just took a throw
165
+ public Bowler currentThrower ; // = the thrower who just took a throw
163
166
164
167
/** Lane()
165
168
*
@@ -172,10 +175,10 @@ public Lane() {
172
175
setter = new Pinsetter ();
173
176
scores = new HashMap ();
174
177
subscribers = new Vector ();
175
-
178
+ ScCalculate = new scoreCalculate ();
176
179
gameIsHalted = false ;
177
180
partyAssigned = false ;
178
-
181
+ smt = new SimulateThrow ();
179
182
gameNumber = 0 ;
180
183
181
184
setter .subscribe ( this );
@@ -207,12 +210,16 @@ public void run() {
207
210
tenthFrameStrike = false ;
208
211
ball = 0 ;
209
212
while (canThrowAgain ) {
210
- setter .ballThrown (); // simulate the thrower's ball hiting
213
+ int count = smt .ballThrown ();
214
+ setter .sendEvent (smt .pins ,count , smt .foul );
215
+ smt .resetPins ();
216
+
217
+ // simulate the thrower's ball hiting
211
218
ball ++;
212
219
}
213
220
214
221
if (frameNumber == 9 ){
215
- finalScores [bowlIndex ][gameNumber ] = cumulScores [bowlIndex ][9 ];
222
+ finalScores [bowlIndex ][gameNumber ] = ScCalculate . cumulScores [bowlIndex ][9 ];
216
223
try {
217
224
Date date = new Date ();
218
225
String dateString = "" + date .getHours () + ":" + date .getMinutes () + " " + date .getMonth () + "/" + date .getDay () + "/" + (date .getYear () + 1900 );
@@ -292,9 +299,12 @@ public void run() {
292
299
* @param pe The pinsetter event that has been received.
293
300
*/
294
301
public void receivePinsetterEvent (PinsetterEvent pe ) {
295
-
302
+
303
+ System .out .print ("throw no " +pe .getThrowNumber ());
304
+ int score = pe .pinsDownOnThisThrow ();
305
+ System .out .print ("score is " +score +"\n " );
296
306
if (pe .pinsDownOnThisThrow () >= 0 ) { // this is a real throw
297
- markScore (currentThrower , frameNumber + 1 , pe . getThrowNumber (), pe . pinsDownOnThisThrow () );
307
+ ScCalculate . markScore (this , ball , score );
298
308
299
309
// next logic handles the ?: what conditions dont allow them another throw?
300
310
// handle the case of 10th frame first
@@ -346,7 +356,7 @@ private void resetBowlerIterator() {
346
356
* resets the scoring mechanism, must be called before scoring starts
347
357
*
348
358
* @pre the party has been assigned
349
- * @post scoring system is initialized
359
+ * @post scoring system is initializedassin
350
360
*/
351
361
private void resetScores () {
352
362
Iterator bowlIt = (party .getMembers ()).iterator ();
@@ -380,11 +390,12 @@ public void assignParty( Party theParty ) {
380
390
partyAssigned = true ;
381
391
382
392
curScores = new int [party .getMembers ().size ()];
383
- cumulScores = new int [party .getMembers ().size ()][10 ];
384
393
finalScores = new int [party .getMembers ().size ()][128 ]; //Hardcoding a max of 128 games, bite me.
385
394
gameNumber = 0 ;
386
-
387
- resetScores ();
395
+ ScCalculate .partyAssigned = true ;
396
+ ScCalculate .party = theParty ;
397
+ ScCalculate .resetScores (theParty );
398
+ ScCalculate .cumulScores = new int [party .getMembers ().size ()][10 ];
388
399
}
389
400
390
401
/** markScore()
@@ -419,6 +430,11 @@ private LaneEvent lanePublish( ) {
419
430
LaneEvent laneEvent = new LaneEvent (party , bowlIndex , currentThrower , cumulScores , scores , frameNumber +1 , curScores , ball , gameIsHalted );
420
431
return laneEvent ;
421
432
}
433
+
434
+ public LaneEvent lanePublish2 (int [][] cumulres ,int recvball ) {
435
+ LaneEvent laneEvent = new LaneEvent (party , bowlIndex , currentThrower , cumulres , scores , frameNumber +1 , curScores , recvball , gameIsHalted );
436
+ return laneEvent ;
437
+ }
422
438
423
439
/** getScore()
424
440
*
@@ -437,9 +453,6 @@ private int getScore( Bowler Cur, int frame) {
437
453
int strikeballs = 0 ;
438
454
int totalScore = 0 ;
439
455
curScore = (int []) scores .get (Cur );
440
- for (int i = 0 ; i != 10 ; i ++){
441
- cumulScores [bowlIndex ][i ] = 0 ;
442
- }
443
456
int current = 2 *(frame - 1 )+ball -1 ;
444
457
//Iterate through each ball until the current one.
445
458
for (int i = 0 ; i != current +2 ; i ++){
@@ -533,8 +546,120 @@ private int getScore( Bowler Cur, int frame) {
533
546
cumulScores [bowlIndex ][9 ] += curScore [i ];
534
547
}
535
548
}
549
+
550
+ for (int i =0 ;i <=current ;i ++)
551
+ {
552
+ if (curScore [i ]!=-1 )
553
+ totalScore = totalScore +curScore [i ];
554
+ }
555
+
556
+ if (frame ==9 )
557
+ {
558
+ for (int i = 0 ; i != 10 ; i ++){
559
+ cumulScores [bowlIndex ][i ] = 80 ;
536
560
}
561
+
562
+ cumulScores [bowlIndex ][9 ] = totalScore ;
537
563
}
564
+
565
+ // for (int i = 0; i != 10; i++){
566
+ // cumulScores[bowlIndex][i] = 0;
567
+ // }
568
+ // int current = 2*(frame - 1)+ball-1;
569
+ // //Iterate through each ball until the current one.
570
+ // for (int i = 0; i != current+2; i++){
571
+ // //Spare:
572
+ // if( i%2 == 1 && curScore[i - 1] + curScore[i] == 10 && i < current - 1 && i < 19){
573
+ // //This ball was a the second of a spare.
574
+ // //Also, we're not on the current ball.
575
+ // //Add the next ball to the ith one in cumul.
576
+ // cumulScores[bowlIndex][(i/2)] += curScore[i+1] + curScore[i];
577
+ // if (i > 1) {
578
+ // //cumulScores[bowlIndex][i/2] += cumulScores[bowlIndex][i/2 -1];
579
+ // }
580
+ // } else if( i < current && i%2 == 0 && curScore[i] == 10 && i < 18){
581
+ // strikeballs = 0;
582
+ // //This ball is the first ball, and was a strike.
583
+ // //If we can get 2 balls after it, good add them to cumul.
584
+ // if (curScore[i+2] != -1) {
585
+ // strikeballs = 1;
586
+ // if(curScore[i+3] != -1) {
587
+ // //Still got em.
588
+ // strikeballs = 2;
589
+ // } else if(curScore[i+4] != -1) {
590
+ // //Ok, got it.
591
+ // strikeballs = 2;
592
+ // }
593
+ // }
594
+ // if (strikeballs == 2){
595
+ // //Add up the strike.
596
+ // //Add the next two balls to the current cumulscore.
597
+ // cumulScores[bowlIndex][i/2] += 10;
598
+ // if(curScore[i+1] != -1) {
599
+ // cumulScores[bowlIndex][i/2] += curScore[i+1] + cumulScores[bowlIndex][(i/2)-1];
600
+ // if (curScore[i+2] != -1){
601
+ // if( curScore[i+2] != -2){
602
+ // cumulScores[bowlIndex][(i/2)] += curScore[i+2];
603
+ // }
604
+ // } else {
605
+ // if( curScore[i+3] != -2){
606
+ // cumulScores[bowlIndex][(i/2)] += curScore[i+3];
607
+ // }
608
+ // }
609
+ // } else {
610
+ // if ( i/2 > 0 ){
611
+ // cumulScores[bowlIndex][i/2] += curScore[i+2] + cumulScores[bowlIndex][(i/2)-1];
612
+ // } else {
613
+ // cumulScores[bowlIndex][i/2] += curScore[i+2];
614
+ // }
615
+ // if (curScore[i+3] != -1){
616
+ // if( curScore[i+3] != -2){
617
+ // cumulScores[bowlIndex][(i/2)] += curScore[i+3];
618
+ // }
619
+ // } else {
620
+ // cumulScores[bowlIndex][(i/2)] += curScore[i+4];
621
+ // }
622
+ // }
623
+ // } else {
624
+ // break;
625
+ // }
626
+ // }else {
627
+ // //We're dealing with a normal throw, add it and be on our way.
628
+ // if( i%2 == 0 && i < 18){
629
+ // if ( i/2 == 0 ) {
630
+ // //First frame, first ball. Set his cumul score to the first ball
631
+ // if(curScore[i] != -2){
632
+ // cumulScores[bowlIndex][i/2] += curScore[i];
633
+ // }
634
+ // } else if (i/2 != 9){
635
+ // //add his last frame's cumul to this ball, make it this frame's cumul.
636
+ // if(curScore[i] != -2){
637
+ // cumulScores[bowlIndex][i/2] += cumulScores[bowlIndex][i/2 - 1] + curScore[i];
638
+ // } else {
639
+ // cumulScores[bowlIndex][i/2] += cumulScores[bowlIndex][i/2 - 1];
640
+ // }
641
+ // }
642
+ // } else if (i < 18){
643
+ // if(curScore[i] != -1 && i > 2){
644
+ // if(curScore[i] != -2){
645
+ // cumulScores[bowlIndex][i/2] += curScore[i];
646
+ // }
647
+ // }
648
+ // }
649
+ // if (i/2 == 9){
650
+ // if (i == 18){
651
+ // cumulScores[bowlIndex][9] += cumulScores[bowlIndex][8];
652
+ // }
653
+ // if(curScore[i] != -2){
654
+ // cumulScores[bowlIndex][9] += curScore[i];
655
+ // }
656
+ // } else if (i/2 == 10) {
657
+ // if(curScore[i] != -2){
658
+ // cumulScores[bowlIndex][9] += curScore[i];
659
+ // }
660
+ // }
661
+ // }
662
+ // }
538
663
return totalScore ;
539
664
}
540
665
0 commit comments