1 # jumpsaround - jumper game 2 # (c) 2021-2021 Alexander Kulbartsch 3 4 Size_x = 6 5 Size_y = 5 6 7 Jump = [[-1,-2], [1,-2], [-2,-1], [2,-1], [-2,1], [2,1], [-1,2], [1,2]] 8 9 10 class Jumper 11 12 def initialize 13 @solutions = 0 14 @roundtrips = 0 15 #@test = Array(Array(Int16)).new() # .new(Size_x * Size_y) { 0 } # ??? 16 @board = [[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]] #0_i16 17 end 18 19 def jumpto(x, y, iter) 20 21 # ok - store position 22 @board[x][y] = iter; 23 24 # finished? 25 if iter >= Size_x*Size_y 26 # check for jumper roundtrips 27 round = false 28 Jump.each{ |jumpx| 29 nx = x + jumpx[0] 30 ny = y + jumpx[1] 31 # check if position is on the board 32 next if nx<0 || nx>(Size_x-1) || ny<0 || ny>(Size_y-1) 33 # detect roundtrip 34 if @board[nx][ny] == 1 35 round = true 36 @roundtrips += 1 37 end 38 break if round 39 } 40 @solutions += 1 41 printboard(round) # if round # <- cond statement at end of line 42 @board[x][y] = 0 43 end 44 45 # try next jump 46 Jump.each{ |jumpx| 47 # new position 48 nx = x + jumpx[0] 49 ny = y + jumpx[1] 50 # check if new position is on the board 51 next if nx<0 || nx>(Size_x-1) || ny<0 || ny>(Size_y-1) 52 # check if new field position is still empty 53 next if @board[nx][ny] != 0 54 jumpto(nx, ny, iter + 1); 55 } 56 57 # this iteration is done 58 @board[x][y] = 0 59 60 puts "===== Solution # #{@solutions} - Roundtripps: #{@roundtrips} ====== " if iter == 0 61 end 62 63 def printboard(round) 64 puts "===== Solution # #{@solutions} - Roundtripps: #{@roundtrips} ====== " 65 #if round 66 puts " *** Roundtrip #{@roundtrips} *** " if round 67 (0..(Size_y-1)).each do |i| 68 (0..(Size_x-1)).each do |j| 69 print "%02d:" % @board[j][i] 70 end 71 puts 72 end 73 # @board.each{ |boardy| 74 # boardy.each{ |boardxy| 75 # print "%02d:" % boardxy 76 # } 77 # puts 78 # } 79 puts "" 80 #end 81 end 82 83 end 84 85 puts "Hello, Jumper!" 86 my_jumper = Jumper.new() 87 my_jumper.jumpto(0, 0, 1) 88 puts "I am done." 89 90 # EOF