Intro -java project
Proj5 Troll Game-starter.gpj
!1513;s J SAU0JGRASP_MAIN_BOUNDS=%<CONTROL_SHELL_BOUNDS>\012PATH+=%<JGRASP_PATHS>%;\012PATH=+%;%<JGRASP_C_PATHS>\012CYGWIN=nodosfilewarning\012GCC_COLORS=\012-Run\012ADD_EXE_PATH==Y\012 SCU0JGRASP_MAIN_BOUNDS=%<CONTROL_SHELL_BOUNDS>\012PATH+=%<JGRASP_PATHS>%;\012PATH=+%;%<JGRASP_C_PATHS>\012CYGWIN=nodosfilewarning\012GCC_COLORS=\012-Run\012ADD_EXE_PATH==Y\012 S+U0JGRASP_MAIN_BOUNDS=%<CONTROL_SHELL_BOUNDS>\012PATH+=%<JGRASP_PATHS>%;\012PATH=+%;%<JGRASP_C_PATHS>\012CYGWIN=nodosfilewarning\012GCC_COLORS=\012-Run\012ADD_EXE_PATH==Y\012 SJU0JGRASP_MAIN_BOUNDS=%<CONTROL_SHELL_BOUNDS>\012CLASSPATH=+%;%<EXTENSION_CLASSPATHS>\012CLASSPATH+=%<JGRASP_CLASSPATHS>%;\012File\012CLASSPATH+=.%;\012CLASSPATH+=%<SRC_CLASSPATHS>%;\012ProjectOrFile\012PATH+=%<JAVA_BIN_DIR>%;\012PATH+=%<JGRASP_PATHS>%;\012Debug +Debug_Applet\012CLASSPATH=+%;%<INJECTION_CLASSPATHS>\012Compile\012NOT_FOUND_MESSAGE==Make sure you have the full JDK, not just the JRE, installed.\\nThe JDK is available from http://www.oracle.com/technetwork/java/index.html.\012Run_Applet +Debug_Applet\012ADD_APPLETVIEWER_CLASSPATH==Y\012Document\012CLASSPATH=+%;%<TOOL_CLASSPATHS>\012CLASSPATH+=%<CLASSES_DIR>%;\012 SXU0 SOU0JGRASP_MAIN_BOUNDS=%<CONTROL_SHELL_BOUNDS>\012PATH+=%<JGRASP_PATHS>%;\012PATH=+%;%<JGRASP_C_PATHS>\012CYGWIN=nodosfilewarning\012GCC_COLORS=\012-Run\012ADD_EXE_PATH==Y\012 SyU0JGRASP_MAIN_BOUNDS=%<CONTROL_SHELL_BOUNDS>\012PATH+=%<PYTHON_PATH>%;\012PATH+=%<JGRASP_PATHS>%;\012CYGWIN=nodosfilewarning\012GCC_COLORS= SPU0 s#i0 s#k0 s#j0 s#l0 !106;u a1 b68 c255 f%\011Proj5 Troll Game-starter.jar g%\011Proj5 Troll Game-starter.zip hManifest-Version\0111.0!85;g %\011EmptyPiece.java %\011TrollGame.java %\011GamePiece.java %\011TrollGameMain.java !85;f %\011EmptyPiece.java %\011TrollGame.java %\011GamePiece.java %\011TrollGameMain.java !24;l %\011TrollGameTest.java
EmptyPiece.java
EmptyPiece.java
/*
* Represents an "empty" game piece. It's type is "Empty" and show is a space character.
*/
public
class
EmptyPiece
extends
GamePiece
{
public
EmptyPiece
()
{
super
();
}
public
String
getType
(){
return
"Empty"
;
}
public
String
show
(){
return
" "
;
}
}
GamePiece.java
GamePiece.java
/*
* Abstract superclass for the Troll Game. Maintains an amount of life points.
* Subclasses must implement the abstract methods getType and show.
*/
public
abstract
class
GamePiece
{
private
int
lifePoints
;
public
GamePiece
()
{
lifePoints
=
0
;
}
public
GamePiece
(
int
initPoints
)
{
lifePoints
=
initPoints
;
}
/*
* Adds the argument "amt" to the lifePoints.
*/
public
void
updateLifePoints
(
double
amt
){
if
(
lifePoints
>
0
)
lifePoints
+=
amt
;
}
/* Get the current life points */
public
int
getLifePoints
(){
return
lifePoints
;
}
/* Return true if lifeLevel is greater than zero, false otherwise. */
public
boolean
isAlive
(){
return
(
lifePoints
>
0
);
}
/* Abstract methods: subclasses must provide an implementation. */
public
abstract
String
getType
();
public
abstract
String
show
();
}
TrollGame.java
TrollGame.java
import
java
.
util
.
Random
;
/* The game is played in a series of rounds. The game board is a 2D array with
* a set number of rows and columns. The array contains Objects of type GamePiece.
* There are four types of GamePieces: Player- the user, Troll- the villain, Treasure- the
* goal, and EmptyPiece- represents cells that are not one of the previous types.
* The player's goal is to reach the treasure without encountering the Troll.
* The Troll's goal is to intercept the player before the player reaches the treasure.
* The player starts in the upper left-hand corner- position 0,0.
* The treasure is located at the lower right-hand corner- position 7, 9.
* The Troll starts at a random position in the board that is not
* the player's starting position or the treasure's position.
* The player makes a move one step in one of four directions: up, down, left, right.
* If the player is on a border and the requested move would take the player off the
* board, the move is ignored- i.e. the player does not move.
* The Troll knows the player's position and after the player moves, the Troll will
* move towards the player. If the Troll's next move reaches the player, the Troll
* wins. If the player reaches the treasure and the Troll also reaches the treasure
* in the same round, the player wins.
* The player starts with 160 life points. Each move the player makes costs 10 life points.
* This deduction occurs for all player moves- even if the player attempts to move off the board.
* If a player's life level drops to 0 or less, the player is not alive an cannot move.
*/
public
class
TrollGame
{
//constants for this version of the game
private
static
final
int
INIT_PLAYER_POINTS
=
160
;
private
static
final
int
PLAYER_POINTS_DEC
=
-
10
;
private
static
final
int
TREASURE_POINTS
=
500
;
private
static
final
int
ROWS
=
8
;
private
static
final
int
COLS
=
10
;
// random number generator
private
Random
rand
;
// the game board, a 2D array of GamePieces
private
GamePiece
[][]
gameBoard
;
// variables to keep track of the locations of player, troll
private
int
curPlayerRow
,
curPlayerCol
;
private
int
curTrollRow
,
curTrollCol
;
// the player's status
private
boolean
playerWins
;
private
boolean
playerLoses
;
/* Constructor that uses an unseeded instance of the random number generator.
* Calls initBoard.
*/
public
TrollGame
()
{
//TODO- implement this method.
}
/* Constructor that uses a seeded instance of the random number generator.
* Calls initBoard.
*/
public
TrollGame
(
int
seed
){
//TODO- implement this method.
}
/*
* Manages a player move. This is a multi-step process that involves:
* 1- if the player is alive:
* - determine the direction of the user's input.
* - check that the user move is valid. If not, player does not change position,
* if valid, adjust player position accordingly.
* - adjust player life level- regardless of validity of move.
* 2- calculate troll's move given the player's new position.
* 3- check if troll has same position as player.if so, player loses. Adjust the GamePieces
* so that the board is displayed properly, i.e. the player is gone and is replaced by the troll.
* 4- check if the player reached the treasure. If so, the player wins. Adjust the GamePieces
* so that the board is displayed properly, i.e. the player now appears in the treasure position.
* Also, update the troll's last position.
* 5- If neither 3 nor 4 above apply, then update the player and trollpositions.
* Adjust the GamePieces so that the board is displayed properly, i.e. the player and troll appear
* in their new positions.
*/
public
void
movePlayer
(
String
direction
)
{
//TODO- implement this method.
}
/* Returns true if the player wins, false otherwise. */
public
boolean
playerWins
(){
//TODO- implement this method.
return
true
;
}
/* Returns true if the player loses, false otherwise. */
public
boolean
playerLoses
(){
//TODO- implement this method.
return
true
;
}
/* Returns the number of treasure points. */
public
int
getTreasurePoints
(){
//TODO- implement this method.
return
-
1000
;
}
/* Resets the game variables and game board (call initBoard).
* Does NOT change the random number generator instance.
*/
public
void
resetGame
(){
//TODO- implement this method.
}
/* Returns a String version of the game. */
public
String
getGameStr
()
{
StringBuilder
outStr
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
ROWS
;
i
++
)
{
for
(
int
j
=
0
;
j
<
COLS
;
j
++
)
outStr
.
append
(
"|"
).
append
(
gameBoard
[
i
][
j
].
show
());
outStr
.
append
(
"|"
).
append
(
System
.
getProperty
(
"line.separator"
));
}
return
outStr
.
toString
();
}
/** private methods below **/
/* Creates the game board array with rows and cols. The player starts in the upper left-hand corner.
* The treasure is at the lower right-hand corner. The rest of the cells are empty pieces.
* The player starts with INIT_PLAYER_POINTS life points. The treasure is initialized to
* TREASURE_POINTS. The Troll position is determined at random by calling the getRandTrollRow
* and getRandTrollCol methods. This method returns the initialized array.
*/
private
GamePiece
[][]
initBoard
(
int
rows
,
int
cols
,
Random
rand
)
{
//TODO- implement this method.
return
null
;
}
/* Returns true if the player is alive, false otherwise.*/
private
boolean
playerAlive
(
int
curPlayerRow
,
int
curPlayerCol
){
//TODO- implement this method.
return
true
;
}
/* Adjusts the player's life level by the amount PLAYER_POINTS_DEC. */
private
void
adjustPlayerLifeLevel
(
int
curPlayerRow
,
int
curPlayerCol
){
//TODO- implement this method.
}
/* Returns true if the player row and column passed in equals
the treasure row and column. */
private
boolean
playerFoundTreasure
(
int
playerRow
,
int
playerCol
){
//TODO- implement this method.
return
true
;
}
/* Returns a random number in [1,numRows-1] */
private
int
getRandTrollRow
(
Random
rand
,
int
numRows
){
//TODO- implement this method.
return
1000
;
}
/* Returns a random number in [1,numCols-1] */
private
int
getRandTrollCol
(
Random
rand
,
int
numCols
){
//TODO- implement this method.
return
1000
;
}
/*
* This method calculates the direction the troll will move to get as close to the player as possible.
* The player and current troll positions are passed in. The method chooses a move in one direction
* that will bring it closer to the player. The method returns an int array [row, col], where row is the
* index of the verical position and col is the index of the horizontal position in the game board.
* The strategy of calculating the new troll position is to minimize the distance between the troll and player.
* This can be done by using the Manhattan, or city block, distance between the two pieces.
* Determine the horizontal distance and then the vertical distance. The troll will want to move to
* decrease the greater of the two distances. Note that the direction is also important. The sign of
* the difference can indicate the direction to move in. Asume the player is never out of bounds.
* Note: the player should loose if they move onto the troll's position. The troll should detect this
* situation and not make a move.
*/
private
int
[]
calcNewTrollCoordinates
(
int
playerRow
,
int
playerCol
,
int
trollRow
,
int
trollCol
){
//TODO- implement this method.
return
null
;
}
// The following three methods may be helpful when adjusting the GamePieces in the movePlayer method. */
/* Overwrite the GamePiece at the coordinates passed in with an empty GamePiece. */
private
void
overwritePositionWithEmpty
(
int
row
,
int
col
){
//TODO- implement this method.
}
/* Overwrite the GamePiece at the new coordinates with the GamePiece at the
old coordinates. Place a new EmptyPiece at the old coordinates. */
private
void
overwritePosition
(
int
oldRow
,
int
oldCol
,
int
newRow
,
int
newCol
){
//TODO- implement this method.
}
/* Swap the position of the GamePiece at the current position with the
* GamePiece at the new position. */
private
void
swapPosition
(
int
curRow
,
int
curCol
,
int
newRow
,
int
newCol
){
//TODO- implement this method.
}
}
TrollGameMain.java
TrollGameMain.java
import
java
.
util
.
Scanner
;
/*
* This class manages the interaction between the user (player) and the TrollGame object.
* It also keeps the player's toal games played, number of games won, and point total.
*/
public
class
TrollGameMain
{
public
static
void
main
(
String
[]
args
)
{
Scanner
scan
=
new
Scanner
(
System
.
in
);
String
inputStr
=
" "
;
boolean
keepPlaying
=
true
;
boolean
gameOver
=
false
;
int
numGamesPlayed
=
0
;
int
playerWinCount
=
0
;
int
playerTotalScore
=
0
;
// troll wins in this configuration
// TrollGame game = new TrollGame(1239);
// player can win
TrollGame
game
=
new
TrollGame
(
123
);
// another seed for testing
// TrollGame game = new TrollGame(123892);
// random version- to be used when delivering the game.
// TrollGame game = new TrollGame();
System
.
out
.
println
(
"Welcome to the Troll Game."
);
System
.
out
.
println
(
game
.
getGameStr
());
while
(
keepPlaying
){
if
(
gameOver
)
{
System
.
out
.
println
(
"Enter Y to play again, X to quit."
);
inputStr
=
scan
.
nextLine
();
if
(
inputStr
.
equalsIgnoreCase
(
"X"
))
break
;
else
if
(
inputStr
.
equalsIgnoreCase
(
"Y"
)){
game
.
resetGame
();
gameOver
=
false
;
System
.
out
.
println
(
game
.
getGameStr
());
}
}
else
if
(
!
gameOver
)
{
System
.
out
.
println
(
"Enter your move: u, d, r, l."
);
inputStr
=
scan
.
nextLine
();
game
.
movePlayer
(
inputStr
);
System
.
out
.
println
(
game
.
getGameStr
());
if
(
game
.
playerLoses
()){
System
.
out
.
println
(
"Player loses!"
);
numGamesPlayed
++
;
gameOver
=
true
;
showGameSummary
(
numGamesPlayed
,
playerWinCount
,
playerTotalScore
);
}
else
if
(
game
.
playerWins
()){
System
.
out
.
println
(
"Player wins!"
);
playerWinCount
++
;
numGamesPlayed
++
;
playerTotalScore
+=
game
.
getTreasurePoints
();
gameOver
=
true
;
showGameSummary
(
numGamesPlayed
,
playerWinCount
,
playerTotalScore
);
}
else
{
System
.
out
.
println
(
"continue..."
);
}
}
}
// end keep playing loop
System
.
out
.
println
(
"Thanks for playing!"
);
}
/* display the current user stats */
public
static
void
showGameSummary
(
int
numGamesPlayed
,
int
playerWinCount
,
int
playerTotalScore
){
System
.
out
.
println
(
"Total games played: "
+
numGamesPlayed
+
", player wins: "
+
playerWinCount
+
" player points: "
+
playerTotalScore
);
}
}
TrollGameTest.java
TrollGameTest.java
import
org
.
junit
.
Assert
;
import
static
org
.
junit
.
Assert
.
*
;
import
org
.
junit
.
Before
;
import
org
.
junit
.
Test
;
import
java
.
lang
.
reflect
.
*
;
import
java
.
util
.
Random
;
@
SuppressWarnings
(
"unchecked"
)
public
class
TrollGameTest
{
private
TrollGame
seededGame
;
private
TrollGame
seededGame2
;
private
TrollGame
seededGame3
;
private
TrollGame
randGame
;
private
static
final
int
INIT_PLAYER_POINTS
=
160
;
private
static
final
int
PLAYER_POINTS_DEC
=
-
10
;
private
static
final
int
TREASURE_POINTS
=
500
;
private
static
final
int
ROWS
=
8
;
private
static
final
int
COLS
=
10
;
/** Fixture initialization (common initialization
* for all tests). **/
@
Before
public
void
setUp
()
{
seededGame
=
new
TrollGame
(
1239
);
//troll is at 6,2.
seededGame2
=
new
TrollGame
(
123
);
//player can win with troll row moves.
seededGame3
=
new
TrollGame
(
991123344
);
//player can win with troll col moves.
randGame
=
new
TrollGame
();
}
/** Test that the instance variables have been initialized to
the correct values in both constructors. **/
@
Test
public
void
initCurPlayerRowValTest1
()
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
int
actualLen
=
0
;
Field
field
=
seededGame
.
getClass
().
getDeclaredField
(
"curPlayerRow"
);
if
(
Modifier
.
isPrivate
(
field
.
getModifiers
()))
field
.
setAccessible
(
true
);
Object
targetObj
=
field
.
get
(
seededGame
);
int
curPlayerRow
=
(
int
)
targetObj
;
assertEquals
(
"Test 1: curPlayerRow is initialized to 0."
,
0
,
curPlayerRow
);
}
@
Test
public
void
initCurPlayerRowValTest2
()
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
int
actualLen
=
0
;
Field
field
=
randGame
.
getClass
().
getDeclaredField
(
"curPlayerRow"
);
if
(
Modifier
.
isPrivate
(
field
.
getModifiers
()))
field
.
setAccessible
(
true
);
Object
targetObj
=
field
.
get
(
randGame
);
int
curPlayerRow
=
(
int
)
targetObj
;
assertEquals
(
"Test 2: curPlayerRow is initialized to 0- unseeded constructor."
,
0
,
curPlayerRow
);
}
@
Test
public
void
initCurPlayerColValTest1
()
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
int
actualLen
=
0
;
Field
field
=
seededGame
.
getClass
().
getDeclaredField
(
"curPlayerCol"
);
if
(
Modifier
.
isPrivate
(
field
.
getModifiers
()))
field
.
setAccessible
(
true
);
Object
targetObj
=
field
.
get
(
seededGame
);
int
curPlayerRow
=
(
int
)
targetObj
;
assertEquals
(
"Test 3: curPlayerCol is initialized to 0."
,
0
,
curPlayerRow
);
}
@
Test
public
void
initCurPlayerColValTest2
()
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
int
actualLen
=
0
;
Field
field
=
randGame
.
getClass
().
getDeclaredField
(
"curPlayerCol"
);
if
(
Modifier
.
isPrivate
(
field
.
getModifiers
()))
field
.
setAccessible
(
true
);
Object
targetObj
=
field
.
get
(
randGame
);
int
curPlayerRow
=
(
int
)
targetObj
;
assertEquals
(
"Test 4: curPlayerCol is initialized to 0- unseeded constructor."
,
0
,
curPlayerRow
);
}
@
Test
public
void
initPlayerWinsValTest1
()
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
int
actualLen
=
0
;
Field
field
=
seededGame
.
getClass
().
getDeclaredField
(
"playerWins"
);
if
(
Modifier
.
isPrivate
(
field
.
getModifiers
()))
field
.
setAccessible
(
true
);
Object
targetObj
=
field
.
get
(
seededGame
);
boolean
playerWins
=
(
boolean
)
targetObj
;
assertEquals
(
"Test 5: playerWins is initialized to false."
,
false
,
playerWins
);
}
@
Test
public
void
initPlayerWinsValTest2
()
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
int
actualLen
=
0
;
Field
field
=
randGame
.
getClass
().
getDeclaredField
(
"playerWins"
);
if
(
Modifier
.
isPrivate
(
field
.
getModifiers
()))
field
.
setAccessible
(
true
);
Object
targetObj
=
field
.
get
(
randGame
);
boolean
playerWins
=
(
boolean
)
targetObj
;
assertEquals
(
"Test 6: playerWins is initialized to false- unseeded constructor."
,
false
,
playerWins
);
}
@
Test
public
void
initPlayerLoosesValTest1
()
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
int
actualLen
=
0
;
Field
field
=
seededGame
.
getClass
().
getDeclaredField
(
"playerLoses"
);
if
(
Modifier
.
isPrivate
(
field
.
getModifiers
()))
field
.
setAccessible
(
true
);
Object
targetObj
=
field
.
get
(
seededGame
);
boolean
playerLoses
=
(
boolean
)
targetObj
;
assertEquals
(
"Test 7: playerLoses is initialized to false."
,
false
,
playerLoses
);
}
@
Test
public
void
initPlayerLoosesValTest2
()
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
int
actualLen
=
0
;
Field
field
=
randGame
.
getClass
().
getDeclaredField
(
"playerLoses"
);
if
(
Modifier
.
isPrivate
(
field
.
getModifiers
()))
field
.
setAccessible
(
true
);
Object
targetObj
=
field
.
get
(
randGame
);
boolean
playerLoses
=
(
boolean
)
targetObj
;
assertEquals
(
"Test 8: playerLoses is initialized to false- unseeded constructor."
,
false
,
playerLoses
);
}
@
Test
public
void
initRandomTest1
()
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
int
actualLen
=
0
;
Field
field
=
seededGame
.
getClass
().
getDeclaredField
(
"rand"
);
if
(
Modifier
.
isPrivate
(
field
.
getModifiers
()))
field
.
setAccessible
(
true
);
Object
targetObj
=
field
.
get
(
seededGame
);
Random
randObj
=
(
Random
)
targetObj
;
assertNotEquals
(
"Test 9: rand should be initialized to an instance of Random."
,
null
,
randObj
);
}
@
Test
public
void
initRandomTest2
()
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
int
actualLen
=
0
;
Field
field
=
randGame
.
getClass
().
getDeclaredField
(
"rand"
);
if
(
Modifier
.
isPrivate
(
field
.
getModifiers
()))
field
.
setAccessible
(
true
);
Object
targetObj
=
field
.
get
(
randGame
);
Random
randObj
=
(
Random
)
targetObj
;
assertNotEquals
(
"Test 10: rand should be initialized to an instance of Random- unseeded constructor."
,
null
,
randObj
);
}
@
Test
public
void
initGameBoardRowsTest1
()
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
int
actualLen
=
0
;
Field
field
=
seededGame
.
getClass
().
getDeclaredField
(
"gameBoard"
);
if
(
Modifier
.
isPrivate
(
field
.
getModifiers
()))
field
.
setAccessible
(
true
);
Object
targetObj
=
field
.
get
(
seededGame
);
GamePiece
[][]
board
=
(
GamePiece
[][])
targetObj
;
int
actualRows
=
board
.
length
;
assertEquals
(
"Test 11: The gameBoard should be initialized to "
+
ROWS
+
"rows."
,
ROWS
,
actualRows
);
}
@
Test
public
void
initGameBoardRowsTest2
()
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
int
actualLen
=
0
;
Field
field
=
randGame
.
getClass
().
getDeclaredField
(
"gameBoard"
);
if
(
Modifier
.
isPrivate
(
field
.
getModifiers
()))
field
.
setAccessible
(
true
);
Object
targetObj
=
field
.
get
(
randGame
);
GamePiece
[][]
board
=
(
GamePiece
[][])
targetObj
;
int
actualRows
=
board
.
length
;
assertEquals
(
"Test 12: The gameBoard should be initialized to "
+
ROWS
+
"rows- unseeded constructor."
,
ROWS
,
actualRows
);
}
@
Test
public
void
initGameBoardColsTest1
()
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
int
actualLen
=
0
;
Field
field
=
seededGame
.
getClass
().
getDeclaredField
(
"gameBoard"
);
if
(
Modifier
.
isPrivate
(
field
.
getModifiers
()))
field
.
setAccessible
(
true
);
Object
targetObj
=
field
.
get
(
seededGame
);
GamePiece
[][]
board
=
(
GamePiece
[][])
targetObj
;
int
actualCols
=
board
[
0
].
length
;
assertEquals
(
"Test 13: The gameBoard should be initialized to "
+
COLS
+
"columns."
,
COLS
,
actualCols
);
}
@
Test
public
void
initGameBoardColsTest2
()
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
int
actualLen
=
0
;
Field
field
=
randGame
.
getClass
().
getDeclaredField
(
"gameBoard"
);
if
(
Modifier
.
isPrivate
(
field
.
getModifiers
()))
field
.
setAccessible
(
true
);
Object
targetObj
=
field
.
get
(
randGame
);
GamePiece
[][]
board
=
(
GamePiece
[][])
targetObj
;
int
actualCols
=
board
[
0
].
length
;
assertEquals
(
"Test 14: The gameBoard should be initialized to "
+
COLS
+
"columns- unseeded constructor."
,
COLS
,
actualCols
);
}
@
Test
public
void
initBoardTestRows1
()
throws
Exception
{
Class
classToCall
=
Class
.
forName
(
"TrollGame"
);
Method
methodToExecute
=
classToCall
.
getDeclaredMethod
(
"initBoard"
,
new
Class
[]{
Integer
.
TYPE
,
Integer
.
TYPE
,
Random
.
class
});
methodToExecute
.
setAccessible
(
true
);
Object
gameBoardObj
=
methodToExecute
.
invoke
(
seededGame
,
new
Object
[]{
8
,
10
,
new
Random
(
12398
)});
GamePiece
[][]
gameBoard
=
(
GamePiece
[][])
gameBoardObj
;
int
result
=
gameBoard
.
length
;
assertEquals
(
"Test 15: initBoard test- correct rows."
,
ROWS
,
result
);
}
@
Test
public
void
initBoardTestCols1
()
throws
Exception
{
Class
classToCall
=
Class
.
forName
(
"TrollGame"
);
Method
methodToExecute
=
classToCall
.
getDeclaredMethod
(
"initBoard"
,
new
Class
[]{
Integer
.
TYPE
,
Integer
.
TYPE
,
Random
.
class
});
methodToExecute
.
setAccessible
(
true
);
Object
gameBoardObj
=
methodToExecute
.
invoke
(
seededGame
,
new
Object
[]{
ROWS
,
COLS
,
new
Random
(
12398
)});
GamePiece
[][]
gameBoard
=
(
GamePiece
[][])
gameBoardObj
;
int
result
=
gameBoard
[
0
].
length
;
assertEquals
(
"Test 16: initBoard test- correct cols."
,
COLS
,
result
);
}
@
Test
public
void
initBoardTestEmptyPiece1
()
throws
Exception
{
Class
classToCall
=
Class
.
forName
(
"TrollGame"
);
Method
methodToExecute
=
classToCall
.
getDeclaredMethod
(
"initBoard"
,
new
Class
[]{
Integer
.
TYPE
,
Integer
.
TYPE
,
Random
.
class
});
methodToExecute
.
setAccessible
(
true
);
Object
gameBoardObj
=
methodToExecute
.
invoke
(
seededGame
,
new
Object
[]{
ROWS
,
COLS
,
new
Random
(
12398
)});
GamePiece
[][]
gameBoard
=
(
GamePiece
[][])
gameBoardObj
;
GamePiece
emptyPiece
=
gameBoard
[
3
][
6
];
// a "random" location that isn't troll, player, treasure.
boolean
result
=
(
emptyPiece
instanceof
EmptyPiece
);
assertEquals
(
"Test 17: initBoard test- instance of EmptyPiece present."
,
true
,
result
);
}
@
Test
public
void
initBoardTestInitPlayerPos1
()
throws
Exception
{
Class
classToCall
=
Class
.
forName
(
"TrollGame"
);
Method
methodToExecute
=
classToCall
.
getDeclaredMethod
(
"initBoard"
,
new
Class
[]{
Integer
.
TYPE
,
Integer
.
TYPE
,
Random
.
class
});
methodToExecute
.
setAccessible
(
true
);
Object
gameBoardObj
=
methodToExecute
.
invoke
(
seededGame
,
new
Object
[]{
ROWS
,
COLS
,
new
Random
(
12398
)});
GamePiece
[][]
gameBoard
=
(
GamePiece
[][])
gameBoardObj
;
GamePiece
playerPiece
=
gameBoard
[
0
][
0
];
boolean
result
=
(
playerPiece
instanceof
Player
);
assertEquals
(
"Test 18: initBoard test- correct player position."
,
true
,
result
);
}
@
Test
public
void
initBoardTestInitPlayerPoints1
()
throws
Exception
{
Class
classToCall
=
Class
.
forName
(
"TrollGame"
);
Method
methodToExecute
=
classToCall
.
getDeclaredMethod
(
"initBoard"
,
new
Class
[]{
Integer
.
TYPE
,
Integer
.
TYPE
,
Random
.
class
});
methodToExecute
.
setAccessible
(
true
);
Object
gameBoardObj
=
methodToExecute
.
invoke
(
seededGame
,
new
Object
[]{
ROWS
,
COLS
,
new
Random
(
12398
)});
GamePiece
[][]
gameBoard
=
(
GamePiece
[][])
gameBoardObj
;
GamePiece
playerPiece
=
gameBoard
[
0
][
0
];
int
result
=
playerPiece
.
getLifePoints
();
assertEquals
(
"Test 19: initBoard test- correct player points."
,
INIT_PLAYER_POINTS
,
result
);
}
@
Test
public
void
initBoardTestInitTreasurePos1
()
throws
Exception
{
Class
classToCall
=
Class
.
forName
(
"TrollGame"
);
Method
methodToExecute
=
classToCall
.
getDeclaredMethod
(
"initBoard"
,
new
Class
[]{
Integer
.
TYPE
,
Integer
.
TYPE
,
Random
.
class
});
methodToExecute
.
setAccessible
(
true
);
Object
gameBoardObj
=
methodToExecute
.
invoke
(
seededGame
,
new
Object
[]{
ROWS
,
COLS
,
new
Random
(
12398
)});
GamePiece
[][]
gameBoard
=
(
GamePiece
[][])
gameBoardObj
;
GamePiece
treasurePiece
=
gameBoard
[
7
][
9
];
boolean
result
=
(
treasurePiece
instanceof
Treasure
);
assertEquals
(
"Test 20: initBoard test- correct treasure position."
,
true
,
result
);
}
@
Test
public
void
initBoardTestInitTreasurePoints1
()
throws
Exception
{
Class
classToCall
=
Class
.
forName
(
"TrollGame"
);
Method
methodToExecute
=
classToCall
.
getDeclaredMethod
(
"initBoard"
,
new
Class
[]{
Integer
.
TYPE
,
Integer
.
TYPE
,
Random
.
class
});
methodToExecute
.
setAccessible
(
true
);
Object
gameBoardObj
=
methodToExecute
.
invoke
(
seededGame
,
new
Object
[]{
ROWS
,
COLS
,
new
Random
(
12398
)});
GamePiece
[][]
gameBoard
=
(
GamePiece
[][])
gameBoardObj
;
GamePiece
treasurePiece
=
gameBoard
[
7
][
9
];
int
result
=
treasurePiece
.
getLifePoints
();
assertEquals
(
"Test 21: initBoard test- correct treasure points."
,
500
,
result
);
}
@
Test
public
void
initBoardTestInitTrollExists1
()
throws
Exception
{
Class
classToCall
=
Class
.
forName
(
"TrollGame"
);
Method
methodToExecute
=
classToCall
.
getDeclaredMethod
(
"initBoard"
,
new
Class
[]{
Integer
.
TYPE
,
Integer
.
TYPE
,
Random
.
class
});
methodToExecute
.
setAccessible
(
true
);
Object
gameBoardObj
=
methodToExecute
.
invoke
(
seededGame
,
new
Object
[]{
ROWS
,
COLS
,
new
Random
(
12398
)});
GamePiece
[][]
gameBoard
=
(
GamePiece
[][])
gameBoardObj
;
int
found
=
0
;
for
(
int
i
=
0
;
i
<
8
;
i
++
)
{
for
(
int
j
=
0
;
j
<
10
;
j
++
){
if
(
gameBoard
[
i
][
j
]
instanceof
Troll
)
found
++
;
}
}
assertEquals
(
"Test 22: initBoard test- one troll exists."
,
1
,
found
);
}
@
Test
public
void
initBoardTestInitRandomTroll1
()
throws
Exception
{
Class
classToCall
=
Class
.
forName
(
"TrollGame"
);
Method
methodToExecute
=
classToCall
.
getDeclaredMethod
(
"initBoard"
,
new
Class
[]{
Integer
.
TYPE
,
Integer
.
TYPE
,
Random
.
class
});
methodToExecute
.
setAccessible
(
true
);
Random
rand
=
new
Random
(
396051
);
int
[]
correctRows
=
{
2
,
1
,
4
,
3
,
1
};
int
[]
correctCols
=
{
6
,
3
,
4
,
8
,
7
};
int
[]
rows
=
new
int
[
5
];
int
[]
cols
=
new
int
[
5
];
for
(
int
k
=
0
;
k
<
5
;
k
++
){
Object
gameBoardObj
=
methodToExecute
.
invoke
(
seededGame
,
new
Object
[]{
ROWS
,
COLS
,
rand
});
GamePiece
[][]
gameBoard
=
(
GamePiece
[][])
gameBoardObj
;
for
(
int
i
=
0
;
i
<
8
;
i
++
)
{
for
(
int
j
=
0
;
j
<
10
;
j
++
){
if
(
gameBoard
[
i
][
j
]
instanceof
Troll
){
rows
[
k
]
=
i
;
cols
[
k
]
=
j
;
}
}
}
}
boolean
allCorrect
=
true
;
for
(
int
i
=
0
;
i
<
5
;
i
++
){
if
(
rows
[
i
]
!=
correctRows
[
i
]
||
cols
[
i
]
!=
correctCols
[
i
])
allCorrect
=
false
;
}
assertEquals
(
"Test 23: initBoard test- troll randomly distributed."
,
true
,
allCorrect
);
}
@
Test
public
void
getRandTrollRowTest1
()
throws
Exception
{
Class
classToCall
=
Class
.
forName
(
"TrollGame"
);
Method
methodToExecute
=
classToCall
.
getDeclaredMethod
(
"getRandTrollRow"
,
new
Class
[]{
Random
.
class
,
Integer
.
TYPE
});
methodToExecute
.
setAccessible
(
true
);
Random
rand
=
new
Random
(
6782315
);
int
[]
correctRows
=
{
3
,
2
,
6
,
6
,
1
};
int
[]
rows
=
new
int
[
5
];
for
(
int
k
=
0
;
k
<
5
;
k
++
){
Object
randRow
=
methodToExecute
.
invoke
(
seededGame
,
new
Object
[]{
rand
,
ROWS
});
rows
[
k
]
=
(
int
)
randRow
;
}
boolean
allCorrect
=
true
;
for
(
int
i
=
0
;
i
<
5
;
i
++
){
if
(
rows
[
i
]
!=
correctRows
[
i
])
allCorrect
=
false
;
}
assertEquals
(
"Test 24: Test getRandTrollRow."
,
true
,
allCorrect
);
}
@
Test
public
void
getRandTrollColTest1
()
throws
Exception
{
Class
classToCall
=
Class
.
forName
(
"TrollGame"
);
Method
methodToExecute
=
classToCall
.
getDeclaredMethod
(
"getRandTrollCol"
,
new
Class
[]{
Random
.
class
,
Integer
.
TYPE
});
methodToExecute
.
setAccessible
(
true
);
Random
rand
=
new
Random
(
9784195
);
int
[]
correctCols
=
{
9
,
7
,
6
,
1
,
5
};
int
[]
cols
=
new
int
[
5
];
for
(
int
k
=
0
;
k
<
5
;
k
++
){
Object
randRow
=
methodToExecute
.
invoke
(
seededGame
,
new
Object
[]{
rand
,
COLS
});
cols
[
k
]
=
(
int
)
randRow
;
}
boolean
allCorrect
=
true
;
for
(
int
i
=
0
;
i
<
5
;
i
++
){
if
(
cols
[
i
]
!=
correctCols
[
i
])
allCorrect
=
false
;
}
assertEquals
(
"Test 25: Test getRandTrollCol."
,
true
,
allCorrect
);
}
@
Test
public
void
testPlayerWinsMethod1
()
throws
Exception
{