Skip to content

Commit 38b2aa6

Browse files
committed
step 8
1 parent 8dd8365 commit 38b2aa6

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/components/BattleBoard.vue

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
<script>
2626
import PlayBoard from "./PlayBoard.vue";
2727
import { generateRandomAssets } from "../services/board-helper.js";
28+
import { shoot } from "../services/play-helper.js";
29+
import { findTargetCell } from "../services/ia-helper.js";
2830
2931
export default {
3032
name: "BattleBoard",
@@ -42,19 +44,42 @@ export default {
4244
boats: {},
4345
},
4446
gameStarted: false,
47+
humanCanPlay: false,
4548
};
4649
},
4750
methods: {
4851
startGame() {
4952
this.setAssets(this.playerAssets);
5053
this.setAssets(this.IAAssets);
5154
this.gameStarted = true;
55+
this.humanCanPlay = true;
5256
},
5357
setAssets(target) {
5458
const targetRandomAssets = generateRandomAssets();
5559
target["boardCells"] = targetRandomAssets.boardCells;
5660
target["boats"] = targetRandomAssets.boats;
5761
},
62+
async play(cell) {
63+
if (this.gameStarted && this.humanCanPlay) {
64+
const isHumanShotAccepted = shoot(
65+
cell,
66+
this.IAAssets.boardCells,
67+
this.IAAssets.boats,
68+
);
69+
70+
if (isHumanShotAccepted) {
71+
this.humanCanPlay = false;
72+
await new Promise((resolve) => setTimeout(resolve, 500));
73+
const playerTargetCell = findTargetCell(this.playerAssets.boardCells);
74+
shoot(
75+
playerTargetCell,
76+
this.playerAssets.boardCells,
77+
this.playerAssets.boats,
78+
);
79+
this.humanCanPlay = true;
80+
}
81+
}
82+
},
5883
},
5984
};
6085
</script>

src/components/PlayBoard.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
:key="''.concat(column).concat('-'.concat(row))"
99
class="cell"
1010
:class="[getCellStatus(row, column), { hidden: !shouldDisplayShips }]"
11+
@click="$emit('play', getCell(row, column))"
1112
></div>
1213
</div>
1314
</div>

0 commit comments

Comments
 (0)