Init commit
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
.vscode/**
 | 
			
		||||
							
								
								
									
										3
									
								
								bin/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								bin/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
*
 | 
			
		||||
 | 
			
		||||
!.gitignore
 | 
			
		||||
							
								
								
									
										54
									
								
								cmd/gol/gol.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								cmd/gol/gol.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"gol/gol"
 | 
			
		||||
	"gol/term"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/signal"
 | 
			
		||||
	"syscall"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	front := gol.New(20, 40)
 | 
			
		||||
	back := gol.New(20, 40)
 | 
			
		||||
 | 
			
		||||
	// glider
 | 
			
		||||
	front[0][3] = true
 | 
			
		||||
	front[1][4] = true
 | 
			
		||||
	front[2][2] = true
 | 
			
		||||
	front[2][3] = true
 | 
			
		||||
	front[2][4] = true
 | 
			
		||||
 | 
			
		||||
	// cross
 | 
			
		||||
 | 
			
		||||
	front[16][21] = true
 | 
			
		||||
	front[17][21] = true
 | 
			
		||||
	front[18][21] = true
 | 
			
		||||
 | 
			
		||||
	sigs := make(chan os.Signal, 1)
 | 
			
		||||
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
 | 
			
		||||
 | 
			
		||||
	term.StartFullscreen()
 | 
			
		||||
	term.TurnCursor(term.CursorOff)
 | 
			
		||||
 | 
			
		||||
	loop := func() {
 | 
			
		||||
		ticker := time.NewTicker(250 * time.Millisecond)
 | 
			
		||||
		for {
 | 
			
		||||
			term.GoHome()
 | 
			
		||||
			front.PrintMe()
 | 
			
		||||
			front.NextGen(back)
 | 
			
		||||
			front, back = back, front
 | 
			
		||||
			select {
 | 
			
		||||
			case <-sigs: // przerwanie
 | 
			
		||||
				return
 | 
			
		||||
			case <-ticker.C: // upłynął czas do kolejnej generacji
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	defer func() {
 | 
			
		||||
		term.FinishFullscreen()
 | 
			
		||||
		term.TurnCursor(term.CursorOn)
 | 
			
		||||
	}()
 | 
			
		||||
	loop()
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										95
									
								
								gol/types.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								gol/types.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,95 @@
 | 
			
		||||
package gol
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
type Arena [][]bool
 | 
			
		||||
 | 
			
		||||
func New(ysize, xsize int) Arena {
 | 
			
		||||
	var arena = make([][]bool, ysize)
 | 
			
		||||
	for i := 0; i < ysize; i++ {
 | 
			
		||||
		arena[i] = make([]bool, xsize)
 | 
			
		||||
	}
 | 
			
		||||
	return arena
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (a Arena) PrintMe() {
 | 
			
		||||
	for i := 0; i < len(a); i++ {
 | 
			
		||||
		for j := 0; j < len(a[0]); j++ {
 | 
			
		||||
			if a[i][j] {
 | 
			
		||||
				fmt.Print("\ue0b6\ue0b4")
 | 
			
		||||
			} else {
 | 
			
		||||
				//fmt.Print("\ue0b7\ue0b5")
 | 
			
		||||
				fmt.Print("\u2022\u2022")
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		fmt.Println()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (a Arena) NextGen(to Arena) {
 | 
			
		||||
	for y := 0; y < len(a); y++ {
 | 
			
		||||
		for x := 0; x < len(a[0]); x++ {
 | 
			
		||||
			to[y][x] = a.Life(x, y)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (a Arena) Life(x, y int) bool {
 | 
			
		||||
	count := a.countN(x, y)
 | 
			
		||||
	current := a[y][x]
 | 
			
		||||
	if !current && count == 3 {
 | 
			
		||||
		return true
 | 
			
		||||
	}
 | 
			
		||||
	if current && (count == 2 || count == 3) {
 | 
			
		||||
		return true
 | 
			
		||||
	}
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type pair struct {
 | 
			
		||||
	x int
 | 
			
		||||
	y int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (a Arena) countN(x, y int) int {
 | 
			
		||||
	ysize := len(a)
 | 
			
		||||
	xsize := len(a[0])
 | 
			
		||||
 | 
			
		||||
	xb := x - 1
 | 
			
		||||
	if xb < 0 {
 | 
			
		||||
		xb = xsize - 1
 | 
			
		||||
	}
 | 
			
		||||
	xa := x + 1
 | 
			
		||||
	if xa == xsize {
 | 
			
		||||
		xa = 0
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	yb := y - 1
 | 
			
		||||
	if yb < 0 {
 | 
			
		||||
		yb = ysize - 1
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ya := y + 1
 | 
			
		||||
	if ya == ysize {
 | 
			
		||||
		ya = 0
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	neigh := [8]pair{
 | 
			
		||||
		{xb, yb},
 | 
			
		||||
		{x, yb},
 | 
			
		||||
		{xa, yb},
 | 
			
		||||
		{xb, y},
 | 
			
		||||
		{xa, y},
 | 
			
		||||
		{xb, ya},
 | 
			
		||||
		{x, ya},
 | 
			
		||||
		{xa, ya},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	count := 0
 | 
			
		||||
	for _, p := range neigh {
 | 
			
		||||
		if a[p.y][p.x] {
 | 
			
		||||
			count++
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return count
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										57
									
								
								term/term.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								term/term.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,57 @@
 | 
			
		||||
package term
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
type CursorFlag bool
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	CursorOn  CursorFlag = true
 | 
			
		||||
	CursorOff CursorFlag = false
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TurnCursor(onOff CursorFlag) {
 | 
			
		||||
	if onOff == CursorOn {
 | 
			
		||||
		fmt.Printf("\033[?25h")
 | 
			
		||||
	} else {
 | 
			
		||||
		fmt.Printf("\033[?25l")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ClearScreen() {
 | 
			
		||||
	fmt.Printf("\033[2J")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ResetTerminal() {
 | 
			
		||||
	fmt.Printf("\033c")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GoHome() {
 | 
			
		||||
	fmt.Printf("\033[H")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func SaveCursor() {
 | 
			
		||||
	fmt.Printf("\033[s")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func RestoreCursor() {
 | 
			
		||||
	fmt.Printf("\033[u")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func EnableAlternateScreen() {
 | 
			
		||||
	fmt.Printf("\033[?1049h")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func DisableAlternateScreen() {
 | 
			
		||||
	fmt.Printf("\033[?1049l")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func StartFullscreen() {
 | 
			
		||||
	SaveCursor()
 | 
			
		||||
	EnableAlternateScreen()
 | 
			
		||||
	ClearScreen()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func FinishFullscreen() {
 | 
			
		||||
	DisableAlternateScreen()
 | 
			
		||||
	RestoreCursor()
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user