Skip to content

Instantly share code, notes, and snippets.

@deltastateonline
Last active December 19, 2019 00:34
Show Gist options
  • Save deltastateonline/08cc6fdf4f7e3c9d9a87f34b6fd234cc to your computer and use it in GitHub Desktop.
Save deltastateonline/08cc6fdf4f7e3c9d9a87f34b6fd234cc to your computer and use it in GitHub Desktop.
Solving Problems the Clojure Way - Rafal Dittwald
function runGame(){
let states = [{
turn : 0,
bCards : [1,2,3,4,5,6,7,8],
pCards : [
[1,2,3,4,5,6,7,8],
[1,2,3,4,5,6,7,8]
],
pScores : [0,0]
}];
while(last(states).bCards.length > 0){
states = states.concat(nextState(last(states)));
console.log(turnMessage(last(states)));
}
console.log(endMessage(last(states).pScores));
}
function selectRandom(arr){
const index = Math.floor(Math.random() * arr.length);
return arr[index];
}
function without(arr,value){
const index = arr.indexOf(value);
return [...arr.slice(0,index) , ...arr.slice(index+1)];
}
function pRStrategy(pCards_ , bCard){
const card = selectRandom(pCards_);
return card;
}
function pEStrategy(pCards_ , bCard){
return bCard;
}
function winMessage(pScores){
if(pScores[0] == pScores[1]){
return (`Players Tie`);
}else if (pScores[0] > pScores[1]){
return (`Player 0 wins`);
}else {
return (`Player 1 wins`);
}
}
function scoreMessage(pScores){
return (`Scores : ${pScores[0]} v ${pScores[1]}`);
}
function endMessage(pScores){
return scoreMessage(pScores) + "\n" + winMessage(pScores);
}
function newScores(pScores, pCards_){
if(pCards_[0] > pCards_[1]){
return [pScores[0]+1, pScores[1] ];
}else if(pCards_[1] > pCards_[0]){
return [pScores[0], pScores[1]+1 ];
}else{
return [pScores[0], pScores[1] ];
}
}
function nextState(state){
const bCard = selectRandom(state.bCards);
const c0 = pRStrategy(state.pCards[0],bCard);
const c1 = pEStrategy(state.pCards[1],bCard);
return {
lastBountyCard: bCard,
lastPlayedCard : [c0,c1],
turn : state.turn +1,
bCards : without(state.bCards,bCard ), // remove the bcard and return a new array
pCards : [
without(state.pCards[0],c0),
without(state.pCards[1],c1)
],
pScores: newScores(state.pScores, [c0,c1])
}
}
function turnMessage(state){
return `Turn ${state.turn} : Bounty : ${state.lastBountyCard}
\tPlayer 0 plays ${state.lastPlayedCard[0]}
\tPlayer 1 plays ${state.lastPlayedCard[1]}`;
}
function last(arr){
//console.log(arr);
return arr[arr.length -1];
}
runGame();
// uses functions, but now keeps a array of states
// add the new state of the game into the array and
function runGame(){
let states = [{
turn : 0,
lastBountyCard: 0,
lastPlayedCard : [0,0],
bCards : [1,2,3,4,5,6,7,8],
pCards : [
[1,2,3,4,5,6,7,8],
[1,2,3,4,5,6,7,8]
],
pScores : [0,0]
}];
while(last(states).bCards.length > 0){ // over each iteration, bCard gets shorter, until there is no card left
states = states.concat(nextState(last(states))); // with the input of the last state, return the next state and append to the states array
}
return states;
}
function selectRandom(arr){
const index = Math.floor(Math.random() * arr.length);
return arr[index];
}
function without(arr,value){
const index = arr.indexOf(value);
return [...arr.slice(0,index) , ...arr.slice(index+1)];
}
function pRStrategy(pCards_ , bCard){
const card = selectRandom(pCards_);
return card;
}
function pEStrategy(pCards_ , bCard){
return bCard;
}
function winMessage(pScores){
if(pScores[0] == pScores[1]){
return (`Players Tie`);
}else if (pScores[0] > pScores[1]){
return (`Player 0 wins`);
}else {
return (`Player 1 wins`);
}
}
function scoreMessage(pScores){
return (`Scores : ${pScores[0]} v ${pScores[1]}`);
}
function endMessage(pScores){
return scoreMessage(pScores) + "\n" + winMessage(pScores);
}
function endMessage(state){
return scoreMessage(state.pScores) + "\n" + winMessage(state.pScores);
}
function newScores(pScores, pCards_){
if(pCards_[0] > pCards_[1]){
return [pScores[0]+1, pScores[1] ];
}else if(pCards_[1] > pCards_[0]){
return [pScores[0], pScores[1]+1 ];
}else{
return [pScores[0], pScores[1] ];
}
}
function nextState(state){
const bCard = selectRandom(state.bCards);
const c0 = pRStrategy(state.pCards[0],bCard);
const c1 = pEStrategy(state.pCards[1],bCard);
return {
lastBountyCard: bCard,
lastPlayedCard : [c0,c1],
turn : state.turn +1,
bCards : without(state.bCards,bCard ), // remove the bcard and return a new array
pCards : [
without(state.pCards[0],c0),
without(state.pCards[1],c1)
],
pScores: newScores(state.pScores, [c0,c1])
}
}
function turnMessage(state){
return `Turn ${state.turn} : Bounty : ${state.lastBountyCard}
\tPlayer 0 plays ${state.lastPlayedCard[0]}
\tPlayer 1 plays ${state.lastPlayedCard[1]}`;
}
function report(states,onTurn, onEnd){
return states.map(onTurn).join("\n") + "\n" + onEnd(last(states));
}
function last(arr){
return arr[arr.length -1];
}
console.log(report(runGame(),turnMessage, endMessage));
// Using functions with return values which do not mutate the input variables
function runGame(){
let state = {
turn : 0,
bCards : [1,2,3,4,5,6,7,8],
pCards : [
[1,2,3,4,5,6,7,8],
[1,2,3,4,5,6,7,8]
],
pScores : [0,0]
}
while(state.bCards.length > 0){
state = nextState(state);
// create function
console.log(turnMessage(state));
}
console.log(endMessage(state.pScores));
}
// return a random item
function selectRandom(arr){
const index = Math.floor(Math.random() * arr.length);
return arr[index];
}
// return a new array without the specified item
function without(arr,value){
const index = arr.indexOf(value);
return [...arr.slice(0,index) , ...arr.slice(index+1)];
}
// return a random card
function pRStrategy(pCards_ , bCard){
const card = selectRandom(pCards_);
return card;
}
// just return the bCard
function pEStrategy(pCards_ , bCard){
return bCard;
}
function winMessage(pScores){
if(pScores[0] == pScores[1]){
return (`Players Tie`);
}else if (pScores[0] > pScores[1]){
return (`Player 0 wins`);
}else {
return (`Player 1 wins`);
}
}
function scoreMessage(pScores){
return (`Scores : ${pScores[0]} v ${pScores[1]}`);
}
function endMessage(pScores){
return scoreMessage(pScores) + "\n" + winMessage(pScores);
}
function newScores(pScores, pCards_){
if(pCards_[0] > pCards_[1]){
return [pScores[0]+1, pScores[1] ];
}else if(pCards_[1] > pCards_[0]){
return [pScores[0], pScores[1]+1 ];
}else{
return [pScores[0], pScores[1] ];
}
}
/**
* Create a new state and return
*/
function nextState(state){
const bCard = selectRandom(state.bCards);
const c0 = pRStrategy(state.pCards[0],bCard);
const c1 = pEStrategy(state.pCards[1],bCard);
return {
lastBountyCard: bCard,
lastPlayedCard : [c0,c1],
turn : state.turn +1,
bCards : without(state.bCards,bCard ), // remove the bcard and return a new array
pCards : [
without(state.pCards[0],c0), // return new array with out the cards which have been played
without(state.pCards[1],c1)
],
pScores: newScores(state.pScores, [c0,c1])
}
}
function turnMessage(state){
return `Turn ${state.turn} : Bounty : ${state.lastBountyCard}
\tPlayer 0 plays ${state.lastPlayedCard[0]}
\tPlayer 1 plays ${state.lastPlayedCard[1]}`;
}
runGame();
function runGame(){
let states = [{
turn : 0,
lastBountyCard: 0,
lastPlayedCard : [0,0],
bCards : [1,2,3,4,5,6,7,8],
pCards : [
[1,2,3,4,5,6,7,8],
[1,2,3,4,5,6,7,8]
],
pScores : [0,0]
}];
return recur(states, nextState, function(state) {return state.bCards.length > 0});
}
function selectRandom(arr){
const index = Math.floor(Math.random() * arr.length);
return arr[index];
}
function without(arr,value){
const index = arr.indexOf(value);
return [...arr.slice(0,index) , ...arr.slice(index+1)];
}
function pRStrategy(pCards_ , bCard){
const card = selectRandom(pCards_);
return card;
}
function pEStrategy(pCards_ , bCard){
return bCard;
}
function winMessage(pScores){
if(pScores[0] == pScores[1]){
return (`Players Tie`);
}else if (pScores[0] > pScores[1]){
return (`Player 0 wins`);
}else {
return (`Player 1 wins`);
}
}
function scoreMessage(pScores){
return (`Scores : ${pScores[0]} v ${pScores[1]}`);
}
function endMessage(pScores){
return scoreMessage(pScores) + "\n" + winMessage(pScores);
}
function endMessage(state){
return scoreMessage(state.pScores) + "\n" + winMessage(state.pScores);
}
function newScores(pScores, pCards_){
if(pCards_[0] > pCards_[1]){
return [pScores[0]+1, pScores[1] ];
}else if(pCards_[1] > pCards_[0]){
return [pScores[0], pScores[1]+1 ];
}else{
return [pScores[0], pScores[1] ];
}
}
function nextState(state){
const bCard = selectRandom(state.bCards);
const c0 = pRStrategy(state.pCards[0],bCard);
const c1 = pEStrategy(state.pCards[1],bCard);
return {
lastBountyCard: bCard,
lastPlayedCard : [c0,c1],
turn : state.turn +1,
bCards : without(state.bCards,bCard ), // remove the bcard and return a new array
pCards : [
without(state.pCards[0],c0),
without(state.pCards[1],c1)
],
pScores: newScores(state.pScores, [c0,c1])
}
}
function turnMessage(state){
return `Turn ${state.turn} : Bounty : ${state.lastBountyCard}
\tPlayer 0 plays ${state.lastPlayedCard[0]}
\tPlayer 1 plays ${state.lastPlayedCard[1]}`;
}
function report(states,onTurn, onEnd){
return states.map(onTurn).join("\n") + "\n" + onEnd(last(states));
}
function last(arr){
return arr[arr.length -1];
}
function recur(states, stateChange, endCond){
if(!endCond(last(states))){
return states;
}else{
return recur(states.concat(stateChange(last(states))), stateChange, endCond);
}
}
console.log(report(runGame(),turnMessage, endMessage));
function runGame(){
let turn = 0;
let bCards = [1,2,3,4,5,6,7,8];
let pCards = [
[1,2,3,4,5,6,7,8],
[1,2,3,4,5,6,7,8]
];
let pScores = [0,0];
while(bCards.length > 0){
const bCard = popRandom(bCards);
console.log(`Turn ${turn} : Bounty : ${bCard}`);
//console.log(bCard);
const c0 = pRStrategy(pCards[0],bCard); // use random strategy
const c1 = pEStrategy(pCards[1],bCard); // return bCard from the array
turn +=1;
if(c0 > c1){
pScores[0] += bCard;
}else if(c1 > c0){
pScores[1] += bCard;
}
}
console.log(`Scores : ${pScores[0]} v ${pScores[1]}`);
if(pScores[0] == pScores[1]){
console.log(`Players Tie`);
}
else if (pScores[0] > pScores[1]){
console.log(`Player 0 wins`);
}else {
console.log(`Player 1 wins`);
}
}
/**
* Given an array, remove a random item and return that value
*/
function popRandom(arr){
const index = Math.floor(Math.random() * arr.length);
const val = arr[index];
arr.splice(index ,1)
return val;
}
/**
* Get Card using a random strategy
**/
function pRStrategy(pCards_ , bCard){
const card = popRandom(pCards_);
console.log(`\tPlayer 0 plays ${card}`);
return card;
}
/*
* Remove bCard from the array and return it.
*/
function pEStrategy(pCards_ , bCard){
pCards_.splice(pCards_.indexOf(bCard),1);
console.log(`\tPlayer 1 plays ${bCard}`);
return bCard;
}
runGame();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment