-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarter.html
273 lines (241 loc) · 7.41 KB
/
starter.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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
<TITLE>Starter code by Paul {Piwowarski,Linton}</TITLE>
<SCRIPT LANGUAGE="JavaScript">
/*
CS316 program 2
Initial Author: Paul Piwowarski 2014/09
Modifications: Paul Linton 2016/09
Modifications: Paul Linton 2017/10
Example code to start Program 2
Source: card images from clker.com
*/
//************* GLOBAL VARIABLES *************************
// DSIZE is size of deck: 52 for normal deck
// Note: do not hardcode deck size. Use this variable
// to simplify my (and your) testing
var DSIZE = 40; // size of deck
var HSIZE = 3; // size of hand
var NOTACARD = 0;
var shuffleddeck = new Array(DSIZE); // shuffled deck
var cardstart = new Array(DSIZE); // starting deck in order
var computehand = new Array(HSIZE);
var playerhand = new Array(HSIZE);
var discarded = NOTACARD;
var decklocation; // index into card array during play
var CLUBS = 1;
var SPADES = 2;
var HEARTS = 3;
var DIAMONDS = 4;
var VALUE = 1;
var SUIT = 2;
var hiddencard = "<img src = 'cardback.png' />";
// Put other global variables here ***********************
shuffle(); // start with new deal
// shuffle
// This is one way to create a shuffled deck
// reshuffle the cards, put in card array
// Preconditions:
// New Deal button pressed
// Postconditions:
// Card array filled and shuffled
function shuffle() {
// cardstart is unshuffled 1,2,3....
// card 1 is ace of clubs (1.png)
// card 2 is two of clubs (2.png)
// card 10 is 10 of clubs (10.png)
// card 11 is ace of spades (11.png)
// ....
// card 21 is ace of hearts (21.png)
// ....
// card 31 is ace of diamonds (31.png)
// ....
// card 40 is 10 of diamonds (40.png)
for (var i = 0; i < DSIZE;i++) {
cardstart[i] = i+1;
}
// select card randomly from cardstart and put in card
var randcard; // randomly selected card
for (var j = 0; j < DSIZE; j++) {
randcard = Math.round(Math.random() * (DSIZE - j - 1)); // choose card
shuffleddeck[j] = cardstart[randcard]; // store random card
cardstart.splice(randcard, 1); // pack array
}
decklocation = 0; // index into card array
}
// calculate
// calculate value of card
// Preconditions:
// Card passed
// 1 <= card <= DSIZE
// valueorsuit
// Postconditions:
// Card value returned
function calculate(the_card, valueorsuit) {
var value; // numerical value of card
var suit;
value = the_card % 10;
if (value == 0) {
value = 10;
}
suit = parseInt(the_card / 10) + 1;
if (valueorsuit == SUIT) {
return suit;
} else {
return value;
}
}
function computescore(cards) {
// alert("computescore called"); // replace with actual code for computescore
}
function newcard(cards) {
// alert("newcard called"); // replace with actual code for newcard
}
function disableNewcard() {
theDeck = document.getElementById("xxxx");
thePile = document.getElementById("yyyy");
// something needs to go here.
}
function enableNewcard() {
theDeck = document.getElementById("deck");
thePile = document.getElementById("discard");
theDeck.onclick = SelectCard;
thePile.onclick = SelectCard;
console.log("End of enableNewcard()");
}
function ignoreClick(what) {
console.log("Something clicked "+what+", ignored!");
}
function SelectCard() {
console.log("beginning of SelectCard()");
var whichCard = null;
console.log("Hey, "+this.id+" was clicked!");
return;
}
// Main function. It should follow the steps in the requirements.
// RIGHT NOW, it only deals cards as an example.
// You need to finish it.
function start() {
//
// ask for their name....
deal();
enableNewcard();
}
// deal
// start by dealing 3 cards to computer and you
function deal() {
// deal cards and display data
dealpressed = 1; // deal button has been pressed
// draw three cards for each player
computehand[0] = shuffleddeck[decklocation++];
computehand[1] = shuffleddeck[decklocation++];
computehand[2] = shuffleddeck[decklocation++];
playerhand[0] = shuffleddeck[decklocation++];
playerhand[1] = shuffleddeck[decklocation++];
playerhand[2] = shuffleddeck[decklocation++];
// var cardback = "<img src = '/~paul/cards/cardback.png' />";
var cardback = hiddencard;
// example use cards from my directory
// var dlcard1 = "<img src = '/~paul/cards/"+computehand[0]+".png' />";
// card from same directory as the program/HTML/js file
var dlcard1 = "<img src = '"+computehand[0]+".png' />";
var dlcard2 = "<img src = '"+computehand[1]+".png' />";
var dlcard3 = "<img src = '"+computehand[2]+".png' />";
// Do not actually display cards until hand is over!
// document.getElementById("cc1").innerHTML = dlcard1;
// document.getElementById("cc2").innerHTML = dlcard2;
// document.getElementById("cc3").innerHTML = dlcard3;
document.getElementById("cc1").innerHTML = hiddencard;
document.getElementById("cc2").innerHTML = hiddencard;
document.getElementById("cc3").innerHTML = hiddencard;
var mycard1 = "<img src = '"+playerhand[0]+".png' />";
var mycard2 = "<img src = '"+playerhand[1]+".png' />";
var mycard3 = "<img src = '"+playerhand[2]+".png' />";
document.getElementById("pc1").innerHTML = mycard1;
document.getElementById("pc2").innerHTML = mycard2;
document.getElementById("pc3").innerHTML = mycard3;
discarded = shuffleddeck[decklocation++];
showpile = "<img src = '"+discarded+".png' />";
document.getElementById("discard").innerHTML = showpile;
document.getElementById("discard").style.borderColor = "green";
document.getElementById("discard").style.borderWidth = "20px";
// Example of changing the style (color, border width) of an element
document.getElementById("thePlayer").style.borderColor = "green";
document.getElementById("thePlayer").style.borderWidth = "5px";
// Example how to display on screen in textboxes
document.getElementById("computerscore").innerHTML ="1";
document.getElementById("playerscore").innerHTML ="2";
document.getElementById("directions").innerHTML =
"Select a new/discarded card";
}
</SCRIPT>
</HEAD>
<BODY >
<CENTER>
<TABLE>
<TH STYLE="border: none; padding: 0in">
<P><INPUT TYPE=BUTTON VALUE="New Game"
STYLE="width: 1.20in; height: 0.4in" ONCLICK="start();"> </P>
</TH>
<TABLE>
<TR>
<TH COLSPAN=2 id="theComputer"
style="border: 2px blue">Computer</TH>
<TH id="computerscore"</TH>
<TH> </TH>
<TH COLSPAN=2 id="thePlayer"
style="border: 1px solid green">PlayerName</TH>
<TH id="playerscore"</TH>
</TR>
<TR> <!-- Cards in play -->
<TD id="cc1"> <img src = 'assets/cardback_small.png' /></TD>
<TD id="cc2"> <img src = 'assets/cardback_small.png' /></TD>
<TD id="cc3"> <img src = 'assets/cardback_small.png' /></TD>
<TD id="spc"> </TD>
<TD id="pc1"> <img src = 'assets/cardback_small.png' /></TD>
<TD id="pc2"> <img src = 'assets/cardback_small.png' /></TD>
<TD id="pc3"> <img src = 'assets/cardback_small.png' /></TD>
</TR>
<TR> <!-- deck/discard -->
<TD COLSPAN=2>
<CENTER>
<TABLE>
<TR>
<TH COLSPAN=2> Hand </TH>
</TR>
<TR>
<TD COLSPAN=2> 0 </TD>
</TR>
</TABLE>
</CENTER>
</TD>
<TD id="deck" > <img src = '/~paul/cards/cardback.png' /></TD>
<TD> </TD>
<TD id="discard" > <img src = '/~paul/cards/cardback.png' /></TD>
<TD COLSPAN=2>
<CENTER>
<TABLE>
<TR>
<TH COLSPAN=2> Games Won/Total </TH>
</TR>
<TR>
<TD COLSPAN=2> 0/0 </TD>
</TR>
</TABLE>
</CENTER>
</TD>
</TR>
</TABLE>
<TABLE>
<TR>
<TD>
</TD>
<TD> </TD>
</TR>
</TABLE>
<p id="directions"> </p>
</CENTER>
</BODY>
</HTML>