海と川とそら
  • Blog
  • mobile-app
  • literacy
  • えひめの由来
  • 伊予のゆ
  • ふるいコンピューター・プログラミング
  • 島々
  • BASIC 256
  • Perfect numbers

15 Puzzle Game

November 13, 2019 at 9:58 am, No comments

BASIC 256


Rosetta Code から、[Liberty BASIC] のコードをもとにBASIC 256 で動くように書き換えたのが、下のコード。これは不完全なコード。


## 遊び方


ゲームをはじめると、次のようなテキスト画面がでる。ゲームのレベルは、「1 (easy)」を選ぶ。中級と上級レベルはテストしていない。


15 PUZZLE GAME

Please enter level of difficulty,

1 (easy), 2 (medium) or 3 (hard): 


この初級レベルでもエラーがでることがある。発見したら、コードを修正してほしい。


# 15-PUZZLE GAME
# ********************************
global d,ds$,sh,level,x,y,z
dim d(17)
dim ds$(17) # Board pieces
dim sh(4)
call introAndLevel
call buildBoard
call printPuzzle
do
  print "To move a piece, enter its number: "
  input x
  while isMoveValid(x) = 0
    print "Wrong move."
    call printPuzzle
    print "To move a piece, enter its number: "
    input x
  end while
  d[z] = x
  d[y] = 0
  call printPuzzle 
until isPuzzleComplete()
print "YOU WON!"
end
 
subroutine printPuzzle()
  for p = 1 to 16
    if d[p] = 0 then
      ds$[p] = "     "
    else
      s$ = string(d[p])
      ds$[p] = left("   ", 3 - length(s$)) + s$ + "  "
    end if
  next p
  print "+-----+-----+-----+-----+"
  print "|"; ds$[1]; "|"; ds$[2]; "|"; ds$[3]; "|"; ds$[4]; "|"
  print "+-----+-----+-----+-----+"
  print "|"; ds$[5]; "|"; ds$[6]; "|"; ds$[7]; "|"; ds$[8]; "|"
  print "+-----+-----+-----+-----+"
  print "|"; ds$[9]; "|"; ds$[10]; "|";ds$[11]; "|"; ds$[12]; "|"
  print "+-----+-----+-----+-----+"
  print "|"; ds$[13]; "|"; ds$[14]; "|"; ds$[15]; "|"; ds$[16]; "|"
  print "+-----+-----+-----+-----+"
end subroutine
 
subroutine introAndLevel()
  cls
  sh[1] = 10
  sh[2] = 50
  sh[3] = 100
  print "15 PUZZLE GAME"
  print
  print
  do
  print "Please enter level of difficulty,"
  print "1 (easy), 2 (medium) or 3 (hard): ";
  input level
  until (1<=level) or (level<=3) 
end subroutine
 
subroutine buildBoard()
 # Set pieces in correct order first
  for p = 1 to 15
    d[p] = p
  next p
  d[16] = 0  # 0 = empty piece/slot
  z = 16    # z = empty position
  print
  print "Shuffling pieces";
  for n = 1 to sh[level]
    print ".";
   
    do
      x = int(rand() * 4) + 1
      begin case
        case x=1
          r = z - 4
        case x=2
          r = z + 4
        case x=3
          if (z - 1) % 4 <> 0 then
            r = z - 1
          end if
        case x=4
          if z % 4 <> 0 then
            r = z + 1
          end if
      end case
    until (r>=1) and (r<=16)
 
    d[z] = d[r]
    z = r
    d[z] = 0
  next n
  cls
end subroutine
 
function isMoveValid(ax)
  mv = 0
  if (ax >= 1) or (ax <= 15) then
    # Find position of piece ax
    p = 1
    while d[p] <> ax
      p = p + 1
      if p > 16 then
        print "UH OH!"
        end
      end if
    end while
    ay = p:y = p
    # Find position of empty piece
    p = 1
    while d[p] <> 0
      p = p + 1
      if p > 16 then
        print "UH OH!"
        end
      end if
    end while
    az = p:z=p
    # Check if empty piece is above, below ...
    if (ay - 4 = az) or (ay + 4 = az) or ((ay - 1 = az) and (az % 4 <> 0)) or ((ay + 1 = az) and (ay % 4 <> 0)) then
      mv = 1
    end if
  end if
  isMoveValid = mv
end function
 
function isPuzzleComplete()
  pc = 0
  p = 1
  while (p < 16) and (d[p] = p)
    p = p + 1
  end while
  if p = 16 then
    pc = 1
  end if
  isPuzzleComplete = pc
end function

No comments

Leave a reply







Recent Posts

  • キンドル本か紙の本か?注文するとき
    24 Mar, 2020
  • 前知事が亡くなった
    24 Mar, 2020
  • ガソリン価格の低下
    24 Mar, 2020
  • 小松左京『明日泥棒』
    19 Mar, 2020
  • 注文していたコーヒーメーカーが届いた
    17 Mar, 2020
  • ひまなのでユークリッドが証明したことを計算して眺める
    15 Mar, 2020
  • コロナウイルスのたんぱく質分解酵素
    14 Mar, 2020
Created with Mozello - the world's easiest to use website builder.

Create your website or online store with Mozello

Quickly, easily, without programming.

Report abuse Learn more