-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputer.java
More file actions
34 lines (29 loc) · 834 Bytes
/
Computer.java
File metadata and controls
34 lines (29 loc) · 834 Bytes
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
import java.util.*;
public class Computer {
public Computer (Board opponent) {
opponentBoard = opponent;
}
public static Board opponentBoard;
public void makeGuess() {
Random r = new Random();
int guessRow = r.nextInt(10);
int guessCol = r.nextInt(10);
int guessRes = opponentBoard.checkHit(guessRow, guessCol);
while(guessRes == 2) { // already guessed
guessRow = r.nextInt(10);
guessCol = r.nextInt(10);
guessRes = opponentBoard.checkHit(guessRow, guessCol);
}
System.out.println("The computer guessed (" + guessCol + ", " + guessRow + ")");
opponentBoard.printBoard();
if (guessRes == 1){
System.out.println("Computer's shot missed.");
}
if (guessRes == 0){
System.out.println("Computer's shot hits.");
}
}
public boolean isWinner() {
return opponentBoard.allShipsSunk();
}
}