Sunday 28 October 2012

Heimcomputer

Commodore 64 startup screen
Back in April I mentioned that back in 1998 (this sounds recursive) I had stumbled on some VBScript examples on my old Aptiva. In much the same way any opportunity to learn French in school was quashed by the limited scope of phrasebook teaching and the absence of anyone to actually speak to in the language, my exposure to VBScript yielded nothing more than the usual surface familiarity. When the PlaySation 2 launched Sony tried to pass it off as a computer by bundling YABASIC and thereby secure certain tax advantages. I recall the official magazine had a programming column for a short time, but copying the text via controller was as tedious as filling out a form for tax relief. I only bothered trying it once with a program called RAINDROP - still on my memory card, 66KB last edited 13:57 April 7th 2002 - which rendered an effect that looked like raindrops in a puddle. A fairly underwhelming reward for the effort.

Fittingly, exactly ten years later in April I decided to jump into programming as my brother and I had been discussing what programming languages we'd been taught in school as he had placed an order for a Raspberry Pi. He wanted to mess around with it without any specific goal in mind, so I figured that was enough reason for me to play about too on the back of mentioning my sub-amateur programming experience on the blog. I had been taught TrueBASIC in school which I picked up quite well until the course started discussing arrays and drawing graphics which lost me due to the crossover with mathematics and algebra. I was actually interested enough in really exploring programming that I smuggled a floppy disk into the class and copied the installation files for TrueBASIC so I could run it at home. That said; when it came to delving back into programming, BASIC in any incarnation looked fairly dated. I can remember QBasic in Windows 3.1, although back then at the age of eight I hadn't a clue what it did. When it came to finding a language that was more modern and powerful and yet quite simple, I ran with VBScript on nothing much more than glancing familiarity from twelve years prior. I know... I should have gone with JavaScript. It was my intention at the time to leave the more useful scripting language until I felt I had mastered Microsoft's forgotten challenger.

I did in fact feel I had mastered VBScript after no more than a week. I found a beginners guide on Google Books which contained examples and problems. What really hooked me was a Rock, Paper, Scissors script. The book only supplied the lines for displaying the rules, asking the user for their choice, and randomly selecting the computer's choice. It was up to you to complete the rest of the program: a way of rejecting invalid input, and a way to determine the winner of the game. Looking online I found a few resources for VBScript which allowed me to find relevant commands and understand the syntax without blindly incurring runtime errors. I was extremely pleased when I solved the problem. The input rejection ran in a DO-LOOP and took the user back to the input screen if anything other than the three choices were selected. The input was then converted into uppercase to ensure the script would parse it and determine the winner using a series of IF-THEN statements. The final screen displayed the player's choice, the computer's choice, and who therefore won the game. All very simple and completed within four days, but a massive step from being unable to do no more than copy someone else's code.

That's the power of the latterly disparaged desktop PC. When I look at the iPads and the iPhones and the Blackberrys I see great convenience and the near realisation of the old futurist dream of a personal area network, but I also see walled gardens and the resurgence of an AOL/MSN vision of the net. It's one thing to use technology, but it's quite another to command it yourself.

For reference and not word count padding:
'Determiner completed 2250BST 08-04-12, Invalid rejection completed 1919BST 12-04-12
Dim WshShl, PlayerAnswer, ComputerAnswer, PlayerStatus, Valid

Set WshShl = WScript.CreateObject("WScript.Shell")

WshShl.Popup "Welcome to rock, paper and scissors game. Here are the rules " & _
"for playing the game: [1] Guess the same thing as the computer to tie. " & _
"[2] Paper covers rock and wins. [3] Rock breaks scissors and wins. [4] " & _
"Scissors cut paper and wins."

Call GetPlayerAnswer
Call Tumbler
Call Determiner
Call Terminus

    Function GetPlayerAnswer
       Do
          PlayerAnswer = InputBox("Type Paper, Rock, or Scissors.","Let's play a game!")
          PlayerAnswer = UCase(PlayerAnswer)
           If PlayerAnswer = "ROCK" or PlayerAnswer = "PAPER" or PlayerAnswer = "SCISSORS" Then
            Valid = vbTrue
          Else
             MsgBox "Your choice is invalid. Please enter it again."
          End If
       Loop While Valid = vbFalse
GetPlayerAnswer = PlayerAnswer
    End Function

Sub Tumbler
Randomize
GetRandomNumber = Round(FormatNumber(Int((3 * Rnd()) + 1)))

If GetRandomNumber = 3 then ComputerAnswer = "ROCK"
If GetRandomNumber = 2 then ComputerAnswer = "SCISSORS"
If GetRandomNumber = 1 then ComputerAnswer = "PAPER"
End Sub

Sub Determiner
If PlayerAnswer = ComputerAnswer then PlayerStatus = "It's a tie"
If PlayerAnswer = "ROCK" AND ComputerAnswer = "SCISSORS" then PlayerStatus = "You win"
If PlayerAnswer = "PAPER" AND ComputerAnswer = "ROCK" then PlayerStatus = "You win"
If PlayerAnswer = "SCISSORS" AND ComputerAnswer = "PAPER" then PlayerStatus = "You win"
If PlayerAnswer = "ROCK" AND ComputerAnswer = "PAPER" then PlayerStatus = "You lost"
If PlayerAnswer = "PAPER" AND ComputerAnswer = "SCISSORS" then PlayerStatus = "You lost"
If PlayerAnswer = "SCISSORS" AND ComputerAnswer = "ROCK" then PlayerStatus = "You lost"
End Sub

Sub Terminus
WshShl.Popup "You picked: " & PlayerAnswer & Space(10) & "The computer picked: " & _
 ComputerAnswer & Space(10) & PlayerStatus & "."
End Sub

[687]

No comments: