Skip to content
Permalink
Browse files

add AI wins counter

  • Loading branch information...
eAlasdair committed Jul 22, 2019
1 parent 99ba856 commit 9415e80a83a42b939f544a29d62bb86829b6348b
@@ -63,8 +63,6 @@ class AI {
this.map['1'] = [1, 100, 0, 0];
this.smartMap['2'] = [2, 0, 100, 0];
this.smartMap['1'] = [1, 100, 0, 0];

console.log(JSON.stringify(this.smartMap));
}

newGame() {
@@ -77,14 +75,13 @@ class AI {
* its neural net and records the decision.
*/
takeTurn() {
console.log('AI taking turn: ', this.sticksLeft);

// Random pick from weighted map of choices
var num = this.chooseNum(this.map[this.sticksLeft.toString()]);

if (num > this.sticksLeft) {
num = this.sticksLeft;
console.log('ERROR: Impossible move chosen, changed');
console.log('ERROR: Impossible AI move chosen, changed');
}

var id = this.sticksLeft.toString();
@@ -96,14 +93,13 @@ class AI {
* Simulates the practice bot taking it's turn, returns the number of sticks to remove
*/
takeBotTurn() {
console.log('Practice bot taking turn: ', this.sticksLeft);

// Random pick from weighted map of choices
var num = this.chooseNum(this.smartMap[this.sticksLeft.toString()]);

if (num > this.sticksLeft) {
num = this.sticksLeft;
console.log('ERROR: Impossible move chosen, changed');
console.log('ERROR: Impossible BOT move chosen, changed');
}

return num;
@@ -16,11 +16,13 @@ var networkTable;
var sticksGrid;
var ai;
var gamesPlayed;
var aiWins;
var isSimulation;

var $dataTable = $('#data-table');
var $sticksArea = $('#sticks-area');
var $remainingText = $('#remaining-sticks');
var $aiWinsText = $('#ai-wins');
var $playedText = $('#games-played');
var $statusText = $('#status-text');
var $splashText = $('#splash-text');
@@ -108,10 +110,13 @@ function reset() {
$('#sensitivity-select').val('5');
$statusText.html(TXT_INITIAL);
gamesPlayed = 0;
aiWins = 0;
networkTable = new TABLE.HtmlTable($dataTable);
sticksGrid = new IMG_GRID.ImageGrid($sticksArea, stickPath, 7);
refresh();
hideQuitButtons();
hideEndButtons();
hideChoiceButtons();
$('#game-parameters').removeClass('d-none');
}

@@ -144,6 +149,7 @@ function simulate(num) {
hideChoiceButtons();
hideEndButtons();
showCancelButton();
console.log('Running ' + num + ' simulations...');
recursiveSim(num);
}

@@ -171,6 +177,7 @@ function endSimulation() {
$splashText.addClass('d-none');
isSimulation = false;
console.log('simulation ended');
enableQuitButtons();
if (isStart) {
isStart = false;
showQuitButtons();
@@ -190,10 +197,11 @@ function getParameters() {
}

/**
* Displays base game variables: Number of sticks remaining and Number of games played
* Displays base game variables: number of sticks remaining, number of wins, and number of games played
*/
function displayBaseVariables() {
$remainingText.html(remainingSticks);
$aiWinsText.html(aiWins);
$playedText.html(gamesPlayed);
}

@@ -209,6 +217,7 @@ function disableChoiceButtons() {

/**
* Enables controls for the user to choose a number of sticks to remove.
* Does not affect visibility
*/
function enableChoiceButtons() {
$('#button_1').prop('disabled', false);
@@ -237,6 +246,20 @@ function hideEndButtons() {
$('#end-buttons').addClass('d-none');
}

/**
* Disables the quit button, does not affect its visibility
*/
function disableQuitButtons() {
$('#button_quit').prop('disabled', true);
}

/**
* Enables the quit button, does not affect its visibility
*/
function enableQuitButtons() {
$('#button_quit').prop('disabled', false);
}

/**
* Shows controls for the user to choose to simulate matches or have a rematch
*/
@@ -281,6 +304,7 @@ function takeAiTurn() {
endGame(isSimulation? PLAYERS.INTELLIBOT : PLAYERS.HUMAN);
} else {
disableChoiceButtons();
disableQuitButtons();
$statusText.html(TXT_WAIT);
var num = ai.takeTurn();
if (!isSimulation) {
@@ -342,6 +366,7 @@ function readyPlayerTurn() {
var num = ai.takeBotTurn();
applyMove(PLAYERS.INTELLIBOT, num);
} else {
enableQuitButtons();
enableChoiceButtons();
}
}
@@ -353,6 +378,7 @@ function endGame(winner) {
gamesPlayed++;
displayBaseVariables();
if (winner == PLAYERS.AI) {
aiWins++;
$statusText.html(TXT_LOSS);
ai.updateAI(false);
networkTable.recolourCells(TABLE.HIGHLIGHTS.WIN);
@@ -19,8 +19,9 @@ <h5>{% blocktrans %}In this game, take turns with the AI (Nathaniel) picking 1,
<div class="row black-background">
<div id="game-area" class="col-7">
<div class="row">
<div class="col-6">{% trans 'Sticks remaining:' %} <span id="remaining-sticks"></span></div>
<div class="col-6">{% trans 'Games played:' %} <span id="games-played"></span></div>
<div class="col-4">{% trans 'Sticks remaining:' %} <span id="remaining-sticks"></span></div>
<div class="col-4">{% trans 'AI wins:' %} <span id="ai-wins"></span></div>
<div class="col-4">{% trans 'Games played:' %} <span id="games-played"></span></div>
</div>
<div id="sticks-area" class="row text-left"></div>
<div id="status-text" class="col-12 text-center"></div>

0 comments on commit 9415e80

Please sign in to comment.
You can’t perform that action at this time.