Skip to content

Commit

Permalink
fix bug which crashes game during chain fire
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdullah Jamal committed Mar 25, 2024
1 parent 8c48859 commit 2497aca
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 48 deletions.
126 changes: 81 additions & 45 deletions src/classes/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Bot extends Player {
target_locked_y: any[];
missed_target_x: any[];
missed_target_y: any[];
stack_x: any[];
stack_y: any[];
potential_targets_x: any[];
potential_targets_y: any[];
hitShipType: number;
constructor() {
super("p2", 2);
Expand All @@ -28,8 +28,8 @@ class Bot extends Player {
this.target_locked_y = [];
this.missed_target_x = [];
this.missed_target_y = [];
this.stack_x = [];
this.stack_y = [];
this.potential_targets_x = [];
this.potential_targets_y = [];
}
getBotFieldsToSave(){
return {
Expand All @@ -41,8 +41,8 @@ class Bot extends Player {
grid: this.grid,
missed_target_x: this.missed_target_x,
missed_target_y: this.missed_target_y,
stack_x: this.stack_x,
stack_y: this.stack_y,
potential_targets_x: this.potential_targets_x,
potential_targets_y: this.potential_targets_y,
hitShipType: this.hitShipType,
gridHidden: this.gridHidden,
gridActual: this.gridActual,
Expand Down Expand Up @@ -71,8 +71,8 @@ class Bot extends Player {
this.grid = data.grid;
this.missed_target_x = data.missed_target_x;
this.missed_target_y = data.missed_target_y;
this.stack_x = data.stack_x;
this.stack_y = data.stack_y;
this.potential_targets_x = data.potential_targets_x;
this.potential_targets_y = data.potential_targets_y;
this.hitShipType = data.hitShipType;
this.gridHidden = data.gridHidden;
this.gridActual = data.gridActual;
Expand Down Expand Up @@ -266,11 +266,8 @@ class Bot extends Player {
}
}
}

while (this.stack_x.length > 0) {
this.stack_x.pop();
this.stack_y.pop();
}
this.potential_targets_x = [];
this.potential_targets_y = [];
};
calcProbabilityDensity() {
let i, j, k;
Expand Down Expand Up @@ -358,27 +355,34 @@ class Bot extends Player {
if (this.checkShipLifeStatus()) {
return true;
}

if (this.missed_target_x.length > 0 && !this.chainFire) {
this.chainFire = true;
let tempX = this.missed_target_x.pop();
let tempY = this.missed_target_y.pop();

// give lock high probability
this.grid[tempX][tempY] = this.grid[tempX][tempY] + 20;
this.target_locked_x.push(tempX);
this.target_locked_y.push(tempY);
this.hitShipType = this.gridActual[tempX][tempY];
}



this.calcProbabilityDensity();

// if (this.missed_target_x.length > 0 && !this.chainFire) {
// this.chainFire = true;
// let tempX = this.missed_target_x.pop();
// let tempY = this.missed_target_y.pop();

// // give lock high probability
// this.grid[tempX][tempY] = this.grid[tempX][tempY] + 20;
// this.target_locked_x.push(tempX);
// this.target_locked_y.push(tempY);
// this.hitShipType = this.gridActual[tempX][tempY];
// }

let max = this.maxProbability();

while (this.stack_x.length > 0) {
this.stack_x.pop();
this.stack_y.pop();
while (this.potential_targets_x.length > 0) {
this.potential_targets_x.pop();
this.potential_targets_y.pop();
}

console.log(this.grid);
console.log("potential targets", this.potential_targets_x, this.potential_targets_y);
console.log("targets locked", this.target_locked_x, this.target_locked_y);
console.log("missed_target", this.missed_target_x, this.missed_target_y);
console.log("gridActual", this.gridActual);

for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
Expand All @@ -388,23 +392,23 @@ class Bot extends Player {
this.target_locked_x[0] === i ||
this.target_locked_y[0] === j
) {
this.stack_x.push(i);
this.stack_y.push(j);
this.potential_targets_x.push(i);
this.potential_targets_y.push(j);
}
}
}
}

// selects target randomly from highest density block

let randomNumber = Math.floor(p5.random(0, this.stack_x.length));
let randomNumber = Math.floor(p5.random(0, this.potential_targets_x.length));

const botHitX = this.stack_x[randomNumber];
const botHitY = this.stack_y[randomNumber];
const botHitX = this.potential_targets_x[randomNumber];
const botHitY = this.potential_targets_y[randomNumber];

while (this.stack_x.length > 0) {
this.stack_x.pop();
this.stack_y.pop();
while (this.potential_targets_x.length > 0) {
this.potential_targets_x.pop();
this.potential_targets_y.pop();
}

// if shot missed execute this
Expand Down Expand Up @@ -451,14 +455,46 @@ class Bot extends Player {

// if ship sinked execute this else
else {
while (this.target_locked_x.length > 0) {
this.target_locked_x.pop();
this.target_locked_y.pop();
if(this.missed_target_x.length === 0){

this.chainFire = false;
// TODO: uncomment
while (this.target_locked_x.length > 0) {
this.target_locked_x.pop();
this.target_locked_y.pop();
}

this.smallSize = 0;
this.hitShipType = 0;

}

this.smallSize = 0;
this.hitShipType = 0;
this.chainFire = false;
else{
this.chainFire = true;

for(let i = this.target_locked_x.length - 1; i >= 0; i--){
if(this.gridActual[this.target_locked_x[i]][this.target_locked_y[i]] === this.hitShipType){
let removedX = this.target_locked_x.splice(i, 1);
let removedY = this.target_locked_y.splice(i, 1);
console.log("removed", removedX, removedY , "on ship sink of ", this.hitShipType);
}
}

let tempX = this.missed_target_x.pop();
let tempY = this.missed_target_y.pop();
this.hitShipType = this.gridActual[tempX][tempY];
this.target_locked_x.push(tempX);
this.target_locked_y.push(tempY);
// push all shipTypes of a particular pop into target locked
for(let i = this.missed_target_x.length - 1; i >= 0; i--){
if(this.gridActual[this.missed_target_x[i]][this.missed_target_y[i]] === this.hitShipType){
this.target_locked_x.push(this.missed_target_x[i]);
this.target_locked_y.push(this.missed_target_y[i]);
this.missed_target_x.splice(i, 1);
this.missed_target_y.splice(i, 1);
}
}
}

}
}

Expand Down
39 changes: 36 additions & 3 deletions src/classes/Player.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {p5, unsetMouseIsPressed} from '../index';
import { GLOBAL_SCALE, RGB_THEME, ISLAND } from '../constants/constants';
import { alerts, randomMap, savePlayer1State, savePlayer2State, persistentGameState } from '../setup/sketch';
import { alerts, randomMap, savePlayer1State, savePlayer2State, persistentGameState, GameStateEnum } from '../setup/sketch';

class Player{
gridHidden: any[][];
Expand Down Expand Up @@ -144,6 +144,24 @@ class Player{
}
}
}
// this.shipDetails.forEach((ship, index) => {
// p5.fill(
// ship.color.r,
// ship.color.g,
// ship.color.b,
// );
// if(ship.begin.x === ship.end.x){
// console.log(ship.begin.x, ship.begin.y, ship.end.x, ship.end.y)
// p5.rect(indent + 90 + 35 * ship.begin.x, indent_y + 90 + 35 * ship.begin.y, 25, (ship.size >= 3 ? 16.3 : 15) * (ship.end.y - ship.begin.y + ship.size), 15, 15, 15, 15);
// // p5.ellipse(indent + 100 + 35 * ship.begin.x, indent_y + 120 + 35 * ship.begin.y, 35 * (ship.end.x - ship.begin.x), 35);
// }
// else{
// console.log(ship.begin.x, ship.begin.y, ship.end.x, ship.end.y)
// p5.rect(indent + 90 + 35 * ship.begin.x, indent_y + 90 + 35 * ship.begin.y, (ship.size >= 3 ? 16.3 : 15) * (ship.end.x - ship.begin.x + ship.size), 25, 15, 15, 15, 15);

// // p5.ellipse(indent + 100 + 35 * ship.begin.x, indent_y + 120 + 35 * ship.begin.y, 35, 35 * (ship.end.y - ship.begin.y));
// }
// })
};
drawGridHidden() {
let i = 1,
Expand Down Expand Up @@ -229,10 +247,25 @@ class Player{
// missed inside block
else if (this.gridHidden[i - 1][j - 1] === -1) {
p5.fill(RGB_THEME.SHIP_MISS);
p5.ellipse(indent + 67.5 + 35 * i, indent_y + 97.5 + 35 * j, 25, 25);
p5.rect(indent + 50 + 35 * i, indent_y + 80 + 35 * j, 35, 35);
// p5.ellipse(indent + 67.5 + 35 * i, indent_y + 97.5 + 35 * j, 25, 25);
}
else{
if (this.gridActual[i - 1][j - 1] > 0
&& this.playerIs === 2
&& persistentGameState.currentState === GameStateEnum.SinglePlayer) {
p5.fill(
this.shipDetails[this.gridActual[i - 1][j - 1] - 1].color.r,
this.shipDetails[this.gridActual[i - 1][j - 1] - 1].color.g,
this.shipDetails[this.gridActual[i - 1][j - 1] - 1].color.b,
75
);
p5.ellipse(indent + 67.5 + 35 * i, indent_y + 97.5 + 35 * j, 25, 25);
}
}
}
}

return 0;
};
arrangeShip() {
Expand Down Expand Up @@ -303,7 +336,7 @@ class Player{
if (!shipOverlapped) {
// updates ships begin coordinate which will be sent to database
this.shipDetails[size - 1].begin.x = a;
this.shipDetails[size - 1].begin.x = b;
this.shipDetails[size - 1].begin.y = b;

for (i = 0; i < num; i++) {
this.gridActual[a + i][b] = size; // vertical arrangement by random
Expand Down
2 changes: 2 additions & 0 deletions src/setup/sketch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,7 @@ function initPlayers() {
}

loadGameState();
// persistentGameState.isDensityLensEnabled = true;


export {persistentGameState, GameStateEnum, alerts, initializeRandomMap, updateCurrentGameState, getCurrentGameState, randomMap, statTable, players, initPlayers, saveGameState, saveBotState, savePlayer1State, savePlayer2State};

0 comments on commit 2497aca

Please sign in to comment.