Commodore 64 startup screen |
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:
Post a Comment