-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp2.js
191 lines (166 loc) · 5.4 KB
/
app2.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//game options - things set only once
let Options = {
numberOfMoves : 4,
numberOfRounds : 3,
possibleMoves : [
{
buttonLabel: "Jab",
code: "jab",
winsAgainst: ["cross", "uppercut"]
},
{
buttonLabel: "Cross",
code: "cross",
winsAgainst: ["rHook", "uppercut"]
},
{
buttonLabel: "Left Hook",
code: "lHook",
winsAgainst: ["cross", "jab"]
},
{
buttonLabel: "Right Hook",
code: "rHook",
winsAgainst: ["jab", "lHook"]
},
{
buttonLabel: "Uppercut",
code: "uppercut",
winsAgainst: ["rHook", "lHook"]
}],
}
//game board - visual ux
let Board = {
buttonArray : [],
playButton : document.createElement("button"),
createButtons () {
let moves = Options.possibleMoves;
let buttonContainer = document.getElementById("buttonContainer");
for (let i = 0; i < moves.length; i++) {
let playersMove = moves[i];
let newButton = document.createElement("button");
newButton.addEventListener("click", () => {
Player.moves.push(playersMove);
Player.moveCounter++;
this.checkCount();
});
newButton.innerText = moves[i]["buttonLabel"];
buttonContainer.appendChild(newButton);
this.buttonArray.push(newButton);
};
let playButton = this.playButton;
playButton.innerText = "play";
playButton.addEventListener("click", () => Engine.runRound());
buttonContainer.appendChild(playButton);
this.disablePlayButton(true);
},
checkCount () {
if (Player.moveCounter >= Options.numberOfMoves){
this.disablePlayButton(false);
this.disablePunchButtons(true);
}
},
disablePlayButton (boolean) {
this.playButton.disabled = boolean;
},
disablePunchButtons (boolean) {
let buttonArray = this.buttonArray;
for (let i = 0; i < buttonArray.length; i++) {
const button = buttonArray[i];
button.disabled = boolean;
}
}
}
//player
let Player = {
moves : [],
score : 0,
moveCounter : 0,
roundScore : 0,
}
//opponent
function Opponent () {
this.moves = [];
this.score = 0;
}
//game engine
let Engine = {
feature_multi_move
currentOpponent : new Opponent(),
round : 1,
punch : 0,
roundResult : "",
setupGame () {
let opponentMoves = this.currentOpponent.moves = [];
for (let i = 0; i < Options.numberOfMoves; i++) {
opponentMoves = this.currentOpponent.moves;
opponentMoves.push(this.randomComputerMove())
};
},
randomComputerMove () {
const randomNum = (Math.floor((Math.random()*5)));
let randomMove = Options.possibleMoves[randomNum];
return randomMove;
},
runRound () {
console.log(`Round: ${this.round} of ${Options.numberOfRounds}`)
let playerMoves = Player.moves;
let opponentMoves = this.currentOpponent.moves;
for (let i = 0; i < playerMoves.length; i++) {
const playerMove = playerMoves[i];
const playerCode = playerMove.code;
const opponentMove = opponentMoves[i];
const opponentCode = opponentMove.code;
if (playerCode === opponentCode) {
this.roundResult = "tie";
} else if (playerMove.winsAgainst.includes(opponentMove.code)) {
this.roundResult = "win";
Player.score++;
Player.roundScore++;
} else {
this.roundResult = "lose";
this.currentOpponent.score++;
}
this.punch++;
console.log(`Punch: ${this.punch}. Player's ${playerMove.buttonLabel} VS Opponent's ${opponentMove.buttonLabel}, You ${this.roundResult}.`);
}
this.round++;
this.resetRound();
},
resetRound () {
this.punch = 0;
Player.moves = [];
Player.moveCounter = 0;
Board.disablePlayButton(true);
Board.disablePunchButtons(false);
//REFACTOR create knock out as a method
if (Player.roundScore == Options.numberOfMoves) {
console.log("KNOCK OUT");
console.log("YOU WIN.");
this.resetGame();
};
Player.roundScore = 0;
//REFACTOR create win screen as a methdd
if (this.round > Options.numberOfRounds) {
console.log(`GAME OVER. FINAL SCORE: YOU: ${Player.score} / OPPONENT: ${this.currentOpponent.score}`);
if (Player.score == this.currentOpponent.score) {
console.log("IT'S A TIE")
} else if (Player.score > this.currentOpponent.score) {
console.log("YOU WIN.");
} else {
console.log("YOU LOSE.");
}
//function to reset round and scores
this.resetGame();
}
},
resetGame () {
this.setupGame();
Player.score = 0;
this.currentOpponent.score = 0;
this.round = 1;
}
}
//initiate
Board.createButtons();
Engine.setupGame();