-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
145 lines (125 loc) · 3.37 KB
/
Main.java
File metadata and controls
145 lines (125 loc) · 3.37 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
import redteam.*;
import blueteam.*;
import esi18.*;
import battleship.core.*;
import java.util.*;
/*
* Duo Battle: Red Team vs Blue Team
* Win the 2v2 battle against the other team.
*/
public class Main extends Game {
public static void main(String[] args) {
int seed = 42;
if (args.length >= 1) {
seed = Integer.parseInt(args[0]);
}
Game mission = Main.launch(seed);
mission.run();
}
public static Game launch(int seed) {
Game mission = new Main();
mission.setSeed(seed);
mission.setArenaFile("./files/arena.txt");
mission.setTurnFile("./files/turns.txt");
mission.setLogFile("./files/logs.txt");
return mission;
}
public Main() {
Arena arena = initializeArena();
setArena(arena);
}
@Override
public String getObjective() {
return "Mission Objective: Win the 2v2 battle against the other team.";
}
private List<Ship> redTeam = new ArrayList<Ship>();
private List<Ship> blueTeam = new ArrayList<Ship>();
@Override
public Arena initializeArena() {
Arena arena = new Arena(10, 10);
// add ship and team here
// Ship ship1 = new [YourShipName]Ship.java
// setShipTeam(ship1, "Team Name");
// setShipColor(ship1, "#ffffff");
// spawnShip(arena, [x coordinate], [y coordinate], ship1);
Ship scarlet = new SimpleShip();
Ship crimson = new SimpleShip();
Ship azure = new AzureShip();
Ship teal = new TealShip();
setShipTeam(scarlet, "Red Team");
setShipColor(scarlet, "#ff291e");
spawnShip(arena, 0, 5, scarlet);
redTeam.add(scarlet);
setShipTeam(crimson, "Red Team");
setShipColor(crimson, "#dc143c");
spawnShip(arena, 0, 6, crimson);
redTeam.add(crimson);
setShipTeam(azure, "Blue Team");
setShipColor(azure, "#1c89ff");
spawnShip(arena, 9, 5, azure);
blueTeam.add(azure);
setShipTeam(teal, "Blue Team");
setShipColor(teal, "#149eba");
spawnShip(arena, 9, 6, teal);
blueTeam.add(teal);
return arena;
}
private int countSunkShips(List<Ship> team) {
int sunkCount = 0;
for (Ship ship : team) {
if (ship.isSunk()) {
sunkCount++;
}
}
return sunkCount;
}
private boolean isTeamSunk(List<Ship> team) {
int sunkCount = countSunkShips(team);
boolean allSunk = sunkCount == team.size();
return allSunk;
}
@Override
public boolean isCompleted() {
boolean redSunk = isTeamSunk(redTeam);
boolean blueSunk = isTeamSunk(blueTeam);
return redSunk || blueSunk;
}
@Override
public String getResults() {
List<Ship> sunk = new ArrayList<Ship>();
List<Ship> allShips = new ArrayList<Ship>(redTeam);
allShips.addAll(blueTeam);
for (Ship ship : allShips) {
if (ship.isSunk()) {
sunk.add(ship);
}
}
// Score = number of enemy ships sunk
int redScore = countSunkShips(blueTeam);
int blueScore = countSunkShips(redTeam);
String res = "";
if (redScore == blueScore) {
res += "Battle was a draw.";
} else if (redScore > blueScore) {
res += "Red Team wins.";
} else {
res += "Blue Team wins.";
}
res += String.format(" Ships Sunk: Red (%d) - Blue (%d)", redScore, blueScore);
for (Ship ship : sunk) {
res += "\n";
res += "- " + ship.getName() + " sunk by " + ship.getSunkBy().getName() + ".";
}
boolean isDraw = redScore == blueScore;
// if (!isDraw) {
// Helper.writeFileLine("./OUTPUT", "100");
// } else {
// Helper.writeFileLine("./OUTPUT", "0");
// }
return res;
}
@Override
public void run() {
this.runMission(getArena());
}
}