-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerBoard.java
More file actions
170 lines (158 loc) · 4.23 KB
/
PlayerBoard.java
File metadata and controls
170 lines (158 loc) · 4.23 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import java.util.*;
public class PlayerBoard extends ComputerBoard {
public static boolean randomize = false;
public void setUpBoard() {
Scanner console = new Scanner(System.in);
System.out.println();
System.out.println();
System.out.println("Welcome to BattleShip! Please set up your ships.");
System.out.println("If you would like the computer to randomize your board, press R now. Otherwise, press any other key.");
String input = console.next();
input = input.toLowerCase();
if(input.equals("r")) {
randomize = true;
}
generateBoard();
System.out.println();
System.out.println("Your board :");
printBoard();
}
public int getStartRow(int size) {
if(randomize) {
return super.getStartRow(size);
} else {
System.out.println("Your ship is of size "+ size);
System.out.println("Please enter the Y coordinate of its placement from 0 to 9.");
Scanner console = new Scanner(System.in);
String starting = console.next();
while (cont(starting)==false){
System.out.println("Input invalid. Please try again.");
starting = console.next();
}
int start = Integer.parseInt(starting);
return start;
}
}
public int getStartCol() {
if(randomize) {
return super.getStartCol();
} else {
printBoard();
System.out.println("Please enter the X coordinate of its placement from 0 to 9.");
Scanner console = new Scanner(System.in);
String starting = console.next();
while (cont(starting)==false){
System.out.println("Input invalid. Please try again.");
starting = console.next();
}
int start = Integer.parseInt(starting);
return start;
}
}
public int findValidDirection(int i, int j, int size) {
if(randomize) {
return super.findValidDirection(i, j, size);
} else {
System.out.println("Enter the direction of your ship : 1 for up, 2 for down, 3 for left, 4 for right");
Scanner console = new Scanner(System.in);
String starting = console.next();
while (cont(starting)==false){
System.out.println("Input invalid. Please try again.");
starting = console.next();
}
int dir = Integer.parseInt(starting);
while (dir < 1 || dir > 4){
System.out.println("Input invalid. Please try again.");
dir = console.nextInt();
}
switch (dir) {
case 1:
if(i - size >= 0 && checkUnoccupied(i, j, size, dir)) {
return dir;
}
System.out.println("Boat does not fit. Please try again.");
return -1;
case 2:
if(i + size >= 0 && checkUnoccupied(i, j, size, dir)) {
return dir;
}
System.out.println("Boat does not fit. Please try again.");
return -1;
case 3:
if(j - size >= 0 && checkUnoccupied(i, j, size, dir)) {
return dir;
}
System.out.println("Boat does not fit. Please try again.");
return -1;
case 4:
if(j + size >= 0 && checkUnoccupied(i, j, size, dir)) {
return dir;
}
System.out.println("Boat does not fit. Please try again.");
return -1;
default:
System.out.println("Boat does not fit. Please try again.");
return -1;
}
}
}
public void printBoard() {
System.out.println(" 0 1 2 3 4 5 6 7 8 9 X");
for(int i= 0; i < 10; i++) {
System.out.print(i + " ");
for(int j = 0; j < 10; j++) {
switch (board[i][j]) {
case 0:
System.out.print("- ");
break;
case 1:
System.out.print("O ");
break;
case 2:
System.out.print("X ");
break;
case 3:
System.out.print("* ");
break;
default:
break;
}
}
System.out.println();
}
System.out.println("Y");
}
public int checkHit(int i, int j) {
if(board[i][j] == 1) {
board[i][j] = 2;
return 0;
} else if (board[i][j] == 0) {
board[i][j] = 3;
return 1;
} else {
System.out.println("You already tried this spot! Pick again.");
return 2;
}
}
public boolean checkValidStart(int i, int j) {
if(board[i][j] == 0){
return true;
} else {
if (randomize == false){
System.out.println("Start point is already occupied. Please try again.");
}
return false;
}
}
public boolean cont(String x){
try {
int n = Integer.parseInt(x);
if(n < 0 || n > 9) {
return false;
}
return true;
} catch(NumberFormatException e) {
return false;
}
}
}