File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed
Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change 2525<script >
2626import PlayBoard from " ./PlayBoard.vue" ;
2727import { generateRandomAssets } from " ../services/board-helper.js" ;
28+ import { shoot } from " ../services/play-helper.js" ;
29+ import { findTargetCell } from " ../services/ia-helper.js" ;
2830
2931export 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 >
Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments