Showing posts with label 33. Show all posts
Showing posts with label 33. Show all posts

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.

Tuesday, October 22, 2013

Raspberry Pi Journal #33


Learning Python


The Raspberry Pi device was conceived as educational enabler device. Part of the target audience was kids. That's why it has Scratch programming on it. Also, the official programming language is Python. In fact, that's the "Pi" in Raspberry Pi.

So, after I built the LCD Display kit, it's time for me to learn Python programming. My chosen language was actually Perl, since it's so easy to write in it. But since the official language is Python, I guess I should learn it.

The book I chose is O'Reilly Learning Python. O'Reilly books are consistently high quality. Very few is sub par. So, if I am to buy a learning book sight unseen, it's O'Reilly.




You can see the the picture is that of a rat. Where's the snake? Sorry. That's another book: Programming Python. About the same size and price of this book. The price? $70 with tax. The size? 1500 pages.

Ahem. 1500 pages. Are you nuts? Is that one book? Don't you think, like maybe, it's better to split it into 3 books? After all, isn't that what Prof. Donald Knuth did with his book? Oh, and the other book is the same size?

That makes it 3000 pages total. And remember, Raspberry Pi is designed to teach kids how to program. Somehow, no matter how hard I try, I just can't imagine kids would be wanting to pick this book up, much less two of them.

I must be getting old. It used to be that I can devour such book in a couple of days. So far, because of other commitments, I managed to read 300 pages into it. And learned different object types.

300 pages to explain the different object types.

470 pages will give you loops.

I admit that the book provides an excellent foundation of how Python works. But 300 pages? It deals with the powerful flexibility that Python have that lets you handle complexities with ease. Except, I don't want to go there. I have yet to see source code that maximized the full potential of the language.

I still remember that Learning Perl and Programming Perl book combined is less that this book. I guess it just goes to show you that today's computer, and programming language, is very powerful indeed.

But still, are you sure you want to foist this ultra powerful programming language to kids, even if they have Scratch programming background?