Мартин обнови решението на 19.01.2014 16:15 (преди над 4 години)
+package main
+
+import (
+ "sync"
+)
+
+type Game struct {
+ Mu sync.Mutex
+ Mall [4][4]rune
+ Moves [][2][2]int
+}
+
+func playMall(mall [4][4]rune) [][2][2]int {
+ var wg sync.WaitGroup
+ game := &Game{Mu: sync.Mutex{}, Mall: mall}
+ for x, slice := range mall {
+ for y, info := range slice {
+ if info == 'X' {
+ wg.Add(1)
+ go func(x int, y int) {
+ oldPos := [2]int{x, y}
+ var i int
+ for i = 0; i < 100; i++ {
+ game.Mu.Lock()
+ pos := getNextPos(game.Mall, oldPos)
+ game.Moves = append(game.Moves, [2][2]int{oldPos, pos})
+ game.Mu.Unlock()
+ if pos[0] == -2 && pos[1] == -2 {
+ break
+ }
+ game.Mu.Lock()
+ game.Mall[oldPos[0]][oldPos[1]] = '-'
+ game.Mall[pos[0]][pos[1]] = 'X'
+ game.Mu.Unlock()
+ oldPos = pos
+ }
+ if i==100 {
+ game.Mu.Lock()
+ game.Moves = append(game.Moves, [2][2]int{oldPos, {-1, -1}})
+ game.Mu.Unlock()
+ }
+ wg.Done()
+ }(x, y)
+ }
+ }
+ }
+ wg.Wait()
+ return game.Moves
+}
+
+func getNextPos(mall [4][4]rune, unit [2]int) [2]int {
+ toMove := [][2]int{{-1,-1}, {1,1}, {-1,1}, {1, -1}}
+ for _, move := range toMove {
+ nextPos := [2]int{(unit[0]+move[0]+len(mall))%len(mall), (unit[1]+move[1]+len(mall))%len(mall)}
+ if mall[nextPos[0]][nextPos[1]] == '-' {
+ return nextPos
+ }
+ }
+ return [2]int{-2, -2}
+}
+
+func main() {
+
+}