-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
370 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
#!/usr/bin/env lively | ||
|
||
class Board | ||
FRUITS = ["🍎", "🍐", "🍊", "🍋", "🍌", "🍉", "🍇", "🍓", "🍈", "🍒"] | ||
|
||
def initialize(width = 20, height = 20) | ||
@width = width | ||
@height = height | ||
|
||
reset! | ||
end | ||
|
||
attr :grid | ||
|
||
attr_accessor :direction | ||
|
||
def add_fruit! | ||
5.times do | ||
y = rand(@height) | ||
x = rand(@width) | ||
|
||
if @grid[y][x].nil? | ||
@grid[y][x] = FRUITS.sample | ||
return y, x | ||
end | ||
end | ||
end | ||
|
||
def reset!(count = 5) | ||
@grid = Array.new(@height) {Array.new(@width)} | ||
@head = [@height/2, @width/2] | ||
|
||
@count = count | ||
@direction = :up | ||
|
||
@grid[@head[0]][@head[1]] = count | ||
|
||
add_fruit! | ||
end | ||
|
||
def decrement | ||
@grid.each do |row| | ||
row.map! do |cell| | ||
if cell.is_a?(Integer) | ||
cell -= 1 | ||
end | ||
|
||
if cell == 0 | ||
nil | ||
else | ||
cell | ||
end | ||
end | ||
end | ||
end | ||
|
||
def direction=(value) | ||
case @direction | ||
when :up | ||
if value == :down | ||
return | ||
end | ||
when :down | ||
if value == :up | ||
return | ||
end | ||
when :left | ||
if value == :right | ||
return | ||
end | ||
when :right | ||
if value == :left | ||
return | ||
end | ||
end | ||
|
||
@direction = value | ||
end | ||
|
||
def step | ||
decrement | ||
|
||
case @direction | ||
when :up | ||
@head[0] -= 1 | ||
when :down | ||
@head[0] += 1 | ||
when :left | ||
@head[1] -= 1 | ||
when :right | ||
@head[1] += 1 | ||
end | ||
|
||
if @head[0] < 0 || @head[0] >= @height || @head[1] < 0 || @head[1] >= @width | ||
reset! | ||
end | ||
|
||
case @grid[@head[0]][@head[1]] | ||
when String | ||
@count += 1 | ||
add_fruit! | ||
when Integer | ||
reset! | ||
end | ||
|
||
@grid[@head[0]][@head[1]] = @count | ||
end | ||
end | ||
|
||
class WormsView < Live::View | ||
def initialize(...) | ||
super | ||
|
||
@board = Board.new | ||
end | ||
|
||
def run!(dt = 0.1) | ||
@game ||= Async do | ||
while true | ||
@board&.step | ||
self.update! | ||
sleep(dt) | ||
end | ||
end | ||
end | ||
|
||
def bind(page) | ||
super | ||
|
||
self.run! | ||
end | ||
|
||
def close | ||
if @game | ||
@game.stop | ||
@game = nil | ||
end | ||
|
||
super | ||
end | ||
|
||
def handle(event) | ||
Console.info(self, event) | ||
|
||
case event[:type] | ||
when "keypress" | ||
detail = event[:detail] | ||
|
||
case detail[:key] | ||
when "w" | ||
@board.direction = :up | ||
when "s" | ||
@board.direction = :down | ||
when "a" | ||
@board.direction = :left | ||
when "d" | ||
@board.direction = :right | ||
end | ||
end | ||
end | ||
|
||
def forward_keypress | ||
"live.forwardEvent(#{JSON.dump(@id)}, event, {value: event.target.value, key: event.key})" | ||
end | ||
|
||
def render(builder) | ||
builder.tag('table', tabIndex: 0, autofocus: true, onKeyPress: forward_keypress) do | ||
@board.grid.each do |row| | ||
builder.tag('tr') do | ||
row.each do |cell| | ||
if cell.is_a?(Integer) | ||
style = "background-color: hsl(#{cell * 10}, 100%, 50%)" | ||
builder.tag('td', style: style) | ||
elsif cell.is_a?(String) | ||
builder.tag('td') do | ||
builder.text(cell) | ||
end | ||
else | ||
builder.tag('td') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
Application = Lively::Application[WormsView] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
PATH | ||
remote: ../.. | ||
specs: | ||
lively (0.7.0) | ||
falcon (~> 0.47) | ||
live (~> 0.9) | ||
xrb | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
activemodel (7.1.3.2) | ||
activesupport (= 7.1.3.2) | ||
activerecord (7.1.3.2) | ||
activemodel (= 7.1.3.2) | ||
activesupport (= 7.1.3.2) | ||
timeout (>= 0.4.0) | ||
activesupport (7.1.3.2) | ||
base64 | ||
bigdecimal | ||
concurrent-ruby (~> 1.0, >= 1.0.2) | ||
connection_pool (>= 2.2.5) | ||
drb | ||
i18n (>= 1.6, < 2) | ||
minitest (>= 5.1) | ||
mutex_m | ||
tzinfo (~> 2.0) | ||
async (2.11.0) | ||
console (~> 1.25, >= 1.25.2) | ||
fiber-annotation | ||
io-event (~> 1.5, >= 1.5.1) | ||
timers (~> 4.1) | ||
async-container (0.18.2) | ||
async (~> 2.10) | ||
async-http (0.66.3) | ||
async (>= 2.10.2) | ||
async-pool (>= 0.6.1) | ||
io-endpoint (~> 0.10, >= 0.10.3) | ||
io-stream (~> 0.4) | ||
protocol-http (~> 0.26.0) | ||
protocol-http1 (~> 0.19.0) | ||
protocol-http2 (~> 0.17.0) | ||
traces (>= 0.10.0) | ||
async-http-cache (0.4.3) | ||
async-http (~> 0.56) | ||
async-ollama (0.1.0) | ||
async | ||
async-rest (~> 0.13.0) | ||
async-pool (0.6.1) | ||
async (>= 1.25) | ||
async-rest (0.13.0) | ||
async-http (~> 0.42) | ||
protocol-http (~> 0.7) | ||
async-service (0.12.0) | ||
async | ||
async-container (~> 0.16) | ||
async-websocket (0.26.1) | ||
async-http (~> 0.54) | ||
protocol-rack (~> 0.5) | ||
protocol-websocket (~> 0.11) | ||
base64 (0.2.0) | ||
bigdecimal (3.1.8) | ||
concurrent-ruby (1.2.3) | ||
connection_pool (2.4.1) | ||
console (1.25.2) | ||
fiber-annotation | ||
fiber-local (~> 1.1) | ||
json | ||
drb (2.2.1) | ||
falcon (0.47.6) | ||
async | ||
async-container (~> 0.18) | ||
async-http (~> 0.66, >= 0.66.3) | ||
async-http-cache (~> 0.4.0) | ||
async-service (~> 0.10) | ||
bundler | ||
localhost (~> 1.1) | ||
openssl (~> 3.0) | ||
process-metrics (~> 0.2.0) | ||
protocol-rack (~> 0.5) | ||
samovar (~> 2.3) | ||
fiber-annotation (0.2.0) | ||
fiber-local (1.1.0) | ||
fiber-storage | ||
fiber-storage (0.1.0) | ||
i18n (1.14.5) | ||
concurrent-ruby (~> 1.0) | ||
io-endpoint (0.10.3) | ||
io-event (1.5.1) | ||
io-stream (0.4.0) | ||
json (2.7.2) | ||
live (0.11.0) | ||
async-websocket (~> 0.23) | ||
xrb | ||
localhost (1.3.1) | ||
mapping (1.1.1) | ||
markly (0.10.0) | ||
mini_portile2 (2.8.6) | ||
minitest (5.22.3) | ||
mutex_m (0.2.0) | ||
openssl (3.2.0) | ||
process-metrics (0.2.1) | ||
console (~> 1.8) | ||
samovar (~> 2.1) | ||
protocol-hpack (1.4.3) | ||
protocol-http (0.26.5) | ||
protocol-http1 (0.19.1) | ||
protocol-http (~> 0.22) | ||
protocol-http2 (0.17.0) | ||
protocol-hpack (~> 1.4) | ||
protocol-http (~> 0.18) | ||
protocol-rack (0.5.1) | ||
protocol-http (~> 0.23) | ||
rack (>= 1.0) | ||
protocol-websocket (0.12.1) | ||
protocol-http (~> 0.2) | ||
rack (3.0.11) | ||
samovar (2.3.0) | ||
console (~> 1.0) | ||
mapping (~> 1.0) | ||
sqlite3 (1.7.3) | ||
mini_portile2 (~> 2.8.0) | ||
sqlite3 (1.7.3-arm64-darwin) | ||
timeout (0.4.1) | ||
timers (4.3.5) | ||
traces (0.11.1) | ||
tzinfo (2.0.6) | ||
concurrent-ruby (~> 1.0) | ||
xrb (0.6.1) | ||
|
||
PLATFORMS | ||
arm64-darwin-23 | ||
ruby | ||
|
||
DEPENDENCIES | ||
activerecord (~> 7.1) | ||
async-ollama | ||
live | ||
lively! | ||
markly | ||
sqlite3 (~> 1.4) | ||
|
||
BUNDLED WITH | ||
2.5.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
source "https://rubygems.org" | ||
|
||
gem "live" | ||
gem "lively", path: "../../" | ||
|
||
gem "activerecord", "~> 7.1" | ||
gem "sqlite3", "~> 1.4" | ||
gem "markly" | ||
|
||
gem "async-ollama" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* Center the table in the page */ | ||
|
||
body { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100vh; | ||
margin: 0; | ||
} | ||
|
||
/* Make a table display as a regular size 10px x 10px per cell */ | ||
|
||
table { | ||
border-collapse: collapse; | ||
border-spacing: 0; | ||
} | ||
|
||
td { | ||
width: 2rem; | ||
height: 2rem; | ||
padding: 0; | ||
margin: 0; | ||
border: 1px solid black; | ||
|
||
/* Center the character in the cell */ | ||
text-align: center; | ||
vertical-align: middle; | ||
} |