Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2010
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.55 KB | None | 0 0
  1. #!/bin/csh
  2.  
  3. # # # # # # # # # # # # # # # # # #
  4. #    The Arrange Numbers Game     #
  5. #  Scripted By Abdulaziz Al-Asiri #
  6. # # # # # # # # # # # # # # # # # #
  7.  
  8.  
  9. #-------------------------------------#
  10. #           Welcome screen            #
  11. #-------------------------------------#
  12.  
  13. clear
  14. #----- Shows ( = ) one by one -----#
  15. set cntr = 1  
  16. printf  "\t"
  17. while ( $cntr <= 37 )
  18.     sleep 0.02
  19.     printf "="
  20.     @ cntr++
  21. end
  22. echo ""
  23.  
  24. #----- Shows welcome message word by word -----#
  25. sleep 0.2
  26. printf "\tWelcome "
  27. sleep 0.2
  28. printf "To "
  29. sleep 0.2
  30. printf "The "
  31. sleep 0.2
  32. printf "Arrange "
  33. sleep 0.2
  34. printf "Numbers "
  35. sleep 0.2
  36. printf  "Game\n"
  37.  
  38. #----- Shows ( = ) one by one -----#
  39. @ cntr = 1
  40. printf "\t"
  41. while ( $cntr <= 37 )
  42.     sleep 0.02
  43.     printf "="
  44.     @ cntr++
  45. end
  46. echo ""
  47.  
  48. #-------------------------------------#
  49. # Cheack if the level is in the range #
  50. #-------------------------------------#
  51.  
  52. check_level:
  53. printf "Chose a level (3-10) : " # Asks user to chose a level between 3 and 10
  54. set level = $<
  55. if ($level > 10 || $level < 3) then # if the level is out of range re-ask the user again
  56.     printf "Sorry the level must be between (3-10)\n"
  57.     goto check_level
  58. endif
  59.  
  60. #----------------------------------#
  61. #      Set the blocks values       #
  62. #----------------------------------#
  63.  
  64. #----- Prepare to set the values
  65. set plate             # Array Stores real values of blocks
  66. set i = 1             # Counter for loops
  67. set buffer             # Array Stores temp values of plate
  68. @ j = $level * $level         # The number of blocks
  69. @ range = $j - 1         # The number of blocks without blank (hole)
  70. set counter = 0         # Counts number of moves
  71.  
  72. printf "Set the nambers randomly [R] or by hand [H] ? : " # default value is RANDOMLY
  73. set choice = $<
  74.  
  75. #----- Set temp values of blocks -----#
  76. while ( $i < $j )
  77.     set buffer = ( $buffer $i )
  78.     @ i++
  79. end
  80.  
  81. #----- Set the blocks values BY HAND -----#
  82. @ i = 1             # Re-set the counter
  83. if ( $choice == h || $choice == H) then
  84.     while ( $i < $j )     # Asks user to enter blocks values one by one
  85.         printf "Enter value of block $i [1 - $range] : "
  86.         set value = $<
  87.         if ( $value > 0 && $value < $j ) then # Checks if the entry value is out of rangr
  88.             if ( $buffer[$value] ==  $value ) then # Checks if value is not used
  89.                 set plate = ( $plate $value ) # Adds the number to the plate array
  90.                 @ buffer[$value] = 0 # Sets value as used
  91.                 @ i++
  92.             else
  93.                 printf "This number already exists .. enter another number \n"
  94.             endif
  95.         else
  96.             printf "The number is out of range .. enter a number again \n"
  97.         endif
  98.        
  99.     end
  100.  
  101. #----- Set the blocks values RANDOMLY -----#
  102. else
  103.     set rand = $$         # Give (rand) a random number (PID)
  104.     while ( $i < $j )
  105.         # Generates a random number
  106.         set Random = `awk ' BEGIN { srand( '$rand' ); print int( rand() * '$range' + 1 ) } ' `  
  107.         @ rand++
  108.         if ( ($buffer[$Random]) == 0 ) then # Checks if value is used then go back to generates
  109.             continue
  110.         endif
  111.         set plate = ( $plate $Random ) # Adds the number to the plate array
  112.         set buffer[$Random] = 0 # Sets value as used
  113.         @ i++
  114.     end
  115. endif
  116.  
  117. set plate = ( $plate 0)     # Add [ 0 ] at last block as a blank block (hole)
  118.  
  119. #----------------------------------#
  120. #         Shows the plate          #
  121. #----------------------------------#
  122.  
  123. set StartTime = `date +%s`     # To save beginning time
  124. set min = 0             # To counts mins
  125.  
  126. show_plate:
  127. clear
  128. @ i = 1             # Re-set the counter
  129. echo ""             # New line (empty)
  130.  
  131. while ( $i <= $j )        # Show the blocks and values
  132.     if ( ( $i - 1 ) % $level == 0) then
  133.         @ cntr = 1     # Re-set the counter
  134.         printf "\t"     # Prints tab at the beginning of horizontal lines
  135.         while ( $cntr <= $level ) # Print the horizontal lines of plate
  136.             printf "+------"
  137.             @ cntr++
  138.         end
  139.         printf "+\n"     # Close the end of horizontal lines
  140.         printf "\t"     # Print tab at the beginning of rows
  141.     endif
  142.  
  143.     if ($plate[$i] == 0) then
  144.         printf "|      " # If block's value is 0 shows blank block (hole)
  145.     else if ( $plate[$i] < 10 ) then
  146.         printf "|   $plate[$i]  " # Print cell if the value just one digit
  147.     else
  148.         printf "|  $plate[$i]  " # Print cell if the value two digit
  149.     endif
  150.  
  151.     if ( $i % $level == 0) then # Close the table and print a new line if the
  152.         printf "|\n"         # elements in a row equal to the number of a level
  153.     endif
  154.     @ i++
  155. end
  156.  
  157. @ cntr = 1             # Re-set the counter
  158. printf "\t"             # Prints tab at the beginning of last horizontal line
  159. while ( $cntr <= $level )
  160.     printf "+------"     # Print the last horizontal line of plate
  161.     @ cntr++
  162. end
  163. printf "+\n\n"             # Close the end of horizontal line and print new empty line
  164.  
  165. #----------------------------------#
  166. # Check if the blocks are arranged #
  167. #----------------------------------#
  168.  
  169. @ i = 1             # Re-set the counter
  170. set check_array = 1
  171. while ( ($plate[$i] == $i) && $i < $j) # Checks if every block in the right position
  172.     @ check_array++
  173.     @ i++
  174. end
  175.  
  176. if ($check_array == $j) then          # if check_array = j that means the plate is arranged so, finish
  177.     set EndTime = `date +%s`     # To saves end time
  178.     @ EndTime = $EndTime - $StartTime # find the real time  
  179.     while ($EndTime > 59)
  180.             @ EndTime = $EndTime - 60
  181.             @ min++
  182.     end
  183.  
  184.     printf "You win !\n"
  185.     printf "You Took ($min min And $EndTime sec And $counter moves) to arrange the blocks\n"
  186.     goto OUT
  187. endif
  188.  
  189. #----------------------------------#
  190. #         Get next movement        #
  191. #----------------------------------#
  192.  
  193. CHOSE:
  194. printf "Enter block number to move it , or [Q] to quit : "
  195. set num = $<
  196. set block = 0             # If the value do not change that means choice is out of range
  197.  
  198. if ($num == q || $num == Q) then
  199.     goto OUT # Quit
  200. endif
  201.  
  202. #----------------------------------#
  203. #    Get the position of block     #
  204. #----------------------------------#
  205.  
  206. set chk_num = 1         # Set the counter
  207. while ($chk_num <= $j)
  208.     if ( $num == $plate[$chk_num]) then
  209.         @ block = $chk_num # Gets the real coordinate of the block
  210.         break
  211.     endif
  212.         @ chk_num++
  213. end
  214.  
  215. #----------------------------------#
  216. # Check if the movement is correct #
  217. #----------------------------------#
  218.  
  219. #----- First : check out the range -----#
  220. if ($block == 0) then
  221.     goto wrong_movement
  222. endif
  223.  
  224. #----- Second : check above the selected block -----#
  225. set temp_block = $block     # Set temp_block
  226. set blocks_move = 0         # Set the counter
  227.  
  228. # The selected block has to be between the second row and last row
  229. while ($temp_block > $level)
  230.     @ temp_block = $temp_block - $level
  231.     @ blocks_move++
  232.     if ( $plate[$temp_block] == 0 ) then # If fined 0 means fined the hole
  233.         goto MOVE_UP
  234.     endif
  235. end
  236.  
  237. #----- Third : check down the selected block -----#
  238. set temp_block = $block     # Re-set temp_block
  239. set blocks_move = 0         # Re-set the counter
  240.  
  241. # The selected block has to be between the first row and before-last row
  242. while ( $temp_block <= ($j - $level) )
  243.     @ temp_block = $temp_block + $level
  244.     @ blocks_move++
  245.     if ( $plate[$temp_block] == 0 ) then # If fined 0 means fined the hole
  246.         goto MOVE_DOWN
  247.     endif
  248. end
  249.  
  250. #----- Fourth : check right the selected block -----#
  251. set temp_block = $block     # Re-set temp_block
  252. set blocks_move = 0         # Re-set the counter
  253.  
  254. # The selected block has to be between the first column and before-last one
  255. while ( $temp_block % $level > 0)
  256.     @ temp_block++
  257.     @ blocks_move++
  258.     if ( $plate[$temp_block] == 0 ) then # If fined 0 means fined the hole
  259.         goto MOVE_RIGHT
  260.     endif
  261. end
  262.  
  263. #----- Fivth : check Left the selected block -----#
  264. set temp_block = $block     # Re-set temp_block
  265. set blocks_move = 0         # Re-set the counter
  266.  
  267. # The selected block has to be between the first column and before-last one
  268. while ( $temp_block % $level > 1 || $temp_block % $level == 0 )
  269.     @ temp_block--
  270.     @ blocks_move++
  271.     if ( $plate[$temp_block] == 0 ) then # If fined 0 means fined the hole
  272.         goto MOVE_LEFT
  273.     endif
  274. end
  275.  
  276.  
  277. #----------------------------------#
  278. #             movements            #
  279. #----------------------------------#
  280.  
  281. #----- Wrong movement -----#
  282. wrong_movement:
  283. printf "Sorry .. the movement is wrong! please chose again\n"
  284. goto CHOSE
  285.  
  286. #----- Moves UP -----#
  287. MOVE_UP:
  288. set X = $temp_block         # Temp variable
  289. while ($blocks_move > 0)     # If there is still moves
  290.     @ X = $X + $level
  291.     @ plate[$temp_block] = $plate[$X]
  292.     @ temp_block = $temp_block + $level
  293.     @ blocks_move--
  294. end
  295.  
  296. @ counter++             # Increments the moves counter
  297. set plate[$X] = 0         # Sets the (hole) in the new position
  298. goto show_plate
  299.  
  300. #----- Moves DOWN -----#
  301. MOVE_DOWN:
  302. set X = $temp_block         # Temp variable
  303. while ($blocks_move > 0)     # If there is still moves
  304.     @ X = $X - $level
  305.     @ plate[$temp_block] = $plate[$X]
  306.     @ temp_block = $temp_block - $level
  307.     @ blocks_move--
  308. end
  309.  
  310. @ counter++             # Increments the moves counter
  311. set plate[$X] = 0         # Sets the (hole) in the new position
  312. goto show_plate
  313.  
  314. #----- Moves RIGHT -----#
  315. MOVE_RIGHT:
  316. set X = $temp_block        # Temp variable
  317. while ($blocks_move > 0)     # If there is still moves
  318.     @ X = $X - 1
  319.     @ plate[$temp_block] = $plate[$X]
  320.     @ temp_block = $temp_block - 1
  321.     @ blocks_move--
  322. end
  323.  
  324. @ counter++             # Increments the moves counter
  325. set plate[$X] = 0        # Sets the (hole) in the new position
  326. goto show_plate
  327.  
  328. #----- Moves LEFT -----#
  329. MOVE_LEFT:
  330. set X = $temp_block         # Temp variable
  331. while ($blocks_move > 0)     # If there is still moves
  332.     @ X = $X + 1
  333.     @ plate[$temp_block] = $plate[$X]
  334.     @ temp_block = $temp_block + 1
  335.     @ blocks_move--
  336. end
  337.  
  338. @ counter++              # Increments the moves counter
  339. set plate[$X] = 0        # Sets the (hole) in the new position
  340. goto show_plate    
  341.  
  342. #----------------------------------#
  343. #           F I N I S H            #
  344. #----------------------------------#
  345.  
  346. OUT:
  347. printf "\n\t+-----------------+\n"
  348. printf "\t|      B Y E      |\n"
  349. printf "\t+-----------------+\n\n"
  350.  
  351. exit 0                 # Exit without errors
  352.  
  353. # EOF #                                            :wq
  354.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement