Initial project import

This commit is contained in:
Agila Forge Importer committed 2026-07-13 15:20:32 +00:00
commit 23f2d29e32
11957 files changed
+1754577

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
# Conway's Game of Life
![](https://github.com/fuyutarow/Conways-Game-of-Life-with-Vlang/raw/master/v-gun.gif)
```
v run life.v
```
Created by fuyutarow: https://github.com/fuyutarow/Conways-Game-of-Life-with-Vlang
+79
View File
@@ -0,0 +1,79 @@
import term
import rand
import time
const cell = '█'
const nothing = ' '
const transformers = [nothing, cell]
struct Game {
mut:
grid [][]string
}
fn (g Game) get_surrounding_alive_count(x int, y int) int {
mut count := 0
for i := x - 1; i <= x + 1; i++ {
for j := y - 1; j <= y + 1; j++ {
if (i != x || j != y) && i >= 0 && j >= 0 && i < g.grid.len && j < g.grid[x].len {
if g.grid[i][j] == cell {
count++
}
}
}
}
return count
}
fn (mut g Game) evolve() {
mut temp_grid := [][]string{}
for x in 0 .. g.grid.len {
temp_grid << []string{}
for y in 0 .. g.grid[x].len {
count := g.get_surrounding_alive_count(x, y)
if count == 3 || (g.grid[x][y] == cell && count == 2) {
temp_grid[x] << cell
} else {
temp_grid[x] << nothing
}
}
}
g.grid = temp_grid
}
fn (mut g Game) display() {
for y in 0 .. g.grid[0].len {
mut line := ''
for x in 0 .. g.grid.len {
line += g.grid[x][y]
}
println(line)
}
}
fn new_game() Game {
w, h := term.get_terminal_size()
mut grid := [][]string{}
for x in 0 .. w {
grid << []string{}
for _ in 0 .. h {
is_live := rand.f64() > 0.82
icon := transformers[int(is_live)]
grid[x] << icon
}
}
return Game{grid}
}
fn main() {
mut g := new_game()
g.display()
for {
g.evolve()
term.erase_clear()
g.display()
time.sleep(100 * time.millisecond)
}
}
+52
View File
@@ -0,0 +1,52 @@
module main
import gg
import automaton
const screen_width = 800
const screen_height = 600
const filled_color = gg.blue
@[live]
fn print_automaton(app &App) {
square_size := 18
for y := 1; y < app.a.field.maxy; y++ {
for x := 1; x < app.a.field.maxx; x++ {
cell := app.a.field.get(x, y)
if cell == 1 {
app.gg.draw_rect_filled(f32(square_size * x), f32(square_size * y),
f32(square_size), f32(square_size), filled_color)
}
}
}
}
struct App {
mut:
gg &gg.Context = unsafe { nil }
a automaton.Automaton
}
fn frame(mut app App) {
app.gg.begin()
app.a.update()
print_automaton(app)
app.gg.end()
}
fn main() {
mut app := App{
a: automaton.gun()
}
app.gg = gg.new_context(
bg_color: gg.white
frame_fn: frame
user_data: &app
width: screen_width
height: screen_height
create_window: true
resizable: false
window_title: 'v life (with gg)'
)
app.gg.run()
}
@@ -0,0 +1,131 @@
module automaton
// ///////////////////////////////////////////////////////////
pub struct A2D {
pub mut:
maxx int
maxy int
data &int
}
fn new_a2d(maxx int, maxy int) &A2D {
size := int(sizeof(int)) * (maxx * maxy)
return &A2D{
maxx: maxx
maxy: maxy
data: unsafe { &int(vcalloc(size)) }
}
}
@[inline]
pub fn (a &A2D) set(x int, y int, newval int) {
unsafe {
mut e := &int(0)
e = a.data + y * a.maxx + x
*e = newval
}
}
@[inline]
pub fn (a &A2D) get(x int, y int) int {
unsafe {
mut e := &int(0)
e = a.data + y * a.maxx + x
_ = e
return *e
}
}
@[inline]
pub fn (a &A2D) clear() {
for y := 0; y < a.maxy; y++ {
for x := 0; x < a.maxx; x++ {
a.set(x, y, 0)
}
}
}
// ///////////////////////////////////////////////////////////
pub struct Automaton {
pub mut:
field &A2D = unsafe { nil }
new_field &A2D = unsafe { nil }
}
fn new_automaton(ftext string) Automaton {
f := ftext.split('\n').map(it.trim_space()).filter(it.len > 0)
maxy := f.len
mut maxx := 0
for y := 0; y < f.len; y++ {
if maxx < f[y].len {
maxx = f[y].len
}
}
field := new_a2d(maxx, maxy)
new_field := new_a2d(maxx, maxy)
for y in 0 .. field.maxy {
for x in 0 .. field.maxx {
val := if x < f[y].len && f[y][x] == `#` { 1 } else { 0 }
field.set(x, y, val)
}
}
return Automaton{
field: field
new_field: new_field
}
}
pub fn (mut aa Automaton) update() {
aa.new_field.clear()
for y := 1; y < aa.field.maxy; y++ {
for x := 1; x < aa.field.maxx; x++ {
moore_sum := (0 + aa.field.get(x - 1, y - 1) + aa.field.get(x, y - 1) + aa.field.get(x +
1, y - 1) + aa.field.get(x - 1, y) + 0 + aa.field.get(x + 1, y) + aa.field.get(x -
1, y + 1) + aa.field.get(x, y + 1) + aa.field.get(x + 1, y + 1))
cell := aa.field.get(x, y)
v := if cell == 1 { moore_sum in [2, 3] } else { moore_sum == 3 }
aa.new_field.set(x, y, if v { 1 } else { 0 })
}
}
tmp := aa.field
aa.field = aa.new_field
aa.new_field = tmp
}
pub fn gun() Automaton {
field := '
*******************************************
* *
* A shooting gun: *
* # *
* # # *
* ## ## ## *
* # # ## ## *
* ## # # ## *
* ## # # ## # # *
* # # # *
* # # *
* ## *
* *
* Tetris Life: *
* *
* ## #### *
* ## *
* *
* *
* *
* # ## *
* ### ## *
* *
* *
* *
* # *
* ### *
* *
* *
* *
* *
*******************************************
'
return new_automaton(field)
}