CSCI 1110 – Fall 2023
Assignment 02 – Due 08/10, 11:59pm
Please start this assignment early; programming and logic take time - if you leave it to the last minute,
you might not have enough time to finish or might make silly mistakes that you otherwise could avoid.
Note that TAs and Instructors will not be able to answer last-minute questions!
All work is to be handed in on Codio, our online code learning environment. You should, however, write
your code on an IDE such as IntelliJ.
To complete this assignment, you will need to know about:
• Basic Java
• Conditionals
• Boolean Variables
• Simple Input Validation
• Loops
• 1D, 2D Arrays
• Methods
Your code must compile. If it does not compile, you will receive a 0 (zero) on that portion of the
assignment, and no partial marks will be given.
Remember that students who hardcode their outputs to match the test cases in Mimir will receive a zero
on the entire assignment.
Grading Scheme: Please see the grading scheme at the end of this document.
Coding Style: You must proper variable names and comments. Please follow the guidelines on
https://web.cs.dal.ca/~franz/CodingStyle.html
You can create more methods than the ones described in the assignment if you want.
Problem 1 – Battleship (50 points)
In this assignment, you will implement a 2-player game of Battleship. In case you don't know this game,
you can find more information about it in the Wikipedia article here:
https://en.wikipedia.org/wiki/Battleship_(game)
You are going to represent the game board using 2D-Arrays, one 2D-array variable for each of the
players. The arrays should be of type char or String. You can choose the data type you rather use. You
must use 2D arrays or you will receive a zero.
Board representation
You should represent the board according to the example below (in this example the board is 5x5):
0,0 0,1 0,2 0,3 0,4
1,0 1,1 1,2 1,3 1,4
2,0 2,1 2,2 2,3 2,4
3,0 3,1 3,2 3,3 3,4
4,0 4,1 4,2 4,3 4,4
Boat representation
• Boats will be presented by the letter 'B' on the player's board. When adding boats to their
board, players will represent their boats’ position, size, and orientation using four numbers one
after the other, separated by spaces:
• The first number represents the X coordinate of the boat
• The second number represents the Y coordinate of the boat
• The third number represents the boat's size, that is how many spaces in the board the ship will
take. This number will be between 1 and 6
• The fourth number is the orientation of the boat:
o 0 for horizontal: the ship will grow from its coordinate right
o 1 for vertical: the ship will grow from its coordinate down
Processing
Your program will function as follows:
1. Read an integer number representing the board size (always square)
2. Initialize all the coordinates of the board with "-"
3. Read an int representing how many boats each player will add to their board
4. Add the ships for each player (one player after the other) following the boat representation
above.
a. Example: 2 boats for each player
b. Player 1 boat 1
c. Player 2 boat 1
d. Player 1 boat 2
e. Player 2 boat 2
5. After adding the boats to the board, you will print both player's boards using the example below
in the Examples section.
6. Then you will read a number representing how many shots will fire.
7. Read two integers representing x and y coordinates for the player's shot. Repeat this step,
alternating the player's turn until you fired all the shots. If the shot coordinate is outside the
board, ignore the shot.
a. Change the shots coordinate from either 'B' or '-' to 'X'
8. Print both player's boards again.
9. Print a String representing the final game state as follows:
a. Draw, that is, there are boats left in both player's boards
b. P1 Win: P2 has no more boats left, that is no more Bs on P2’s board). P1 still have boats
c. P2 Win: P1 has no more boats left, that is no more Bs on P1’s board. P2 still have boats
d. All destroyed: Both players have no boats left on their boards
Input/Output Considerations:
• If a boat grows past the bounds of the 2D array, you should "shrink it" so it fits the play area.
• You must separate each player board using the tab scape character ('\t'). There is no spacing
between each cell of the boards.
o Codio will use strict whitespace. If you are not using \t between the boards your tests
will not pass.
• You must add a new line between the two printing sequences.
• There should be a new line after printing the last board
Required Methods
The following methods are required in this assignment. If you fail to create and use the methods, you
will not receive full marks.
• Create a method that receives the players boards and prints them to the console
• Create a method that receives both boards and determines who won. This method should not
print to the console but rather return a value informing which player won (or draw or all
destroyed).
Examples
Input Output
P2 Won!
Problem 2 – Bomb Special (30 points)
This problem builds on Problem 1. Processing is still the same except that the last shot of each player
will be a special area-of-effect (AoE) damage, think of it as a bomb. The shot will cause damage on a
radius of 2 in the horizontal and vertical directions and of radius 1 in the diagonal directions.
Be aware of the index out of bounds issues that might occur.
You are allowed to copy your code from Problem 1 to this problem.
Bomb example
The board size is 10x10, the player's AoE is set on the coordinate (3,4). The damage area is depicted with
Another example
The board size is 10x10, the player's AoE is set on the coordinate (5,9). The damaged area is depicted
with 'X'
Grading
Each problem on the assignment will be graded based on three criteria:
Functionality
"Does it work according to specifications?" This is determined in an automated fashion by running your
program on a number of inputs and ensuring that the outputs match the expected outputs. The score is
determined based on the number of tests that your program passes.
Quality of Solution
"Is it a good solution?" This considers whether the solution is correct, efficient, covers boundary
conditions, does not have any obvious bugs, etc. This is determined by visual inspection of the code.
Initially full marks are given to each solution and marks are deducted based on faults found in the
solution.
Code Clarity
"Is it well written?" This considers whether the solution is properly formatted, well-documented, and
follows coding style guidelines. A single code clarity score is assigned for all solutions. If your program
does not compile, it is considered non-functional and of extremely poor quality, meaning you will
receive 0 for the solution.
PROBLEM PARTIAL
POINTS
POINTS
PROBLEM 1 50
Functionality 40
Quality of Solution 10
PROBLEM 2 30
Functionality 20
Quality of Solution 10
ALL PROBLEMS Code Clarity 20 20
TOTAL 100 100
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。