Skip to content

Commit 0dd3748

Browse files
committed
add list of boats alive or dead
1 parent 462561d commit 0dd3748

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

src/components/BattleBoard.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
<template>
22
<div class="battle-board">
33
<div class="boards">
4+
<Boats :boats="playerBoats" />
45
<PlayBoard :title="'Player'" :board="playerCellsBoard" :ships-visible="true" />
56
<PlayBoard :title="'IA'" :board="IACellsBoard" :ships-visible="false" @play="play" />
7+
<Boats :boats="IABoats" />
68
</div>
79
<button class="start-button" @click="startGame">Start Game</button>
810
</div>
911
</template>
1012

1113
<script>
1214
import PlayBoard from "./PlayBoard.vue";
15+
import Boats from "./Boats.vue";
1316
import { generateRandomBoard } from "../services/board-helper.js";
1417
import { shoot } from "../services/play-helper.js";
1518
import { findTargetCellV2 } from "../services/ia-helper.js";
@@ -19,12 +22,15 @@ import Vue from "vue";
1922
export default {
2023
name: "BattleBoard",
2124
components: {
22-
PlayBoard
25+
PlayBoard,
26+
Boats
2327
},
2428
data: function() {
2529
return {
2630
playerCellsBoard: {},
2731
IACellsBoard: {},
32+
IABoats: {},
33+
playerBoats: {},
2834
gameStarted: false,
2935
humanCanPlay: true
3036
};

src/components/Boats.vue

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<template>
2+
<div>
3+
<ol class="boats-liste">
4+
<li
5+
v-for="boat in boatsInfos"
6+
:key="boat.name"
7+
v-bind:class="{sunk: boat.isSunk}"
8+
>{{boat.name}} ({{boat.nbCells}})</li>
9+
</ol>
10+
</div>
11+
</template>
12+
13+
<script>
14+
export default {
15+
name: "Boats",
16+
props: {
17+
boats: {
18+
type: Object,
19+
required: true
20+
}
21+
},
22+
computed: {
23+
boatsInfos: function() {
24+
const x = Object.keys(this.boats)
25+
.map(name => ({
26+
name,
27+
nbCells: this.boats[name].cells.length,
28+
isSunk: this.boats[name].nbOfAliveCells === 0
29+
}))
30+
.sort((boat1, boat2) =>
31+
boat1.nbCells < boat2.nbCells ||
32+
(boat1.nbCells === boat2.nbCells && boat1.name < boat2.name)
33+
? 1
34+
: -1
35+
);
36+
return x;
37+
}
38+
}
39+
};
40+
</script>
41+
42+
<style lang="scss" scoped>
43+
.sunk {
44+
text-decoration: line-through;
45+
color: red;
46+
}
47+
.boats-liste {
48+
list-style: none;
49+
padding: 100% 0;
50+
margin: 0;
51+
font-size: 18px;
52+
font-weight: 500;
53+
}
54+
</style>

0 commit comments

Comments
 (0)