Task: Number reversal game





Java: import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;


public class ReversalGame {
    private List<Integer> gameList;

    public ReversalGame() {
        initialize();
    }

    public void play() throws Exception {
        int i = 0;
        int moveCount = 0;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println(gameList);
            System.out.println("Please enter a index to reverse from 2 to 9. Enter 99 to quit");
            i = scanner.nextInt();
            if (i == 99) {
                break;
            }
            if (i < 2 || i > 9) {
                System.out.println("Invalid input");
            } else {
                moveCount++;
                reverse(i);
                if (isSorted()) {
                    System.out.println("Congratulations you solved this in " + moveCount + " moves!");
                    break;
                }
            }

        }
        scanner.close();
    }

    private void reverse(int position) {
        Collections.reverse(gameList.subList(0, position));
    }

    private boolean isSorted() {
        for (int i=0; i < gameList.size() - 1; ++i) {
            if (gameList.get(i).compareTo(gameList.get(i + 1)) > 0) {
                return false;
            }
        }
        return true;
    }

    private void initialize() {
        this.gameList = new ArrayList<Integer>(9);
        for (int i=1; i < 10; ++i) {
            gameList.add(i);
        }
        while (isSorted()) {
            Collections.shuffle(gameList);
        }
    }


    public static void main(String[] args) {
        try {
            ReversalGame game = new ReversalGame();
            game.play();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}







Ada: with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
with Ada.Numerics.Discrete_Random;
 
procedure NumberReverse is
 
   subtype RandRange is Integer range 1..9;
   type NumArrayType is array (Integer range 1..9) of Integer;
 
   package RandNumbers is new Ada.Numerics.Discrete_Random(RandRange);
   use RandNumbers;

   G : Generator;

   procedure FillArray (A : in out NumArrayType) is
      Temp : RandRange;
   begin
      A := (others => 0);
      for I in 1..9 loop
         Temp := Random(G);
         while A(Temp) /= 0 loop
            Temp := Random(G);
         end loop;
         A(Temp) := I;
      end loop;
   end FillArray;
 
   procedure Put(A : in NumArrayType) is
   begin
      for I in 1..9 loop
         Put(A(I), 0);
         Put(" ");
      end loop;
   end Put;
 
   procedure Prompt (Index : out Integer) is
   begin
      New_Line;
      Put("How many numbers would you like to reverse: ");
      Get(Index);
   end Prompt;
 
   procedure ReverseArray(Arr : in out NumArrayType;
                          Index : in Integer) is
      Temp : RandRange;
   begin
      for I in 1..Index/2 loop
         Temp := Arr(I);
         Arr(I) := Arr(Index + 1 - I);
         Arr(Index + 1 - I) := Temp;
      end loop;
   end ReverseArray;
 
   Sorted : constant NumArrayType := (1,2,3,4,5,6,7,8,9);
   Arr    : NumArrayType;
   Index  : Integer;
   Count  : Integer := 0;
begin
   Reset(G);
   loop
      FillArray(Arr);
      exit when Sorted /= Arr;
   end loop;
   loop
      Put(Arr);
      Prompt(Index);
      Count := Count + 1;
      ReverseArray(Arr, Index);
      exit when Sorted = Arr;
   end loop;
   Put(Arr);
   New_Line;
   Put("Congratulations! You win. It took " & 
         Integer'Image(Count) & " tries.");
end NumberReverse;