programming
1. A starter solution has been provided to you as zip file.When submitting your own unit test methods,zipthe contents of thesrcdirectory into a single zipped file.Make sure the zipped file has a .zip extension (not .tar, .rar, .7z, etc.).
ObstaclesWarrior.zip
#2
Obstacle Warrior
Objectives:Create a Java program using programming fundamentals (file I/O, loops, conditional statements, arrays, functions)
Problem:In an effort to win a coding competition, you decided to create an awesome Obstacle Warrior game. The game is played on a 2-dimensional board similar to a Chess board, but the dimensions may be different. The minimum size of the board is 2×2. The board will have a Start square and an Exit square that are not stored on the board. Start and Exit squares cannot be the same. Other board squares may contain obstacles in the form of an integer that will define how the warrior position and score will be affected. The obstacle squares can have values from 0 to -10 only. The Start square is always a clear square. All clear squares are marked with # on the board. The Exit square may contain an obstacle that is not a zero. The size of the board, number of obstacles, and Start and Exit squares are all unknow to your code prior to running. This information is stored in a file that your code will read at the beginning of the game. The board.dat file must be read into a 2-D array.
A warrior must start at the Start square and find their way to the Exit square. The warrior can move on the board in any direction including diagonally, one square at a time. A warrior has a running score (integer) maintained from the start of the game until the warrior exits the board. If the warrior lands on an obstacle square with a value of zero, the warrior is sent back to the starting position and the obstacle square will become a normal square (obstacle removed). If the obstacle square has a negative number, that number will reduce the warrior’s score by the value of the obstacle, and the obstacle square will become a clear square (obstacle removed). Each VALID move that the warrior makes without landing on an obstacle will earn the warrior one point. The moves for the warrior are randomly generated by your code in the form of a direction (0-UP, 1-DOWN, 2-LEFT, 3-RIGHT, 4-UPRIGHT, 5-DOWNRIGHT, 6-UPLEFT, 7-DOWNLEFT). If the warrior is at the boundary of the board and your code generates an invalid move, that move will be ignored. Your code will keep generating moves until the warrior exits at the Exit square. Once the warrior exits, your program will store the updated board information to a new file ResultBoard.dat as single-space separated data. The program will also display the total number of valid moves, the total time elapsed in milliseconds since the first move until the warrior exited the board, the final score of the warrior and the formatted board information (right aligned columns with total of 5 spaces).
Output Format:
Each column in the final board display must be of total width of 5 spaces
Data in each column must be right aligned
Enter the board data file path: C:board.dat //Repeat prompt until valid file OR show error and exit.
Type “Start” to start the game or “Exit” to exit the game: exit //Your program must exit
Enter the board file path: C:board.dat
Type “Start” to start the game or “Exit” to exit the game: start //You may display the moves and the running score after each move but it is not required
The warrior made 270 valid moves in 503 milliseconds. The final score is 175 points.
# # # # #
# # # 0 #
# # # # #
# -3 # # -4
# # # # #
Press any key to exit!
Program Structure:Your code should be modular and easy to understand. In addition to the main method, the following methods are required to be in your code. These methods will be used by the unit testing to test the accuracy of your code.
public static String[][] ReadBoardFromFile(String fileName, Position startPosition, Position exitPosition)
public static boolean WriteBoardToFile(String fileName, String[][] boardArray)
public static int GenerateDirection()
public static boolean MoveWarrior(int direction, String[][] boardArray, Position currentPosition)
public static int CalculateWarriorScore(int currentScore, Position currentPosition, String[][] boardArray)
public static String DisplayResults(int currentScore, int numberOfMoves, int timeElapsed, String[][] boardArray)
Program Flow:
Program starts in main() method
Prompt user for Board.dat file path
Read board from file
Generate a direction
Move the warrior
Calculate new score
Check conditions and clear square if needed
Repeat until the warrior is at the exit square
Display the results
Prompt user to exit game
Board.dat file format:
The data in the file will be separated by one space
Assume that all data in the file is valid
Clear and Start squares (no obstacles) will be marked with # in the file
The first line of the file contains the dimensions of the board (rows and columns) e.g. 3 7
The second line contains the Start square indexes (rowIndex, columnIndex)
The third line contains the Exit square indexes (rowIndex, columnIndex)
The rest of the lines represent the contents, including obstacles, of a row on the board
Example of a Board size 5×5 data file:
5 5
2 2
4 3
# -5 # # #
# # # 0 #
# # # # #
# -3 # # -4
-10 # # # #
**ResultBoard.dat file format: **
Data must be separated by a single space
# # # # #
# # # 0 #
# # # # #
# -3 # # -4
# # # # #
Grading:
Coding standards, style and comments (10 Points)
Unit testing methods x6, one for each of the methods mentioned above (10 Points)
ReadBoardFromFile (10 Points)
WriteBoardToFile (10 Points)
GenerateDirection (10 Points)
MoveWarrior (20 Points)
CalculateWarriorScore (20 Points)
DisplayResults (10 Points)
Notes:
The Position class has been provided to you. You may add new methods/constructors to this class, but don’t change existing methods
Part 1 deliverables: Unit Test methods only – Submit on eLearning
Part 2 deliverables: Functioning program – Submit on zyBooks (You may resubmit your Unit Test method on eLearning)
__MACOSX/._ObstaclesWarrior
ObstaclesWarrior/.DS_Store
__MACOSX/ObstaclesWarrior/._.DS_Store
__MACOSX/ObstaclesWarrior/._target
ObstaclesWarrior/pom.xml
4.0.0
ObstaclesWarrior
ObstaclesWarrior
1.0-SNAPSHOT
ObstaclesWarrior
http://www.example.com
UTF-8
12
12
junit
junit
4.11
test
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-jar-plugin
3.0.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
maven-site-plugin
3.7.1
maven-project-info-reports-plugin
3.0.0
__MACOSX/ObstaclesWarrior/._pom.xml
ObstaclesWarrior/.classpath
__MACOSX/ObstaclesWarrior/._.classpath
__MACOSX/ObstaclesWarrior/._.settings
ObstaclesWarrior/.project
ObstaclesWarrior
org.eclipse.jdt.core.javabuilder
org.eclipse.m2e.core.maven2Builder
org.eclipse.jdt.core.javanature
org.eclipse.m2e.core.maven2Nature
1599412327219
30
org.eclipse.core.resources.regexFilterMatcher
node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__
__MACOSX/ObstaclesWarrior/._.project
__MACOSX/ObstaclesWarrior/._src
__MACOSX/ObstaclesWarrior/target/._test-classes
ObstaclesWarrior/target/.DS_Store
__MACOSX/ObstaclesWarrior/target/._.DS_Store
__MACOSX/ObstaclesWarrior/target/._classes
ObstaclesWarrior/.settings/org.eclipse.jdt.core.prefs
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=12
org.eclipse.jdt.core.compiler.compliance=12
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=12
__MACOSX/ObstaclesWarrior/.settings/._org.eclipse.jdt.core.prefs
ObstaclesWarrior/.settings/org.eclipse.jdt.apt.core.prefs
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false
__MACOSX/ObstaclesWarrior/.settings/._org.eclipse.jdt.apt.core.prefs
ObstaclesWarrior/.settings/org.eclipse.m2e.core.prefs
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
__MACOSX/ObstaclesWarrior/.settings/._org.eclipse.m2e.core.prefs
ObstaclesWarrior/.settings/org.eclipse.core.resources.prefs
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/
__MACOSX/ObstaclesWarrior/.settings/._org.eclipse.core.resources.prefs
ObstaclesWarrior/src/.DS_Store
__MACOSX/ObstaclesWarrior/src/._.DS_Store
__MACOSX/ObstaclesWarrior/src/._test
__MACOSX/ObstaclesWarrior/src/._main
ObstaclesWarrior/target/test-classes/.DS_Store
__MACOSX/ObstaclesWarrior/target/test-classes/._.DS_Store
__MACOSX/ObstaclesWarrior/target/test-classes/._ObstaclesWarrior
ObstaclesWarrior/target/classes/.DS_Store
__MACOSX/ObstaclesWarrior/target/classes/._.DS_Store
__MACOSX/ObstaclesWarrior/target/classes/._ObstaclesWarrior
ObstaclesWarrior/src/test/.DS_Store
__MACOSX/ObstaclesWarrior/src/test/._.DS_Store
__MACOSX/ObstaclesWarrior/src/test/._java
ObstaclesWarrior/src/main/.DS_Store
__MACOSX/ObstaclesWarrior/src/main/._.DS_Store
__MACOSX/ObstaclesWarrior/src/main/._java
ObstaclesWarrior/target/test-classes/ObstaclesWarrior/MainTest.class
__MACOSX/ObstaclesWarrior/target/test-classes/ObstaclesWarrior/._MainTest.class
ObstaclesWarrior/target/classes/ObstaclesWarrior/Position.class
__MACOSX/ObstaclesWarrior/target/classes/ObstaclesWarrior/._Position.class
ObstaclesWarrior/target/classes/ObstaclesWarrior/Main.class
__MACOSX/ObstaclesWarrior/target/classes/ObstaclesWarrior/._Main.class
ObstaclesWarrior/src/test/java/.DS_Store
__MACOSX/ObstaclesWarrior/src/test/java/._.DS_Store
__MACOSX/ObstaclesWarrior/src/test/java/._ObstaclesWarrior
ObstaclesWarrior/src/main/java/.DS_Store
__MACOSX/ObstaclesWarrior/src/main/java/._.DS_Store
__MACOSX/ObstaclesWarrior/src/main/java/._ObstaclesWarrior
ObstaclesWarrior/src/test/java/ObstaclesWarrior/MainTest.java
ObstaclesWarrior/src/test/java/ObstaclesWarrior/MainTest.java
package
ObstaclesWarrior
;
import
static
org
.
junit
.
Assert
.
assertArrayEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
java
.
io
.
File
;
import
java
.
io
.
PrintWriter
;
import
org
.
junit
.
Test
;
/**
*Unittest
*/
public
class
MainTest
{
@
Test
public
void
ReadBoardFromFileTest
()
{
final
String
FILE_NAME
=
“Board.dat”
;
//EitherdynamicallycreatetheBoard.datfileorassumeitalreadyexists
File
file
=
new
File
(
FILE_NAME
);
PrintWriter
printToFile
=
new
PrintWriter
(
file
);
printToFile
.
println
(
“44”
);
printToFile
.
println
(
“02”
);
printToFile
.
println
(
“22”
);
printToFile
.
println
(
“0###”
);
printToFile
.
println
(
“#-3#-5”
);
printToFile
.
println
(
“####”
);
printToFile
.
println
(
“##-1#”
);
printToFile
.
close
();
//Createstartandexitpositionstopasstothemethod.
//Theseobjectswillbesetwithactualvaluesfromthe
//boardfilebyyourcodeinsidetheReadBoardFromFile()method
Position
actualStartPosition
=
new
Position
(
0
,
0
);
Position
actualExitPosition
=
new
Position
(
0
,
0
);
//Thesearetheexpectedvaluesforthestartandexitpostions
Position
expectedStartPosition
=
new
Position
(
0
,
2
);
Position
expectedExitPosition
=
new
Position
(
2
,
2
);
//Createtheexpectedarraywiththedata
String
[][]
expectedBoardArray
=
{
{
“0”
,
“#”
,
“#”
,
“#”
},
{
“#”
,
“-3”
,
“#”
,
“-5”
},
{
“#”
,
“#”
,
“#”
,
“#”
},
{
“#”
,
“#”
,
“-1”
,
“#”
},
};
//InvoketheReadBoardFromFile()methodandcapturethereturnedarray
String
[][]
actualBoardArray
=
Main
.
ReadBoardFromFile
(
FILE_NAME
,
actualStartPosition
,
actualExitPosition
);
//Checkifthestartandexitpositionsmatch
if
((
expectedStartPosition
.
getX
()
!=
actualStartPosition
.
getX
())
||
(
expectedStartPosition
.
getY
()
!=
actualStartPosition
.
getY
()))
{
assertTrue
(
“Startpositiondoesnotmatch”
,
false
);
}
if
((
expectedExitPosition
.
getX
()
!=
actualExitPosition
.
getX
())
||
(
expectedExitPosition
.
getY
()
!=
actualExitPosition
.
getY
()))
{
assertEquals
(
“Exitpositiondoesnotmatch”
,
false
);
}
//ComparetheactualBoardArraywiththetestBoardArray.
//Sizeanddatamustmatch.
//Makesurethenumberofrowsmatch
assertArrayEquals
(
“Boardarrayreadfromfiledoesnotmatchexpectedarray”
,
expectedBoardArray
,
actualBoardArray
);
}
@
Test
public
void
WriteBoardToFileTest
()
{
}
@
Test
public
void
GenerateDirectionTest
()
{
}
@
Test
public
void
MoveWarriorTest
()
{
}
@
Test
public
void
CalculateWarriorScoreTest
()
{
}
@
Test
public
void
DisplayResultsTest
()
{
}
}
__MACOSX/ObstaclesWarrior/src/test/java/ObstaclesWarrior/._MainTest.java
ObstaclesWarrior/src/main/java/ObstaclesWarrior/Position.java
ObstaclesWarrior/src/main/java/ObstaclesWarrior/Position.java
package
ObstaclesWarrior
;
/**
*Position
*/
public
class
Position
{
private
int
x
;
private
int
y
;
public
Position
(
int
xValue
,
int
yValue
)
{
x
=
xValue
;
y
=
yValue
;
}
public
int
getX
()
{
return
x
;
}
public
void
setX
(
int
x
)
{
this
.
x
=
x
;
}
public
int
getY
()
{
return
y
;
}
public
void
setY
(
int
y
)
{
this
.
y
=
y
;
}
}
__MACOSX/ObstaclesWarrior/src/main/java/ObstaclesWarrior/._Position.java
ObstaclesWarrior/src/main/java/ObstaclesWarrior/Main.java
ObstaclesWarrior/src/main/java/ObstaclesWarrior/Main.java
package
ObstaclesWarrior
;
/**
*ObstaclesWarrior
*
*/
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
}
public
static
String
[][]
ReadBoardFromFile
(
String
fileName
,
Position
startPosition
,
Position
exitPosition
)
{
//Thiscodewasaddedjusttoenableyoutoruntheprovidedunittest.
//Replacethiscodewithyourowncode.
String
[][]
gameBoard
=
{
{
“0”
,
“#”
,
“#”
,
“#”
},
{
“#”
,
“-3”
,
“#”
,
“-5”
},
{
“#”
,
“#”
,
“#”
,
“#”
},
{
“#”
,
“#”
,
“-1”
,
“#”
},
};
startPosition
.
setX
(
0
);
startPosition
.
setY
(
2
);
exitPosition
.
setX
(
2
);
exitPosition
.
setY
(
2
);
return
gameBoard
;
}
public
static
boolean
WriteBoardToFile
(
String
fileName
,
String
[][]
boardArray
)
{
return
true
;
}
public
static
int
GenerateDirection
()
{
return
0
;
}
public
static
Boolean
MoveWarrior
(
int
direction
,
String
[][]
boardArray
,
Position
currentPosition
)
{
return
true
;
}
public
static
int
CalculateWarriorScore
(
int
currentScore
,
Position
currentPosition
,
String
[][]
boardArray
)
{
return
0
;
}
public
static
String
DisplayResults
(
int
currentScore
,
int
numberOfMoves
,
int
timeElapsed
,
String
[][]
boardArray
)
{
return
“”
;
}
}
__MACOSX/ObstaclesWarrior/src/main/java/ObstaclesWarrior/._Main.java 1. A starter solution has been provided to you as zip file.When submitting your own unit test methods,zipthe contents of thesrcdirectory into a single zipped file.Make sure the zipped file has a .zip extension (not .tar, .rar, .7z, etc.).
ObstaclesWarrior.zip
#2
Obstacle Warrior
Objectives:Create a Java program using programming fundamentals (file I/O, loops, conditional statements, arrays, functions)
Problem:In an effort to win a coding competition, you decided to create an awesome Obstacle Warrior game. The game is played on a 2-dimensional board similar to a Chess board, but the dimensions may be different. The minimum size of the board is 2×2. The board will have a Start square and an Exit square that are not stored on the board. Start and Exit squares cannot be the same. Other board squares may contain obstacles in the form of an integer that will define how the warrior position and score will be affected. The obstacle squares can have values from 0 to -10 only. The Start square is always a clear square. All clear squares are marked with # on the board. The Exit square may contain an obstacle that is not a zero. The size of the board, number of obstacles, and Start and Exit squares are all unknow to your code prior to running. This information is stored in a file that your code will read at the beginning of the game. The board.dat file must be read into a 2-D array.
A warrior must start at the Start square and find their way to the Exit square. The warrior can move on the board in any direction including diagonally, one square at a time. A warrior has a running score (integer) maintained from the start of the game until the warrior exits the board. If the warrior lands on an obstacle square with a value of zero, the warrior is sent back to the starting position and the obstacle square will become a normal square (obstacle removed). If the obstacle square has a negative number, that number will reduce the warrior’s score by the value of the obstacle, and the obstacle square will become a clear square (obstacle removed). Each VALID move that the warrior makes without landing on an obstacle will earn the warrior one point. The moves for the warrior are randomly generated by your code in the form of a direction (0-UP, 1-DOWN, 2-LEFT, 3-RIGHT, 4-UPRIGHT, 5-DOWNRIGHT, 6-UPLEFT, 7-DOWNLEFT). If the warrior is at the boundary of the board and your code generates an invalid move, that move will be ignored. Your code will keep generating moves until the warrior exits at the Exit square. Once the warrior exits, your program will store the updated board information to a new file ResultBoard.dat as single-space separated data. The program will also display the total number of valid moves, the total time elapsed in milliseconds since the first move until the warrior exited the board, the final score of the warrior and the formatted board information (right aligned columns with total of 5 spaces).
Output Format:
Each column in the final board display must be of total width of 5 spaces
Data in each column must be right aligned
Enter the board data file path: C:board.dat //Repeat prompt until valid file OR show error and exit.
Type “Start” to start the game or “Exit” to exit the game: exit //Your program must exit
Enter the board file path: C:board.dat
Type “Start” to start the game or “Exit” to exit the game: start //You may display the moves and the running score after each move but it is not required
The warrior made 270 valid moves in 503 milliseconds. The final score is 175 points.
# # # # #
# # # 0 #
# # # # #
# -3 # # -4
# # # # #
Press any key to exit!
Program Structure:Your code should be modular and easy to understand. In addition to the main method, the following methods are required to be in your code. These methods will be used by the unit testing to test the accuracy of your code.
public static String[][] ReadBoardFromFile(String fileName, Position startPosition, Position exitPosition)
public static boolean WriteBoardToFile(String fileName, String[][] boardArray)
public static int GenerateDirection()
public static boolean MoveWarrior(int direction, String[][] boardArray, Position currentPosition)
public static int CalculateWarriorScore(int currentScore, Position currentPosition, String[][] boardArray)
public static String DisplayResults(int currentScore, int numberOfMoves, int timeElapsed, String[][] boardArray)
Program Flow:
Program starts in main() method
Prompt user for Board.dat file path
Read board from file
Generate a direction
Move the warrior
Calculate new score
Check conditions and clear square if needed
Repeat until the warrior is at the exit square
Display the results
Prompt user to exit game
Board.dat file format:
The data in the file will be separated by one space
Assume that all data in the file is valid
Clear and Start squares (no obstacles) will be marked with # in the file
The first line of the file contains the dimensions of the board (rows and columns) e.g. 3 7
The second line contains the Start square indexes (rowIndex, columnIndex)
The third line contains the Exit square indexes (rowIndex, columnIndex)
The rest of the lines represent the contents, including obstacles, of a row on the board
Example of a Board size 5×5 data file:
5 5
2 2
4 3
# -5 # # #
# # # 0 #
# # # # #
# -3 # # -4
-10 # # # #
**ResultBoard.dat file format: **
Data must be separated by a single space
# # # # #
# # # 0 #
# # # # #
# -3 # # -4
# # # # #
Grading:
Coding standards, style and comments (10 Points)
Unit testing methods x6, one for each of the methods mentioned above (10 Points)
ReadBoardFromFile (10 Points)
WriteBoardToFile (10 Points)
GenerateDirection (10 Points)
MoveWarrior (20 Points)
CalculateWarriorScore (20 Points)
DisplayResults (10 Points)
Notes:
The Position class has been provided to you. You may add new methods/constructors to this class, but don’t change existing methods
Part 1 deliverables: Unit Test methods only – Submit on eLearning
Part 2 deliverables: Functioning program – Submit on zyBooks (You may resubmit your Unit Test method on eLearning)