Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
LI Qi
Bowling
Commits
0965f53c
Commit
0965f53c
authored
Feb 06, 2016
by
Qi LI
Browse files
enter points and find next player
parent
7823a4d7
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/game/Set.java
View file @
0965f53c
package
game
;
import
java.util.HashMap
;
import
java.util.List
;
public
class
Set
{
private
final
List
<
Player
>
players
;
private
HashMap
<
Player
,
ScoreTable
>
tables
;
private
int
currentPlayerIndex
=
0
;
private
int
frameCount
=
0
;
public
Set
(
List
<
Player
>
players
)
{
this
.
players
=
players
;
this
.
tables
=
new
HashMap
<
Player
,
ScoreTable
>();
for
(
Player
player
:
players
)
{
tables
.
put
(
player
,
new
ScoreTable
());
}
}
public
int
getPlayerNumber
()
{
return
players
.
size
();
public
void
enterPoint
(
int
point
)
{
ScoreTable
tableToEnter
=
tables
.
get
(
players
.
get
(
currentPlayerIndex
));
tableToEnter
.
enterPoint
(
point
);
updatePlayerIndex
();
}
public
List
getPlayers
()
{
return
players
;
public
Player
getNextPlayer
()
{
return
players
.
get
(
currentPlayerIndex
);
}
public
boolean
isOver
()
{
return
currentPlayerIndex
==
-
1
;
}
private
void
updatePlayerIndex
()
{
if
(
frameCount
<
10
)
{
Frame
currentFrame
=
tables
.
get
(
players
.
get
(
currentPlayerIndex
)).
getCurrentFrame
();
if
(
currentFrame
.
isFinished
())
currentPlayerIndex
++;
if
(
currentPlayerIndex
==
players
.
size
())
{
currentPlayerIndex
=
0
;
frameCount
++;
}
}
if
(
frameCount
==
10
)
{
int
nextPlayerIndex
=
-
1
;
for
(
int
i
=
0
;
i
<
players
.
size
()
&&
nextPlayerIndex
==
-
1
;
++
i
)
{
if
(!
tables
.
get
(
players
.
get
(
i
)).
isComplete
())
nextPlayerIndex
=
i
;
}
currentPlayerIndex
=
nextPlayerIndex
;
}
}
}
src/test/TestSet.java
0 → 100644
View file @
0965f53c
package
test
;
import
java.util.Arrays
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
game.Player
;
import
game.Set
;
public
class
TestSet
{
public
TestSet
()
{}
@Test
public
void
testCase0
()
{
Player
alice
=
new
Player
(
"Alice"
);
Player
bob
=
new
Player
(
"Bob"
);
Set
set
=
new
Set
(
Arrays
.
asList
(
new
Player
[]
{
alice
,
bob
}));
int
[]
points
=
{
10
,
7
,
2
,
8
,
0
,
10
,
7
};
Player
[]
expected
=
{
alice
,
bob
,
bob
,
alice
,
alice
,
bob
,
alice
};
Player
[]
actuals
=
new
Player
[
expected
.
length
];
for
(
int
i
=
0
;
i
<
points
.
length
;
++
i
)
{
actuals
[
i
]
=
set
.
getNextPlayer
();
set
.
enterPoint
(
points
[
i
]);
}
Assert
.
assertArrayEquals
(
expected
,
actuals
);
}
@Test
public
void
testCase1
()
{
Player
alice
=
new
Player
(
"Alice"
);
Player
bob
=
new
Player
(
"Bob"
);
Player
charlie
=
new
Player
(
"Charlie"
);
Set
set
=
new
Set
(
Arrays
.
asList
(
new
Player
[]
{
alice
,
bob
,
charlie
}));
for
(
int
i
=
0
;
i
<
9
;
++
i
)
{
for
(
int
j
=
0
;
j
<
3
;
++
j
)
set
.
enterPoint
(
10
);
}
int
[]
points
=
{
4
,
5
,
7
,
3
,
10
,
9
,
10
,
8
};
Player
[]
expectedPlayer
=
{
alice
,
alice
,
bob
,
bob
,
charlie
,
bob
,
charlie
,
charlie
};
Player
[]
actualPlayer
=
new
Player
[
points
.
length
];
boolean
[]
expectedOver
=
new
boolean
[
points
.
length
];
boolean
[]
actualOver
=
new
boolean
[
points
.
length
];
for
(
int
i
=
0
;
i
<
points
.
length
;
++
i
)
{
actualPlayer
[
i
]
=
set
.
getNextPlayer
();
set
.
enterPoint
(
points
[
i
]);
expectedOver
[
i
]
=
false
;
actualOver
[
i
]
=
set
.
isOver
();
}
expectedOver
[
points
.
length
-
1
]
=
true
;
Assert
.
assertArrayEquals
(
expectedPlayer
,
actualPlayer
);
Assert
.
assertArrayEquals
(
expectedOver
,
actualOver
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment