-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextInterface.java
More file actions
75 lines (73 loc) · 2.53 KB
/
Copy pathTextInterface.java
File metadata and controls
75 lines (73 loc) · 2.53 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
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class TextInterface {
enum Player {USER, COMPUTER}
public static void main(String[] Args) throws IOException{
Game game = new Game();
Scanner input = new Scanner(System.in);
Random rand = new Random();
Player player;
if (rand.nextBoolean())
player = Player.USER;
else
player = Player.COMPUTER;
System.out.print("Welcome to Battleship!\n\n");
if (player == Player.USER)
System.out.println("You won the coin toss and get to go first.");
else
System.out.println("The computer won the coin toss and gets to go first.");
while (!game.computerDefeated() && !game.playerDefeated()){
if (player == Player.USER){
System.out.print("Your turn: ");
String entry = input.nextLine().toUpperCase();
//Validate user input. The user must enter a letter A-J followed by a
//number 1-10. The loop repeats until the user enters a valid value.
while (entry.length() < 2 || entry.length() >3 ||
(entry.charAt(0) < 'A' || entry.charAt(0) > 'J') ||
(Integer.parseInt(entry.substring(1)) < 1 ||
Integer.parseInt(entry.substring(1)) > 10))
{
System.out.print("Invalid move, try again: ");
entry = input.nextLine().toUpperCase();
}
String sunk = game.makePlayerMove(entry);
if (sunk != null)
{
System.out.println("The Computer says: \"" + sunk + "\"");
}
System.out.println(game);
//The user has completed their turn, so we change the value of player
//so that the next iteration of this loop will process a move by the
//computer.
player = Player.COMPUTER;
}
else
{
System.out.println("Computer's turn. Press any key to continue.");
input.nextLine();
//We call a Game function which returns a computer move. This allows us
//to print that move for the user to see.
String[] result = game.makeComputerMove();
System.out.println("Computer Chose : " + result[0]);
if (result[1] != null)
{
System.out.println("You spontaneously exclaim: \"" + result[1] +
"\"");
}
System.out.println(game);
//The computer has completed their turn, so we change the value of player
//so that the next iteration of
//this loop will process a move by the computer.
player = Player.USER;
}
}
System.out.println("Game Over!");
if (player == Player.USER)
System.out.println("The Computer wins!");
else
{
System.out.println("You defeated the Computer. That was easy.");
}
}
}