-
Notifications
You must be signed in to change notification settings - Fork 0
/
tttftb.led
310 lines (214 loc) · 11.2 KB
/
tttftb.led
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
Tic Tac Toe
by Qianji Zheng and J. Nelson Rushton, Texas Tech University
July 12, 2014
This is an LED program that defines a simple tic tac toe game to
run in the Easel enviornment.
The game begins with an empty grid and player x to move. When an empty cell is clicked, the player whose turn it occupies that cell and it becomes the other player's turn, until the game is over. When the game is over a message is displayed giving the result of the game. The player can press the restart button at any time to restart the game.
======================================================================
DATA MODEL
======================================================================
The types *player*, *cell*, *move*, and *state* are defined as follows:
*) A *player* is the symbol `x or the symbol `o.
*) A *cell* is an integer in {1..9}. Cells represent squares on the
tic tac toe board as pictured below:
1|2|3
-----
4|5|6
-----
7|8|9
*) A *move* is a pair (p,c) where p is a player and c is a cell.
The move (c, p)represents a move by player p in cell c.
*) A *state* is a set of moves, thought of as the set of moves made
so far in the game. In this case, the state of the game is also the
state of the program.
In this program, the variables *p*, *c*, and *m* will range over players, cells, and moves, respectively. The constants *Gamma* and *click* are uninterpreted constants denoting the current game state, and coordinates of the most recent mouse click in the game window, respectively.
The data model of EaselLED.py is used for images and displays.
======================================================================
GAME RULES
======================================================================
This section defines the rules of tic-tac-toe in LED.
Since the state of the game is the set of moves that have been made, the beginning state *initialState* is the empty set.
/-----------
initialState := {}
-------------/
*cells* is the set of all cells on the board:
/---------------------
cells := {1..9}
---------------------/
/---------------------
gameBoard := {1..9}
---------------------/
Player p *occupies* cell c if the move (p,c) is a member of Gamma; and cell c is *occupied* if it is occupied by `x or by `o.
/--------------------------------------------------
occupies(p,c,S) iff (p,c) in S
occupied(c,S) iff occupies(`x,c,S) or occupies(`o,c,S)
---------------------------------------------------/
A *row* is a set of cells that form three in a row either horizontally, vertically, or diagonally. Techincally, they could be called 'lines', but "three in a row" is an Anglo-American cultural idiom, while "three in a line" is not.
/------------------------------------------
rows := hRows U vRows U diagonals
where
hRows = {{1,2,3},{4,5,6},{7,8,9}} &
vRows = {{1,4,7},{2,5,8},{3,6,9}} &
diagonals = {{1,5,9},{3,5,7}}
-------------------------------------------/
Player p has *three in a row* if he occupies all of the cells in
some row.
/---------------------------------------------
threeInRow(p,S) iff
some R in rows. all c in R. occupies(p,c,S)
---------------------------------------------/
The *board is full* if all cells are occupied.
/----------------------------------
boardFull(S) iff
all c in cells. occupied(c,S)
-----------------------------------/
The *game is over* if either the board is full, or one of the players
has three in a row.
/-------------------------------------------------
gameOver(S) iff
boardFull(S) or threeInRow(`x,S) or threeInRow(`o,S)
-------------------------------------------------/
If the game is not over, the *player to move* is `x if an even number of moves have been made,
and `o otherwise.
/-----------------------
If ~gameOver(S) then
currentPlayer(S) :=
`x if even(|S|);
`o otherwise
even(n) iff n mod 2=0
------------------------/
*legalToMoveIn(c)* means that it is legal for the player whose turn it is to move in cell c in the current state of the game -- that is, if the game is not over and the cell is not occupied.
/-----------------------------------------------
legalToMoveIn(c,S) iff ~occupied(c,S) & ~gameOver(S)
------------------------------------------------/
======================================================================
VIDEO OUTPUT
======================================================================
The default color used in this game is BLACK
/----------------------------
BLACK := (0,0,0)
WHITE := (255, 255, 255)
BLUE := (0,0,255)
GREEN := ( 0, 255, 0)
RED := (255, 0, 0)
-----------------------------/
This section defines the *display* function, specifying the images to display on the screen in each game state. The data model for this section can be found in EaselLED.py.
The *grid* consists is a set of four line segments that make up the tic tac toe playing area.
/----------------------------
gridDisplay := {L1,L2,L3,L4}
where
L1 = (`seg,(200,700),(200,400),BLACK) &
L2 = (`seg,(300,700),(300,400),BLACK) &
L3 = (`seg,(100,600),(400,600),BLACK) &
L4 = (`seg,(100,500),(400,500),BLACK)
-----------------------------/
The default font size for displayed text in this program is 36.
/--------------
fontSize := 36
---------------/
*centerX(c)* and *centerO(c)* are the x and y coordinates of the center of cell c, respectively.
/-----------------------------------
centerX(c):= 150+100*((c-1) mod 3)
centerO(c):= 650-100*(floor((c-1)/3))
------------------------------------/
*cellDisplay(c)* is a display of a text character 'x' or an 'o' in cell c, or the empty display, respectively in case cell c is occupied by `x, occupied by `o, or not occupied in the current game state.
/-----------------------------------------------
xImage(c):=
(`txt,"x",(centerX(c), centerO(c)),fontSize,BLUE)
oImage(c) :=
(`txt,"o",(centerX(c), centerO(c)),fontSize,GREEN)
cellDisplay(c,S) :=
{xImage(c)} if (`x,c) in S;
{oImage(c)} if (`o,c) in S;
{} otherwise
------------------------------------------------/
*playerImages* is the set of all images of x's and o's on the board in the current state.
/--------------------------------------
cellDisplays(S) :=
Union[c in gameBoard] cellDisplay(c,S)
---------------------------------------/
If the game is not over, *currentPlayerDisplay* is a text in the upper left hand region of the game window indicating the player to move, either "play x's turn" or "play o's turn",
/-----------------------------------------------------
currentPlayerDisplay(S) :=
{(`txt,"x's turn",(100,750),fontSize,BLACK)} if currentPlayer(S)=`x;
{(`txt,"o's turn",(100,750),fontSize,BLACK)} if currentPlayer(S)=`o
--------------------------------------------------------/
The *restart button* consists of a rectangle around a "restart" text, displayed in the upper right region of the screen. Formally, it is a set of four line segments and a text.
/------------------------------------
restartButton := {A1,A2,A3,A4,txt}
where
A1 = (`seg,(400,725),(500,725),BLACK) &
A2 = (`seg,(400,775),(500,775),BLACK) &
A3 = (`seg,(400,725),(400,775),BLACK) &
A4 = (`seg,(500,725),(500,775),BLACK) &
txt = (`txt,"restart",(450, 750), fontSize,BLACK)
-------------------------------------/
If the game is over, *gameResultDisplay* is a display in the upper left region of the screen telling the outcome of the game, either 'x won!', "o won!", or "The cat got it!"
/-------------------------------------------------------
If gameOver(S) then
gameResultDisplay(S) :=
{(`txt,"x won",(100,750),fontSize,BLUE)} if threeInRow(`x,S);
{(`txt,"o won",(100,750),fontSize,GREEN)} if threeInRow(`o,S);
{(`txt,"cat got it",(100,750),fontSize,RED)} otherwise
---------------------------------------------------------/
The *display* is the screen display for the current game state. The grid, cell displays, and restart button are always displayed. Additionally, the display includes the game results if the game is over, and the player to move if the game is not over.
/------------------------------------------
alwaysDisplay(S) :=
gridDisplay U cellDisplays(S) U restartButton
gameInPlayDisplay(S) :=
alwaysDisplay(S) U currentPlayerDisplay(S)
gameOverDisplay(S) :=
alwaysDisplay(S) U gameResultDisplay(S)
images(S) :=
gameOverDisplay(S) if gameOver(S);
gameInPlayDisplay(S) otherwise
------------------------------------------/
======================================================================
MOUSE INPUT
======================================================================
This section defines *update*, which specifies the program's response to mouse input. The variable *pt* will vary over points.
*xMin(c)*, *xMax(c)*, *yMin(c)*, and *yMax(c)* denote the graphical boundaries of cell c, in the obvious manner.
/----------------------------------
xMin(c) := 100+100*((c-1) mod 3)
xMax(c) := 200+100*((c-1) mod 3)
yMin(c) := 600-100*(floor((c-1)/3))
yMax(c) := 700-100*(floor((c-1)/3))
------------------------------------/
The x and y coordinates of a point are its 1st and 2nd coordinates, respectively.
/-------------------------------------------
xCoord(pt):= pt[1] yCoord(pt):= pt[2]
--------------------------------------------/
clicked(c) means that cell c has been clicked.
/------------------------------------------------------------
clicked(c,I) iff
~I[1]=`nil &
(xCoord(I[1])>xMin(c)) & (xCoord(I[1])< xMax(c)) &
(yCoord(I[1])>yMin(c)) & (yCoord(I[1])< yMax(c))
-------------------------------------------------------------/
pressed(c,I) means that cell c has been pressed
/------------------------------------------------------------
pressed(c,I) iff
|I[2]|>0 & c=I[2][1][1]-48
-------------------------------------------------------------/
*restartClicked* means that the most recent mouse click is inside the region of the play again button.
/-------------------------------------------
restartClicked(I) iff
~I[1]=`nil &
xCoord(I[1])>400 & xCoord(I[1])<500 &
yCoord(I[1])>725 & yCoord(I[1])<775
---------------------------------------------/
*restartPressed* means that "r" has been pressed on the keyboard
/-------------------------------------------
restartPressed(I) iff
|I[2]|>0 & I[2][1]="r"
-------------------------------------------/
*moveMade(c)* means that cell c has been clicked and the player to move may legally move there. *movesMade* is the set of all moves made by the most recent mouse click, which will either be a singleton or empty.
/-------------------------------------------------------------
moveMadeIn(c,I,S) iff (clicked(c,I) or pressed(c,I) ) & legalToMoveIn(c,S)
movesMade(I,S) := {(currentPlayer(S),c) | c in cells & moveMadeIn(c,I,S)}
--------------------------------------------------------------/
/----------------------------------------
transition(I,S) :=
initialState if restartClicked(I) or restartPressed(I);
S U movesMade(I,S) otherwise
-----------------------------------------/