-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgamelogic.lua
246 lines (221 loc) · 5.91 KB
/
gamelogic.lua
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
require 'middleclass'
-------------------------------------------------------------------
-- GameLogic - is a main game class , which describe game logic ;)
-- Argument:
-- sides:
-- sides - xCount , yCount - Size of x and y sides of game screen.
-- maxTypes:
-- desribe how much types of ball. Standart for classic Lines - 7 types ob ball
-- addCountBalls:
-- describe how much add balls on the game tabel after each step
-------------------------------------------------------------------
Gamelogic = class('Gamelogic') --this is the same as class('Person', Object) or Object:subclass('Person')
-- Default game screen size in Lines = 9x9
Gamelogic.xCount = 9
Gamelogic.yCount = 9
-- Default ball types = 7
Gamelogic.maxTypes = 7
-- Default Max added Count Balls
Gamelogic.addCountBalls = 3
-- Îñíàâíàÿ òàáëèöà âñåõ îáúåêòîâ èãðîâîãî ïîëÿ
local _body = {}
-- Âñïîìîãàòåëüíàÿ òàáëèöà äëÿ âû÷èñëåíèÿ ïîèñêà ïóòåé
local _temp = {}
-- Ôëàã îïèñûâàþùèé ñîñòîÿíèå âûäåëåí ëè øàð èëè íåò
local ballClick = false
-- Ìåòîä îáíóëåíèÿ è ñîçäàíèÿ òàáëèöû èãðîâîãî ïîëÿ
local _initGameTable = function()
local body = {}
for i=1,Gamelogic.xCount do
-- âñòàâëÿåì íîâóþ "ñòðîêó" â äâóõìåðíûé ìàññèâ, ò.å. çàïîëíåíèå ïî âåðòèêàëè
table.insert(body,{})
for j=1,Gamelogic.yCount do
-- çàïîëíÿåì ìàññèâ ïî ãîðèçîíòàëè
table.insert(body[i],0)
end
end
return (body)
end
-- Gamelogic constructor
function Gamelogic:initialize(sides,maxTypes,addCountBalls)
self.xCount , self.yCount = sides[1], sides[2]
if maxTypes<=7 then
self.maxTypes = maxTypes
end
self.addCountBalls = addCountBalls
_body = _initGameTable()
_temp = _initGameTable()
Gamelogic:print()
end
----------------------------------
-- Generator game objects block --
----------------------------------
-- Temp fuction for set new value in any position of game table
function Gamelogic:setNewBall( i, j, typeBall)
if _body[i][j] == 0 then
print("Spawn ball: ",i,j,typeBall)
_body[i][j] = typeBall
return true
end
return false
end
--Generate balls on the game table
-- Max Count of balls = self.addCountBalls
function Gamelogic:genBalls()
local _count = 0
repeat
local x = math.random(1,self.xCount)
local y = math.random(1,self.yCount)
local ballType = math.random(1,self.maxTypes)
if self:setNewBall( x, y, ballType) then
_count = _count + 1
end
until _count == self.addCountBalls
end
function Gamelogic:getGameTable()
return(_body)
end
function Gamelogic:getPathTable()
return(_temp)
end
-- Check all lines for delete
local _deleteBallLine = function()
print("Check game table...")
-- Delete line or not
local isDelLine = false
-- vertical line check:
for i=1,7 do
for j=1,9 do
if (_body[i][j]==_body[i+1][j]) and (_body[i][j]==_body[i+2][j]) and (_body[i][j]>0) then
_temp[i][j] = -1
_temp[i+1][j] = -1
_temp[i+2][j] = -1
end
end
end
-- horizontal line check:
for i=1,9 do
for j=1,7 do
if (_body[i][j]==_body[i][j+1]) and (_body[i][j]==_body[i][j+2]) and (_body[i][j]>0) then
_temp[i][j] = -1
_temp[i][j+1] = -1
_temp[i][j+2] = -1
end
end
end
-- main diagonal line check:
for i=1,7 do
for j=1,7 do
if (_body[i][j]==_body[i+1][j+1]) and (_body[i][j]==_body[i+2][j+2]) and (_body[i][j]>0) then
_temp[i][j] = -1
_temp[i+1][j+1] = -1
_temp[i+2][j+2] = -1
end
end
end
-- not main diagonal line check:
for i=1,7 do
for j=2,9 do
if (_body[i][j]==_body[i+1][j-1]) and (_body[i][j]==_body[i+2][j-2]) and (_body[i][j]>0) then
_temp[i][j] = -1
_temp[i+1][j-1] = -1
_temp[i+2][j-2] = -1
end
end
end
for i=1,9 do
for j=1,9 do
if _temp[i][j] == -1 then
_body[i][j] = 0
_temp[i][j] = 0
isDelLine = true
print("Delete ball")
end
end
end
return (isDelLine)
end
-- Move Ball
function Gamelogic:moveBall(xPosBall,yPosBall,xTarget,yTarget)
--MOVE BALL
print("Move ball")
_body[xTarget][yTarget] = _body[xPosBall][yPosBall]
_body[xPosBall][yPosBall] = 0
if _deleteBallLine() == false then
--generate new balls
Gamelogic:genBalls()
end
end
-- Ðåêóðñèâíàÿ ôóíêöèÿ îáõîäà ÿ÷ååê òàáëèöû.
-- Ïðîâåðÿåì ñîñåäíèè è åñëè ÿ÷åéêè çàíÿòû òî ñòàâèì 1
-- Ïðîäîëæàåì ïðîâåðÿòü ïîêà íå ïðîéäåì âñå âîçìîæíûå âàðèàíòû.
pathFinder = function(x,y)
for i=x-1,x+1 do
for j=y-1,y+1 do
if (i >= 1) and (i <= Gamelogic.xCount) and (j >= 1) and (j <= Gamelogic.yCount)
and ((_body[i][j]==0) or (_body[i][j] == Gamelogic.KEY) or (_body[i][j] == Gamelogic.CHEST)) and (_temp[i][j]==0) then
local continue = false
if (i==x-1) and (j==y-1) then continue = true
end
if (i==x+1) and (j==y-1) then continue = true
end
if (i==x+1) and (j==y+1) then continue = true
end
if (i==x-1) and (j==y+1) then continue = true
end
if continue ~= true then
_temp[i][j] = 1
pathFinder(i,j)
end
end
end
end
end
-- Event onClickTable , when smb click on the game table ;) Yes! Click!!!
function Gamelogic:onClickTable(i,j)
x , y = j , i
print ("MouseClick",x,y)
print (_body[x][y])
-- If ball?
if _body[x][y]>0 and _body[x][y] <= 7 then
--î÷èñòêà ìàññèâà äëÿ ïîñëåäóþùåãî ïîèñêà ïóòè
for i=1,self.xCount do
for j=1,self.yCount do
_temp[i][j]=0
end
end
xTemp = x
yTemp = y
ballClick = true
--
-- path find for x,y ball..
--
pathFinder(x,y)
return true
end
-- If Zero?
if _body[x][y]==0 and ballClick == true and _temp[x][y]==1 then
ballClick = false
Gamelogic:moveBall(xTemp,yTemp,x,y)
return false
end
return false
end
function Gamelogic:print()
--print('{' .. self.xCount ..'.'.. self.yCount .. '}')
--print('maxTypes = ' .. self.maxTypes)
-- âûâîäèì ñîäåðæèìîå äâóõìåðíîãî ìàññèâà
local str = "";
for i=1,self.xCount do
-- êàæäóþ "ñòðîêó" ìàññèâà áóäåì ñíà÷àëà ñîáèðàòü â ñòðîêó, ÷òîáû
-- ïðèâåñòè åå ê óäîáî÷èòàåìîìó òàáëè÷íîìó âèäó
for j=1,self.yCount do
str = str.."\t".._body[i][j]
end
-- âûâîäèì ñòðîêó
str= str.."\r\n"
end
print("\r\n"..str,"")
--
end
print("Gamelogic class init...")