Compare commits
3 Commits
v20231212
...
Go-Routine
Author | SHA1 | Date | |
---|---|---|---|
06aa9b54c7 | |||
76e745801a | |||
e1ce42614c |
24
.gitignore
vendored
24
.gitignore
vendored
@ -1,23 +1 @@
|
|||||||
# ---> Go
|
builds
|
||||||
# If you prefer the allow list template instead of the deny list, see community template:
|
|
||||||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
|
||||||
#
|
|
||||||
# Binaries for programs and plugins
|
|
||||||
*.exe
|
|
||||||
*.exe~
|
|
||||||
*.dll
|
|
||||||
*.so
|
|
||||||
*.dylib
|
|
||||||
|
|
||||||
# Test binary, built with `go test -c`
|
|
||||||
*.test
|
|
||||||
|
|
||||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
|
||||||
*.out
|
|
||||||
|
|
||||||
# Dependency directories (remove the comment below to include it)
|
|
||||||
# vendor/
|
|
||||||
|
|
||||||
# Go workspace file
|
|
||||||
go.work
|
|
||||||
|
|
10
build.sh
Executable file
10
build.sh
Executable file
@ -0,0 +1,10 @@
|
|||||||
|
#!/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,7 +73,6 @@ 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
|
||||||
@ -171,8 +170,8 @@ func quit(game *Game) {
|
|||||||
func (game *Game) randPos() Position {
|
func (game *Game) randPos() Position {
|
||||||
var pos Position
|
var pos Position
|
||||||
|
|
||||||
x := 2 + rand.Intn(76)
|
x := 1 + rand.Intn(77)
|
||||||
y := 2 + rand.Intn(21)
|
y := 1 + rand.Intn(22)
|
||||||
|
|
||||||
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 != 2 {
|
if game.player.position.x != 1 {
|
||||||
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 != 2 && game.player.position.y != 1 {
|
if game.player.position.x != 1 && 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++
|
||||||
|
@ -3,6 +3,12 @@ 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 {
|
||||||
@ -11,11 +17,10 @@ func (game *Game) initRobots() {
|
|||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rndRobot := Robot{
|
|
||||||
id: i,
|
robot.position = rndPos
|
||||||
position: rndPos,
|
game.robots = append(game.robots, robot)
|
||||||
}
|
go game.Robot(robot)
|
||||||
game.robots = append(game.robots, rndRobot)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,21 +31,15 @@ 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, r := range game.robots {
|
for i, _ := range game.robots {
|
||||||
|
|
||||||
// Determine in which direction to go
|
game.robots[i].comms <- game.player.position
|
||||||
if game.player.position.x < r.position.x {
|
robotPos = <-game.robots[i].comms
|
||||||
game.robots[i].position.x--
|
game.robots[i].position = robotPos
|
||||||
} 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++
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,14 +88,66 @@ 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 != r {
|
if robot.id != r.id {
|
||||||
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,5 +41,6 @@ type Game struct {
|
|||||||
|
|
||||||
type Robot struct {
|
type Robot struct {
|
||||||
id int
|
id int
|
||||||
|
comms chan Position
|
||||||
position Position
|
position Position
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user