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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Number reversal game step by step in the Eiffel 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 Eiffel programming language

Source code in the eiffel programming language

class
	APPLICATION

create
	make

feature {NONE}

	make
			-- Plays Number Reversal Game.
		local
			count: INTEGER
		do
			initialize_game
			io.put_string ("Let's play the number reversal game.%N")
			across
				numbers as ar
			loop
				io.put_string (ar.item.out + "%T")
			end
			from
			until
				is_sorted (numbers, 1, numbers.count)
			loop
				io.put_string ("%NHow many numbers should be reversed?%N")
				io.read_integer
				reverse_array (io.last_integer)
				across
					numbers as ar
				loop
					io.put_string (ar.item.out + "%T")
				end
				count := count + 1
			end
			io.put_string ("%NYou needed " + count.out + " reversals.")
		end

feature {NONE}

	initialize_game
			-- Array with numbers from 1 to 9 in a random unsorted order.
		local
			random: V_RANDOM
			item, i: INTEGER
		do
			create random
			create numbers.make_empty
			from
				i := 1
			until
				numbers.count = 9 and not is_sorted (numbers, 1, numbers.count)
			loop
				item := random.bounded_item (1, 9)
				if not numbers.has (item) then
					numbers.force (item, i)
					i := i + 1
				end
				random.forth
			end
		end

	numbers: ARRAY [INTEGER]

	reverse_array (upper: INTEGER)
			-- Array numbers with first element up to nth element reversed.
		require
			upper_positive: upper > 0
			ar_not_void: numbers /= Void
		local
			i, j: INTEGER
			new_array: ARRAY [INTEGER]
		do
			create new_array.make_empty
			new_array.deep_copy (numbers)
			from
				i := 1
				j := upper
			until
				i > j
			loop
				new_array [i] := numbers [j]
				new_array [j] := numbers [i]
				i := i + 1
				j := j - 1
			end
			numbers := new_array
		end

	is_sorted (ar: ARRAY [INTEGER]; l, r: INTEGER): BOOLEAN
			-- Is Array 'ar' sorted in ascending order?
		require
			ar_not_empty: not ar.is_empty
		do
			Result := True
			across
				1 |..| (r - 1) as c
			loop
				if ar [c.item] > ar [c.item + 1] then
					Result := False
				end
			end
		end

end


  

You may also check:How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the D programming language
You may also check:How to resolve the algorithm Partition an integer x into n primes step by step in the C programming language
You may also check:How to resolve the algorithm Arithmetic numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm Increasing gaps between consecutive Niven numbers step by step in the Raku programming language
You may also check:How to resolve the algorithm Sorting Algorithms/Circle Sort step by step in the 11l programming language