-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.html
187 lines (170 loc) · 5.46 KB
/
tests.html
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
<!DOCTYPE html>
<html>
<head>
<title>Test Fixture</title>
<meta charset="utf-8">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
<link href="https://code.jquery.com/qunit/qunit-1.22.0.css" rel="stylesheet">
</head>
<body>
<!--QUnit Testing Fixture, Visibility Togglable via button-->
<div id="qunit" class="tests"></div>
<div id="qunit-fixture" class="tests">
<script>
var controller = newController();
var scoreBoard = newScoreBoard();
var computer = overseasFactory();
//Controller: Takes GUI input and runs the game
function newController() {
var Controller = {
//turn counter for logging
turn: 1,
play: function(humanMove) {
//Associative array of moves and the moves they lose to
var counterMove = {
"rock": "paper",
"paper": "scissors",
"scissors": "rock"
};
//Get computer's move
var compMove = computer.move();
//print standard part of log
$("#log").append(this.turn++ + ". Player: " + humanMove + ", Computer: " + compMove + " | ")
if (humanMove === compMove) {//It's a draw!
scoreBoard.tie();
$("#log").append("You Tied!<br>");
}
else if (humanMove === counterMove[compMove]){ //You Win!
scoreBoard.win();
$("#log").append("You Won!<br>");
}
else { //You Lose!
scoreBoard.lose();
$("#log").append("You lost!<br>");
}
//Update computer move count with most frequently successful move (counter of the user's most frequent)
computer.moveCount[counterMove[humanMove]]++;
//update last move property to counter player's next move for last move strategy
computer.lastMoveCounter = counterMove[humanMove];
},
//Change the computer's gameplay strategy
newStrategy: function(strategy) {
computer.strategy = strategy;
$("#strategy span").text(strategy);
},
//Resart the game
reset: function() {
scoreBoard.reset();
computer = overseasFactory();
$("#log").text("");
this.turn = 1;
}
}
return Controller;
};
//Computer Opponent
function overseasFactory() {
var computer = {
//counts the human's moves to see what is most frequent
moveCount: {
"rock": 0,
"paper": 0,
"scissors": 0
},
//Returns the counter of the most popular player move
faveMove: function() {
//uses the max value to find the corresponding key in an inverted array (Underscore.js)
var maxVal = Math.max(this.moveCount.rock, this.moveCount.paper, this.moveCount.scissors);
var invertedMoveCount = _.invert(this.moveCount);
return invertedMoveCount[maxVal];
},
//moveSequence: new Array(); a stack implementation for further implementation of various gameplay strategies
//Determines which gameplay strategy function to call
strategy: "random",
move: function() {
switch(this.strategy) {
case "Last":
return this.lastMove();
case "Most Popular":
return this.faveMove();
default:
return this.randomMove();
}
},
//Plays move that beats the player's last move
lastMoveCounter: "",
lastMove: function() {
if (this.lastMoveCounter === "") {
return this.randomMove();//defaults to a random move for the first turn
}
else {
return this.lastMoveCounter;
}
},
//Plays a random move
randomMove: function() {
var moves = new Array("rock", "paper", "scissors");
return moves[Math.round(2 * Math.random())];
},
}
return computer;
};
//Records score and updates player dashboard
function newScoreBoard() {
var board = {
scores: {
"wins": 0,
"losses": 0,
"ties": 0
},
win: function() {
$("#wins").text(++this.scores["wins"]);
},
lose: function() {
$("#losses").text(++this.scores["losses"]);
},
tie: function() {
$("#ties").text(++this.scores["ties"]);
},
reset: function() {
//resets object properties
this.scores["wins"] = 0;
this.scores["losses"] = 0;
this.scores["ties"] = 0;
//reset gui
$("#wins").text(0);
$("#ties").text(0);
$("#losses").text(0);
}
};
return board;
};
//---jQuery GUI helpers---
//Start a new game
$("#reset").click(function() {
controller.reset();
});
//Pick a move
$("#playerMoves button.move").click(function(){
controller.play($(this).val());
});
// Change computer strategy
$(".strButton").click(function(){
var strategy = $(this).val();
controller.newStrategy(strategy);
});
//toggles display of testing fixture
$("#testBtn").click(function(){
$(".tests").toggle();
});
</script>
</div>
<footer>
<!--JS-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="js/underscore-min.js" type="text/javascript"></script>
<script src="//code.jquery.com/qunit/qunit-1.22.0.js"></script>
<script src="js/tests.js" type="text/javascript"></script>
</footer>
</body>
</html>