1 // jumpsaround.go - jumper game 2 // (c) 2020 Alexander Kulbartsch 3 // 4 // (slightly performace optimized version: valid jumpes checked bevore iteration.) 5 6 package main 7 8 import ( 9 "fmt" 10 ) 11 12 const SIZEX = 6 13 const SIZEY = 5 14 15 type boardt [SIZEX][SIZEY]int32 16 var board boardt 17 var jump = [8][2]int32{{-1,-2}, {1,-2}, {-2,-1}, {2,-1}, {-2,1}, {2,1}, {-1,2}, {1,2}} 18 var solutions int32 = 0 19 var roundtrips int32 = 0 20 21 22 func main() { 23 fmt.Println("Hello, Jumper!") 24 25 jumpto(0, 0, 1) 26 27 fmt.Println("I am done.") 28 } 29 30 31 func jumpto(x, y, iter int32) { 32 33 // ok - store position 34 board[x][y] = iter 35 36 // finished? 37 if iter >= SIZEX*SIZEY { 38 39 // check for jumper roundtrips 40 round := false 41 for _, d := range jump { 42 nx := x + d[0] 43 ny := y + d[1] 44 // check if position is on the board 45 if nx<0 || nx>(SIZEX-1) || ny<0 || ny>(SIZEY-1) { continue } 46 // detect roundtrip 47 if board[nx][ny] == 1 { round = true; roundtrips++; break } // True 48 } 49 50 solutions++ 51 52 printboard(round) 53 54 board[x][y] = 0 55 56 return 57 } 58 59 // try next jump 60 for _, d := range jump { 61 nx := x + d[0] 62 ny := y + d[1] 63 64 // check if position is on the board 65 if nx<0 || nx>(SIZEX-1) || ny<0 || ny>(SIZEY-1) { continue } 66 67 // check if new field position is still empty 68 if board[nx][ny] != 0 { continue } // True 69 70 jumpto(nx, ny, iter + 1); 71 } 72 73 // this iteration is done 74 board[x][y] = 0; 75 76 } 77 78 79 func printboard(round bool) { 80 fmt.Println("===== Solution #", solutions," - Roundtripps:", roundtrips, " ====== ") 81 if round == true { fmt.Println(" *** Roundtrip *** ") } 82 fmt.Println("") 83 for i := 0; i < SIZEY; i++ { 84 for j := 0; j < SIZEX; j++ { 85 fmt.Printf("%2d:", board[j][i]) 86 } 87 fmt.Println("") 88 } 89 fmt.Println("") 90 } 91 92 // EOF