-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameBoard.java
More file actions
143 lines (131 loc) · 3.78 KB
/
GameBoard.java
File metadata and controls
143 lines (131 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.util.Random;
public class GameBoard {
public boolean[][] board;
public int[][] eBoard;
public int score = 0, boats = 0;
public GameBoard() {
board = new boolean[10][10];
eBoard = new int[10][10]; //0=nothing 1=miss 2=hit
System.out.println(
"====================================================================================\r\n" +
"\r\n\r\n" +
"\tWelcome To Battleships" +
"\r\n\r\n\r\n" +
"\tHow to Play:" +
"\r\n\r\n" +
"\t1. Enter Coordinates as [Letter][Number], For example: A9" +
"\r\n\r\n" +
"\t2. Guess all the Enemy Battleship Loations before the enemy does" +
"\r\n\r\n\r\n" +
"====================================================================================\r\n" +
"\r\n\r\n"
);
}
// create random boats
void generateBoats(int num) {
boats = num;
int curNum = 0;
while (curNum < num) {
// get random location
Random random = new Random();
int posx = random.nextInt(10);
int posy = random.nextInt(10);
// check if its empty
if (!board[posx][posy]) {
// if yes, add ship there
board[posx][posy] = true;
curNum++;
}
// if its not, repeat without increasing ship number
}
}
void printBoard() {
// print column labels
System.out.println(" 0 1 2 3 4 5 6 7 8 9 \t \t 0 1 2 3 4 5 6 7 8 9");
for (int i = 0; i < board.length; i++) {
// print row labels
System.out.print((char)('@' + i + 1) + " ");
for (int j = 0; j < board.length; j++) {
if (board[i][j]) {
System.out.print("X ");
} else {
System.out.print("- ");
}
}
System.out.print('\t');
System.out.print('\t');
//enemy
System.out.print((char)('@' + i + 1) + " ");
for (int j = 0; j < board.length; j++) {
switch (eBoard[i][j]) {
case 0:
System.out.print("- ");
break;
case 1:
System.out.print("M ");
break;
case 2:
System.out.print("H ");
break;
default:
break;
}
}
System.out.println();
}
}
boolean checkhit(int x, int y) {
// function to check if coordinates are a hit
boolean isHit = board[x][y];
if (isHit) {
boats--;
System.out.println("\nYour ship has been Hit");
} else {
System.out.println("\nYour ship has been Missed");
}
return (isHit);
}
void returnHit(int x, int y, boolean isHit) {
if (isHit) {
eBoard[x][y] = 2;
} else {
eBoard[x][y] = 1;
}
}
void matchResult(boolean hasLost) {
// function to display victory or defeat screen
System.out.print("\033[H\033[2J");
System.out.flush();
if (hasLost) {
System.out.println(
"====================================================================================\r\n" +
"\r\n\r\n" +
"\tDefeat" +
"\r\n\r\n\r\n" +
"\tThe enemy has sunken all of your ships" +
"\r\n\r\n" +
"====================================================================================\r\n" +
"\r\n\r\n"
);
} else {
System.out.println(
"====================================================================================\r\n" +
"\r\n\r\n" +
"\tVictory" +
"\r\n\r\n\r\n" +
"\tYou have sunken all enemy ships" +
"\r\n\r\n\r\n" +
"====================================================================================\r\n" +
"\r\n\r\n"
);
}
}
void addscore() {
score++;
}
public static void main(String[] args) {
GameBoard player = new GameBoard();
player.generateBoats(2);
player.printBoard();
}
}