How to resolve the algorithm Number reversal game step by step in the UNIX Shell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Number reversal game step by step in the UNIX Shell programming language

Table of Contents

Problem Statement

Given a jumbled list of the numbers   1   to   9   that are definitely   not   in ascending order. Show the list,   and then ask the player how many digits from the left to reverse. Reverse those digits,   then ask again,   until all the digits end up in ascending order.

The score is the count of the reversals needed to attain the ascending order.

Note: Assume the player's input does not need extra validation.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Number reversal game step by step in the UNIX Shell programming language

Source code in the unix programming language

print "\nWelcome to the number reversal game!\n"

print "You must put the numbers in order from 1 to 9."
print "Your only moves are to reverse groups of numbers"
print "on the left side of the list."

integer list score

# start a new game
function newgame {
	integer i j t

	# score = number of moves
	((score = 0))

	# list = array of numbers
	set -A list 1 2 3 4 5 6 7 8 9
	while true; do
		# Knuth shuffle
		((i = 9))
		while ((i > 1)); do
			# get random j from 0 to i - 1
			((j = RANDOM))
			((j < 32768 % i)) && continue
			((j %= i))

			# decrement i, swap list[i] with list[j]
			((i -= 1))
			((t = list[i]))
			((list[i] = list[j]))
			((list[j] = t))
		done
		win || break
	done
}

# numbers in order?
function win {
	integer i

	# check if list[0] == 1, list[1] == 2, ...
	((i = 0))
	while ((i < 9)); do
		((list[i] != i + 1)) && return 1
		((i += 1))
	done
	return 0
}

# reverse first $1 elements of list
function reverse {
	integer left right t

	((left = 0))
	((right = $1 - 1))
	while ((right > left)); do
		((t = list[left]))
		((list[left] = list[right]))
		((list[right] = t))
		((left += 1))
		((right -= 1))
	done
}


integer i

newgame
while true; do
	print -n "\nYour list: "
	((i = 0))
	while ((i < 8)); do
		printf "%d, " ${list[i]}
		((i += 1))
	done
	printf "%d\n" ${list[8]}

	if win; then
		print "YOU WIN!"
		printf "Your score is %d moves.\n" $score
		print -n "Would you like to play again (y/n)? "

		if read again && [[ $again = @(y|Y)* ]]; then
			newgame
		else
			print "\n\nBye!"
			exit
		fi
	else
		print -n "How many numbers to reverse? "

		if read i; then
			((score += 1))
			reverse $i
		else
			print "\n\nBye!"
			exit
		fi
	fi
done


  

You may also check:How to resolve the algorithm LZW compression step by step in the Dylan programming language
You may also check:How to resolve the algorithm Order two numerical lists step by step in the Klingphix programming language
You may also check:How to resolve the algorithm Idiomatically determine all the lowercase and uppercase letters step by step in the Java programming language
You may also check:How to resolve the algorithm Find the intersection of a line with a plane step by step in the D programming language
You may also check:How to resolve the algorithm Variables step by step in the SPL programming language