Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3c20dab
Introduce a missing intentional difference
mrmanc Jan 8, 2024
97a5e8c
Merge pull request #1 from mrmanc/patch-1
codingmugen007 Jan 8, 2024
135a047
Update README.md
codingmugen007 Jan 8, 2024
7d26e6c
Refactor game logic and variable names
codingmugen007 Jan 8, 2024
3a5bf59
Increase iteration count in golden master test
codingmugen007 Jan 8, 2024
a071934
rename variables
codingmugen007 Jan 8, 2024
582f310
rename variable
codingmugen007 Jan 8, 2024
d1caad0
Refactor currentCategory to getCurrentCategory
codingmugen007 Jan 8, 2024
481d4cd
Refactor category check in getCurrentCategory function
codingmugen007 Jan 8, 2024
623059f
Refactored category logic in game.js
codingmugen007 Jan 8, 2024
a50e18c
Refactor getCurrentCategory function to use modulo operator
codingmugen007 Jan 8, 2024
6197cb9
Fix isWinner function to use strict inequality operator
codingmugen007 Jan 8, 2024
1c87834
Refactor player addition logic in Game constructor
codingmugen007 Jan 8, 2024
3b11a38
Refactor askQuestion function to use displayQuestion helper
codingmugen007 Jan 8, 2024
bc14f69
Refactor penalty box logic in game.js
codingmugen007 Jan 8, 2024
8531706
Refactor game.js to use shorthand assignment operators
codingmugen007 Jan 8, 2024
914c4c0
Refactor board update logic in game.js
codingmugen007 Jan 8, 2024
6f88b68
Refactor game.js: Initialize arrays using array literal syntax
codingmugen007 Jan 8, 2024
1d5fa36
Refactor game.js to use constants for maximum players and winning gol…
codingmugen007 Jan 9, 2024
9fa1b55
Refactor add() method in Game class
codingmugen007 Jan 9, 2024
c4cb135
add logPlayerRoll method
codingmugen007 Jan 9, 2024
80308c6
add movePlayer Method
codingmugen007 Jan 9, 2024
1f19b50
move logic to ask question to movePlayer method
codingmugen007 Jan 9, 2024
3fb3975
refactor correctAnswer method
codingmugen007 Jan 9, 2024
641e275
Refactor changePlayer method in Game class
codingmugen007 Jan 9, 2024
7e5fecc
Add Player class and update game logic
codingmugen007 Jan 9, 2024
1c74f20
add player purse to Player class
codingmugen007 Jan 9, 2024
44182fd
refactor correctAnswer method
codingmugen007 Jan 9, 2024
0b6eddb
add isGettingOutOfPenaltyBox to Player class
codingmugen007 Jan 9, 2024
9272d87
add penaltybox state for player
codingmugen007 Jan 9, 2024
2422bba
Update exercises/nodejs/src/game.js
codingmugen007 Jan 9, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Structuring retros so that they can inform thinking for individual's personal learning records (off the job training record tab in their learning logs)
- Introducing some sort of test or quiz on basic concept learning points from the bootcamp to validate that they have taken stuff in, and provide organisation mentors with results to help them focus follow ups
--->
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/c2a57370836b4d058561fc41c24836ab)](https://app.codacy.com/gh/TashanDuncan/apprentice-boot-camp-improving-code/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)

## Competencies, Behaviours and Knowledge units

Expand Down
207 changes: 106 additions & 101 deletions exercises/nodejs/src/game.js
Original file line number Diff line number Diff line change
@@ -1,154 +1,159 @@
import generator from 'random-seed'
import { Player } from './player'

var Game = function () {
var players = new Array()
var places = new Array(6)
var purses = new Array(6)
var inPenaltyBox = new Array(6)
const MAX_PLAYERS = 6
const winningGoldCoins = 6
var players = []
var board = new Array(MAX_PLAYERS)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could potentially extract Board into its own class?


var popQuestions = new Array()
var scienceQuestions = new Array()
var sportsQuestions = new Array()
var rockQuestions = new Array()
var popQuestions = []

var scienceQuestions = []
var sportsQuestions = []
var rockQuestions = []

var currentPlayer = 0
var isGettingOutOfPenaltyBox = false

var didPlayerWin = function () {
return !(purses[currentPlayer] == 6)
var shouldContinueGame = function () {
return players[currentPlayer].purse !== winningGoldCoins
}

var currentCategory = function () {
if (places[currentPlayer] == 0) { return 'Pop' }
if (places[currentPlayer] == 4) { return 'Pop' }
if (places[currentPlayer] == 8) { return 'Pop' }
if (places[currentPlayer] == 1) { return 'Science' }
if (places[currentPlayer] == 5) { return 'Science' }
if (places[currentPlayer] == 9) { return 'Science' }
if (places[currentPlayer] == 2) { return 'Sports' }
if (places[currentPlayer] == 6) { return 'Sports' }
if (places[currentPlayer] == 10) { return 'Sports' }
return 'Rock'
var getCurrentCategory = function () {
const categoryIndex = board[currentPlayer] % 4
if (categoryIndex == 0) { return 'Pop' }
if (categoryIndex == 1) { return 'Science' }
if (categoryIndex == 2) { return 'Sports' }
if (categoryIndex == 3) { return 'Rock' }
}

this.createRockQuestion = function (index) {
return 'Rock Question ' + index
}

for (var i = 0; i < 50; i++) {
popQuestions.push('Pop Question ' + i)
scienceQuestions.push('Science Question ' + i)
sportsQuestions.push('Sports Question ' + i)
rockQuestions.push(this.createRockQuestion(i))
this.getCurrentPlayer = function () {
return players[currentPlayer]
}

for (var index = 0; index < 50; index++) {
popQuestions.push('Pop Question ' + index)
scienceQuestions.push('Science Question ' + index)
sportsQuestions.push('Sports Question ' + index)
rockQuestions.push(this.createRockQuestion(index))
}

this.isPlayable = function (howManyPlayers) {
return howManyPlayers >= 2
}

this.add = function (playerName) {
players.push(playerName)
places[this.howManyPlayers() - 1] = 0
purses[this.howManyPlayers() - 1] = 0
inPenaltyBox[this.howManyPlayers() - 1] = false
players.push(new Player(playerName))
const player = this.playerCount() -1
this.initializePlayer(player)
this.logNewPlayer(playerName)
return true
}
this.initializePlayer = function (player) {
board[player] = 0

}

this.logNewPlayer = function (playerName) {
console.log(playerName + ' was added')
console.log('They are player number ' + players.length)

return true
}

this.howManyPlayers = function () {
this.playerCount = function () {
return players.length
}
const displayQuestion = (questions) => {
console.log(questions.shift())
}

var askQuestion = function () {
if (currentCategory() == 'Pop') { console.log(popQuestions.shift()) }
if (currentCategory() == 'Science') { console.log(scienceQuestions.shift()) }
if (currentCategory() == 'Sports') { console.log(sportsQuestions.shift()) }
if (currentCategory() == 'Rock') { console.log(rockQuestions.shift()) }
this.askQuestion = function () {
const currentCategory = getCurrentCategory()
if (currentCategory == 'Pop') { displayQuestion(popQuestions) }
if (currentCategory == 'Science') { displayQuestion(scienceQuestions) }
if (currentCategory == 'Sports') { displayQuestion(sportsQuestions) }
if (currentCategory == 'Rock') { displayQuestion(rockQuestions) }
}

this.roll = function (roll) {
console.log(players[currentPlayer] + ' is the current player')
this.logPlayerRoll = function (roll) {
console.log(players[currentPlayer].name + ' is the current player')
console.log('They have rolled a ' + roll)
}

if (inPenaltyBox[currentPlayer]) {
if (roll % 2 != 0) {
isGettingOutOfPenaltyBox = true

console.log(players[currentPlayer] + ' is getting out of the penalty box')
places[currentPlayer] = places[currentPlayer] + roll
if (places[currentPlayer] > 11) {
places[currentPlayer] = places[currentPlayer] - 12
}

console.log(players[currentPlayer] + "'s new location is " + places[currentPlayer])
console.log('The category is ' + currentCategory())
askQuestion()
} else {
console.log(players[currentPlayer] + ' is not getting out of the penalty box')
isGettingOutOfPenaltyBox = false
}
} else {
places[currentPlayer] = places[currentPlayer] + roll
if (places[currentPlayer] > 11) {
places[currentPlayer] = places[currentPlayer] - 12
}

console.log(players[currentPlayer] + "'s new location is " + places[currentPlayer])
console.log('The category is ' + currentCategory())
askQuestion()
this.movePlayer = function (roll) {
board[currentPlayer] += roll
if (board[currentPlayer] > 11) {
board[currentPlayer] -= 12
}
console.log(players[currentPlayer].name + "'s new location is " + board[currentPlayer])
console.log('The category is ' + getCurrentCategory())
}

this.wasCorrectlyAnswered = function () {
if (inPenaltyBox[currentPlayer]) {
if (isGettingOutOfPenaltyBox) {
console.log('Answer was correct!!!!')
purses[currentPlayer] += 1
console.log(players[currentPlayer] + ' now has ' +
purses[currentPlayer] + ' Gold Coins.')

var winner = didPlayerWin()
currentPlayer += 1
if (currentPlayer == players.length) { currentPlayer = 0 }

return winner
this.roll = function (roll) {
const isRollOdd = roll % 2 != 0
this.logPlayerRoll(roll)

if (players[currentPlayer].inPenaltyBox) {
if (isRollOdd) {
players[currentPlayer].isGettingOutOfPenaltyBox = true
console.log(players[currentPlayer].name + ' is getting out of the penalty box')
this.movePlayer(roll)
this.askQuestion()
} else {
currentPlayer += 1
if (currentPlayer == players.length) { currentPlayer = 0 }
return true
console.log(players[currentPlayer].name + ' is not getting out of the penalty box')
players[currentPlayer].isGettingOutOfPenaltyBox = false
}
} else {
console.log('Answer was correct!!!!')
this.movePlayer(roll)
this.askQuestion()
}
}

purses[currentPlayer] += 1
console.log(players[currentPlayer] + ' now has ' +
purses[currentPlayer] + ' Gold Coins.')
this.changePlayer = function () {
currentPlayer += 1
if (currentPlayer == players.length) { currentPlayer = 0 }
}

var winner = didPlayerWin()
this.addToPurse = function () {
players[currentPlayer].purse += 1
console.log(players[currentPlayer].name + ' now has ' +
players[currentPlayer].purse + ' Gold Coins.')
}

currentPlayer += 1
if (currentPlayer == players.length) { currentPlayer = 0 }
this.addToPurseCheckWinnerChangePlayer = function () {
this.addToPurse()
var winner = isWinner()
this.changePlayer()
return winner
}

return winner
this.correctAnswer = function () {
if(players[currentPlayer].inPenaltyBox && !players[currentPlayer].isGettingOutOfPenaltyBox) {
this.changePlayer()
return true
} if(!players[currentPlayer].inPenaltyBox){
console.log('Answer was corrent!!!!')
}else {
console.log('Answer was correct!!!!')
}
return this.addToPurseCheckWinnerChangePlayer()
}

this.wrongAnswer = function () {
console.log('Question was incorrectly answered')
console.log(players[currentPlayer] + ' was sent to the penalty box')
inPenaltyBox[currentPlayer] = true
console.log(players[currentPlayer].name + ' was sent to the penalty box')
players[currentPlayer].inPenaltyBox = true

currentPlayer += 1
if (currentPlayer == players.length) { currentPlayer = 0 }
this.changePlayer()
return true
}
}

const gameRunner = (i) => {
var notAWinner = false
const runGame = (i) => {
var isWinner = false

var game = new Game()

Expand All @@ -162,11 +167,11 @@ const gameRunner = (i) => {
game.roll(random.range(5) + 1)

if (random.range(9) == 7) {
notAWinner = game.wrongAnswer()
isWinner = game.wrongAnswer()
} else {
notAWinner = game.wasCorrectlyAnswered()
isWinner = game.correctAnswer()
}
} while (notAWinner)
} while (isWinner)
}

export default gameRunner
export default runGame
8 changes: 8 additions & 0 deletions exercises/nodejs/src/player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class Player {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job creating Player class 👍

constructor(name) {
this.name = name
this.purse = 0
this.inPenaltyBox = false
this.isGettingOutOfPenaltyBox = false
}
}
4 changes: 2 additions & 2 deletions exercises/nodejs/test/golden-master.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs'
import gameRunner from '../src/game'
import runGame from '../src/game'
import captureOutput from './capture-console-output'

function createFolder(foldername) {
Expand All @@ -19,7 +19,7 @@ const generateExpectedResult = (i) => {
flags: 'w'
})

const output = captureOutput(() => gameRunner(i))
const output = captureOutput(() => runGame(i))

masterFile.write(output)

Expand Down
6 changes: 3 additions & 3 deletions exercises/nodejs/test/golden-master.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import getResult from './golden-master'
import gameRunner from '../src/game'
import runGame from '../src/game'
import captureOutput from './capture-console-output'

describe('Running the golden master', () => {
it('should run against 50 results from the golden master and be equivalent', () => {
for (let i = 0; i < 50; i++) {
for (let i = 0; i < 500; i++) {
const result = getResult(i)

expect(result).toEqual(captureOutput(() => gameRunner(i)))
expect(result).toEqual(captureOutput(() => runGame(i)))
}
})
})