Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pom.xml.asc
/.nrepl-port
.hgignore
.hg/
.DS_Store
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-leap-day
25 changes: 25 additions & 0 deletions index-runALLGame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function runALLGame() {

console.log("\n\njust run it <===\n")
require('./myJS/cardA-impertive.js')

console.log("\n\nstore it as variables then run it <---\n")
var oneGame = require('./myJS/cardA-impertive.js');
oneGame();

console.log("\n\njust run it but now it is a Class<===\n")
require('./myJS/cardB-OO.js')

console.log("\n\nstore it as variables then run it <---\n")
oneGame = require('./myJS/cardB-OO.js');
new oneGame();

console.log("\n\njust run it but now it is a FP<===\n")
require('./myJS/cardC-FP-0.js')

console.log("\n\nstore it as variables then run it <---\n")
oneGame = require('./myJS/cardC-FP-0.js');
oneGame();
};

module.exports = runALLGame;
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//console.log("\n\njust run it but now it is ALL<===\n")
//var ALLGame = require('./index-runALLGame.js')
//ALLGame(); // <-- note the different syntax of the OO or new class thing

//console.log("\n\njust run it but now it is a FP<-----------===\n");
//require('./myJS/cardC-FP-1-8cards.js');

console.log("\n\njust run it but now it is a FP with 13 cards still 2 players<-----------===\n");
require('./myJS/cardC-FP-1-13cards.js');
70 changes: 70 additions & 0 deletions index.js.log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
node index.js


just run it but now it is a FP<-----------===

--------------------------------
Turn 0:
(-- last Bounty Cards : 0,)
(-- last Playing Cards : 0 and 0,)
current player Scores : 0 and 0,
remaining bounty Cards : 1,2,3,4,5,6,7,8,
on hand player Cards : 1,2,3,4,5,6,7,8 and 1,2,3,4,5,6,7,8.
--------------------------------
Turn 1:
(-- last Bounty Cards : 5,)
(-- last Playing Cards : 3 and 5,)
current player Scores : 0 and 5,
remaining bounty Cards : 1,2,3,4,6,7,8,
on hand player Cards : 1,2,4,5,6,7,8 and 1,2,3,4,6,7,8.
--------------------------------
Turn 2:
(-- last Bounty Cards : 4,)
(-- last Playing Cards : 5 and 4,)
current player Scores : 4 and 5,
remaining bounty Cards : 1,2,3,6,7,8,
on hand player Cards : 1,2,4,6,7,8 and 1,2,3,6,7,8.
--------------------------------
Turn 3:
(-- last Bounty Cards : 7,)
(-- last Playing Cards : 1 and 7,)
current player Scores : 4 and 12,
remaining bounty Cards : 1,2,3,6,8,
on hand player Cards : 2,4,6,7,8 and 1,2,3,6,8.
--------------------------------
Turn 4:
(-- last Bounty Cards : 6,)
(-- last Playing Cards : 2 and 6,)
current player Scores : 4 and 18,
remaining bounty Cards : 1,2,3,8,
on hand player Cards : 4,6,7,8 and 1,2,3,8.
--------------------------------
Turn 5:
(-- last Bounty Cards : 2,)
(-- last Playing Cards : 4 and 2,)
current player Scores : 6 and 18,
remaining bounty Cards : 1,3,8,
on hand player Cards : 6,7,8 and 1,3,8.
--------------------------------
Turn 6:
(-- last Bounty Cards : 8,)
(-- last Playing Cards : 7 and 8,)
current player Scores : 6 and 26,
remaining bounty Cards : 1,3,
on hand player Cards : 6,8 and 1,3.
--------------------------------
Turn 7:
(-- last Bounty Cards : 1,)
(-- last Playing Cards : 6 and 1,)
current player Scores : 7 and 26,
remaining bounty Cards : 3,
on hand player Cards : 8 and 3.
--------------------------------
Turn 8:
(-- last Bounty Cards : 3,)
(-- last Playing Cards : 8 and 3,)
current player Scores : 10 and 26,
remaining bounty Cards : ,
on hand player Cards : and .
Scores: 10 v 26
PLayer 1 Wins!
63 changes: 63 additions & 0 deletions myJS/cardA-impertive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// node cardA-impertive.js

function runGame() {
let turn = 0;
let bountyCards = [1, 2, 3, 4, 5, 6, 7, 8];
let playerCards = [[1, 2, 3, 4, 5, 6, 7, 8],
[1, 2, 3, 4, 5, 6, 7, 8]];
let playerScores = [0, 0];

while (bountyCards.length > 0) {

const bountyCard = popRandom(bountyCards);
console.log(`Turn ${turn}: Bounty: ${bountyCard}`);
const card0 = playRandomStrategy(playerCards[0], bountyCard);
const card1 = playEqualStrategy(playerCards[1], bountyCard);
turn +=1;

if (card0 > card1) {
playerScores[0] += bountyCard;
} else if (card1 > card0){
playerScores[1] += bountyCard;
} else {
//console.log(`\ncard0 is ${card0}`);
//console.log(`card1 is ${card1}`);
console.log("the competition card is discarded!!")
}
}

console.log(`Scores: ${playerScores[0]} v ${playerScores[1]}`);

if (playerScores[0] == playerScores[1]) {
console.log("PLayer Tie.")
} else if (playerScores[0] > playerScores[1]) {
console.log("PLayer 0 Wins!")
} else {
console.log("PLayer 1 Wins!")
}
}

runGame();

function popRandom(arr){
const index = Math.floor(Math.random() * arr.length);
const value = arr[index];
arr.splice(index, 1);
return value;
}

function playRandomStrategy(playerCards, bountyCard){
const card = popRandom(playerCards);
console.log(`\tPlayer 0 plays: ${card}`);
return card;
}

function playEqualStrategy(playerCards, bountyCard){
playerCards.splice(playerCards.indexOf(bountyCard),1);
console.log(`\tPlayer 1 plays: ${bountyCard}`);
return bountyCard;
}

module.exports = runGame;

// see https://evdokimovm.github.io/javascript/nodejs/2016/06/13/NodeJS-How-to-Use-Functions-from-Another-File-using-module-exports.html
105 changes: 105 additions & 0 deletions myJS/cardB-OO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// node cardB-OO.js

class Deck {
constructor() {
this.cards = [1, 2, 3, 4, 5, 6, 7, 8];
}

popRandom(){
const index = Math.floor(Math.random() * this.cards.length);
const value = this.cards[index];
this.cards.splice(index, 1);
return value;
}

remove(cardValue){
this.cards.splice(this.cards.indexOf(cardValue), 1);
}

hasCards() {
return this.cards.length > 0;
}
}

class Player{
constructor() {
this.cards = new Deck();
this.score = 0;
}

scorePoint(value){
this.score +=value;
}

getScore() {
return this.score;
}
}

class RandomPlayer extends Player{
playCard(scoreCard){
return this.cards.popRandom();
}
}

class EqualPlayer extends Player{
playCard(scoreCard){
this.cards.remove(scoreCard);
return scoreCard;
}
}

class Game {
constructor() {
this.players =[new RandomPlayer(), new EqualPlayer()];
this.scoreCards = new Deck();
this.turn = 0;
}

playTurn(){
const scoreCard = this.scoreCards.popRandom();

console.log(`Turn ${this.turn}: Bounty: ${scoreCard}`);

const card0 = this.players[0].playCard(scoreCard);
const card1 = this.players[1].playCard(scoreCard);

console.log(`\tP0: ${card0}`);
console.log(`\tP1: ${card1}`);

if (card0 > card1) {
this.players[0].scorePoint(scoreCard)
} else if (card1 > card0){
this.players[1].scorePoint(scoreCard)
} else {
//console.log(`\ncard0 is ${card0}`);
//console.log(`card1 is ${card1}`);
console.log("the competition card is discarded")
}

this.turn += 1;
}

playGame(){
while (this.scoreCards.hasCards()){
this.playTurn();
}

console.log(`Scores: ${this.players[0].score} v ${this.players[1].score}`)

if (this.players[0].score == this.players[1].score){
console.log("PLayer Tie.")
} else if (this.players[0].score > this.players[1].score) {
console.log("PLayer 0 Wins!")
} else {
console.log("PLayer 1 Wins!")
}
}
}

// need these lines to creat the game and run it !!

game = new Game();
game.playGame()

module.exports = Game;
64 changes: 64 additions & 0 deletions myJS/cardC-FP-0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// from node card0-impertive.js
// refactoring by stages

// adopt the competition card is discarded as in wiki: https://en.wikipedia.org/wiki/Goofspiel

function runGame() {
let turn = 0;
let bountyCards = [1, 2, 3, 4, 5, 6, 7, 8];
let playerCards = [[1, 2, 3, 4, 5, 6, 7, 8],
[1, 2, 3, 4, 5, 6, 7, 8]];
let playerScores = [0, 0];

while (bountyCards.length > 0) {

const bountyCard = popRandom(bountyCards);
console.log(`Turn ${turn}: Bounty: ${bountyCard}`);
const card0 = playRandomStrategy(playerCards[0], bountyCard);
const card1 = playEqualStrategy(playerCards[1], bountyCard);
turn +=1;

if (card0 > card1) {
playerScores[0] += bountyCard;
} else if (card1 > card0){
playerScores[1] += bountyCard;
} else {
//console.log(`\ncard0 is ${card0}`);
//console.log(`card1 is ${card1}`);
console.log("the competition card is discarded")
}
}

console.log(`Scores: ${playerScores[0]} v ${playerScores[1]}`);

if (playerScores[0] == playerScores[1]) {
console.log("PLayer Tie.")
} else if (playerScores[0] > playerScores[1]) {
console.log("PLayer 0 Wins!")
} else {
console.log("PLayer 1 Wins!")
}
}

runGame();

function popRandom(arr){
const index = Math.floor(Math.random() * arr.length);
const value = arr[index];
arr.splice(index, 1);
return value;
}

function playRandomStrategy(playerCards, bountyCard){
const card = popRandom(playerCards);
console.log(`\tPlayer 0 plays: ${card}`);
return card;
}

function playEqualStrategy(playerCards, bountyCard){
playerCards.splice(playerCards.indexOf(bountyCard),1);
console.log(`\tPlayer 1 plays: ${bountyCard}`);
return bountyCard;
}

module.exports = runGame;
Loading