Quick Programs
There are many programs that can be done quickly. Suppose that you only have 15 minutes to write a computer program. What do you think you would write? Let's start a quick one. The quickest program that you can write would be a simple game. Coin flips. Heads or Tail. Or in this case, A/B buttons. The program simply checks whether or not you pushed the right buttons. It's a simple one-liner.
- ?"A/B?":WAIT 180:IF BUTTON(0)==16*RND(2) THEN "YOU WIN!" ELSE "YOU LOSE!"
Yes, it's that simple! There's nothing to it, is there? Let's take it a little more and do a simple Rock-Paper-Scissor game. Let's take a look at this table(A-B) (RPS=012):
B\A
R
P
S
R
0
1
2
P
-1
0
1
S
-2
-1
0
Now, consider this: Looking at the diagonals, we see that the values are the same. zero means draw. one means win. two means lose. -1, -2 means lose, win. So, how about if we shift it up and set the range to modulus 3?
- O=(2+A-B)%3:'O=OUTCOME
The range then becomes 0-2, 0=Win,1=Lose,2=Draw, and everything is peachy! The program then clearly writes itself:
- CLS:CLEAR
- DIM T$(3)
- T$(0)="ROCK"
- T$(1)="PAPER"
- T$(2)="SCISSORS"
- ?"A=ROCK,B=PAPER,X=SCISSORS"
- WAIT 180
- B=BUTTON(0)
- A=0
- IF B AND 32 THEN A=1
- IF B AND 64 THEN A=2
- ?"YOU CHOSE ";T$(A)
- B=RND(3)
- ?"I CHOOSE ";T$(B)
- O=(2+A-B)%3
- IF O==0 THEN ?"YOU WIN" ELSE IF O==1 THEN ?"YOU LOSE" ELSE ?"DRAW"
That's all there is to it. Pretty quick to do and implement.
Let's keep moving on. How about random outcome? That's important in a game. However, there's something that you can do for a purely random device, such as dice. Here's a quick implementation of the Magic 8 Ball, although I don't bother putting in too many responses.
- CLS:CLEAR
- DIM T$(6)
- T$(0)="ABSOLUTELY YES"
- T$(1)="YES"
- T$(2)="MAYBE"
- T$(3)="NO"
- T$(4)="ABSOLUTELY NOT"
- T$(5)="NO WAY JOSE!"
- ?T$(RND(6))
If the example looks lean, that's because it is. The Magic 8 Ball is quite famous. Another example, using the same structure, would be Yoda's Wisdom. Quotes of Yoda from Star Wars, presented with pictures of him is a rather entertaining program. Another one would be Bingo, or lottery, or anything random. One of my stand by program is a dice roller for playing board games.
Some games requires you to roll so many dice, and they're not all six dice, either. Let's say that you want to roll 1-4 dice, ranging from D6,D10,D20. It's a simple process using the touch screen and do it. I'm doing it quick, so there's no text. You certainly want to dress it up for practical use.
- CLS:CLEAR
- DIM D(3)
- D[0]=6
- D[1]=10
- D[2]=20
- @MAIN
- VSYNC 1:TX=TCHX:TY=TCHY
- R=0
- FOR I=0 TO FLOOR(TX/64)
- R=R+RND(D[FLOOR(TY/64)])
- NEXT I
- IF TCHST THEN LOCATE 0,0:?"YOU ROLL ";R;" "
- GOTO @MAIN
I actually implemented Snake and Ladder game in less than 10 minutes. One thing that is interesting about it, is that the program really doesn't require any user input other than rolling the dice. In that sense, all the activities that you do can be boiled down into rolling the dice. We can improve this further simply by letting the computer run it straight.
- CLS:CLEAR
- A=0:B=0
- @MAIN
- ?A,B
- A=A+RND(2):B=B+RND(2)
- IF A>100 OR B>100 THEN GOTO @END
- GOTO @MAIN
- @END
- IF A>100 THEN ?"A WINS!"
- IF B>100 THEN ?"B WINS!"
- IF A==B THEN ?"IT'S A DRAW!"
The program will resolve the race immediately. You may find it interesting to input some kind of delay in the output, so as to make it more readable as it goes.
All of these programs can be done really quick. In fact, I do these while waiting for something. Or maybe when I was in bed, trying to sleep. Most people would take a book to read in bed. I would take a Nintendo 3DS and write programs, instead.
One more quick example of the program, and this time it's useful when you're trying to do something regarding sleep: An alarm clock.
- CLS
- AH=6:'ALARM HOUR
- AM=0:'ALARM MINUTE
- LOCATE 0,2:?AH;":";AM
- @MAIN
- WAIT 60
- LOCATE 0,0:?TIME$
- CH=VAL(LEFT$(TIME$,2))
- CM=VAL(MID$(TIME$,3,2))
- IF AH==CH AND AM==CM THEN BEEP
- GOTO @MAIN
Oh, one more bonus program since not one hour has passed, yet. You know how timers have lap times? Usually, there's only one room for lap times. With this, you can have as many as you want. I'm going to limit it to just 20 for simplicity. You may modify to suit.
- CLS
- I=0:T=0
- @MAIN
- VSYNC 1:T=T+1
- IF BTRIG() THEN I=I+1
- J$=RIGHT$("00"+STR$(T%60),2)
- S$=RIGHT$("00"+STR$(FLOOR((T/60))%60),2)
- M$=RIGHT$("00"+STR$(FLOOR((T/3600))%60),2)
- LOCATE 0,I:?I,M$;":";S$;":";J$
- IF I>=20 THEN END
- GOTO @MAIN
These programs are rough, and really, for distribution, you may want to dress them up a little. However, for something that you can doodle in 10 minutes, while waiting for something, these are perfectly suited as a pleasant time waster. Did I just do 7 programs in one hour? I guess so.