-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
executable file
·134 lines (125 loc) · 4.18 KB
/
Game.java
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
package battleships;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Scanner;
public class Game {
static Scanner scan = new Scanner(System.in);
public LinkedList<Player> players = new LinkedList<Player>();
public void runGame(int playernbr) throws IOException {
Utils.printEmpty();
System.out.println("Welcome to Battle Ships!");
System.out.println("========================");
System.out.println();
// Playernbr 1, launch 1 player and AI
if (playernbr == 1) {
newPlayer(playernbr);
newComputerPlayer(2);
AIplay();
}
// Playernbr 2, launch 2 players, no AI
else if (playernbr == 2) {
newPlayer(playernbr);
play();
}
}
public void newPlayer(int playernbr) {
for (int i = 1; i <= playernbr; i++) {
boolean inputCorrect = true;
String name = null;
int hitrate = 0;
int damagerate = 0;
int id = 0;
int health = 0;
Player player = new Player(name, health, id, hitrate, damagerate);
players.add(player);
System.out.println("Player " + i + ", please enter your name: ");
name = scan.next();
player.setName(name);
player.setID(i);
System.out.println();
Utils.printEmpty();
System.out.println("Welcome " + player.getName() + "!");
System.out.println("Would you like to place your own ships?");
System.out.println("1. Yes");
System.out.println("2. No");
while (inputCorrect) {
try {
int choice = scan.nextInt();
while (choice < 1 || choice > 2) {
System.out.println("Incorrect input. Try again.");
choice = scan.nextInt();
}
switch (choice) {
case 1:
System.out.println(player.getName() + " get ready to place your ships.");
player.makeGrid(1);
Utils.printEmpty();
inputCorrect = false;
break;
case 2:
System.out.println(player.getName() + " the computer is placing your ships...");
player.makeGrid(2);
System.out.println("Your grid with ships placed!");
player.printPlayerGrid();
Utils.pressAnyKeyToContinue();
Utils.printEmpty();
inputCorrect = false;
break;
}
} catch (InputMismatchException e) {
System.out.println("Incorrect input. Only use numbers. Try again.");
System.out.println();
scan.nextLine();
}
}
}
System.out.println();
}
public void newComputerPlayer(int playernbr) {
String name = "Mr Robot";
int hitrate = 0;
int damagerate = 0;
int health = 0;
Player player = new Player(name, health, playernbr, hitrate, damagerate);
players.add(player);
player.makeGrid(2);
}
public void play() throws IOException {
int i = 0;
int j = 1;
Player player = null;
Highscore list = new Highscore();
while (players.get(0).getHealth() > 0 && players.get(1).getHealth() > 0) {
players.get(i).turnToPlay(players.get(j));
players.get(j).turnToPlay(players.get(i));
player = (players.get(0).getHealth() < players.get(1).getHealth()) ? players.get(1) : players.get(0);
}
System.out.println(player.getName() + " you have sunk all ships, you win!");
System.out.println("Your time was " + player.getPlayerTime() + " seconds.");
if (player.getHitrate() >= list.checkHighscoreList((int) player.getHitrate(), (int) player.getShots(), (int) player.getPlayerTime())) {
System.out.println("Congratulations! You made the highscore list!");
list.savingHighscore((int) player.getHitrate(), player.getShots(), player.getName(),
(int) player.getPlayerTime());
}
System.out.println("Returning to main menu.");
System.out.println();
}
public void AIplay() {
int i = 0;
int j = 1;
Player player = null;
while (players.get(0).getHealth() > 0 && players.get(1).getHealth() > 0) {
players.get(i).turnToPlay(players.get(j));
players.get(j).ComputerTurnToPlay(players.get(i));
player = (players.get(0).getHealth() < players.get(1).getHealth()) ? players.get(1) : players.get(0);
}
if (players.get(j).getHealth() > 0)
System.out
.println(players.get(i).getName() + " you lost to the bot, " + player.getName() + "... Awkward...");
else
System.out.println(player.getName() + " you have sunk all ships, you win!");
System.out.println("Returning to main menu.");
System.out.println();
}
}