This is a working text-based game for mIRC, designed after the original 'Shiffre' which was a game in DoS, where you had to input the number that dissapeared on the screen.
This code was meant to display a working game structure in mIRC, that is why all the comments were left intact. I suggest you load it in a new remote file.
It is actually very hard, I suggest you try it out yourself! Enjoy!
START by typing: /shiffre
;You have full right to take this code/reproduce it, and you do not need my permission for it
alias shiffre {
;Write starting values to the hash table
var %hcommand = hadd -m shiffre
%hcommand level 1
%hcommand x 2
%hcommand y 2
;About game enabled:
;game_enabled = 0 | game is disabled
;game_enabled = 1 | game is waiting
;game_enabled = 2 | user is allowed to press a key
;Startshiffre: continues with the rest of the code
startshiffre
}
alias -l startshiffre {
;Write some default settings, that need to be set each time after a new level, you should not change these
var %hcommand = hadd -m shiffre
%hcommand game_enabled 1
%hcommand timer 1
%hcommand countdown 4
;Open the window
window -ak0 @shiffre
;Clear the window
clear @shiffre
;Create the play field
shiffrecreatefield
;Timer to do certain operations
.timershiffretimer 3 1 shiffretimer
}
alias -l shiffretimer {
;Is the game enabled? Check first. (Because this is a timed function this is important)
if ($hget(shiffre,game_enabled) = 0) {
.timershiffretimer off
return
}
;Set the label for the goto command
var %label = $hget(shiffre,timer)
;increase one
hinc -m shiffre timer
;Goto the label to perform commands
goto %label
:1 | return
:2 | removerandomnumber | return
:3 | askfornumber | return
}
alias -l shiffrecreatefield {
;Create Play Field function
;Double while loop to create the play field
var %y = $hget(shiffre,y)
while %y {
var %x = $hget(shiffre,x), %output
while %x {
%output = $+(%output,$rand(0,9))
dec %x
}
aline @shiffre %output
dec %y
}
}
alias -l removerandomnumber {
;Remove a random number from the field
;Set the line to replace
var %linetoreplace = $rand(1,$hget(shiffre,y))
;This is the line
var %line = $line(@shiffre,%linetoreplace)
;Calculate which Nth number has to be removed
var %char = $rand(1,$hget(shiffre,x))
;Store that number
hadd -m shiffre number $mid(%line,%char,1)
;Make the new line with a loop, so that position %char can be removed
var %a = 1, %newline, %charadd
while %a <= $len(%line) {
%charadd = $mid(%line,%a,1)
if (%a = %char) %charadd = $chr(160)
%newline = $+(%newline,%charadd)
inc %a
}
;Replace the line
rline @shiffre %linetoreplace %newline
}
alias -l askfornumber {
;Ask for the removed number function
;Is the game enabled? Check first.
if ($hget(shiffre,game_enabled) = 0) return
;Get the current countdown number
var %countdown = $calc($hget(shiffre,countdown) - 1)
;You are gameover
if (%countdown = 0) gameover
;Decrease the countdown in the ini file, if it reaches 0, then you are game over
hadd -m shiffre countdown %countdown
clear @shiffre
echo @shiffre Enter number ( $+ %countdown $+ )
;Now the user is allowed to type in a number
hadd -m shiffre game_enabled 2
;Timed loop through this function for the countdown
.timershiffrecountdown 1 1 askfornumber
}
alias -l gameover {
;Game over function
;First immediately disable the game
hadd -m shiffre game_enabled 0
;Output a Game Over message
clear @shiffre
echo @shiffre Game Over (the number was $hget(shiffre,number) $+ ) $+ ! You reached level $+($hget(shiffre,level),!)
;Finally end the script by halting
halt
}
on *:CLOSE:@shiffre:{
;User closed the window, the game is now disabled
hadd -m shiffre game_enabled 0
}
on 1:KEYDOWN:@shiffre:*:{
;First check if the script has asked to input something
if ($hget(shiffre,game_enabled) = 2) {
;Calculate the number pressed
var %keypressed
if ($keyval < 60) %keypressed = $calc($keyval - 48)
;Also allow keys from the numpad
else %keypressed = $calc($keyval - 96)
;Checks if it was a valid input, else return
if (($len(%keypressed) != 1) || (%keypressed !isin 1234567890)) return
;Check if it was the correct number
if (%keypressed = $hget(shiffre,number)) {
;You pressed the right number
;The game doesn't allow any input anymore now
hadd -m shiffre game_enabled 0
;Increase level by 1
hinc -m shiffre level
;Increase x by 1, max value is 65 to avoid errors at very high levels
if ($hget(shiffre,y) < 65) hinc -m shiffre y
;Increase y by 1, max value is 25 to avoid errors at very high levels
if ($hget(shiffre,x) < 25) hinc -m shiffre x
;Display a message
clear @shiffre
echo @shiffre That is correct!
;In 1 second a new level will start
.timernewlevel 1 1 startshiffre
}
else {
;wrong number: game over
gameover
}
}
}