Monthly Archives: April 2017

STOP the game

Back in the good old days of DOS and Microsoft BASIC (MSBASIC), the BASIC written by Bill Gates himself, someone somewhere held a coding competition: who can write the ‘smallest playable game’. I still remember this game, although I do not remember if it won or not, but here it is, modified to be runnable with Visual Basic, under Visual Studio.

How to play?
All you have to do is run the code. On the console window, wait for the GO, then try typing the word STOP as fast as you can. You have 1 second. If you are able to type STOP in under 1 second, you win and your typing time is displayed, other wise you lose. But then, you can always run it again.

How to write the code?
Easy, in Visual Studio, or any VB, create a new project (File -> New Project) then select VB on the left pane finally select CONSOLE APPLICATION on the right, then OK. Now copy the whole code from below and paste as-is in the page MODULE1.VB. You won’t need to use any of the imports so paste replacing all. Finally, click the function key [F5] to run the code and wait for the GO signal. Start typing!

Minimal computer requirement: CPM, 8086 4 MHZ CPU running Microsoft DOS and 512K of RAM.

Enjoy.

Compliment of Albert Zakhia.

The below code has been modified on April the 18th, 2017, adding more hints to the game.


Module Module1
Sub Main()
Dim s = ""
Dim t = New Stopwatch
Dim pos As Int16
Dim key As Char
While True
Console.WriteLine("Press [X] to exit or any key to start!")
key = Console.ReadKey().KeyChar
If Char.ToUpper(key) = "X" Then Exit While
pos = 1
Console.WriteLine("GO!")
Console.SetCursorPosition(11, Console.CursorTop)
Console.Write("|")
t.Restart()
While Not s.ToLower().Contains("stop") And t.ElapsedMilliseconds <= 1000
If Console.KeyAvailable Then s = s & Console.ReadKey().KeyChar
Console.SetCursorPosition(pos, Console.CursorTop)
pos = pos + 1
Threading.Thread.Sleep(100)
End While
If t.ElapsedMilliseconds > 1000 Then
Console.WriteLine("X - Timeout, you lose")
Else
Console.WriteLine("{0}Great! You typed: {1} in {2} seconds", vbCrLf, s, t.ElapsedMilliseconds / 1000)
End If
While Console.KeyAvailable
Console.ReadKey()
End While
End While
End Sub
End Module

Displaying Hello World unconventionally by using a Random Number Generator

A very good friend of mine, with whom I enjoy discussing coding techniques, brought to my attention unfamiliar ways of displaying the infamous “Hello World”. Displaying “hello world” is usually the first program anyone tries in a new language.

The code below shows how we can display the said string by the use of a random number generator.

 

System.out.println(randomString(-229985452) + " " + randomString(-147909649));

public static String randomString(int i)
{
    Random ran = new Random(i);
    StringBuilder sb = new StringBuilder();
    while (true)
    {
        int k = ran.nextInt(27);
        if (k == 0)
            break;

        sb.append((char)('`' + k));
    }

    return sb.toString();
}

 

How does it work? Well let’s see your best guess. Shouldn’t be that hard.

Original post:

http://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world