-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.fg
105 lines (84 loc) · 3.17 KB
/
memory.fg
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
#!syntax:pythonic
# Utility functions
#==========================================
def makeCardDeck(p_numberOfCopy):
# Each card has a back and a face
js (p_numberOfCopy):
const cardDeck = []
for(let copy=1; copy<=p_numberOfCopy; copy++){
for( let cardNum=1; cardNum<=7; cardNum++){
cardDeck.push({back : "cardBack.svg", face : "card"+ cardNum +".jpg" });
}
}
return cardDeck
def shuffleCards(p_cardDeck):
# shuffle the deck
js (p_cardDeck):
//~ copied from https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
for (let i = p_cardDeck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[p_cardDeck[i], p_cardDeck[j]] = [p_cardDeck[j], p_cardDeck[i]];
}
def distributeCards(p_cardDeck):
# place all the cards face downward onto the game table
js (p_cardDeck):
const gameTable = document.getElementById("gameTable");
for(let card of p_cardDeck){
const cardElement = document.createElement('img');
cardElement.src= "img/" + card.back
card.cardElement = cardElement
gameTable.appendChild(cardElement)
}
def showFace(p_card):
js (p_card):
p_card.cardElement.src = "img/" + p_card.face
def showBack(p_card):
js (p_card):
p_card.cardElement.src = "img/" + p_card.back
# Prepare game table
#=======================
var numberOfCopy := 3
var cardDeck := makeCardDeck(numberOfCopy)
# shuffle deck
shuffleCards(cardDeck)
# place cards face downward onto the game table
distributeCards(cardDeck)
# Start of game
#=======================
# transform JavaScript Array into FuncSug "parallel list"
var allRemainingCards := listToPar(cardDeck)
# now, allRemainingCards is a "parallel list" of all cards placed on the table
while not isNovalue(allRemainingCards):
# choose numberOfCopy cards
#--------------------------
# This block:
# parallel(forEachValueOf <variable>, select <number>):
# select:
# <instructions1>
# do:
# <instructions2>
# launch, in parallel, a sequence of "<instructions1> then <instructions2>" for each value of <variable>
# and when <number> branches (called "selected branches") have reached the end of <instructions1>, all others branches are definitively interrupted
# When all the "selected branches" are finished, the block finished and return an union of the returns of all the "selected branches"
var allClickedCards := parallel(for anyCard in allRemainingCards, select numberOfCopy):
# In the following code, anyCard is only a single card
select:
awaitClickBeep(anyCard.cardElement)
do:
# now, anyCard can only be a clicked card
# show the face of the clicked card
showFace(anyCard)
# return the clicked card (which is then "union-ed" to allClickedCards by "allClickedCards := parallelOfSelected...")
anyCard
# Compare visible faces
#----------------------
if allEqual(allClickedCards.face):
print('OK')
js (allClickedCards):
allClickedCards.cardElement.classList.add('OK')
allRemainingCards := valuesFrom(allRemainingCards, 'butNotFrom', allClickedCards)
else:
print('Missed')
waitSeconds(3)
showBack(allClickedCards)
displayNewMessage('Congratulations, you have found all identical cards!')