-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtictactoe1.p8
202 lines (189 loc) · 8.41 KB
/
tictactoe1.p8
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
pico-8 cartridge // http://www.pico-8.com
version 27
__lua__
-- tictactoe1
-- by apa64
-- todo:
-- title screen with instructions
-- test with scores > 9
-- cursor cell, index to field: 1,2,3
local cursor_x = 2
local cursor_y = 2
-- width/height of a cell
local cell_width = 32
-- playing field
local field = {}
-- field coords
-- 16,16 48,16 80,16
-- 16,48 48,48 80,48
-- 16,80 48,80 80,80
-- top right of first cell
local field_x0 = 16
local field_y0 = 16
-- top right of last cell
local field_x1 = field_x0 + 2*cell_width
local field_y1 = field_y0 + 2*cell_width
-- game states
gs_selecting, gs_win, gs_draw = 0, 1
-- current state
local gamestate = gs_selecting
-- player marks
player_o, player_x = 0, 1
-- current player mark
local current_player = player_o
-- count moves
local moves = 0
local score = {}
score.o = 0
score.x = 0
function _init()
moves = 0
-- init 3x3 field with coords
field = {}
for x = field_y0, field_y1, cell_width do
local row = {}
for y = field_x0, field_x1, cell_width do
local cell = {}
cell.x = x
cell.y = y
cell.mark = nil -- nil, x, o
add(row, cell)
end
add(field, row)
end
gamestate = gs_selecting
end
-->8
-- ###################### update
function _update()
if (gamestate == gs_selecting) then
-- move cursor
if btnp(0) then
cursor_x = (cursor_x - 1) % 4
if (cursor_x == 0) cursor_x = 3
end
if btnp(1) then
cursor_x = (cursor_x + 1) % 4
if (cursor_x == 0) cursor_x = 1
end
if btnp(2) then
cursor_y = (cursor_y - 1) % 4
if (cursor_y == 0) cursor_y = 3
end
if btnp(3) then
cursor_y = (cursor_y + 1) % 4
if (cursor_y == 0) cursor_y = 1
end
-- put a mark to cursor current cell
if (btnp(4) or btnp(5)) then
-- check validity
if (field[cursor_x][cursor_y]["mark"] == nil) then
field[cursor_x][cursor_y]["mark"] = current_player
moves += 1
-- check victory
if (check_victory(cursor_x, cursor_y)) then
-- current_player wins
gamestate = gs_win
local plr = (current_player == player_o) and "o" or "x"
score[plr] += 1
elseif (moves == 9) then
gamestate = gs_draw
else
-- switch player
current_player = (current_player + 1) % 2
end
end
end
else -- gs_draw or gs_win
if (btnp(4) or btnp(5)) then
-- switch player
current_player = (current_player + 1) % 2
-- restart
_init()
end
end
end
-- checks if the last move was victory
-- params: last move x, y
function check_victory(x, y)
local mark = field[x][y]["mark"]
if (field[1][y]["mark"] == mark and field[2][y]["mark"] == mark and field[3][y]["mark"] == mark) then
-- horizontal row win
return true
elseif (field[x][1]["mark"] == mark and field[x][2]["mark"] == mark and field[x][3]["mark"] == mark) then
--- vertical column win
return true
end
-- diagonals
if (field[1][1]["mark"] == mark and field[2][2]["mark"] == mark and field[3][3]["mark"] == mark) then
return true
elseif (field[3][1]["mark"] == mark and field[2][2]["mark"] == mark and field[1][3]["mark"] == mark) then
return true
end
return false
end
-->8
-- ######################## draw
function _draw()
cls(5)
draw_ui()
draw_field()
if (gamestate == gs_selecting) draw_cursor()
end
-- draws ui texts
function draw_ui()
local current_player_str = (current_player == player_o) and "o" or "x"
if (gamestate == gs_win) then
print(current_player_str.." wins!", 51, 7, 8)
elseif (gamestate == gs_draw) then
print("draw!", 55, 7, 10)
else -- gs_selecting
print("player: "..current_player_str, 48, 7, 7)
end
print("score: x: "..score.x.." - o: "..score.o, 30, 116, 7)
end
-- draws the playing field and state
function draw_field()
map(0, 0, 16, 16, 12, 12)
foreach(field, function(row)
foreach(row, function(cell)
if (cell.mark == player_x) then
-- sprite x: 56,0
sspr(56, 0, 8, 8, cell.x + 8, cell.y + 8, 16, 16)
elseif (cell.mark == player_o) then
-- sprite o: 64,0
sspr(64, 0, 8, 8, cell.x + 8, cell.y + 8, 16, 16)
end
end)
end
)
end
-- draws the cursor box
function draw_cursor()
-- current cell x,y
local x = field[cursor_x][cursor_y]["x"]
local y = field[cursor_x][cursor_y]["y"]
rect(x + 1, y + 1, x + cell_width - 2, y + cell_width - 2, 7)
end
__gfx__
00000000000000000000000000066000000000000006600000066000700000070077770000000000000000000000000000000000000000000000000000000000
00000000000000000000000000066000000000000006600000066000070000700700007000000000000000000000000000000000000000000000000000000000
00700700000000000000000000066000000000000005650000565000007007007000000700000000000000000000000000000000000000000000000000000000
00077000000005666666666600066000665000000000666666660000000770007000000700000000000000000000000000000000000000000000000000000000
00077000000066666666666600066000666600000000056666500000000770007000000700000000000000000000000000000000000000000000000000000000
00700700000565000000000000066000005650000000000000000000007007007000000700000000000000000000000000000000000000000000000000000000
00000000000660000000000000066000000660000000000000000000070000700700007000000000000000000000000000000000000000000000000000000000
00000000000660000000000000066000000660000000000000000000700000070077770000000000000000000000000000000000000000000000000000000000
__map__
0102020401020204010202040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0300000303000003030000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0300000303000003030000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0502020605020206050202060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0102020401020204010202040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0300000303000003030000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0300000303000003030000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0502020605020206050202060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0102020401020204010202040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0300000303000003030000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0300000303000003030000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0502020605020206050202060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000