Compare commits
6 Commits
Go-Routine
...
trunk
Author | SHA1 | Date | |
---|---|---|---|
e8d5ecc772 | |||
07f81679d8 | |||
1eb18e7aeb | |||
69a1c68fb6 | |||
b08e0ea3e8 | |||
34bfeaed0e |
2
.gitignore
vendored
2
.gitignore
vendored
@ -1 +1 @@
|
|||||||
builds
|
builds
|
||||||
|
7
.pre-commit-config.yaml
Normal file
7
.pre-commit-config.yaml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
repos:
|
||||||
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
|
rev: v4.6.0 # this is optional, use `pre-commit autoupdate` to get the latest rev!
|
||||||
|
hooks:
|
||||||
|
- id: check-yaml
|
||||||
|
- id: end-of-file-fixer
|
||||||
|
- id: trailing-whitespace
|
15
Taskfile.dist.yaml
Normal file
15
Taskfile.dist.yaml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
version: '3'
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
default:
|
||||||
|
cmds:
|
||||||
|
- go run .
|
||||||
|
build:
|
||||||
|
cmds:
|
||||||
|
- mkdir -p builds
|
||||||
|
- rm builds/* || true
|
||||||
|
- go tool dist list | grep -v android | grep -v ios | grep -v wasip1 | awk -F '/' '{printf "echo Compiling %s/%s; env CGO_ENABLED=1 GOOS=%s GOARCH=%s go build -o builds/rowboz.%s-%s\n",$1,$2,$1,$2,$1,$2 }' | sh
|
||||||
|
- for i in `ls builds/*windows*`; do mv -v $i $i.exe; done
|
||||||
|
lint:
|
||||||
|
cmds:
|
||||||
|
- golangci-lint run
|
10
build.sh
10
build.sh
@ -1,10 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
mkdir -p builds
|
|
||||||
|
|
||||||
rm builds/*
|
|
||||||
|
|
||||||
go tool dist list | grep -v android | grep -v ios | grep -v wasip1 | awk -F '/' '{printf "echo Compiling %s/%s; env CGO_ENABLED=1 GOOS=%s GOARCH=%s go build -o builds/rowboz.%s-%s\n",$1,$2,$1,$2,$1,$2 }' | sh
|
|
||||||
|
|
||||||
cd builds
|
|
||||||
for i in `ls *windows*`; do mv -v $i $i.exe; done
|
|
@ -73,6 +73,7 @@ func initialize() (Game, error) {
|
|||||||
func (game *Game) gameDirector() {
|
func (game *Game) gameDirector() {
|
||||||
defer quit(game)
|
defer quit(game)
|
||||||
|
|
||||||
|
// TODO reorder this
|
||||||
for game.gameover == 0 {
|
for game.gameover == 0 {
|
||||||
|
|
||||||
// Clean the screen
|
// Clean the screen
|
||||||
@ -98,7 +99,7 @@ func (game *Game) gameDirector() {
|
|||||||
|
|
||||||
// Draw stuff last
|
// Draw stuff last
|
||||||
game.drawScore()
|
game.drawScore()
|
||||||
game.drawDebug()
|
//game.drawDebug()
|
||||||
|
|
||||||
// Draw the screen
|
// Draw the screen
|
||||||
game.screen.Show()
|
game.screen.Show()
|
||||||
@ -170,8 +171,8 @@ func quit(game *Game) {
|
|||||||
func (game *Game) randPos() Position {
|
func (game *Game) randPos() Position {
|
||||||
var pos Position
|
var pos Position
|
||||||
|
|
||||||
x := 1 + rand.Intn(77)
|
x := 2 + rand.Intn(76)
|
||||||
y := 1 + rand.Intn(22)
|
y := 2 + rand.Intn(21)
|
||||||
|
|
||||||
pos.x = x
|
pos.x = x
|
||||||
pos.y = y
|
pos.y = y
|
||||||
|
@ -12,7 +12,7 @@ func (game *Game) movePlayer(press int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if press == Left {
|
if press == Left {
|
||||||
if game.player.position.x != 1 {
|
if game.player.position.x != 2 {
|
||||||
game.player.position.x--
|
game.player.position.x--
|
||||||
game.player.moves++
|
game.player.moves++
|
||||||
}
|
}
|
||||||
@ -32,7 +32,7 @@ func (game *Game) movePlayer(press int) {
|
|||||||
game.player.moves++
|
game.player.moves++
|
||||||
}
|
}
|
||||||
} else if press == upleft {
|
} else if press == upleft {
|
||||||
if game.player.position.x != 1 && game.player.position.y != 1 {
|
if game.player.position.x != 2 && game.player.position.y != 1 {
|
||||||
game.player.position.x--
|
game.player.position.x--
|
||||||
game.player.position.y--
|
game.player.position.y--
|
||||||
game.player.moves++
|
game.player.moves++
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package robots
|
package robots
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -42,26 +41,28 @@ func (game *Game) drawBox() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// drawCoords Print the coordinates of the player
|
// drawCoords Print the coordinates of the player
|
||||||
func (game *Game) drawCoords() {
|
// func (game *Game) drawCoords() {
|
||||||
var x, y int = 25, 24
|
// var x, y int = 25, 24
|
||||||
for _, r := range []rune("[ x:" + strconv.FormatInt(int64(game.player.position.x), 10) + " y:" + strconv.FormatInt(int64(game.player.position.y), 10) + " ]") {
|
// for _, r := range []rune("[ x:" + strconv.FormatInt(int64(game.player.position.x), 10) + " y:" + strconv.FormatInt(int64(game.player.position.y), 10) + " ]") {
|
||||||
game.screen.SetContent(x, y-1, r, nil, game.style)
|
// game.screen.SetContent(x, y-1, r, nil, game.style)
|
||||||
x++
|
// x++
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (game *Game) drawDebug() {
|
// func (game *Game) drawDebug() {
|
||||||
var x, y int = 40, 24
|
// var x, y int = 40, 24
|
||||||
for _, r := range []rune("[ NumGoRoutine: " + strconv.FormatInt(int64(runtime.NumGoroutine()), 10) + " Bots: " + strconv.FormatInt(int64(len(game.robots)), 10) + " ]") {
|
// for _, r := range []rune("[ NumGoRoutine: " + strconv.FormatInt(int64(runtime.NumGoroutine()), 10) + " Bots: " + strconv.FormatInt(int64(len(game.robots)), 10) + " ]") {
|
||||||
game.screen.SetContent(x, y-1, r, nil, game.style)
|
// game.screen.SetContent(x, y-1, r, nil, game.style)
|
||||||
x++
|
// x++
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (game *Game) drawScore() {
|
func (game *Game) drawScore() {
|
||||||
var x, y int = 5, 24
|
var x, y int = 5, 24
|
||||||
for _, r := range []rune("[ Score: " + strconv.FormatInt(int64(game.player.score), 10) + " Level: " + strconv.FormatInt(int64(game.level), 10) + " ]") {
|
content := "[ Score: " + strconv.FormatInt(int64(game.player.score), 10) + " Level: " + strconv.FormatInt(int64(game.level), 10) + " ]"
|
||||||
game.screen.SetContent(x, y-1, r, nil, game.style)
|
|
||||||
|
for i := 0; i < len(content); i++ {
|
||||||
|
game.screen.SetContent(x, y-1, rune(content[i]), nil, game.style)
|
||||||
x++
|
x++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,6 @@ package robots
|
|||||||
func (game *Game) initRobots() {
|
func (game *Game) initRobots() {
|
||||||
var fabricate int = game.level * 16
|
var fabricate int = game.level * 16
|
||||||
for i := 0; i < fabricate; i++ {
|
for i := 0; i < fabricate; i++ {
|
||||||
|
|
||||||
var robot Robot
|
|
||||||
|
|
||||||
robot.id = i
|
|
||||||
robot.comms = make(chan Position)
|
|
||||||
|
|
||||||
var found bool
|
var found bool
|
||||||
var rndPos Position
|
var rndPos Position
|
||||||
for !found {
|
for !found {
|
||||||
@ -17,10 +11,11 @@ func (game *Game) initRobots() {
|
|||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
rndRobot := Robot{
|
||||||
robot.position = rndPos
|
id: i,
|
||||||
game.robots = append(game.robots, robot)
|
position: rndPos,
|
||||||
go game.Robot(robot)
|
}
|
||||||
|
game.robots = append(game.robots, rndRobot)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,15 +26,21 @@ func (game *Game) drawRobots() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) moveRobots() {
|
func (game *Game) moveRobots() {
|
||||||
|
|
||||||
var robotPos Position
|
|
||||||
|
|
||||||
// Iterate through the robots
|
// Iterate through the robots
|
||||||
for i, _ := range game.robots {
|
for i, r := range game.robots {
|
||||||
|
|
||||||
game.robots[i].comms <- game.player.position
|
// Determine in which direction to go
|
||||||
robotPos = <-game.robots[i].comms
|
if game.player.position.x < r.position.x {
|
||||||
game.robots[i].position = robotPos
|
game.robots[i].position.x--
|
||||||
|
} else if game.player.position.x > r.position.x {
|
||||||
|
game.robots[i].position.x++
|
||||||
|
}
|
||||||
|
|
||||||
|
if game.player.position.y < r.position.y {
|
||||||
|
game.robots[i].position.y--
|
||||||
|
} else if game.player.position.y > r.position.y {
|
||||||
|
game.robots[i].position.y++
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,66 +89,14 @@ func (game *Game) deleteRobot(robot Robot) {
|
|||||||
|
|
||||||
var rowboz []Robot
|
var rowboz []Robot
|
||||||
|
|
||||||
// Delete from Array
|
|
||||||
for _, r := range game.robots {
|
for _, r := range game.robots {
|
||||||
if robot.id != r.id {
|
if robot != r {
|
||||||
rowboz = append(rowboz, r)
|
rowboz = append(rowboz, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
game.robots = nil
|
game.robots = nil
|
||||||
game.robots = rowboz
|
game.robots = rowboz
|
||||||
|
|
||||||
// Close the channel
|
|
||||||
close(robot.comms)
|
|
||||||
|
|
||||||
game.player.score++
|
game.player.score++
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) Robot(robot Robot) {
|
|
||||||
var player Position
|
|
||||||
var robotPos Position = robot.position
|
|
||||||
var emptyPos Position
|
|
||||||
|
|
||||||
for {
|
|
||||||
// Wait for input from the channel
|
|
||||||
player = <-robot.comms
|
|
||||||
|
|
||||||
if player == emptyPos {
|
|
||||||
close(robot.comms)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//fmt.Println(player, robotPos)
|
|
||||||
|
|
||||||
// Determine direction to move in
|
|
||||||
robotPos = game.moveRobot(player, robot.position)
|
|
||||||
robot.position = robotPos
|
|
||||||
|
|
||||||
// Return new position of robot
|
|
||||||
robot.comms <- robotPos
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (game *Game) moveRobot(player Position, robot Position) Position {
|
|
||||||
|
|
||||||
var newPosition Position
|
|
||||||
|
|
||||||
// Determine in which direction to go
|
|
||||||
if player.x < robot.x {
|
|
||||||
newPosition.x = robot.x - 1
|
|
||||||
} else if player.x > robot.x {
|
|
||||||
newPosition.x = robot.x + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
if player.y < robot.y {
|
|
||||||
newPosition.y = robot.y - 1
|
|
||||||
} else if player.y > robot.y {
|
|
||||||
newPosition.y = robot.y + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
//fmt.Println(player, robot, newPosition)
|
|
||||||
|
|
||||||
return newPosition
|
|
||||||
}
|
|
||||||
|
@ -41,6 +41,5 @@ type Game struct {
|
|||||||
|
|
||||||
type Robot struct {
|
type Robot struct {
|
||||||
id int
|
id int
|
||||||
comms chan Position
|
|
||||||
position Position
|
position Position
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user