Skip to content

Instantly share code, notes, and snippets.

@stackola
Created September 12, 2019 11:06
Show Gist options
  • Save stackola/b969dbc2b910f4b2e0811baa21832e7c to your computer and use it in GitHub Desktop.
Save stackola/b969dbc2b910f4b2e0811baa21832e7c to your computer and use it in GitHub Desktop.

Revisions

  1. stackola renamed this gist Sep 12, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. stackola created this gist Sep 12, 2019.
    166 changes: 166 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,166 @@
    let assert = require("assert");

    let rngesus = (deck, enemyDeck, score, bonus) => {
    // rngesus ist immer Spieler 2
    switch (isMatchBall(score, bonus)) {
    case "winning":
    if (bonus >= 1) {
    return deck.sort((a, b) => b - a)[0];
    } else {
    return deck.sort((a, b) => a - b)[0];
    }
    case "losing":
    return deck.sort((a, b) => b - a)[0];
    default:
    if (bonus >= 1) {
    return deck.sort((a, b) => b - a)[0];
    }
    return neverGonnaGiveYouUp(deck, enemyDeck, score);
    }
    return 1;
    };
    function neverGonnaGiveYouUp(deck, enemyDeck, score) {
    switch (true) {
    case score[2] - score[1] > 0:
    return deck.sort((a, b) => a - b)[0];
    default:
    return pickBestCard(deck.sort((a, b) => a - b), deckMedian(enemyDeck));
    }
    }
    function pickBestCard(sortedDeck, median) {
    for (i in sortedDeck) {
    if (sortedDeck[i] > median) {
    return sortedDeck[i];
    }
    }
    return sortedDeck[sortedDeck.length - 1];
    }
    function isMatchBall(score, bonus) {
    switch (true) {
    case score[1] + bonus >= 6:
    return "losing";
    case score[2] + bonus >= 6:
    return "winning";
    default:
    return;
    }
    }
    function deckMedian(deck) {
    var absolute = 0;
    for (i in deck) {
    absolute += deck[i];
    }
    return absolute / deck.length;
    }

    let sneak = (deck, enemyDeck, score, bonus) => {
    if (bonus > 0) {
    return deck.sort((a, b) => b - a)[0];
    }
    //plays a random card
    return deck[Math.floor(Math.random() * deck.length)];
    };

    let rnd = (deck, enemyDeck) => {
    //plays a random card
    return deck[Math.floor(Math.random() * deck.length)];
    };

    let lowest = (deck, enemyDeck, score) => {
    //plays the lowest card in own deck
    return deck.sort((a, b) => a - b)[0];
    };

    let highest = (deck, enemyDeck, score) => {
    //plays the lowest card in own deck
    return deck.sort((a, b) => b - a)[0];
    };

    class Game {
    constructor(s1, s2, gameLength) {
    this.s1 = s1;
    this.s2 = s2;
    this.gameLength = gameLength;
    this.reset();
    }
    reset() {
    this.score = { 1: 0, 2: 0 };
    this.bonus = 0;
    this.decks = {
    1: Array.from(Array(this.gameLength), (x, index) => index + 1),
    2: Array.from(Array(this.gameLength), (x, index) => index + 1)
    };
    }
    runGame() {
    this.reset();
    for (let i = 0; i < this.gameLength; i++) {
    this.runRound();
    }

    if (this.score[1] > this.score[2]) {
    return 1;
    }
    if (this.score[1] < this.score[2]) {
    return 2;
    }
    return 0;
    }

    runRound() {
    let cards = {
    1: this.s1(this.decks[1], this.decks[2], this.score, this.bonus),
    2: this.s2(this.decks[2], this.decks[1], this.score, this.bonus)
    };

    assert(this.decks[1].includes(cards[1]));
    assert(this.decks[2].includes(cards[2]));

    this.decks[1] = this.decks[1].filter(c => c !== cards[1]);
    this.decks[2] = this.decks[2].filter(c => c !== cards[2]);
    if (cards[1] > cards[2]) {
    //p1 wins.
    this.score[1] += 1;
    this.score[1] += this.bonus;
    this.bonus = 0;
    }
    if (cards[1] < cards[2]) {
    this.score[2] += 1;
    this.score[2] += this.bonus;
    this.bonus = 0;
    }
    if (cards[1] == cards[2]) {
    //increment bonus.
    this.bonus++;
    }
    }
    }

    let globScore = { 0: 0, 1: 0, 2: 0 };
    let gameCount = 100000000;
    let updateEvery = 1000;
    let cardsInHand = 11;

    console.clear();
    //Use strategies here

    // MATCH UPS:
    let g = new Game(sneak, rngesus, cardsInHand);
    //let g = new Game(sneak, rnd, cardsInHand);
    //let g = new Game(rnd, rngesus, cardsInHand);

    for (let i = 0; i < gameCount; i++) {
    let t = g.runGame();
    globScore[t]++;
    if ((i + 1) % updateEvery == 0) {
    console.clear();
    console.log(globScore);
    console.log("After " + (i + 1) + " games");
    console.log("Player 1 wins " + (globScore[1] / (i + 1)) * 100 + "%");
    console.log("Player 2 wins " + (globScore[2] / (i + 1)) * 100 + "%");
    }
    }
    console.clear();
    console.log(globScore);
    console.log("After " + gameCount + " games");
    console.log("Player 1 wins " + (globScore[1] / gameCount) * 100 + "%");
    console.log("Player 2 wins " + (globScore[2] / gameCount) * 100 + "%");