Task: Penney's game



C#: using static System.Console;
using static System.Threading.Thread;
using System;

public static class PenneysGame
{
    const int pause = 500;
    const int N = 3;
    static Random rng = new Random();

    static int Toss() => rng.Next(2);

    static string AsString(this int sequence) {
        string s = "";
        for (int b = 0b100; b > 0; b >>= 1) {
            s += (sequence & b) > 0 ? 'T' : 'H';
        }
        return s;
    }

    static int UserInput() {
        while (true) {
            switch (ReadKey().Key) {
                case ConsoleKey.Escape: return -1;
                case ConsoleKey.H: return 0;
                case ConsoleKey.T: return 1;
            }
            Console.Write('\b');
        }
    }

    public static void Main2() {
        int yourScore = 0, myScore = 0;
        while (true) {
            WriteLine($"Your score: {yourScore}, My score: {myScore}");
            WriteLine("Determining who goes first...");
            Sleep(pause);
            bool youStart = Toss() == 1;
            WriteLine(youStart ? "You go first." : "I go first.");
            int yourSequence = 0, mySequence = 0;
            if (youStart) {
                WriteLine("Choose your sequence of (H)eads and (T)ails (or press Esc to exit)");
                int userChoice;
                for (int i = 0; i < N; i++) {
                    if ((userChoice = UserInput()) < 0) return;
                    yourSequence = (yourSequence << 1) + userChoice;
                }
                mySequence = ((~yourSequence << 1) & 0b100) | (yourSequence >> 1);
            } else {
                for (int i = 0; i < N; i++) {
                    mySequence = (mySequence << 1) + Toss();
                }

                WriteLine("I chose " + mySequence.AsString());
                do {
                    WriteLine("Choose your sequence of (H)eads and (T)ails (or press Esc to exit)");
                    int choice;
                    yourSequence = 0;
                    for (int i = 0; i < N; i++) {
                        if ((choice = UserInput()) < 0) return;
                        yourSequence = (yourSequence << 1) + choice;
                    }
                    if (yourSequence == mySequence) {
                        WriteLine();
                        WriteLine("You cannot choose the same sequence.");
                    }
                } while (yourSequence == mySequence);
            }

            WriteLine();
            WriteLine($"Your sequence: {yourSequence.AsString()}, My sequence: {mySequence.AsString()}");
            WriteLine("Tossing...");
            int sequence = 0;
            for (int i = 0; i < N; i++) {
                Sleep(pause);
                int toss = Toss();
                sequence = (sequence << 1) + toss;
                Write(toss > 0 ? 'T' : 'H');
            }
            while (true) {
                if (sequence == yourSequence) {
                    WriteLine();
                    WriteLine("You win!");
                    yourScore++;
                    break;
                } else if (sequence == mySequence) {
                    WriteLine();
                    WriteLine("I win!");
                    myScore++;
                    break;
                }
                Sleep(pause);
                int toss = Toss();
                sequence = ((sequence << 1) + toss) & 0b111;
                Write(toss > 0 ? 'T' : 'H');
            }
            WriteLine("Press a key.");
            ReadKey();
            Clear();
        }
    }

}






VB: Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const HT As String = "H T"

Public Sub PenneysGame()
Dim S$, YourSeq$, ComputeSeq$, i&, Seq$, WhoWin$, flag As Boolean

    Do
        S = WhoWillBeFirst(Choice("Who will be first"))
        If S = "ABORT" Then Exit Do
        Debug.Print S; " start"
        YourSeq = Choice("Your sequence", 3)
        If YourSeq Like "*ABORT*" Then Exit Do
        Debug.Print "Your sequence : " & YourSeq
        ComputeSeq = vbNullString
        For i = 1 To 3
            ComputeSeq = ComputeSeq & Random
        Next i
        Debug.Print "Computer sequence : " & ComputeSeq
        Seq = vbNullString
        Do
            Seq = Seq & Random
            Debug.Print Seq
            Sleep 1000
        Loop While Not Winner(ComputeSeq, YourSeq, Seq, WhoWin)
        Debug.Print WhoWin; " win"
        If MsgBox(WhoWin & " win" & vbCrLf & "Play again?", vbYesNo) = vbNo Then flag = True
        Debug.Print ""
    Loop While Not flag
    Debug.Print "Game over"
End Sub

Private Function WhoWillBeFirst(YourChoice As String) As String
Dim S$
    S = Random
    Select Case YourChoice
        Case "ABORT": WhoWillBeFirst = YourChoice
        Case Else:
            WhoWillBeFirst = IIf(S = YourChoice, "You", "Computer")
    End Select
End Function

Private Function Choice(Title As String, Optional Seq As Integer) As String
Dim S$, i&, t$
    If Seq = 0 Then Seq = 1
    t = Title
    For i = 1 To Seq
        S = vbNullString
        Do
            S = InputBox("Choose between H or T : ", t)
            If StrPtr(S) = 0 Then S = "Abort"
            S = UCase(S)
        Loop While S <> "H" And S <> "T" And S <> "ABORT"
        Choice = Choice & S
        t = Title & " " & Choice
        If Choice Like "*ABORT*" Then Exit For
    Next i
End Function

Private Function Random() As String
Randomize Timer
    Random = Split(HT, " ")(CInt(Rnd))
End Function

Private Function Winner(Cs$, Ys$, S$, W$) As Boolean
    If Len(S) < 3 Then
        Winner = False
    Else
        If Right(S, 3) = Cs And Right(S, 3) = Ys Then
            Winner = True
            W = "Computer & you"
        ElseIf Right(S, 3) = Cs And Right(S, 3) <> Ys Then
            Winner = True
            W = "Computer"
        ElseIf Right(S, 3) = Ys And Right(S, 3) <> Cs Then
            Winner = True
            W = "You"
        End If
    End If
End Function