update,
This commit is contained in:
209
_resources/it114105/itp3914/Assignment/18-19/Assignment.java
Normal file
209
_resources/it114105/itp3914/Assignment/18-19/Assignment.java
Normal file
@@ -0,0 +1,209 @@
|
||||
/*Name:Kwok Ho Hin
|
||||
StudenID: 180160427
|
||||
Class: IT114105-1B
|
||||
Description of purpose: 6x6 Reversi game in console mode. The letter 1 mean black, the letter 2 mean white and the letter 0 mean empty space.
|
||||
*/
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Assignment {
|
||||
|
||||
public static Scanner input = new Scanner(System.in);
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[][] reversi = new int[6][6];/*Reversi Array*/
|
||||
boolean End = false; /*End is false*/
|
||||
int inputX, inputY, countWhite = 0, countBlack = 0, player = 1, countPlay = 1;/*default player is 1, start the Countturn is 1, inputX and inputY for the Scanner can position*/
|
||||
reversi[2][2] = 1;
|
||||
reversi[3][3] = 1;
|
||||
reversi[3][2] = 2;
|
||||
reversi[2][3] = 2;
|
||||
endGamebk:
|
||||
do {
|
||||
boolean inputEnd = false; /*reset inputEnd is false, player need input the values*/
|
||||
System.out.println(" 0 1 2 3 4 5\n -------------");
|
||||
for (int count = 0; count < 6; count++) { /*print the gameboard*/
|
||||
System.out.print(count + " | ");
|
||||
for (int i = 0; i <= 5; i++) {
|
||||
System.out.print(reversi[count][i] + " ");
|
||||
}
|
||||
System.out.print("\n");
|
||||
}
|
||||
|
||||
if (countPlay % 2 == 0) {
|
||||
player = 2;
|
||||
} else {
|
||||
player = 1;
|
||||
} /*calculator the player odd number of turn is 1, even number of turn is 2*/
|
||||
truebk:
|
||||
for (int count = 0; count < 6; count++) {
|
||||
for (int i = 0; i <= 5; i++) {
|
||||
if (reversi[count][i] == 0 && checkMove(reversi, count, i, player, true)) {
|
||||
break truebk; /*find have or have not empty can put, if find it break the loop*/
|
||||
} else if (count == 5 && i == 5 && !checkMove(reversi, 5, 5, 2, true) && !checkMove(reversi, 5, 5, 1, true)) {
|
||||
break endGamebk; /*not empty space put so finish the game*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
System.out.print("Please enter the position of '" + player + "' :");
|
||||
inputX = input.nextInt();
|
||||
inputY = input.nextInt();
|
||||
if (inputX > 5 || inputX < 0 || inputY > 5 || inputY < 0) {
|
||||
/*Check input value*/
|
||||
System.out.println("Error - input numbers should be 0 to 5!");
|
||||
}else if(reversi[inputX][inputY]!=0){ /*check input position whether empty*/
|
||||
System.out.println("Error - input cell is not empty");
|
||||
} else if (checkMove(reversi, inputX, inputY, player, false)) {
|
||||
reversi[inputX][inputY] = player;
|
||||
int countInput = 0;
|
||||
for (int i = 0; i < 6; i++) { /*Check can a input next turn*/
|
||||
for (int count = 0; count < 6; count++) {
|
||||
if ((countPlay + 1) % 2 == 0 && checkMove(reversi, i, count, 2, true) == false) {
|
||||
countInput++;
|
||||
} else if ((countPlay + 1) % 2 != 0 && checkMove(reversi, i, count, 1, true) == false) {
|
||||
countInput++;
|
||||
}
|
||||
if (countInput > 35) { /*Check next turn player whether can put */
|
||||
countPlay++; /*next turn*/
|
||||
}
|
||||
}
|
||||
}
|
||||
countPlay++; /*next turn*/
|
||||
inputEnd = true; /*End of input*/
|
||||
} else {
|
||||
System.out.println("Error - invalid move");
|
||||
}
|
||||
|
||||
} while (inputEnd == false);
|
||||
} while (End == false);
|
||||
for (int i = 0; i < 6; i++) {
|
||||
/*Counter*/
|
||||
for (int count = 0; count < 6; count++) {
|
||||
if (reversi[i][count] == 1) {
|
||||
countBlack++;
|
||||
} else if (reversi[i][count] == 2) { /*cannot use 'else' because the reversi game not finish in use all table may include a letter'0'*/
|
||||
countWhite++;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.print("Game Finishes.\n\t'1' - " + countBlack + "\n\t'2' - " + countWhite);
|
||||
if (countWhite < countBlack) {
|
||||
System.out.print("\nBlack wins.");
|
||||
} else {
|
||||
System.out.print("\nWhite wins.");
|
||||
}
|
||||
}
|
||||
/*Check the position and faceing up, if the inputtest is true that mean the method onlu check the position but not faceing up*/
|
||||
public static boolean checkMove(int reversi[][], int inputX, int inputY, int player, boolean inputtest) {
|
||||
boolean[] check = new boolean[8];
|
||||
if (reversi[inputX][inputY] == 0) { /*only empty space can put*/
|
||||
for (int i = 1; i <= inputX; i++) { /*checkTop*/
|
||||
if (inputX - i < 0 || reversi[inputX - 1][inputY] == 0) { /*if arrary expection -> break*/
|
||||
break;
|
||||
} else if (reversi[inputX - 1][inputY] != player && reversi[inputX - i][inputY] == player && reversi[inputX - i][inputY] != 0) { /*on top of not a same and find do you have*/
|
||||
check[0] = true;
|
||||
if (inputtest == true) { /*Check test or not, if this for the test not need facing up */
|
||||
break;
|
||||
}
|
||||
for (int count = 1; count <= i; count++) {
|
||||
reversi[inputX - count][inputY] = player;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 1; i <= 5 - inputX; i++) {/*checkdown*/
|
||||
if (inputX + i > 5 || reversi[inputX + 1][inputY] == 0) {/*if arrary expection -> break*/
|
||||
break;
|
||||
} else if (reversi[inputX + 1][inputY] != player && reversi[inputX + i][inputY] == player && reversi[inputX + i][inputY] != 0) {
|
||||
check[1] = true;
|
||||
if (inputtest == true) { /*Check test or not, if this for the test not need facing up */
|
||||
break;
|
||||
}
|
||||
for (int count = 1; count <= i; count++) {
|
||||
reversi[inputX + count][inputY] = player;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 1; i <= inputY; i++) {/*checkleft*/
|
||||
if (inputY - i < 0 || reversi[inputX][inputY - 1] == 0) { /*if arrary expection -> break*/
|
||||
break;
|
||||
} else if (reversi[inputX][inputY - 1] != player && reversi[inputX][inputY - i] == player && reversi[inputX][inputY - i] != 0) {
|
||||
check[2] = true;
|
||||
if (inputtest == true) {/*Check test or not, if this for the test not need facing up */
|
||||
break;
|
||||
}
|
||||
for (int count = 1; count <= i; count++) {
|
||||
reversi[inputX][inputY - count] = player;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 1; i <= 5 - inputY; i++) {/*checkRight*/
|
||||
if (inputY + i > 5 || reversi[inputX][inputY + 1] == 0) { /*if arrary expection -> break*/
|
||||
break;
|
||||
} else if (reversi[inputX][inputY + 1] != player && reversi[inputX][inputY + i] == player && reversi[inputX][inputY + i] != 0) {
|
||||
check[3] = true;
|
||||
if (inputtest == true) { /*Check test or not, if this for the test not need facing up */
|
||||
break;
|
||||
}
|
||||
for (int count = 1; count <= i; count++) {
|
||||
reversi[inputX][inputY + count] = player;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 1; i <= 5; i++) {/*checkRightTopL*/
|
||||
if ((inputX - i < 0 || inputY + i > 5) || reversi[inputX - i][inputY + i] == 0) { /*if arrary expection -> break*/
|
||||
break;
|
||||
} else if (reversi[inputX - 1][inputY + 1] != player && reversi[inputX - i][inputY + i] == player && reversi[inputX - i][inputY + i] != 0) {
|
||||
check[4] = true;
|
||||
if (inputtest == true) { /*Check test or not, if this for the test not need facing up */
|
||||
break;
|
||||
}
|
||||
for (int count = 1; count <= i; count++) {
|
||||
reversi[inputX - count][inputY + count] = player;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 1; i <= 5; i++) {/*RightDownL*/
|
||||
if ((inputX + i > 5 || inputY + i > 5) || reversi[inputX + i][inputY + i] == 0) { /*if arrary expection -> break*/
|
||||
break;
|
||||
} else if (reversi[inputX + 1][inputY + 1] != player && reversi[inputX + i][inputY + i] == player && reversi[inputX + i][inputY + i] != 0) {
|
||||
check[5] = true;
|
||||
if (inputtest == true) { /*Check test or not, if this for the test not need facing up */
|
||||
break;
|
||||
}
|
||||
for (int count = 1; count <= i; count++) {
|
||||
reversi[inputX + count][inputY + count] = player;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 1; i <= 5; i++) {/*checkLeftTopL*/
|
||||
if ((inputX - i < 0 || inputY - i < 0) || reversi[inputX - i][inputY - i] == 0) { /*if arrary expection -> break*/
|
||||
break;
|
||||
} else if (reversi[inputX - 1][inputY - 1] != player && reversi[inputX - i][inputY - i] == player && reversi[inputX - i][inputY - i] != 0) {
|
||||
check[6] = true;
|
||||
if (inputtest == true) { /*Check test or not, if this for the test not need facing up */
|
||||
break;
|
||||
}
|
||||
for (int count = 1; count <= i; count++) {
|
||||
reversi[inputX - count][inputY - count] = player;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 1; i <= 5; i++) {/*checkLeftDownL*/
|
||||
if ((inputX + i > 5 || inputY - i < 0) || reversi[inputX + i][inputY - i] == 0) { /*if arrary expection -> break*/
|
||||
break;
|
||||
} else if (reversi[inputX + 1][inputY - 1] != player && reversi[inputX + i][inputY - i] == player && reversi[inputX + i][inputY - i] != 0) {
|
||||
check[7] = true;
|
||||
if (inputtest == true) { /*Check test or not, if this for the test not need facing up */
|
||||
break;
|
||||
}
|
||||
for (int count = 1; count <= i; count++) {
|
||||
reversi[inputX + count][inputY - count] = player; /*if */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return check[0] || check[1] || check[2] || check[3] || check[4] || check[5] || check[6] || check[7];
|
||||
}
|
||||
}
|
@@ -0,0 +1,62 @@
|
||||
# ITP3914-Assignment 19-20 (Reversi Game)
|
||||
|
||||
- [x] Pass All test cases and hidden test cases
|
||||
|
||||
|
||||
Sample Result:
|
||||
```
|
||||
0 : 1 1 1 1 1 1
|
||||
1 : 2 2 2 2 1 1
|
||||
2 : 2 2 1 1 1 1
|
||||
3 : 2 2 2 2 2 2
|
||||
4 : 2 2 2 2 2 2
|
||||
5 : 2 2 2 2 2 2
|
||||
0 1 2 3 4 5
|
||||
Game Finishes.
|
||||
'1' - 12
|
||||
'2' - 24
|
||||
White wins.
|
||||
```
|
||||
|
||||
|
||||
Sample input:
|
||||
```
|
||||
-1 5
|
||||
3 6
|
||||
2 2
|
||||
0 0
|
||||
2 1
|
||||
3 1
|
||||
2 1
|
||||
1 3
|
||||
2 4
|
||||
1 4
|
||||
4 2
|
||||
3 4
|
||||
4 3
|
||||
2 0
|
||||
3 0
|
||||
5 2
|
||||
5 3
|
||||
5 1
|
||||
5 0
|
||||
4 0
|
||||
0 3
|
||||
4 4
|
||||
4 1
|
||||
0 2
|
||||
3 5
|
||||
0 4
|
||||
1 2
|
||||
5 4
|
||||
4 5
|
||||
1 1
|
||||
0 1
|
||||
0 0
|
||||
1 5
|
||||
2 5
|
||||
1 0
|
||||
0 5
|
||||
5 5
|
||||
```
|
||||
|
104
_resources/it114105/itp3914/Assignment/21-22/BingoGameBoard.java
Normal file
104
_resources/it114105/itp3914/Assignment/21-22/BingoGameBoard.java
Normal file
@@ -0,0 +1,104 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class BingoGameBoard {
|
||||
|
||||
private Scanner input;
|
||||
private List playerList;
|
||||
private List records;
|
||||
|
||||
public BingoGameBoard(){
|
||||
input = new Scanner(System.in);
|
||||
playerList = new List();
|
||||
records = new List();
|
||||
}
|
||||
|
||||
public void regPlayer(Player player){
|
||||
playerList.append(player);
|
||||
player.setBingoGameCard(BingoGameCard.generateGameCard(playerList.getSize()));
|
||||
}
|
||||
|
||||
public void printGameBoard(){
|
||||
for(int i = 0; i < playerList.getSize(); i++){
|
||||
Player player = (Player) playerList.getItemAt(i);
|
||||
System.out.printf("Player%s's Card:\n", player.getName());
|
||||
player.getBingoGameCard().print();
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public boolean hasWin(){
|
||||
for(int i = 0; i < playerList.getSize(); i++){
|
||||
Player player = (Player) playerList.getItemAt(i);
|
||||
if(player.getBingoGameCard().isBingo())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getUserInput(){
|
||||
int num = -1;
|
||||
do{
|
||||
|
||||
try{
|
||||
System.out.print("Game Host call (0 to exit): ");
|
||||
num = Integer.parseInt(this.input.nextLine());
|
||||
if(num < 0 || num > 25)
|
||||
throw new NumberFormatException();
|
||||
|
||||
if(duplicateHistory(num))
|
||||
throw new NumberRepeatException(num);
|
||||
|
||||
}catch(NumberFormatException e){
|
||||
System.out.println("The number must be between 1 to 25, please call again!");
|
||||
}catch(NumberRepeatException e){
|
||||
System.out.println(e.getMessage());
|
||||
num = -1;
|
||||
}
|
||||
}while(!(num < 26 && num > -1));
|
||||
return num;
|
||||
}
|
||||
|
||||
public boolean duplicateHistory(int num){
|
||||
for(int i = 0; i < records.getSize(); i++){
|
||||
if((int)records.getItemAt(i) == num)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void place(int number){
|
||||
for(int i = 0; i < playerList.getSize(); i++){
|
||||
Player player = (Player) playerList.getItemAt(i);
|
||||
player.getBingoGameCard().replace(number);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void start(){
|
||||
while(!hasWin()){
|
||||
printGameBoard();
|
||||
int num = getUserInput();
|
||||
if(num == 0)
|
||||
System.exit(1);
|
||||
place(num);
|
||||
records.append(num);
|
||||
}
|
||||
printGameBoard();
|
||||
printBingoList();
|
||||
}
|
||||
|
||||
private void printBingoList(){
|
||||
for(int i = 0; i < playerList.getSize(); i++){
|
||||
Player player = (Player) playerList.getItemAt(i);
|
||||
if(player.getBingoGameCard().isBingo())
|
||||
System.out.printf("Player%s Bingo!\n", player.getName());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class NumberRepeatException extends Exception{
|
||||
NumberRepeatException(int num){
|
||||
super("The number " + num +" is repeated, please call again!");
|
||||
}
|
||||
}
|
110
_resources/it114105/itp3914/Assignment/21-22/BingoGameCard.java
Normal file
110
_resources/it114105/itp3914/Assignment/21-22/BingoGameCard.java
Normal file
@@ -0,0 +1,110 @@
|
||||
public class BingoGameCard {
|
||||
|
||||
int[][] card;
|
||||
private final static int[][][] CARD = {
|
||||
{
|
||||
{24,2,8,1,25},
|
||||
{12,16,7,17,15},
|
||||
{5,6,20,19,13},
|
||||
{14,23,22,4,3},
|
||||
{10,18,11,21,9}
|
||||
},
|
||||
{
|
||||
{24,21,17,15,6},
|
||||
{10,3,8,18,20},
|
||||
{14,7,16,12,5},
|
||||
{25,23,13,19,11},
|
||||
{22,4,9,1,2}
|
||||
}
|
||||
};
|
||||
|
||||
public BingoGameCard(int[][] card){
|
||||
this.card = card;
|
||||
}
|
||||
|
||||
public void print(){
|
||||
for(int i = 0; i < card.length; i++){
|
||||
for(int j = 0; j < card[i].length; j++){
|
||||
if(card[i][j] == -1)
|
||||
System.out.print(" XX ");
|
||||
else
|
||||
System.out.printf(" %2d ", card[i][j]);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public static BingoGameCard generateGameCard(int index){
|
||||
if(index % 2 == 0)
|
||||
return new BingoGameCard(CARD[1]);
|
||||
return new BingoGameCard(CARD[0]);
|
||||
}
|
||||
|
||||
public boolean isBingo(){
|
||||
for(int i = 0; i < card.length; i++){
|
||||
int count = 0;
|
||||
for( int j = 0; j < card[i].length; j++){
|
||||
if(j>0 && count == 0)
|
||||
break;
|
||||
if(card[i][j] == -1)
|
||||
count++;
|
||||
}
|
||||
if(count == card[i].length)
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
for(int i = 0; i < card[0].length; i++){
|
||||
int count = 0;
|
||||
for(int j = 0; j < card.length; j++){
|
||||
|
||||
if(j>0 && count == 0)
|
||||
break;
|
||||
|
||||
if(card[j][i] == -1)
|
||||
count++;
|
||||
}
|
||||
if(count == card.length)
|
||||
return true;
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
for(int i = 0; i < card.length; i++){
|
||||
if(i>0 && count == 0)
|
||||
break;
|
||||
|
||||
if(card[i][card[i].length-i-1] == -1)
|
||||
count++;
|
||||
}
|
||||
|
||||
if (count == card.length)
|
||||
return true;
|
||||
|
||||
count = 0;
|
||||
for(int i = 0; i < card.length; i++){
|
||||
if(i>0 && count == 0)
|
||||
break;
|
||||
|
||||
if(card[i][i] == -1)
|
||||
count++;
|
||||
}
|
||||
|
||||
if(count == card.length)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
public void replace(int number){
|
||||
for(int i = 0; i < card.length; i++){
|
||||
for(int j = 0; j < card[i].length; j++){
|
||||
if(card[i][j] == number){
|
||||
card[i][j] = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
42
_resources/it114105/itp3914/Assignment/21-22/List.java
Normal file
42
_resources/it114105/itp3914/Assignment/21-22/List.java
Normal file
@@ -0,0 +1,42 @@
|
||||
public class List {
|
||||
private Node head;
|
||||
private Node tail;
|
||||
private int size;
|
||||
|
||||
public List(){
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean isEmply(){
|
||||
return head == null;
|
||||
}
|
||||
|
||||
public void append(Object data){
|
||||
if(head == null){
|
||||
head = tail = new Node(data);
|
||||
size++;
|
||||
return;
|
||||
}
|
||||
|
||||
tail.next = new Node(data);
|
||||
tail = tail.next;
|
||||
size++;
|
||||
}
|
||||
|
||||
public Object getItemAt(int index){
|
||||
Node tmp = head;
|
||||
int tmpIndex = 0;
|
||||
while(tmpIndex < index){
|
||||
tmp = tmp.next;
|
||||
tmpIndex++;
|
||||
}
|
||||
return tmp.data;
|
||||
}
|
||||
|
||||
public int getSize(){
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
}
|
8
_resources/it114105/itp3914/Assignment/21-22/Main.java
Normal file
8
_resources/it114105/itp3914/Assignment/21-22/Main.java
Normal file
@@ -0,0 +1,8 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
BingoGameBoard game = new BingoGameBoard();
|
||||
game.regPlayer(new Player("1"));
|
||||
game.regPlayer(new Player("2"));
|
||||
game.start();
|
||||
}
|
||||
}
|
14
_resources/it114105/itp3914/Assignment/21-22/Node.java
Normal file
14
_resources/it114105/itp3914/Assignment/21-22/Node.java
Normal file
@@ -0,0 +1,14 @@
|
||||
public class Node {
|
||||
public Object data;
|
||||
public Node next;
|
||||
|
||||
public Node(Object data){
|
||||
this.data = data;
|
||||
next = null;
|
||||
}
|
||||
|
||||
public Node(Object data, Node next){
|
||||
this.data = data;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
21
_resources/it114105/itp3914/Assignment/21-22/Player.java
Normal file
21
_resources/it114105/itp3914/Assignment/21-22/Player.java
Normal file
@@ -0,0 +1,21 @@
|
||||
public class Player {
|
||||
|
||||
private BingoGameCard card;
|
||||
private String name;
|
||||
|
||||
public Player(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setBingoGameCard(BingoGameCard card){
|
||||
this.card = card;
|
||||
}
|
||||
|
||||
public BingoGameCard getBingoGameCard(){
|
||||
return card;
|
||||
}
|
||||
}
|
168
_resources/it114105/itp3914/Assignment/21-22/README.md.original
Normal file
168
_resources/it114105/itp3914/Assignment/21-22/README.md.original
Normal file
@@ -0,0 +1,168 @@
|
||||
## BINGO game
|
||||
### Test case
|
||||
```
|
||||
28
|
||||
-2
|
||||
16
|
||||
10
|
||||
10
|
||||
22
|
||||
6
|
||||
20
|
||||
18
|
||||
2
|
||||
23
|
||||
```
|
||||
|
||||
### Expected Output
|
||||
```
|
||||
Player1's Card:
|
||||
24 2 8 1 25
|
||||
12 16 7 17 15
|
||||
5 6 20 19 13
|
||||
14 23 22 4 3
|
||||
10 18 11 21 9
|
||||
|
||||
Player2's Card:
|
||||
24 21 17 15 6
|
||||
10 3 8 18 20
|
||||
14 7 16 12 5
|
||||
25 23 13 19 11
|
||||
22 4 9 1 2
|
||||
|
||||
Game Host call (0 to exit): 28
|
||||
-2
|
||||
16
|
||||
10
|
||||
10
|
||||
22
|
||||
6
|
||||
20
|
||||
18
|
||||
2
|
||||
23
|
||||
The number must be between 1 to 25, please call again!
|
||||
Game Host call (0 to exit): The number must be between 1 to 25, please call again!
|
||||
Game Host call (0 to exit):
|
||||
Player1's Card:
|
||||
24 2 8 1 25
|
||||
12 XX 7 17 15
|
||||
5 6 20 19 13
|
||||
14 23 22 4 3
|
||||
10 18 11 21 9
|
||||
|
||||
Player2's Card:
|
||||
24 21 17 15 6
|
||||
10 3 8 18 20
|
||||
14 7 XX 12 5
|
||||
25 23 13 19 11
|
||||
22 4 9 1 2
|
||||
|
||||
Game Host call (0 to exit):
|
||||
Player1's Card:
|
||||
24 2 8 1 25
|
||||
12 XX 7 17 15
|
||||
5 6 20 19 13
|
||||
14 23 22 4 3
|
||||
XX 18 11 21 9
|
||||
|
||||
Player2's Card:
|
||||
24 21 17 15 6
|
||||
XX 3 8 18 20
|
||||
14 7 XX 12 5
|
||||
25 23 13 19 11
|
||||
22 4 9 1 2
|
||||
|
||||
Game Host call (0 to exit): The number 10 is repeated, please call again!
|
||||
Game Host call (0 to exit):
|
||||
Player1's Card:
|
||||
24 2 8 1 25
|
||||
12 XX 7 17 15
|
||||
5 6 20 19 13
|
||||
14 23 XX 4 3
|
||||
XX 18 11 21 9
|
||||
|
||||
Player2's Card:
|
||||
24 21 17 15 6
|
||||
XX 3 8 18 20
|
||||
14 7 XX 12 5
|
||||
25 23 13 19 11
|
||||
XX 4 9 1 2
|
||||
|
||||
Game Host call (0 to exit):
|
||||
Player1's Card:
|
||||
24 2 8 1 25
|
||||
12 XX 7 17 15
|
||||
5 XX 20 19 13
|
||||
14 23 XX 4 3
|
||||
XX 18 11 21 9
|
||||
|
||||
Player2's Card:
|
||||
24 21 17 15 XX
|
||||
XX 3 8 18 20
|
||||
14 7 XX 12 5
|
||||
25 23 13 19 11
|
||||
XX 4 9 1 2
|
||||
|
||||
Game Host call (0 to exit):
|
||||
Player1's Card:
|
||||
24 2 8 1 25
|
||||
12 XX 7 17 15
|
||||
5 XX XX 19 13
|
||||
14 23 XX 4 3
|
||||
XX 18 11 21 9
|
||||
|
||||
Player2's Card:
|
||||
24 21 17 15 XX
|
||||
XX 3 8 18 XX
|
||||
14 7 XX 12 5
|
||||
25 23 13 19 11
|
||||
XX 4 9 1 2
|
||||
|
||||
Game Host call (0 to exit):
|
||||
Player1's Card:
|
||||
24 2 8 1 25
|
||||
12 XX 7 17 15
|
||||
5 XX XX 19 13
|
||||
14 23 XX 4 3
|
||||
XX XX 11 21 9
|
||||
|
||||
Player2's Card:
|
||||
24 21 17 15 XX
|
||||
XX 3 8 XX XX
|
||||
14 7 XX 12 5
|
||||
25 23 13 19 11
|
||||
XX 4 9 1 2
|
||||
|
||||
Game Host call (0 to exit):
|
||||
Player1's Card:
|
||||
24 XX 8 1 25
|
||||
12 XX 7 17 15
|
||||
5 XX XX 19 13
|
||||
14 23 XX 4 3
|
||||
XX XX 11 21 9
|
||||
|
||||
Player2's Card:
|
||||
24 21 17 15 XX
|
||||
XX 3 8 XX XX
|
||||
25 23 13 19 11
|
||||
XX 4 9 1 XX
|
||||
|
||||
Game Host call (0 to exit):
|
||||
Player1's Card:
|
||||
24 XX 8 1 25
|
||||
12 XX 7 17 15
|
||||
5 XX XX 19 13
|
||||
14 XX XX 4 3
|
||||
XX XX 11 21 9
|
||||
|
||||
Player2's Card:
|
||||
24 21 17 15 XX
|
||||
XX 3 8 XX XX
|
||||
14 7 XX 12 5
|
||||
25 XX 13 19 11
|
||||
XX 4 9 1 XX
|
||||
|
||||
Player1 Bingo!
|
||||
Player2 Bingo!
|
||||
```
|
206
_resources/it114105/itp3914/Assignment/22-23/Battleships.java
Normal file
206
_resources/it114105/itp3914/Assignment/22-23/Battleships.java
Normal 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;
|
||||
}
|
||||
}
|
11
_resources/it114105/itp3914/Assignment/22-23/Main.java
Normal file
11
_resources/it114105/itp3914/Assignment/22-23/Main.java
Normal 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();
|
||||
}
|
||||
}
|
153
_resources/it114105/itp3914/Assignment/22-23/README.md.original
Normal file
153
_resources/it114105/itp3914/Assignment/22-23/README.md.original
Normal 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)
|
Reference in New Issue
Block a user