Skip to content

Commit 34ca231

Browse files
authored
Merge branch 'master' into Husen_Branch
2 parents d0f111e + 51aa765 commit 34ca231

File tree

4 files changed

+384
-46
lines changed

4 files changed

+384
-46
lines changed

BowlingAlley/code/Lane.java

Lines changed: 142 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,23 @@
136136
import java.util.HashMap;
137137
import java.util.Date;
138138

139+
139140
public class Lane extends Thread implements PinsetterObserver {
140141
private Party party;
141142
private Pinsetter setter;
142143
private HashMap scores;
143144
private Vector subscribers;
144145

145146
private boolean gameIsHalted;
146-
147+
private scoreCalculate ScCalculate;
148+
private SimulateThrow smt;
149+
147150
private boolean partyAssigned;
148151
private boolean gameFinished;
149152
private Iterator bowlerIterator;
150153
private int ball;
151-
private int bowlIndex;
152-
private int frameNumber;
154+
public int bowlIndex;
155+
public int frameNumber;
153156
private boolean tenthFrameStrike;
154157

155158
private int[] curScores;
@@ -159,7 +162,7 @@ public class Lane extends Thread implements PinsetterObserver {
159162
private int[][] finalScores;
160163
private int gameNumber;
161164

162-
private Bowler currentThrower; // = the thrower who just took a throw
165+
public Bowler currentThrower; // = the thrower who just took a throw
163166

164167
/** Lane()
165168
*
@@ -172,10 +175,10 @@ public Lane() {
172175
setter = new Pinsetter();
173176
scores = new HashMap();
174177
subscribers = new Vector();
175-
178+
ScCalculate = new scoreCalculate();
176179
gameIsHalted = false;
177180
partyAssigned = false;
178-
181+
smt = new SimulateThrow();
179182
gameNumber = 0;
180183

181184
setter.subscribe( this );
@@ -207,12 +210,16 @@ public void run() {
207210
tenthFrameStrike = false;
208211
ball = 0;
209212
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
211218
ball++;
212219
}
213220

214221
if (frameNumber == 9){
215-
finalScores[bowlIndex][gameNumber] = cumulScores[bowlIndex][9];
222+
finalScores[bowlIndex][gameNumber] = ScCalculate.cumulScores[bowlIndex][9];
216223
try{
217224
Date date = new Date();
218225
String dateString = "" + date.getHours() + ":" + date.getMinutes() + " " + date.getMonth() + "/" + date.getDay() + "/" + (date.getYear() + 1900);
@@ -292,9 +299,12 @@ public void run() {
292299
* @param pe The pinsetter event that has been received.
293300
*/
294301
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");
296306
if (pe.pinsDownOnThisThrow() >= 0) { // this is a real throw
297-
markScore(currentThrower, frameNumber + 1, pe.getThrowNumber(), pe.pinsDownOnThisThrow());
307+
ScCalculate.markScore(this, ball, score);
298308

299309
// next logic handles the ?: what conditions dont allow them another throw?
300310
// handle the case of 10th frame first
@@ -346,7 +356,7 @@ private void resetBowlerIterator() {
346356
* resets the scoring mechanism, must be called before scoring starts
347357
*
348358
* @pre the party has been assigned
349-
* @post scoring system is initialized
359+
* @post scoring system is initializedassin
350360
*/
351361
private void resetScores() {
352362
Iterator bowlIt = (party.getMembers()).iterator();
@@ -380,11 +390,12 @@ public void assignParty( Party theParty ) {
380390
partyAssigned = true;
381391

382392
curScores = new int[party.getMembers().size()];
383-
cumulScores = new int[party.getMembers().size()][10];
384393
finalScores = new int[party.getMembers().size()][128]; //Hardcoding a max of 128 games, bite me.
385394
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];
388399
}
389400

390401
/** markScore()
@@ -419,6 +430,11 @@ private LaneEvent lanePublish( ) {
419430
LaneEvent laneEvent = new LaneEvent(party, bowlIndex, currentThrower, cumulScores, scores, frameNumber+1, curScores, ball, gameIsHalted);
420431
return laneEvent;
421432
}
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+
}
422438

423439
/** getScore()
424440
*
@@ -437,9 +453,6 @@ private int getScore( Bowler Cur, int frame) {
437453
int strikeballs = 0;
438454
int totalScore = 0;
439455
curScore = (int[]) scores.get(Cur);
440-
for (int i = 0; i != 10; i++){
441-
cumulScores[bowlIndex][i] = 0;
442-
}
443456
int current = 2*(frame - 1)+ball-1;
444457
//Iterate through each ball until the current one.
445458
for (int i = 0; i != current+2; i++){
@@ -533,8 +546,120 @@ private int getScore( Bowler Cur, int frame) {
533546
cumulScores[bowlIndex][9] += curScore[i];
534547
}
535548
}
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;
536560
}
561+
562+
cumulScores[bowlIndex][9] = totalScore;
537563
}
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+
// }
538663
return totalScore;
539664
}
540665

BowlingAlley/code/Pinsetter.java

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,27 @@ public class Pinsetter {
9898
* @pre none
9999
* @post all subscribers have recieved pinsetter event with updated state
100100
* */
101-
private void sendEvent(int jdpins) { // send events when our state is changd
101+
public void sendEvent(int jdpins) {
102+
// send events when our state is changd
103+
// throwNumber++;
104+
System.out.println("send vene tetet eee");
105+
102106
for (int i=0; i < subscribers.size(); i++) {
103107
((PinsetterObserver)subscribers.get(i)).receivePinsetterEvent(
104108
new PinsetterEvent(pins, foul, throwNumber, jdpins));
105109
}
106110
}
111+
112+
public void sendEvent(boolean[] pins_recv,int jdpins,boolean foul) {
113+
// send events when our state is changd
114+
throwNumber++;
115+
for (int i=0; i < subscribers.size(); i++) {
116+
((PinsetterObserver)subscribers.get(i)).receivePinsetterEvent(
117+
new PinsetterEvent(pins_recv, foul, throwNumber, jdpins));
118+
}
119+
}
120+
121+
107122

108123
/** Pinsetter()
109124
*
@@ -128,33 +143,7 @@ public Pinsetter() {
128143
* @pre none
129144
* @post pins may have been knocked down and the thrownumber has been incremented
130145
*/
131-
public void ballThrown() { // simulated event of ball hits sensor
132-
int count = 0;
133-
foul = false;
134-
double skill = rnd.nextDouble();
135-
for (int i=0; i <= 9; i++) {
136-
if (pins[i]) {
137-
double pinluck = rnd.nextDouble();
138-
if (pinluck <= .04){
139-
foul = true;
140-
}
141-
if ( ((skill + pinluck)/2.0 * 1.2) > .5 ){
142-
pins[i] = false;
143-
}
144-
if (!pins[i]) { // this pin just knocked down
145-
count++;
146-
}
147-
}
148-
}
149-
150-
try {
151-
Thread.sleep(500); // pinsetter is where delay will be in a real game
152-
} catch (Exception e) {}
153146

154-
sendEvent(count);
155-
156-
throwNumber++;
157-
}
158147

159148
/** reset()
160149
*
@@ -165,14 +154,15 @@ public void ballThrown() { // simulated event of ball hits sensor
165154
*/
166155
public void reset() {
167156
foul = false;
168-
throwNumber = 1;
157+
throwNumber = 0;
169158
resetPins();
170159

171160
try {
172161
Thread.sleep(1000);
173162
} catch (Exception e) {}
174163

175-
sendEvent(-1);
164+
165+
//sendEvent(-1);
176166
}
177167

178168
/** resetPins()

BowlingAlley/code/SimulateThrow.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.Random;
2+
3+
public class SimulateThrow {
4+
5+
boolean foul;
6+
public boolean[] pins;
7+
private Random rnd;
8+
public SimulateThrow()
9+
{
10+
foul = false;
11+
pins = new boolean[10];
12+
rnd = new Random();
13+
resetPins();
14+
}
15+
16+
public void resetPins() {
17+
for (int i=0; i <= 9; i++) {
18+
pins[i] = true;
19+
}
20+
}
21+
22+
public int ballThrown() {
23+
// simulated event of ball hits sensor
24+
try {
25+
Thread.sleep(500);
26+
} catch (InterruptedException e1) {
27+
// TODO Auto-generated catch block
28+
e1.printStackTrace();
29+
}
30+
int count = 0;
31+
foul = false;
32+
double skill = rnd.nextDouble();
33+
for (int i=0; i <= 9; i++) {
34+
if (pins[i]) {
35+
double pinluck = rnd.nextDouble();
36+
if (pinluck <= .04){
37+
foul = true;
38+
}
39+
if ( ((skill + pinluck)/2.0 * 1.2) > .5 ){
40+
pins[i] = false;
41+
}
42+
if (!pins[i]) { // this pin just knocked down
43+
count++;
44+
45+
}
46+
}
47+
}
48+
49+
try {
50+
Thread.sleep(500); // pinsetter is where delay will be in a real game
51+
} catch (Exception e) {}
52+
System.out.println("Count is "+count);
53+
return count;
54+
55+
}
56+
57+
}

0 commit comments

Comments
 (0)