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

Published on 12 May 2024 09:40 PM

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

Source code in the autohotkey programming language

; Submitted by MasterFocus --- http://tiny.cc/iTunis

ScrambledList := CorrectList := "1 2 3 4 5 6 7 8 9" ; Declare two identical correct sequences
While ( ScrambledList = CorrectList )
  Sort, ScrambledList, Random D%A_Space% ; Shuffle one of them inside a While-loop to ensure it's shuffled

Attempts := 0
While ( ScrambledList <> CorrectList ) ; Repeat until the sequence is correct
{
  InputBox, EnteredNumber, Number Reversal Game, Attempts so far: %Attempts%`n`nCurrent sequence: %ScrambledList%`n`nHow many numbers (from the left) should be flipped?, , 400, 200
  If ErrorLevel
    ExitApp ; Exit if user presses ESC or Cancel
  If EnteredNumber is not integer
    Continue ; Discard attempt if entered number is not an integer
  If ( EnteredNumber <= 1 )
    Continue ; Discard attempt if entered number is <= 1
  Attempts++ ; Increase the number of attempts
  ; Reverse the first part of the string and add the second part
  ; The entered number is multiplied by 2 to deal with the spaces
  ScrambledList := Reverse(SubStr(ScrambledList,1,(EnteredNumber*2)-1)) SubStr(ScrambledList,EnteredNumber*2)
}

MsgBox, You took %Attempts% attempts to get the correct sequence. ; Final message

Return

;-------------------

Reverse(Str) ; Auxiliar function (flips a string)
{
  Loop, Parse, Str
    Out := A_LoopField Out
  Return Out
}


  

You may also check:How to resolve the algorithm Hailstone sequence step by step in the Scilab programming language
You may also check:How to resolve the algorithm Special characters step by step in the 8086 Assembly programming language
You may also check:How to resolve the algorithm Polynomial regression step by step in the VBA programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the COBOL programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Lingo programming language