Own-Programming-Language-Tu.../examples/game/pipes.own
2016-02-14 21:24:06 +02:00

178 lines
4.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use "std"
use "canvas"
///-------------------------------------
/// PIPES CELL
///-------------------------------------
CELL_START = 0
HORIZONTAL = 0
VERTICAL = 1
LEFT_TO_DOWN = 2
LEFT_TO_UP = 3
RIGHT_TO_UP = 4
RIGHT_TO_DOWN = 5
CROSS = 6
CELL_LAST = 6
// лево, право, верх, низ
HorizontalCell = [1,1,0,0]
VerticalCell = [0,0,1,1]
LeftToDownCell = [1,0,0,1]
LeftToUpCell = [1,0,1,0]
RightToUpCell = [0,1,1,0]
RightToDownCell = [0,1,0,1]
CrossCell = [1,1,1,1]
support = [
HorizontalCell, VerticalCell,
LeftToDownCell, LeftToUpCell,
RightToUpCell, RightToDownCell,
CrossCell
]
def draw(v, cellSize) {
c2 = cellSize / 2
match v {
case HORIZONTAL : fillRect(0, c2 - 2, cellSize, 4)
case VERTICAL : fillRect(c2 - 2, 0, 4, cellSize)
case LEFT_TO_DOWN : {
fillRect(0, c2 - 2, c2, 4)
fillRect(c2 - 2, c2 - 2, 4, c2 + 2)
}
case LEFT_TO_UP : {
fillRect(0, c2 - 2, c2, 4)
fillRect(c2 - 2, c2 - 2, 4, c2 + 2)
}
case RIGHT_TO_UP : {
fillRect(c2 - 2, c2 - 2, c2 + 2, 4)
fillRect(c2 - 2, 0, 4, c2 + 2)
}
case RIGHT_TO_DOWN : {
fillRect(c2 - 2, c2 - 2, c2 + 2, 4)
fillRect(c2 - 2, c2 - 2, 4, c2 + 2)
}
case CROSS : {
fillRect(c2 - 2, 0, 4, cellSize)
fillRect(0, c2 - 2, cellSize, 4)
}
}
}
def supportLeft(v) = support[v][0]
def supportRight(v) = support[v][1]
def supportUp(v) = support[v][2]
def supportDown(v) = support[v][3]
///-------------------------------------
///-------------------------------------
/// PIPES BOARD
///-------------------------------------
SIZE = 10
// Создаём игровое поле
board = newarray(SIZE, SIZE)
def createBoard() {
for i=0, i<SIZE, i++
for j=0, j<SIZE, j++
board[i][j] = rand(CELL_LAST)
}
def switchCell(x, y) {
nextType = board[x][y] + 1
board[x][y] = nextType > CELL_LAST ? CELL_START : nextType
}
def isFinished() {
// Стартовая труба должна иметь левую точку соприкосновения
if (!supportLeft(board[0][0])) return 0
// А конечная труба - правую
if (!supportRight(board[SIZE - 1][SIZE - 1])) return 0
visited = newarray(SIZE, SIZE)
return isConnected(0, 0, visited)
}
def isConnected(curX, curY, visited) {
// Если достигли конечной ячейки - выходим.
if ( (curX == SIZE - 1) && (curY == SIZE - 1) ) return 1
// Если уже посещали - выходим.
if (visited[curX][curY]) return 0
// Отмечаем посещение.
visited[curX][curY] = 1
current = board[curX][curY]
if ( supportLeft(current) && (curX > 0) && (supportRight(board[curX - 1][curY])) ) {
if (isConnected(curX - 1, curY, visited)) return 1
}
if ( supportRight(current) && (curX < SIZE - 1) && (supportLeft(board[curX + 1][curY])) ) {
if (isConnected(curX + 1, curY, visited)) return 1
}
if ( supportUp(current) && (curY > 0) && (supportDown(board[curX][curY - 1])) ) {
if (isConnected(curX, curY - 1, visited)) return 1
}
if ( supportDown(current) && (curY < SIZE - 1) && (supportUp(board[curX][curY + 1])) ) {
if (isConnected(curX, curY + 1, visited)) return 1
}
return 0
}
///-------------------------------------
///-------------------------------------
/// PIPES MAIN
///-------------------------------------
translateX = 0 translateY = 0
/* frect с поддержкой translate*/
def fillRect(x,y,w,h) {
frect(translateX+x, translateY+y, w, h)
}
// JAVA ME
// showcanvas()
// JAVA SE
WIDTH = 480 HEIGHT = 480
window("Pipes", WIDTH, HEIGHT)
cellSize = WIDTH / SIZE
createBoard()
// курсор
curX = 0
curY = 0
run = 1
while run {
//key = gameaction(keypressed())
key = keypressed()
if (key == VK_LEFT && curX > 0) curX--
else if (key == VK_RIGHT && curX < SIZE - 1) curX++
else if (key == VK_UP && curY > 0) curY--
else if (key == VK_DOWN && curY < SIZE - 1) curY++
else if key == VK_FIRE switchCell(curX,curY)
else if key == 48 run = 0
// фон
color(isFinished() ? #00FF00 : #FFFFFF)
frect(0,0,WIDTH,HEIGHT)
// курсор
color(#4444FF)
frect(curX*cellSize, curY*cellSize, cellSize, cellSize)
for (i=0, i<SIZE, i++) {
color(0)
ic = i*cellSize
line(0, ic, cellSize*SIZE, ic)
line(ic, 0, ic, cellSize*SIZE)
color(#FF0000)
for j=0, j<SIZE, j++ {
translateX = ic
translateY = j*cellSize
draw(board[i][j], cellSize)
translateX = -ic
translateY = -j*cellSize
}
}
repaint()
sleep(50)
}