Skip to content

Commit 25d335a

Browse files
committed
Исправлена игра pipes
1 parent 0a60869 commit 25d335a

File tree

1 file changed

+38
-52
lines changed

1 file changed

+38
-52
lines changed

examples/game/pipes.own

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use "std"
22
use "canvas"
33

4-
///-------------------------------------
5-
/// PIPES CELL
6-
///-------------------------------------
4+
/// --- PIPES CELL ---
75
CELL_START = 0
86
HORIZONTAL = 0
97
VERTICAL = 1
@@ -14,23 +12,16 @@ RIGHT_TO_DOWN = 5
1412
CROSS = 6
1513
CELL_LAST = 6
1614

17-
// лево, право, верх, низ
18-
HorizontalCell = [1,1,0,0]
19-
VerticalCell = [0,0,1,1]
20-
LeftToDownCell = [1,0,0,1]
21-
LeftToUpCell = [1,0,1,0]
22-
RightToUpCell = [0,1,1,0]
23-
RightToDownCell = [0,1,0,1]
24-
CrossCell = [1,1,1,1]
25-
26-
support = [
27-
HorizontalCell, VerticalCell,
28-
LeftToDownCell, LeftToUpCell,
29-
RightToUpCell, RightToDownCell,
30-
CrossCell
15+
Cells = [
16+
{"support": [1, 1, 0, 0], "index": HORIZONTAL, "next": VERTICAL},
17+
{"support": [0, 0, 1, 1], "index": VERTICAL, "next": HORIZONTAL},
18+
{"support": [1, 0, 0, 1], "index": LEFT_TO_DOWN, "next": LEFT_TO_UP},
19+
{"support": [1, 0, 1, 0], "index": LEFT_TO_UP, "next": RIGHT_TO_UP},
20+
{"support": [0, 1, 1, 0], "index": RIGHT_TO_UP, "next": RIGHT_TO_DOWN},
21+
{"support": [0, 1, 0, 1], "index": RIGHT_TO_DOWN, "next": LEFT_TO_DOWN},
22+
{"support": [1, 1, 1, 1], "index": CROSS, "next": CROSS}
3123
]
3224

33-
3425
def draw(v, cellSize) {
3526
c2 = cellSize / 2
3627
match v {
@@ -42,7 +33,7 @@ def draw(v, cellSize) {
4233
}
4334
case LEFT_TO_UP : {
4435
fillRect(0, c2 - 2, c2, 4)
45-
fillRect(c2 - 2, c2 - 2, 4, c2 + 2)
36+
fillRect(c2 - 2, 0, 4, c2 + 2)
4637
}
4738
case RIGHT_TO_UP : {
4839
fillRect(c2 - 2, c2 - 2, c2 + 2, 4)
@@ -59,19 +50,16 @@ def draw(v, cellSize) {
5950
}
6051
}
6152

62-
def supportLeft(v) = support[v][0]
63-
def supportRight(v) = support[v][1]
64-
def supportUp(v) = support[v][2]
65-
def supportDown(v) = support[v][3]
66-
///-------------------------------------
53+
def supportLeft(v) = Cells[v].support[0]
54+
def supportRight(v) = Cells[v].support[1]
55+
def supportUp(v) = Cells[v].support[2]
56+
def supportDown(v) = Cells[v].support[3]
6757

6858

69-
///-------------------------------------
70-
/// PIPES BOARD
71-
///-------------------------------------
59+
/// --- PIPES BOARD ---
7260
SIZE = 10
7361

74-
// Создаём игровое поле
62+
// Creating game board
7563
board = newarray(SIZE, SIZE)
7664

7765
def createBoard() {
@@ -81,51 +69,49 @@ def createBoard() {
8169
}
8270

8371
def switchCell(x, y) {
84-
nextType = board[x][y] + 1
85-
board[x][y] = nextType > CELL_LAST ? CELL_START : nextType
72+
board[x][y] = Cells[board[x][y]].next
8673
}
8774

8875
def isFinished() {
89-
// Стартовая труба должна иметь левую точку соприкосновения
90-
if (!supportLeft(board[0][0])) return 0
91-
// А конечная труба - правую
92-
if (!supportRight(board[SIZE - 1][SIZE - 1])) return 0
76+
// Start pipe must have left touchpoint
77+
if (!supportLeft(board[0][0])) return false
78+
// Finish pipe - right touchpoint
79+
if (!supportRight(board[SIZE - 1][SIZE - 1])) return false
9380

9481
visited = newarray(SIZE, SIZE)
82+
// Recursive traversal from left upper pipe
9583
return isConnected(0, 0, visited)
9684
}
9785

9886
def isConnected(curX, curY, visited) {
99-
// Если достигли конечной ячейки - выходим.
100-
if ( (curX == SIZE - 1) && (curY == SIZE - 1) ) return 1
87+
// If it is a last cell - game is finished
88+
if ( (curX == SIZE - 1) && (curY == SIZE - 1) ) return true
10189

102-
// Если уже посещали - выходим.
103-
if (visited[curX][curY]) return 0
104-
// Отмечаем посещение.
90+
// Already visited - exit
91+
if (visited[curX][curY]) return false
92+
// Mark visited
10593
visited[curX][curY] = 1
94+
// Check pipes matching
10695
current = board[curX][curY]
10796
if ( supportLeft(current) && (curX > 0) && (supportRight(board[curX - 1][curY])) ) {
108-
if (isConnected(curX - 1, curY, visited)) return 1
97+
if (isConnected(curX - 1, curY, visited)) return true
10998
}
11099
if ( supportRight(current) && (curX < SIZE - 1) && (supportLeft(board[curX + 1][curY])) ) {
111-
if (isConnected(curX + 1, curY, visited)) return 1
100+
if (isConnected(curX + 1, curY, visited)) return true
112101
}
113102
if ( supportUp(current) && (curY > 0) && (supportDown(board[curX][curY - 1])) ) {
114-
if (isConnected(curX, curY - 1, visited)) return 1
103+
if (isConnected(curX, curY - 1, visited)) return true
115104
}
116105
if ( supportDown(current) && (curY < SIZE - 1) && (supportUp(board[curX][curY + 1])) ) {
117-
if (isConnected(curX, curY + 1, visited)) return 1
106+
if (isConnected(curX, curY + 1, visited)) return true
118107
}
119-
return 0
108+
return false
120109
}
121-
///-------------------------------------
122110

123111

124-
///-------------------------------------
125-
/// PIPES MAIN
126-
///-------------------------------------
112+
/// --- PIPES MAIN ---
127113
translateX = 0 translateY = 0
128-
/* frect с поддержкой translate*/
114+
/* frect with translate ability */
129115
def fillRect(x,y,w,h) {
130116
frect(translateX+x, translateY+y, w, h)
131117
}
@@ -138,7 +124,7 @@ window("Pipes", WIDTH, HEIGHT)
138124
cellSize = WIDTH / SIZE
139125
createBoard()
140126

141-
// курсор
127+
// cursor
142128
curX = 0
143129
curY = 0
144130

@@ -153,10 +139,10 @@ while run {
153139
else if key == VK_FIRE switchCell(curX,curY)
154140
else if key == 48 run = 0
155141

156-
// фон
142+
// background
157143
color(isFinished() ? #00FF00 : #FFFFFF)
158144
frect(0,0,WIDTH,HEIGHT)
159-
// курсор
145+
// cursor
160146
color(#4444FF)
161147
frect(curX*cellSize, curY*cellSize, cellSize, cellSize)
162148
for (i=0, i<SIZE, i++) {

0 commit comments

Comments
 (0)