-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrypto_backend.js
106 lines (99 loc) · 2.17 KB
/
krypto_backend.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//CLASSES FOR GAME
// A deck are all the possible numbers that can be populated to krypto
class Deck {
constructor() {
let deck = new Array()
for (let i = 1; i < 26; i++) {
if (i <= 6) {
for (let j = 0; j < 3; j++) {
deck.push(i)
}
} else if (i >= 7 && i <= 10) {
for (let j = 0; j < 4; j++) {
deck.push(i)
}
} else if (i >= 11 && i <= 17) {
for (let j = 0; j < 2; j++) {
deck.push(i)
}
} else {
deck.push(i)
}
}
this.cards = deck
}
//get deck
getDeck() {
return this.cards
}
}
// A hand are the selected numbers for a particular puzzle.
class Hand {
constructor(cards) {
this.cards = cards
}
getHand() {
return this.cards
}
takeCard(card) {
this.cards.push(card)
}
}
// A target is the target number that you are trying to arrive to.
class Target {
constructor(card) {
this.card = card
}
getTarget() {
return this.card
}
}
// A round contains the deck, and the hand that it deals.
class Round {
constructor(deck, hands) {
let roundDeck = new Deck()
let hand = []
for (let i = 0; i < 5; i++) {
hand.push(randomDeal(roundDeck))
}
this.hand = hand
this.target = randomDeal(roundDeck)
this.deck = roundDeck
}
printRound() {}
}
// DEALING FUNCTIONS
function randomDeal(deck) {
let index = Math.floor(Math.random() * 54)
let takenCard = deck.cards.splice(index, 1)
return takenCard[0]
}
// VALIDATION FUNCTIONS
function checkIfNumbersInHand(numbers, hand) {
//check if numbers in hand
for (let i = 0; i < numbers.length; i++) {
if (!hand.includes(numbers[i])) {
return false
}
//check if each number is used the appropriate number of times
else if (
!countInstance(numbers[i], hand) === countInstance(numbers[i], numbers)
) {
return false
}
}
}
function countInstance(number, hand) {
let count = 0
hand.forEach((num) => {
if (number === num) {
count++
}
})
return count
}
//EVALUATION FUNCTIONS
function checkResult(expression, target) {
let result = evaluateExpression(expression)
return target === result
}