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();
        }
    }
}




VB: Private Function shuffle(ByVal a As Variant) As Variant
    Dim t As Variant, i As Integer
    For i = UBound(a) To LBound(a) + 1 Step -1
        j = Int((UBound(a) - LBound(a) + 1) * Rnd + LBound(a))
        t = a(i)
        a(i) = a(j)
        a(j) = t
    Next i
    shuffle = a
End Function
Private Sub reverse(ByRef a As Variant, n As Integer)
    Dim b As Variant
    b = a
    For i = 1 To n
        a(i) = b(n + 1 - i)
    Next i
End Sub
Public Sub game()
    Debug.Print "Given a jumbled list of the numbers 1 to 9"
    Debug.Print "you must select how many digits from the left to reverse."
    Debug.Print "Your goal is to get the digits in order with 1 on the left and 9 on the right."
     
    inums = [{1,2,3,4,5,6,7,8,9}]
    Dim nums As Variant
    Dim turns As Integer, flip As Integer
     
    shuffled = False
    Do While Not shuffled
        nums = shuffle(inums)
        For i = LBound(nums) To UBound(nums)
            If nums(i) <> inums(i) Then
                shuffled = True
                Exit For
            End If
        Next i
    Loop

     
    Do While True
        Debug.Print turns; ":";
        For Each x In nums: Debug.Print x;: Next x
        Debug.Print
        flag = False
        For i = LBound(nums) To UBound(nums)
            If nums(i) <> inums(i) Then
                flag = True
                Exit For
            End If
        Next i
        If flag Then
            flip = InputBox(" -- How many numbers should be flipped? ")
            reverse nums, flip
            turns = turns + 1
        Else
            Exit Do
        End If
    Loop
     
    Debug.Print "You took"; turns; "turns to put the digits in order."
End Sub