From 70009668610b52df12eef90c7b27fb17a0ebe950 Mon Sep 17 00:00:00 2001 From: Sam Hoile Date: Wed, 16 Oct 2024 11:07:29 +0100 Subject: [PATCH] added player class --- api/numberGame/Player.js | 21 +++++++++++++++++++++ api/tests/numberGame/Player.test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 api/numberGame/Player.js create mode 100644 api/tests/numberGame/Player.test.js diff --git a/api/numberGame/Player.js b/api/numberGame/Player.js new file mode 100644 index 0000000..d5bd2be --- /dev/null +++ b/api/numberGame/Player.js @@ -0,0 +1,21 @@ +class Player { + constructor(id, name) { + this.id = id; + this.name = name; + this.currentGuess = null, + this.totalScore = 0 + } + + guess(num) { + this.currentGuess = num + } + + getTotalScore() { + return this.totalScore + } + wonRound() { + this.totalScore++ + } +} + +module.exports = Player \ No newline at end of file diff --git a/api/tests/numberGame/Player.test.js b/api/tests/numberGame/Player.test.js new file mode 100644 index 0000000..d3ee203 --- /dev/null +++ b/api/tests/numberGame/Player.test.js @@ -0,0 +1,27 @@ +const Player = require("../../numberGame/Player") + +describe('player', () => { + test('initiates with id, name, currentGuess (null) and totalScore (0)', () => { + const player = new Player('17326746', 'Bob') + expect(player.id).toBe('17326746') + expect(player.name).toBe('Bob') + expect(player.currentGuess).toEqual(null) + expect(player.totalScore).toEqual(0) + }) + + test('guess() replaces the players guess', () => { + const player = new Player('17326746', 'Bob') + player.guess(5) + expect(player.currentGuess).toBe(5) + }) + + test('getTotalScore returns total score', () => { + const player = new Player('17326746', 'Bob') + expect(player.getTotalScore()).toBe(0) + }) + test('wonRound adds 1 to total score', () => { + const player = new Player('17326746', 'Bob') + player.wonRound() + expect(player.totalScore).toEqual(1) + }) +}) \ No newline at end of file