-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.html
More file actions
executable file
·236 lines (218 loc) · 7.79 KB
/
Copy patheditor.html
File metadata and controls
executable file
·236 lines (218 loc) · 7.79 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Editor</title>
<link rel="stylesheet" type='text/css' media="screen" href="custom.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
<script src="skulpt/skulpt.min.js" type="text/javascript"></script>
<script src="skulpt/skulpt-stdlib.js" type="text/javascript"></script>
<script src="http://cdn.jquerytools.org/1.2.7/full/jquery.tools.min.js" charset="utf-8"></script>
</head>
<body>
<div id="editor" onclick="logCursor()">import random
class Card(object):
suit_names = ["Diamonds","Clubs","Hearts","Spades"]
rank_levels = [1,2,3,4,5,6,7,8,9,10,11,12,13]
faces = {1:"Ace",11:"Jack",12:"Queen",13:"King"}
def __init__(self, suit=0,rank=2):
self.suit = self.suit_names[suit]
if rank in self.faces: # self.rank handles printed representation
self.rank = self.faces[rank]
else:
self.rank = rank
self.rank_num = rank # To handle winning comparison
class Deck(object):
def __init__(self): # Don't need any input to create a deck of cards
# This working depends on Card class existing above
self.cards = []
for suit in range(4):
for rank in range(1,14):
card = Card(suit,rank)
self.cards.append(card) # appends in a sorted order
def __str__(self):
total = []
for card in self.cards:
total.append(card.__str__())
# shows up in whatever order the cards are in
return "\n".join(total) # returns a multi-line string listing each card
def pop_card(self, i=-1):
# removes and returns a card from the Deck
# default is the last card in the Deck
return self.cards.pop(i) # this card is no longer in the deck -- taken off
def shuffle(self):
random.shuffle(self.cards)
def replace_card(self, card):
card_strs = []
for c in self.cards:
card_strs.append(c.__str__())
if card.__str__() not in card_strs:
self.cards.append(card)
def sort_cards(self):
# Basically, remake the deck in a sorted way
# This is assuming you cannot have more than the normal 52 cars in a deck
self.cards = []
for suit in range(4):
for rank in range(1,14):
card = Card(suit,rank)
self.cards.append(card)
def play_war_game():
player1 = Deck()
player2 = Deck()
p1_score = 0
p2_score = 0
player1.shuffle()
player2.shuffle()
print("\n*** BEGIN THE GAME ***\n")
for i in range(52):
p1_card = player1.pop_card()
p2_card = player2.pop_card()
print("Player 1 plays", p1_card,"& Player 2 plays", p2_card)
if p1_card.rank_num > p2_card.rank_num:
print("Player 1 wins a point!")
p1_score += 1
elif p1_card.rank_num < p2_card.rank_num:
print("Player 2 wins a point!")
p2_score += 1
else:
print("Tie. Next turn.")
if p1_score > p2_score:
return "Player1", p1_score, p2_score
elif p2_score > p1_score:
return "Player2", p1_score, p2_score
else:
return "Tie", p1_score, p2_score
if __name__ == "__main__":
result = play_war_game()
print("""\n\n******\nTOTAL SCORES:\nPlayer 1: {}\nPlayer 2: {}\n\n""".format(result[1],result[2]))
if result[0] != "Tie":
print(result[0], "wins")
else:
print("TIE!")
c = Card(1,1)
d = Card(1,1)
e = c
print(c==e)
</div>
<div id="headings2">
<button onclick="runit()" id="run">RUN</button>
<button onclick="loadInstructions()" id="task">TASKS</button>
</div>
<pre id="output"></pre>
<script src="src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
var editor = ace.edit("editor");
editor.setOptions({fontSize: "16px"});
editor.setTheme("ace/theme/clouds");
editor.session.setMode("ace/mode/python");
editor.getSession().setUseWrapMode(true);
</script>
<script type="text/javascript">
log = {run:
{ errors:
{count:0,
message:[]
},
success:0
},
cursor:
{ row:[], col:[] },
tasks: 0
};
//console.time('starting');
if(window.sessionStorage){sessionStorage.setItem("MyLog", log)}
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];
}
function runit() {
var editor = ace.edit("editor");
var prog = editor.getValue();
var mypre = document.getElementById("output");
mypre.innerHTML = '';
Sk.pre = "output";
Sk.configure({output:outf, read:builtinRead});
var myPromise = Sk.misceval.asyncToPromise(function() {
try {
eval(Sk.importMainWithBody("<stdin>",false,prog));
$('#output').css('color','black');
log['run']['success'] += 1;
console.log('Success: ',log['run']['success']);
sessionStorage.MyLog = JSON.stringify(log); //returns "Some Value"
var p=sessionStorage.getItem("MyLog");
console.log(p);
}
catch(err) {
console.log(err.toString());
let ret = err.toString(); // Simple output message
// Create stacktrace message
if (err.traceback) {
for (let i = 0; i < err.traceback.length; i++) {
ret += "\n at " + err.traceback[i].filename + " line " + err.traceback[i].lineno;
//Sk.misceval.print_(ret + '\n');
if ("colno" in err.traceback[i]) {
ret += " column " + err.traceback[i].colno;
Sk.misceval.print_(ret + '\n');
$('#output').css('color','#b30000');
}
}
}
log['run']['errors']['count'] += 1;
log['run']['errors']['message'].push(ret)
console.log('Errors: ',log['run']['errors']);
sessionStorage.MyLog = JSON.stringify(log); //returns "Some Value"
var p=sessionStorage.getItem("MyLog");
console.log(p);
}
});
}
function loadInstructions(){
var taskdisp = document.getElementById("output");
/** taskdisp.innerHTML = '<ol>\
<li>What is happening in lines 19-23?</li>\
<li>What is happening in lines 67-69?</li>\
<li>What is happening in lines 73-80?</li>\
<li>At line 97, a card object is initialized with parameters suit = 2 and rank = 11 to Card constructor. \
<ol><li>What is c.rank?</li><li>What is c.suit?</li></ol></li>\
<li>How many times does the for loop run in the Deck constructor?</li></ol>' **/
taskdisp.innerHTML = '<form>\
<label>1. What is happening in lines 19-23?</label>\
<textarea rows="6" cols="60"></textarea></br></br>\
<label>2. What is happening in lines 67-69?</label>\
<textarea rows="6" cols="60"></textarea></br></br>\
<label>3. What is happening in lines 73-80?</label>\
<textarea rows="6" cols="60"></textarea></br>\
<p>4. At line 97, a card object is initialized with parameters suit = 2 and rank = 11 :</p>\
<p>What is c.rank? <input type="text" name="fname"></br>What is c.suit? <input type="text" name="lname" style="margin-left:7px;margin-top:3px;"></p>\
<p>5. How many times does the for loop run in the Deck constructor?<input type="text" name="fname"></br></p>\
</form>'
log['tasks'] += 1;
sessionStorage.MyLog = JSON.stringify(log); //returns "Some Value"
var p=sessionStorage.getItem("MyLog");
console.log(p);
}
function logCursor(){
var editor = ace.edit("editor");
row = editor.getCursorPosition()['row']+1;
col = editor.getCursorPosition()['column']+1;
console.log('Cursor: ',row,col);
log['cursor']['row'].push(row)
log['cursor']['col'].push(col)
sessionStorage.MyLog = JSON.stringify(log); //returns "Some Value"
var p=sessionStorage.getItem("MyLog");
console.log(p);
}
</script>
<script>
$(function() {
$("ul.tabs").tabs("div.panes > div");
});
</script>
</body>
</html>