Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/csh
- # # # # # # # # # # # # # # # # # #
- # The Arrange Numbers Game #
- # Scripted By Abdulaziz Al-Asiri #
- # # # # # # # # # # # # # # # # # #
- #-------------------------------------#
- # Welcome screen #
- #-------------------------------------#
- clear
- #----- Shows ( = ) one by one -----#
- set cntr = 1
- printf "\t"
- while ( $cntr <= 37 )
- sleep 0.02
- printf "="
- @ cntr++
- end
- echo ""
- #----- Shows welcome message word by word -----#
- sleep 0.2
- printf "\tWelcome "
- sleep 0.2
- printf "To "
- sleep 0.2
- printf "The "
- sleep 0.2
- printf "Arrange "
- sleep 0.2
- printf "Numbers "
- sleep 0.2
- printf "Game\n"
- #----- Shows ( = ) one by one -----#
- @ cntr = 1
- printf "\t"
- while ( $cntr <= 37 )
- sleep 0.02
- printf "="
- @ cntr++
- end
- echo ""
- #-------------------------------------#
- # Cheack if the level is in the range #
- #-------------------------------------#
- check_level:
- printf "Chose a level (3-10) : " # Asks user to chose a level between 3 and 10
- set level = $<
- if ($level > 10 || $level < 3) then # if the level is out of range re-ask the user again
- printf "Sorry the level must be between (3-10)\n"
- goto check_level
- endif
- #----------------------------------#
- # Set the blocks values #
- #----------------------------------#
- #----- Prepare to set the values
- set plate # Array Stores real values of blocks
- set i = 1 # Counter for loops
- set buffer # Array Stores temp values of plate
- @ j = $level * $level # The number of blocks
- @ range = $j - 1 # The number of blocks without blank (hole)
- set counter = 0 # Counts number of moves
- printf "Set the nambers randomly [R] or by hand [H] ? : " # default value is RANDOMLY
- set choice = $<
- #----- Set temp values of blocks -----#
- while ( $i < $j )
- set buffer = ( $buffer $i )
- @ i++
- end
- #----- Set the blocks values BY HAND -----#
- @ i = 1 # Re-set the counter
- if ( $choice == h || $choice == H) then
- while ( $i < $j ) # Asks user to enter blocks values one by one
- printf "Enter value of block $i [1 - $range] : "
- set value = $<
- if ( $value > 0 && $value < $j ) then # Checks if the entry value is out of rangr
- if ( $buffer[$value] == $value ) then # Checks if value is not used
- set plate = ( $plate $value ) # Adds the number to the plate array
- @ buffer[$value] = 0 # Sets value as used
- @ i++
- else
- printf "This number already exists .. enter another number \n"
- endif
- else
- printf "The number is out of range .. enter a number again \n"
- endif
- end
- #----- Set the blocks values RANDOMLY -----#
- else
- set rand = $$ # Give (rand) a random number (PID)
- while ( $i < $j )
- # Generates a random number
- set Random = `awk ' BEGIN { srand( '$rand' ); print int( rand() * '$range' + 1 ) } ' `
- @ rand++
- if ( ($buffer[$Random]) == 0 ) then # Checks if value is used then go back to generates
- continue
- endif
- set plate = ( $plate $Random ) # Adds the number to the plate array
- set buffer[$Random] = 0 # Sets value as used
- @ i++
- end
- endif
- set plate = ( $plate 0) # Add [ 0 ] at last block as a blank block (hole)
- #----------------------------------#
- # Shows the plate #
- #----------------------------------#
- set StartTime = `date +%s` # To save beginning time
- set min = 0 # To counts mins
- show_plate:
- clear
- @ i = 1 # Re-set the counter
- echo "" # New line (empty)
- while ( $i <= $j ) # Show the blocks and values
- if ( ( $i - 1 ) % $level == 0) then
- @ cntr = 1 # Re-set the counter
- printf "\t" # Prints tab at the beginning of horizontal lines
- while ( $cntr <= $level ) # Print the horizontal lines of plate
- printf "+------"
- @ cntr++
- end
- printf "+\n" # Close the end of horizontal lines
- printf "\t" # Print tab at the beginning of rows
- endif
- if ($plate[$i] == 0) then
- printf "| " # If block's value is 0 shows blank block (hole)
- else if ( $plate[$i] < 10 ) then
- printf "| $plate[$i] " # Print cell if the value just one digit
- else
- printf "| $plate[$i] " # Print cell if the value two digit
- endif
- if ( $i % $level == 0) then # Close the table and print a new line if the
- printf "|\n" # elements in a row equal to the number of a level
- endif
- @ i++
- end
- @ cntr = 1 # Re-set the counter
- printf "\t" # Prints tab at the beginning of last horizontal line
- while ( $cntr <= $level )
- printf "+------" # Print the last horizontal line of plate
- @ cntr++
- end
- printf "+\n\n" # Close the end of horizontal line and print new empty line
- #----------------------------------#
- # Check if the blocks are arranged #
- #----------------------------------#
- @ i = 1 # Re-set the counter
- set check_array = 1
- while ( ($plate[$i] == $i) && $i < $j) # Checks if every block in the right position
- @ check_array++
- @ i++
- end
- if ($check_array == $j) then # if check_array = j that means the plate is arranged so, finish
- set EndTime = `date +%s` # To saves end time
- @ EndTime = $EndTime - $StartTime # find the real time
- while ($EndTime > 59)
- @ EndTime = $EndTime - 60
- @ min++
- end
- printf "You win !\n"
- printf "You Took ($min min And $EndTime sec And $counter moves) to arrange the blocks\n"
- goto OUT
- endif
- #----------------------------------#
- # Get next movement #
- #----------------------------------#
- CHOSE:
- printf "Enter block number to move it , or [Q] to quit : "
- set num = $<
- set block = 0 # If the value do not change that means choice is out of range
- if ($num == q || $num == Q) then
- goto OUT # Quit
- endif
- #----------------------------------#
- # Get the position of block #
- #----------------------------------#
- set chk_num = 1 # Set the counter
- while ($chk_num <= $j)
- if ( $num == $plate[$chk_num]) then
- @ block = $chk_num # Gets the real coordinate of the block
- break
- endif
- @ chk_num++
- end
- #----------------------------------#
- # Check if the movement is correct #
- #----------------------------------#
- #----- First : check out the range -----#
- if ($block == 0) then
- goto wrong_movement
- endif
- #----- Second : check above the selected block -----#
- set temp_block = $block # Set temp_block
- set blocks_move = 0 # Set the counter
- # The selected block has to be between the second row and last row
- while ($temp_block > $level)
- @ temp_block = $temp_block - $level
- @ blocks_move++
- if ( $plate[$temp_block] == 0 ) then # If fined 0 means fined the hole
- goto MOVE_UP
- endif
- end
- #----- Third : check down the selected block -----#
- set temp_block = $block # Re-set temp_block
- set blocks_move = 0 # Re-set the counter
- # The selected block has to be between the first row and before-last row
- while ( $temp_block <= ($j - $level) )
- @ temp_block = $temp_block + $level
- @ blocks_move++
- if ( $plate[$temp_block] == 0 ) then # If fined 0 means fined the hole
- goto MOVE_DOWN
- endif
- end
- #----- Fourth : check right the selected block -----#
- set temp_block = $block # Re-set temp_block
- set blocks_move = 0 # Re-set the counter
- # The selected block has to be between the first column and before-last one
- while ( $temp_block % $level > 0)
- @ temp_block++
- @ blocks_move++
- if ( $plate[$temp_block] == 0 ) then # If fined 0 means fined the hole
- goto MOVE_RIGHT
- endif
- end
- #----- Fivth : check Left the selected block -----#
- set temp_block = $block # Re-set temp_block
- set blocks_move = 0 # Re-set the counter
- # The selected block has to be between the first column and before-last one
- while ( $temp_block % $level > 1 || $temp_block % $level == 0 )
- @ temp_block--
- @ blocks_move++
- if ( $plate[$temp_block] == 0 ) then # If fined 0 means fined the hole
- goto MOVE_LEFT
- endif
- end
- #----------------------------------#
- # movements #
- #----------------------------------#
- #----- Wrong movement -----#
- wrong_movement:
- printf "Sorry .. the movement is wrong! please chose again\n"
- goto CHOSE
- #----- Moves UP -----#
- MOVE_UP:
- set X = $temp_block # Temp variable
- while ($blocks_move > 0) # If there is still moves
- @ X = $X + $level
- @ plate[$temp_block] = $plate[$X]
- @ temp_block = $temp_block + $level
- @ blocks_move--
- end
- @ counter++ # Increments the moves counter
- set plate[$X] = 0 # Sets the (hole) in the new position
- goto show_plate
- #----- Moves DOWN -----#
- MOVE_DOWN:
- set X = $temp_block # Temp variable
- while ($blocks_move > 0) # If there is still moves
- @ X = $X - $level
- @ plate[$temp_block] = $plate[$X]
- @ temp_block = $temp_block - $level
- @ blocks_move--
- end
- @ counter++ # Increments the moves counter
- set plate[$X] = 0 # Sets the (hole) in the new position
- goto show_plate
- #----- Moves RIGHT -----#
- MOVE_RIGHT:
- set X = $temp_block # Temp variable
- while ($blocks_move > 0) # If there is still moves
- @ X = $X - 1
- @ plate[$temp_block] = $plate[$X]
- @ temp_block = $temp_block - 1
- @ blocks_move--
- end
- @ counter++ # Increments the moves counter
- set plate[$X] = 0 # Sets the (hole) in the new position
- goto show_plate
- #----- Moves LEFT -----#
- MOVE_LEFT:
- set X = $temp_block # Temp variable
- while ($blocks_move > 0) # If there is still moves
- @ X = $X + 1
- @ plate[$temp_block] = $plate[$X]
- @ temp_block = $temp_block + 1
- @ blocks_move--
- end
- @ counter++ # Increments the moves counter
- set plate[$X] = 0 # Sets the (hole) in the new position
- goto show_plate
- #----------------------------------#
- # F I N I S H #
- #----------------------------------#
- OUT:
- printf "\n\t+-----------------+\n"
- printf "\t| B Y E |\n"
- printf "\t+-----------------+\n\n"
- exit 0 # Exit without errors
- # EOF # :wq
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement