Skip to content

Instantly share code, notes, and snippets.

@nullkal
Created Aug 1, 2012
Embed
What would you like to do?
Life Game for Kuin
func Init(cfg : Kuin@CCfg)
do cfg.Title :: "Life Game for Kuin"
do cfg.FullScr :: false
do cfg.PadNum :: 1
do cfg.WaitTime :: 60
end func
func Main()
var WIDTH : int :: 168
var HEIGHT : int :: 96
var board : [][]int :: @new [HEIGHT + 2][]int
var before_board : [][]int :: @new [HEIGHT + 2][]int
for i(0, HEIGHT + 2 - 1)
do board[i] :: @new[WIDTH + 2]int
do before_board[i] :: @new[WIDTH + 2]int
for j(0, WIDTH + 2 - 1)
do board[i][j] :: 0
do before_board[i][j] :: 0
end for
end for
var last_x : int :: 0
var last_y : int :: 0
while(true)
do Kuin@Act()
do D3D@Clear()
if (DI@MouseBtn(DI@EMouseBtn#Left) > 0)
var mouse_x : float
var mouse_y : float
do DI@MousePos(&mouse_x, &mouse_y)
var cell_x : int :: (mouse_x * (WIDTH $ float) / 1298.0) $ int + 1
var cell_y : int :: (mouse_y * (HEIGHT $ float) / 710.0) $ int + 1
if (last_x <> cell_x | last_y <> cell_y)
if (0 < cell_x & 0 < cell_y & cell_x <= WIDTH & cell_y <= HEIGHT)
do board[cell_y][cell_x] :: 1 - board[cell_y][cell_x]
end if
do last_x :: cell_x
do last_y :: cell_y
end if
else
do last_x :: 0
do last_y :: 0
end if
if (DI@MouseBtn(DI@EMouseBtn#Right) <= 0 & Kuin@Cnt() % 2 = 0)
var tmp : [][]int :: board
do board :: before_board
do before_board :: tmp
for i(1, HEIGHT)
for j(1, WIDTH)
var living_num : int :: before_board[i - 1][j - 1] + before_board[i - 1][j] + before_board[i - 1][j + 1] + before_board[i][j - 1] + before_board[i][j + 1] + before_board[i + 1][j - 1] + before_board[i + 1][j] + before_board[i + 1][j + 1]
if (living_num = 3 | (before_board[i][j] = 1 & living_num = 2))
do board[i][j] :: 1
else
do board[i][j] :: 0
end if
end for
end for
end if
do D3D@DrawRect(0.0, 0.0, 1600.0, 900.0, 0.2, 0.2, 0.2, 1.0)
for i(1, HEIGHT)
for j(1, WIDTH)
var x1 : float :: (1598.0 / WIDTH $ float) * (j - 1) $ float + 2.0
var y1 : float :: (898.0 / HEIGHT $ float) * (i - 1) $ float + 2.0
var x2 : float :: (1598.0 / WIDTH $ float) * j $ float
var y2 : float :: (898.0 / HEIGHT $ float) * i $ float
var color : float :: 1.0 - board[i][j] $ float
do D3D@DrawRect(x1, y1, x2, y2, color, color, color, 1.0)
end for
end for
do D3D@Flip()
end while
end func
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment