Friday, January 10, 2014

Petit Computer Journal #33


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.


  1. ?"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?


  1. 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:


  1. CLS:CLEAR
  2. DIM T$(3)
  3. T$(0)="ROCK"
  4. T$(1)="PAPER"
  5. T$(2)="SCISSORS"
  6. ?"A=ROCK,B=PAPER,X=SCISSORS"
  7. WAIT 180
  8. B=BUTTON(0)
  9. A=0
  10. IF B AND 32 THEN A=1
  11. IF B AND 64 THEN A=2
  12. ?"YOU CHOSE ";T$(A)
  13. B=RND(3)
  14. ?"I CHOOSE ";T$(B)
  15. O=(2+A-B)%3
  16. 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.


  1. CLS:CLEAR
  2. DIM T$(6)
  3. T$(0)="ABSOLUTELY YES"
  4. T$(1)="YES"
  5. T$(2)="MAYBE"
  6. T$(3)="NO"
  7. T$(4)="ABSOLUTELY NOT"
  8. T$(5)="NO WAY JOSE!"
  9. ?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.


  1. CLS:CLEAR
  2. DIM D(3)
  3. D[0]=6
  4. D[1]=10
  5. D[2]=20

  6. @MAIN
  7. VSYNC 1:TX=TCHX:TY=TCHY
  8. R=0
  9. FOR I=0 TO FLOOR(TX/64)
  10. R=R+RND(D[FLOOR(TY/64)])
  11. NEXT I
  12. IF TCHST THEN LOCATE 0,0:?"YOU ROLL ";R;"    "
  13. 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.


  1. CLS:CLEAR
  2. A=0:B=0
  3. @MAIN
  4. ?A,B
  5. A=A+RND(2):B=B+RND(2)
  6. IF A>100 OR B>100 THEN GOTO @END
  7. GOTO @MAIN

  8. @END
  9. IF A>100 THEN ?"A WINS!"
  10. IF B>100 THEN ?"B WINS!"
  11. 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.


  1. CLS
  2. AH=6:'ALARM HOUR
  3. AM=0:'ALARM MINUTE
  4. LOCATE 0,2:?AH;":";AM
  5. @MAIN
  6. WAIT 60
  7. LOCATE 0,0:?TIME$
  8. CH=VAL(LEFT$(TIME$,2))
  9. CM=VAL(MID$(TIME$,3,2))
  10. IF AH==CH AND AM==CM THEN BEEP
  11. 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.


  1. CLS
  2. I=0:T=0
  3. @MAIN
  4. VSYNC 1:T=T+1
  5. IF BTRIG() THEN I=I+1
  6. J$=RIGHT$("00"+STR$(T%60),2)
  7. S$=RIGHT$("00"+STR$(FLOOR((T/60))%60),2)
  8. M$=RIGHT$("00"+STR$(FLOOR((T/3600))%60),2)
  9. LOCATE 0,I:?I,M$;":";S$;":";J$
  10. IF I>=20 THEN END
  11. 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.

2 comments:

  1. Tex Murphy Are you still programming on petit computer? Do you know a good way to control multiple enemies. Should their actions be scripted. I have posted a platform game on nintendolife qr code.

    ReplyDelete
    Replies
    1. Wow, AI bot programming? That subject can fill a book, so I don't know how to respond to that. I usually do it state-based. Hunting-Running-Waiting-Patrolling and so on. Within each state, there would be logic that causes it to change state. As to what you put in there is up to you. Most enemies just go back and forth and maybe do some shooting. Sorry I can't be of any more of help. That is a rather deep and complicated subject you're asking.

      Even if I limit the subject to just Path Finding, it'd still be too complicated in a forum post. Sorry.

      If you have Google+ account, you can follow this blog. I'll be putting in PTC stuff in about a couple of weeks. I also have a Weekend Programmer Community that is open to beginners in Google+.

      Delete