-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.html
278 lines (242 loc) · 7.03 KB
/
game.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
<script src="skulpt/skulpt.js" type="text/javascript"></script>
<script src="skulpt/builtin.js" type="text/javascript"></script>
<style>
table {margin-top:50px; margin-left: auto; margin-right: auto;}
img {margin-left: auto; margin-right: auto; display:block;}
h1 {text-align: center;}
#reset-image {margin-top:50px; margin-left: auto; margin-right: auto;}
</style>
</head>
<body>
<script type="text/javascript">
// output functions are configurable. This one just appends some text
// to a pre element.
function outf(text) {
var mypre = document.getElementById("output");
mypre.innerHTML = mypre.innerHTML + text;
}
function builtinRead(x) {
if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
throw "File not found: '" + x + "'";
return Sk.builtinFiles["files"][x];
}
// Here's everything you need to run a python program in skulpt
// grab the code from your textarea
// get a reference to your pre element for output
// configure the output function
// call Sk.importMainWithBody()
function runit() {
var prog = document.getElementById("yourcode").value;
var mypre = document.getElementById("output");
mypre.innerHTML = '';
Sk.canvas = "mycanvas";
Sk.pre = "output";
Sk.configure({output:outf, read:builtinRead});
eval(Sk.importMainWithBody("<stdin>",false,prog));
}
</script>
<!--
<h3>SkulptCard</h3>
-->
<form>
<textarea id="yourcode" cols="40" rows="10" style="display: none;">import turtle
t = turtle.Turtle()
t.forward(100)
print "Hello World"
</textarea><br />
<!--
<button type="button" onclick="runit()">Run</button>
-->
</form>
<pre id="output" ></pre>
<!-- If you want turtle graphics include a canvas
<canvas id="mycanvas" ></canvas> -->
<script type="text/javascript">
/* function to map cards to proper ordering...
PNGs:
101-113 = diamonds
114-126 = clubs
127-139 = hearts
140-152 = spades
Actual order:
101-113 = spades
114-126 = clubs
127-139 = diamonds
140-152 = hearts
this func should take a card in 'actual order'
and return the 'source order', i.e. the png
related to the card
*/
function cardSrc(oldNum){
var newNum = 0;
if (oldNum < 114) {newNum = oldNum+39}
else if (oldNum >= 127 && oldNum <= 139) {newNum = oldNum-26}
else if (oldNum >= 140 && oldNum <= 152) {newNum = oldNum-13}
else {newNum = oldNum}
return "cards/"+newNum+".png";;
}
function cardFlip(card, pos, flip) {
if (!roundOver) {
/* flip the card */
if (flip == "back") {
card.src = cardSrc(cardArray[pos]);
card.setAttribute("flip", "front");
/* update score */
if (roundStarted) {
totalFlips += 1;
currentFlips += 1;
updateScore();
/* check if we won */
if (cardArray[pos] == chosenCard) {
roundOver = true;
/*
computerTurn();
*/
}
}
} else if (!roundStarted) {
card.src = cardSrc(155);
card.setAttribute("flip", "back");
};
}
}
function computerTurn() {
var cards = document.getElementsByClassName('computercard');
var notFound = true;
/* start by defining picks as an empty array.
quickly calculate what all the picks will be.
then slowly display the action to the user */
var pickArray = new Array();
while (notFound) {
randcard = Math.floor(Math.random() * nrCols);
if (pickArray.indexOf(randcard) < 0) {
pickArray.push(randcard);
if (cardArray[randcard] == chosenCard) {
notFound = false;
}
}
}
pickArray.reverse();
var flipID = setInterval(function(){flipcard(cards)}, 1000);
function flipcard(cards){
if (pickArray.length >= 1) {
randcard = pickArray.pop();
cards[randcard].src = cardSrc(cardArray[randcard]); /* flip the card */
cards[randcard].setAttribute("flip", "front");
computerCurFlips += 1;
computerTotalFlips += 1;
updateScore();
}
else {
clearInterval(flipID);
};
}
computerCurFlips = 0;
}
function updateScore() {
scorePar.textContent = "Round: "+roundNumber;
totalPar.textContent = "Total flips: "+totalFlips;
}
var cardArray = new Array();
var scorePar = document.createElement('h1');
var totalPar = document.createElement('h1');
var totalFlips = 0;
var currentFlips = 0;
var computerCurFlips = 0;
var computerTotalFlips = 0;
var computerAvgFlips = 0;
var roundNumber = 0;
var roundOver = true;
var roundStarted = false;
var chosenCard;
var pickme =document.createElement('img');
pickme.setAttribute("onclick","dealCards();");
pickme.setAttribute("id","reset-image");
var playerRow=document.createElement('tr');
var computerRow=document.createElement('tr');
var nrCols=15;
function dealCards(){
if (!roundOver) {
return;
}
currentFlips = 0;
roundNumber += 1;
roundStarted = false;
roundOver = false;
/* deal some cards */
cardArray = new Array();
for(var j=0;j<nrCols;j++){
var cardNotFound = true;
while (cardNotFound) {
cardnum = 101+Math.floor(Math.random() * 52);
if (cardArray.indexOf(cardnum) < 0) {
cardArray.push(cardnum)
cardNotFound = false;
}
}
}
/* sort them */
cardArray.sort();
/* choose one at random */
chosenCard = cardArray[Math.floor(Math.random() * nrCols)];
pickme.src = cardSrc(chosenCard);
/* delete any children currently in cardrow */
if (playerRow.childElementCount == nrCols) {
for(var j=0;j<nrCols;j++){
playerRow.deleteCell(0);
/*
computerRow.deleteCell(0);
*/
}
}
/* add them to the row */
for(var j=0;j<nrCols;j++){
var cell = document.createElement('td');
var card = document.createElement("img");
card.src = cardSrc(cardArray[j]);
card.setAttribute("arrayPos", j);
card.setAttribute("flip", "front");
cardFlip(card, j, "front");
card.setAttribute("onclick","cardFlip(this,this.getAttribute(\"arrayPos\"),this.getAttribute(\"flip\"));");
card.setAttribute("title", j+1);
cell.appendChild(card);
playerRow.appendChild(cell);
}
/*
for(var j=0;j<nrCols;j++){
var cell = document.createElement('td');
var card = document.createElement("img");
card.src = cardSrc(cardArray[j]);
card.setAttribute("arrayPos", j);
card.setAttribute("flip", "front");
card.setAttribute("class", "computercard");
card.setAttribute("number", cardArray[j]);
cardFlip(card, j, "front");
cell.appendChild(card);
computerRow.appendChild(cell);
}
*/
roundStarted = true;
updateScore();
}
onload=function(){
var root=document.getElementById('mydiv');
root.appendChild(scorePar);
root.appendChild(totalPar);
root.appendChild(pickme);
var tab=document.createElement('table');
var tbo=document.createElement('tbody');
tab.className="mytable";
tbo.appendChild(playerRow);
tbo.appendChild(computerRow);
tab.appendChild(tbo);
root.appendChild(tab);
dealCards();
}
</script>
<div id="mydiv"></div>
</body>
</html>