Skip to content

Commit

Permalink
Add setting initial positions
Browse files Browse the repository at this point in the history
  • Loading branch information
mhawryluk committed Jan 8, 2024
1 parent fd3c9bf commit 895b8ce
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
8 changes: 3 additions & 5 deletions js/computerOpponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ class MonteCarloOpponent extends ComputerOpponent {

if (otherTilesFoundIndices.length === 1) {
if (player === this.player) {
score += 100;
score += 100 * (this.MOVES_PER_GAME - depth);
} else {
score -= 100;
score -= 100 * (this.MOVES_PER_GAME - depth);
}
}
}
Expand Down Expand Up @@ -140,9 +140,7 @@ class MonteCarloOpponent extends ComputerOpponent {
const newTile = playerPaths[player][newPosition];

if (safeTiles.findIndex(safeTile => arraysEqual(safeTile, newTile)) !== -1) {
if (player === this.player) {
score = 10;
}

} else {
for (let otherPlayer of this.game.players) {
if (otherPlayer === nextPlayer) continue;
Expand Down
42 changes: 32 additions & 10 deletions js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ let game;

class Game {

constructor(players, computerOpponentLevels = []) {
constructor(players, computerOpponentLevels = [], tokenPositons = undefined) {
this.players = players;
this.numberOfPlayers = players.length;
this.currentPlayerIndex = 0;
this.currentPossibleMoves = [];
this.lastRolledValue = 0;

this.tokenPositons = {};
for (let player of this.players) {
this.tokenPositons[player] = [-6, -6, -6, -6];
if (tokenPositons === undefined) {
for (let player of this.players) {
this.tokenPositons[player] = [-6, -6, -6, -6];
}
} else {
this.tokenPositons = tokenPositons;
}

// get references to tokens and set their callbacks
Expand All @@ -38,7 +42,14 @@ class Game {
});

const startPosition = startPositions[player][i];
token.setAttribute('translation', `${startPosition[1]} 1 ${startPosition[0]}`);
const position = this.tokenPositons[player][i];
if (position === -6) {
token.setAttribute('translation', `${startPosition[1]} 1 ${startPosition[0]}`);
} else {
const tile = playerPaths[player][position];
token.setAttribute('translation', `${tile[1]} 1 ${tile[0]}`);
this.distributeTokensOnOneTile(tile);
}
}
}

Expand Down Expand Up @@ -241,7 +252,7 @@ class Game {

this.resetRolledValue();

}, movesCount * 250);
}, movesCount * 250 + 50);

this.clearPossibleMoves();

Expand Down Expand Up @@ -294,18 +305,24 @@ class Game {
for (let i = 0; i < 4; i++) {
const tokenTile = playerPaths[player][this.tokenPositons[player][i]];
if (!tokenTile) continue;
if (arraysEqual(tokenTile, tile)) allTokensOnTile.push(tokens[player][i]);
if (arraysEqual(tokenTile, tile)) {
allTokensOnTile.push(tokens[player][i]);
}
}
}

const tokenCount = allTokensOnTile.length;
const r = 0.35;

console.log(allTokensOnTile);

for (let i = 0; i < tokenCount; i++) {
const xOffset = tokenCount == 1 ? 0 : r * Math.cos(Math.PI / 180 * i * (360 / tokenCount));
const yOffset = tokenCount == 1 ? 0 : r * Math.sin(Math.PI / 180 * i * (360 / tokenCount));
if (allTokensOnTile[i]) {
const xOffset = tokenCount == 1 ? 0 : r * Math.cos(Math.PI / 180 * i * (360 / tokenCount));
const yOffset = tokenCount == 1 ? 0 : r * Math.sin(Math.PI / 180 * i * (360 / tokenCount));

allTokensOnTile[i].setAttribute('translation', `${tile[1] + yOffset} 1 ${tile[0] + xOffset}`);
allTokensOnTile[i].setAttribute('translation', `${tile[1] + yOffset} 1 ${tile[0] + xOffset}`);
}
}
}

Expand Down Expand Up @@ -349,7 +366,12 @@ window.addEventListener('keypress', event => {
});

function setPlayers(players, computerOpponentLevels) {
game = new Game(players, computerOpponentLevels);
game = new Game(players, computerOpponentLevels, {
red: [-6, -6, 5, 5],
yellow: [20, 20, 20, 20],
blue: [-6, 3, 3, 3],
green: [2, 3, 4, 54],
});

hideSetupPage();

Expand Down

0 comments on commit 895b8ce

Please sign in to comment.