This commit is contained in:
louiscklaw
2025-02-01 01:58:47 +08:00
parent b3da7aaef5
commit 04dbefcbaf
1259 changed files with 280657 additions and 0 deletions

View File

@@ -0,0 +1,206 @@
import java.util.Scanner;
public class Battleships {
public final static int DEFAULT_GAME_BOARD_SIZE = 10;
public final static int DEFAULT_GAME_NUM_GROUP_SHIP = 3;
private int[][] board;
private Scanner input;
private int launched = 1, hit = 0;
// -1 missing
// 0 null
// 1 hit
// 2 ship
public Battleships() {
board = new int[DEFAULT_GAME_BOARD_SIZE][DEFAULT_GAME_BOARD_SIZE];
setShips();
}
public Battleships(int size) {
board = new int[size][size];
setShips();
}
public Battleships(int size, Ship[] ships) {
board = new int[size][size];
setShips(ships);
}
public void setScanner(Scanner scanner) {
this.input = scanner;
}
public void start() {
System.out.println("---- Battleship Game----");
showBoard(false);
while (!isWin()) {
System.out.print("Set your missile [XY] (x to exit) :");
char[] misile = input.nextLine().toCharArray();
if (misile.length == 1) {
if (misile[0] == 'c')
showBoard(true);
else if (misile[0] == 'x') {
System.out.println("Bye!");
System.exit(1);
} else
System.out.println("Mismatch!!");
} else if (misile.length == 2) {
int x = Integer.parseInt(String.valueOf(misile[0]));
int y = Integer.parseInt(String.valueOf(misile[1]));
put(x, y);
showBoard(false);
showStatus();
} else {
System.out.println("Mismatch!!");
}
}
System.out.println("You have hit all battleships!");
}
public void put(int x, int y) {
int val = board[x][y];
if (val == 0) {
System.out.println("Missed.");
board[x][y] = -1;
launched++;
} else if (val == 2) {
System.out.println("It's a hit!!");
board[x][y] = 1;
hit++;
launched++;
} else {
System.out.println("You have already fired this area.");
}
}
public void setShips(Ship[] ships) {
for (Ship ship : ships) {
this.board[ship.x][ship.y] = 2;
}
}
public void setShips() {
setShips(createShip(this.board));
}
public static Ship[] createShip(int[][] boards) {
return createShip(boards, DEFAULT_GAME_NUM_GROUP_SHIP, DEFAULT_GAME_NUM_GROUP_SHIP);
}
public static Ship[] createShip(int[][] board, int numOfGroupShip, int numOfShip) {
Ship[] ships = new Ship[numOfGroupShip * numOfShip];
for (int i = 0; i < numOfGroupShip * numOfShip; i += 3) {
int count = 0;
while (count++ < numOfShip) {
int cX;
int cY;
boolean isHorizontally = (Math.random() > 0.5);
do {
cX = (int) Math.ceil(Math.random() * (board.length - 1 - 2));
cY = (int) Math.ceil(Math.random() * (board[0].length - 1 - 2));
ships[i] = new Ship(cX, cY);
} while ((!isHorizontally && board[cX][cY] == 2 || board[cX + 1][cY] == 2 || board[cX + 2][cY] == 2)
|| (isHorizontally && board[cX][cY] == 2 || board[cX][cY + 1] == 2 || board[cX][cY + 2] == 2));
for (int j = 0; j < numOfShip; j++) {
if (!isHorizontally)
ships[i + j] = new Ship(cX + j, cY);
else
ships[i + j] = new Ship(cX, cY + j);
}
}
}
return ships;
}
public void showStatus() {
System.out.printf("Launched:%d, Hit: %d\n", launched, hit);
}
public boolean isWin() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
int val = board[i][j];
if (val == 2)
return false;
}
}
return true;
}
public void showBoard(boolean showHidden) {
System.out.println();
int size = Integer.toString(board.length - 1).length();
if (size < 2)
size++;
for (int i = 0; i < size + 1; i++)
System.out.print(" ");
System.out.print(" ");
for (int i = 0; i < board.length; i++)
System.out.printf(" %d", i);
System.out.println();
for (int i = 0; i < size + 1; i++)
System.out.print("-");
System.out.print("+-");
for (int i = 0; i < board.length; i++)
for (int j = 0; j < Integer.toString(i).length() + 1; j++)
System.out.print("-");
System.out.println();
for (int i = 0; i < board.length; i++) {
System.out.printf("%d", i);
for (int j = 0; j < size - Integer.toString(i).length() + 1; j++)
System.out.print(" ");
System.out.print("| ");
for (int j = 0; j < board[i].length; j++) {
int val = board[i][j];
if (val == 1)
System.out.print("#");
else if (val == -1)
System.out.print("O");
else if (showHidden && val == 2)
System.out.print("S");
else
System.out.print(".");
for (int k = 0; k < Integer.toString(j).length(); k++)
System.out.print(" ");
}
System.out.println();
}
}
}
class Ship {
public int x;
public int y;
public Ship(int x, int y) {
this.x = x;
this.y = y;
}
}

View File

@@ -0,0 +1,11 @@
import java.util.Scanner;
class Main{
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
Battleships game = new Battleships();
game.setScanner(scanner);
game.start();
}
}

View File

@@ -0,0 +1,153 @@
# ITP3914 Programming Assignment (PTE)
## Instructions
1. This assignment is an individual assignment and should be done by you only. Plagiarism will be treated seriously. Any submitted assignment is found that involved wholly or partly in plagiarism (no matter the assignments are from the original authors or from the plagiarists) will be scored Zero mark and the students involved will be received discipline penalty set by the institute accordingly.
2. Grading of your programs will be based on correctness, quality, style and efficiency.
3. You are required to hand in
- softcopy of the program source codes upload to Moodle. (named: BattleShip.java)
- test report (sample screen of your program) (named: TestReport.docx)
4. Late submission will NOT be accepted.
5. Each student may be arranged to conduct an interview with your lecturer to explain some parts of your program code and answer some questions. Marks will be deducted if you cannot explain your code well.
## Assignment Specification
You are asked to write a Battleship game using Java.
### Setting up the game
1. When the program runs, there is a 10 x 10 battlefield created. Computer will set 3 battleship on the field randomly.
2. Each battleship's occupies consecutively 3 cells, and can be placed horizontally or vertically, the chance should be 50:50.
3. The following is an example of two battleships set horizontally while one set vertically.
4. The three battleships should not overlap.
```
0 1 2 3 4 5 6 7 8 9
--+--------------------
0 | . . . S S S . . . .
1 | . . . . . . . . . .
2 | . . . . . . . . . .
3 | . . . . . S . . . .
4 | . . . . . S . . . .
5 | . . . . . S . . . .
6 | . . . . . . . . . .
7 | . . . . . . . . . .
8 | S S S . . . . . . .
9 | . . . . . . . . . .
```
#### Game play
1. Show the greeting message and battlefield to player as below. Hide the battleship (of course!)
```
---- Battleship Game----
0 1 2 3 4 5 6 7 8 9
--+--------------------
0 | . . . . . . . . . .
1 | . . . . . . . . . .
2 | . . . . . . . . . .
3 | . . . . . . . . . .
4 | . . . . . . . . . .
5 | . . . . . . . . . .
6 | . . . . . . . . . .
7 | . . . . . . . . . .
8 | . . . . . . . . . .
9 | . . . . . . . . . .
```
2. Prompt player to input the place where to he/she wants to hit.
3. The player will enter two characters range from 0-9, where the first character is the nth column, and the second character is the mth row. E.g. [45] means the player wants to hit column 4, row 5 of the battlefield.
```
Set your missile [XY] (x to exit) :45
Missed.
Launched:1, Hit: 0
```
4. If player hit the battleship, the cell will shows [#].
5. If player missed the battleship, the cell will shows [o].
```
Set your missile [XY] (x to exit) :45
Missed.
Launched:1, Hit: 0
0 1 2 3 4 5 6 7 8 9
--+--------------------
0 | . . . . . . . . . .
1 | . . . . . . . . . .
2 | . . . . . . . . . .
3 | . . . . . . . . . .
4 | . . . . . . . . . .
5 | . . . . o . . . . .
6 | . . . . . . . . . .
7 | . . . . . . . . . .
8 | . . . . . . . . . .
9 | . . . . . . . . . .
```
6. Cumulated and show the number of missile launched and the number of successful hit.
7. If the player chose a cell which have been hit before, show [You have already fired this area.] and do NOT cumulate the number of missile launched.
#### Extra feature
1. Add a cheating function: whenever the player enter "c", shows the battleships with mark "S".
```
Set your missile [XY] (x to exit) :c
0 1 2 3 4 5 6 7 8 9
--+--------------------
0 | . . . . . . . . . .
1 | . . . o . . . . . .
2 | . . o . . . . . o .
3 | . . . . . . . . . .
4 | . . . . . . . . . .
5 | . . . . o S S S . .
6 | . . S S S . . S S S
7 | . . . . . . o . . .
8 | . . . . o . . . . .
9 | . . . . . . . . . .
```
2. Whenever the player enter "x", end the game
```
0 1 2 3 4 5 6 7 8 9
--+--------------------
0 | . . . . . . . . . .
1 | . . . o . . . . . .
2 | . . o . . . . . o .
3 | . . . . . . . . . .
4 | . . . . . . . . . .
5 | . . . . o . . . . .
6 | . . . # . . . . . .
7 | . . . . . . o . . .
8 | . . . . o . . . . .
9 | . . . . . . . . . .
Set your missile [XY] (x to exit) :x
Bye!
```
3. When the player hit all battleships, show " You have hit all battleships!" and end the game.
```
Set your missile [XY] (x to exit) :96
It's a hit!!
Launched:19, Hit: 9
Set your missile [XY] (x to exit) :96
It's a hit!!
Launched:19, Hit: 9
0 1 2 3 4 5 6 7 8 9
--+--------------------
0 | . . . . . . . . . .
1 | . . . o . . . . . .
2 | . . o . . . . . o .
3 | . . . . . . . . . .
4 | . . . . . . . . . .
5 | . . . . o # # # o .
6 | . o # # # o o # # #
7 | . . . . . . o . . .
8 | . . . . o . . . . .
9 | . . . . . . . . . .
Launched:19, Hit: 9
You have hit all battleships!
```
## Remark
1. No data validation is needed, you may assume the user will always input correctly. The user will only input a two-character number such as [04] or [99], [c] (to cheat) or [x] (to terminate).
2. You should put all your program code in ONE single JAVA file named BattleShip.java.
3. Total number of code should be around 100-200 lines.
## Deliverables
1. Program source code - Battleship.java
2. Test report - at least two sample running output (in doc / docx / pdf / txt format)