-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattleship.java
More file actions
57 lines (55 loc) · 1.14 KB
/
Battleship.java
File metadata and controls
57 lines (55 loc) · 1.14 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
import java.util.*;
public class Battleship{
public static void main(String[] args) {
ComputerBoard cb = new ComputerBoard();
PlayerBoard pb = new PlayerBoard();
Computer c = new Computer(pb);
Player p = new Player(cb);
System.out.println("Let's see who goes first...");
Random r = new Random();
int res = r.nextInt(2);
if(res == 0) {
System.out.println("The computer will go first!");
c.makeGuess();
pause(2000);
p.makeGuess();
pause(1000);
} else {
System.out.println("You go first!");
System.out.println();
p.makeGuess();
pause(1000);
c.makeGuess();
pause(2000);
}
while(!p.isWinner() && !c.isWinner()) {
if(res == 0) {
c.makeGuess();
pause(2000);
p.makeGuess();
pause(1000);
} else {
p.makeGuess();
pause(1000);
c.makeGuess();
pause(2000);
}
}
pb.printBoard();
if (p.isWinner()){
System.out.println("Congratulations! You Win!");
} else {
System.out.println("Sorry. You Lose.");
}
}
public static void pause(int ms) {
try
{
Thread.sleep(ms);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
}