Skip to content

Commit

Permalink
New IA
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Klaus committed Sep 12, 2019
1 parent 6193042 commit 462561d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/components/BattleBoard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import PlayBoard from "./PlayBoard.vue";
import { generateRandomBoard } from "../services/board-helper.js";
import { shoot } from "../services/play-helper.js";
import { findTargetCell } from "../services/ia-helper.js";
import { findTargetCellV2 } from "../services/ia-helper.js";
import Vue from "vue";
Expand Down Expand Up @@ -59,7 +59,7 @@ export default {
this.humanCanPlay = false;
await new Promise(resolve => setTimeout(resolve, 500));
const playerTargetCell = findTargetCell(this.playerCellsBoard);
const playerTargetCell = findTargetCellV2(this.playerCellsBoard);
shoot(playerTargetCell, this.playerCellsBoard, this.playerBoats);
this.humanCanPlay = true;
Expand Down
10 changes: 5 additions & 5 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Vue from 'vue'
import App from './App.vue'
import Vue from "vue";
import App from "./App.vue";

Vue.config.productionTip = false
Vue.config.productionTip = false;

new Vue({
render: h => h(App),
}).$mount('#app')
render: h => h(App)
}).$mount("#app");
58 changes: 57 additions & 1 deletion src/services/ia-helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ITERATION_LIMIT, getCell, BOARD_SIZE } from "./board-helper";
import {
ITERATION_LIMIT,
getCell,
BOARD_SIZE,
getNeighbors,
getCellIndexes
} from "./board-helper";

export function findTargetCell(boardCells) {
let cell = "";
Expand All @@ -13,3 +19,53 @@ export function findTargetCell(boardCells) {
}
}
}

function getCommonDirection(cell1, cell2) {
const { row: row1 } = getCellIndexes(cell1);
const { row: row2 } = getCellIndexes(cell2);
if (row1 === row2) {
return ["left", "right"];
}
return ["up", "down"];
}

function findUnexploredNeighbors(cell, directions, boardCells) {
const { row, column } = getCellIndexes(cell);
const neighbors = Object.keys(getNeighbors(row, column))
.filter(key => directions.includes(key))
.map(key => getNeighbors(row, column)[key]);
const unexploredCells = neighbors.filter(cellAddress =>
["empty", "ship"].includes(boardCells[cellAddress].status)
);
return unexploredCells;
}

function nextTarget(candidates, boardCells) {
let filteredDirections = ["up", "down", "left", "right"];
if (candidates.length > 1) {
filteredDirections = getCommonDirection(candidates[0], candidates[1]);
}
for (let cell in candidates) {
const unexploredNeighbors = findUnexploredNeighbors(
candidates[cell],
filteredDirections,
boardCells
);
if (unexploredNeighbors.length !== 0) {
return unexploredNeighbors[0];
}
}
}

export function findTargetCellV2(boardCells) {
const candidates = Object.keys(boardCells).filter(
keyCell => boardCells[keyCell].status === "hit"
);
if (candidates.length === 0) {
const missed = Object.keys(boardCells).filter(
keyCell => !["hit", "sunk", "missed"].includes(boardCells[keyCell].status)
);
return missed[Math.floor(Math.random() * missed.length)];
}
return nextTarget(candidates, boardCells);
}

0 comments on commit 462561d

Please sign in to comment.