Monday, April 1, 2013

Petit Computer Journal #14

Petit Computer Journal#14
Amazing Mazes!

It has been a long time, hasn't it? I took one month sabbatical. It became 3 months. How time flies! I got jolted into action by the bugs on my windshield! It's spring! Time to get moving!

Haha, joking aside, it IS time for me to get up and do something.

I have been working on this maze generation program that I planned to use on D&D type of game. The eventual code will create "rooms" out of the maze.

And here is the whole program

'MAZE
'APRIL 2013
'HARRY HARDJONO
CLS:CLEAR
DIM XDIR[5]
DIM YDIR[5]


'ROOM: MAKE ROOMS
'PATH: DRAW PATH
'CHOICE: CHOOSE PATH
'INIT: SET PARAMS
'MAZE: MAKE MAZE

GOTO @INIT
So far, so good. I'm allocating memory in the beginning of the program. I also map out the structure of the program in terms of subroutines.
@ROOM
'I'M SKIPPING THIS FOR NOW. TOO LAZY.
'LOOK IT UP ON QR CODE
RETURN
You're probably wondering what goes on here. All it does is scan the maze for straight walls, and removing them. Any bend in the wall and it will be left alone. The resulting "rooms" are unique. Setting the LINK variable to 0 will render this subroutine useless. For lots of spaces, I find LINK=8 is a good value to use. Higher values means less room.
@PATH
LOCATE CX,CY:?CHR$(32);
FOR PC=1 TO LEN(M$)-1
PDX=XDIR[INSTR(CIDX$,MID$(M$,PC,1))]
PDY=YDIR[INSTR(CIDX$,MID$(M$,PC,1))]
CX=CX+PDX:CY=CY+PDY
LOCATE CX,CY:?CHR$(32);
CX=CX+PDX:CY=CY+PDY
LOCATE CX,CY:?CHR$(32);
NEXT
RETURN
Path really draws the path of the maze. It goes by "UDLR" values of XDIR,YDIR arrays. I have the idea of making the subroutine generic by using string and draws all the path. No error checking here, so be careful when using this subroutine as-is.
@CHOICE
CS$=""
FOR CI=0 TO 3
CJ$=MID$(CIDX$,CI,1)
CPX=CX+(2*XDIR[CI])
CPY=CY+(2*YDIR[CI])
IF LEN(M$)>250 THEN CJ$=""
IF CPX<SCXMIN THEN CJ$=""
IF CPX>SCXMAX THEN CJ$=""
IF CPY<SCYMIN THEN CJ$=""
IF CPY>SCYMAX THEN CJ$=""
IF LINK==0 OR RND(LINK) THEN IF CHKCHR(CPX,CPY)!=151 THEN CJ$=""
CS$=CS$+CJ$
NEXT
C$=MID$(CS$,RND(LEN(CS$)),1)
RETURN
This subroutine merely take the current cursor position and looks at 4 different directions. Valid direction gets added to CS$. Invalid directions are those that are nested too deep, out-of-bounds, and dead-ended in a maze.
@INIT
CIDX$="UDLR"
XDIR(0)=0:YDIR(0)=-1
XDIR(1)=0:YDIR(1)=1
XDIR(2)=-1:YDIR(2)=0
XDIR(3)=1:YDIR(3)=0
SCXMIN=1:SCXMAX=29
SCYMIN=1:SCYMAX=21
SCXCUR=RND(SCXMAX/2)*2+1
SCYCUR=RND(SCYMAX/2)*2+1
LINK=88:'CHANGE THIS FOR CONNECTEDNESS
ROOM=FALSE

CLS:M$=""
FOR II=SCYMIN-1 TO SCYMAX+1:?CHR$(151)*31:NEXT
@INIT subroutine establishes the parameters of the program. The maze characteristics, other than SCXMAX,SCYMAX which defines the size of the maze would be LINK and ROOM=(TRUE/FALSE). Set ROOM to TRUE if you want open areas in the maze.

Also draws the initial background.

@MAZE
'HEAVY WIZARDRY
CX=SCXCUR:CY=SCYCUR
GOSUB @PATH
GOSUB @CHOICE
M$=M$+C$+C$
M$=LEFT$(M$,LEN(M$)-1)
IF LEN(M$) GOTO @MAZE
BEEP:IF ROOM THEN GOSUB @ROOM
This is the heart of the program. Traditionally, this is implemented by a recursive function. Obviously, BASIC has no recursion. I managed to flattened the whole algorithm. This used to be a double FOR-LOOP. Now, it's a single REPEAT-LOOP. Very clean and simple. I like it.
@END
IF BUTTON(0) GOTO @INIT
GOTO @END
Press any key to continue...

And there you have it. A short, simple and sweet maze generator routine. Feel free to use it on your games! I'm looking forward to see what you can come up with. Happy coding!

Sunday, January 20, 2013

Busy...

I have been rather busy taking care of New Year stuff, but now I finally caught up. For the next few weeks, expect some reviews to come in. I'm also working on Zombie Apocalypse Pack (ZAP), a bug out bag to take in case, you know, there's some Zombie outbreak somewhere. In terms of projects, Petit Computer has the priority. I need to finish up these projects so I can get back to Small Basic. Small Basic isn't perfect. I prefer Processing, myself. But Small Basic is one of those projects that deserve support.

My next Petit Computer project will be virtual keyboard. That is very useful, since all other projects will use it. I'm thinking of doing a multipart project on it, simply because I am so busy! Biting more than I can chew, really. Stock market and PPH (Personal Productivity Hour) projects are two that I want to finish ASAP as those will really boost my productivity.

There may be Pathfinding project in the future, but that one is so convoluted that not only it will be multi-part, but also waiting for some level editor to be finished. Speaking of level editor, I'm thinking of writing a level editor for RPG games. That one should be fun. And since I did Turtle Logo in one hour using Small Basic, maybe I should do something similar to that in Petit Computer.

Monday, December 17, 2012

Petit Computer Journal #13

Importing Data Into Petit Computer

One of the most important aspect in computing is data. In fact, I would say that the most important factor in computing is data. Everything that is important in computer can be traced to a database. Whether it is searching book titles such as Amazon.com, email, or managing your finances, data is usually the most important part of computing experience.

This has not always been true. Such early computer, say, having only 4 kilobytes of memory, downplayed the importance of data. However, without a good data reader, such as tape recorder, or worse, punch cards, such computer capability is wasted. Sure,there was Pong, but what good is it? The seminal early program was Adventure, or Collosal Cave, modelling the Mammoth Cave in Kentucky. Data is very important!

How can we enter data into the computer, specifically Petit Computer? Well, we can use the DATA keyword and type it in. But what if we already have it in our main computer? Here are the steps I use to import stock market data into Petit Computer.

1. Download the Historical stock data into your computer as CSV (Comma Separated Values)

Goto Yahoo.com and download a historical stock prices of your favorite stock. Make sure to save it as CSV format.

2. Open the file into your spreadsheet.

I use Microsoft Works, a cheaper alternative to Microsoft Office. Whatever works. Import the CSV file into the spreadsheet. Remove any unwanted columns. Insert column in the beginning. Fill it with "DATA". Save.

3. Open the file into Text Editor

I use Notepad. Replace all "DATA" with DATA, where you type a space character right after the text DATA. Add DATA "End",-1,-1,-1,-1,-1 at the end. This is assuming you're keeping the same data as me. Adjust accordingly.

4. Open up PTCUtilities.

Open up PRG Editor. Copy and Paste the text into the window. Comment out the first line. Make QR code out of it.

5. Scan with Petit Computer

Pick up your Nintendo DSi. Run Petit Computer program. Select "Scan QR Code". Import it to your Petit Computer. Let's call it STOKDATA.

6. Open up your main program.

I'm using the stock candlestick chart. The source code is at the end of the file. Load the program. Then on the RUN window, type "APPEND STOKDATA". This will add the content of STOKDATA to your current program. Save accordingly.

And there you have it, an easy way to import data to your Petit Computer Program.


'Stock Chart
@MAINLOOP
ACLS
GPAINT 6,6,6

C=0:MN=9999:MX=0
RESTORE @STOK
@STEP1
READ A$,OP,HI,LO,CL,AD
IF OP==-1 GOTO @STEP2
C=C+1:?C
IF MN>LO THEN MN=LO
IF HI>MX THEN MX=HI
GOTO @STEP1

@STEP2
RESTORE @STOK

FOR I=C TO 1 STEP -1
READ A$,OP,HI,LO,CL,AD
X1=C:X2=I:X3=1:Y1=247:Y3=24
GOSUB @MAP:DX=Y2
X1=MN:X2=HI:X3=MX:Y1=190:Y3=20
GOSUB @MAP:DHI=Y2
X1=MN:X2=LO:X3=MX:Y1=190:Y3=20
GOSUB @MAP:DLO=Y2

X1=MN:X2=OP:X3=MX:Y1=190:Y3=20
GOSUB @MAP:DOP=Y2
X1=MN:X2=CL:X3=MX:Y1=190:Y3=20
GOSUB @MAP:DCL=Y2

WAIT 1
GLINE DX,DHI,DX,DLO,8
IF OP>CL THEN GFILL DX-1,DOP,DX+1,DCL,15
IF CL>OP THEN GFILL DX-1,DOP,DX+1,DCL,14
GBOX DX-1,DOP,DX+1,DCL,15

NEXT

FOR I=BUTTON(3) TO 1:I=BUTTON(3):NEXT
GOTO @MAINLOOP

@MAP
Y2=(((X2-X1)/(X3-X1))*(Y3-Y1))+Y1
RETURN



@STOK
'DATA "Date","Open","High","Low","Close","Adj Close"
DATA "2012-12-10",11.41,11.58,11.03,11.1,11.1
DATA "2012-12-03",11.56,11.7,11.18,11.48,11.48
DATA "2012-11-26",11.05,11.6,10.97,11.45,11.45
DATA "2012-11-19",10.65,11.1,10.65,11.1,11.1
DATA "2012-11-12",11.03,11.16,10.38,10.5,10.5
DATA "2012-11-05",11.15,11.59,10.71,10.93,10.93
DATA "2012-10-31",10.7,11.38,10.6,11.17,11.17
DATA "2012-10-22",10.14,10.49,9.97,10.36,10.31
DATA "2012-10-15",10.11,10.57,10.09,10.18,10.13
DATA "2012-10-08",10.06,10.26,9.95,10.12,10.07
DATA "2012-10-01",9.89,10.28,9.71,10.16,10.11
DATA "2012-09-24",10.3,10.4,9.81,9.86,9.81
DATA "2012-09-17",10.27,10.66,10.26,10.4,10.35
DATA "2012-09-10",10.08,10.57,10.06,10.53,10.48
DATA "2012-09-04",9.37,10.23,9.35,10.14,10.09
DATA "2012-08-27",9.51,9.52,9.25,9.34,9.29
DATA "2012-08-20",9.58,9.7,9.4,9.49,9.44
DATA "2012-08-13",9.35,9.67,9.25,9.63,9.58
DATA "2012-08-06",9.13,9.46,9.09,9.35,9.3
DATA "2012-07-30",9.04,9.42,8.82,9.09,9.05
DATA "2012-07-23",9.08,9.24,8.83,9,8.91
DATA "2012-07-16",9.22,9.52,9.12,9.21,9.12
DATA "2012-07-09",9.46,9.55,9.12,9.27,9.18
DATA "2012-07-02",9.5,9.79,9.3,9.5,9.4
DATA "2012-06-25",10.13,10.18,9.46,9.59,9.49
DATA "2012-06-18",10.35,10.74,10.18,10.19,10.09
DATA "2012-06-11",10.77,10.8,10.21,10.35,10.24
DATA "2012-06-04",10.15,10.78,9.91,10.66,10.55
DATA "2012-05-29",10.69,10.88,10.06,10.12,10.02
DATA "2012-05-21",10.02,10.68,10,10.6,10.49
DATA "2012-05-14",10.41,10.53,9.96,10.01,9.91
DATA "2012-05-07",10.53,10.86,10.4,10.58,10.47
DATA "2012-04-30",11.42,11.47,10.63,10.67,10.56
DATA "2012-04-23",11.15,12.04,11.15,11.6,11.43
DATA "2012-04-16",12.01,12.05,11.39,11.41,11.24
DATA "2012-04-09",12.26,12.29,11.65,11.92,11.75
DATA "2012-04-02",12.5,12.95,12.32,12.47,12.29
DATA "2012-03-26",12.45,12.61,12.18,12.48,12.3
DATA "2012-03-19",12.52,12.68,12.18,12.32,12.14
DATA "2012-03-12",12.57,13.04,12.37,12.51,12.33
DATA "2012-03-05",12.67,12.73,12,12.58,12.4
DATA "2012-02-27",12.11,12.94,11.99,12.72,12.54
DATA "2012-02-21",12.74,12.76,12.16,12.23,12.05
DATA "2012-02-13",12.74,12.88,12.33,12.75,12.57
DATA "2012-02-06",12.85,13,12.37,12.44,12.26
DATA "2012-01-30",12.06,12.84,12,12.79,12.6
DATA "2012-01-23",12.69,13.05,11.79,12.21,12.03
DATA "2012-01-17",12.2,12.72,11.96,12.59,12.36
DATA "2012-01-09",11.83,12.18,11.63,12.04,11.82
DATA "2012-01-03",11,11.8,10.99,11.71,11.5
DATA "2011-12-27",10.87,10.98,10.43,10.76,10.56
DATA "2011-12-19",10.25,11,9.99,10.95,10.75
DATA "END",-1,-1,-1,-1,-1



Tuesday, December 11, 2012

Petit Computer Picross


I'm curious of how well can people copying code from text as opposed to just scan QR code. So, this will come only as source code, no QR. Let's see how well you can copy this, and hopefully understand it. I won't be explaining anything. The source code tells all!
Major bragging rights to the first person who can change P[] array to meaningful numbers. 
PS: I did this in 2 hours pecking at the screen with the stylus! How far can you go? Keep pushing!
CLS:CLEAR
DIM P[16]
GOSUB @INIT
GOSUB @DISP
@LOOP
GOSUB @PANE
GOSUB @MOVE
IF G$==P$ GOTO @END
GOTO @LOOP
@END
BEEP 7:LOCATE 18,18:?"WIN!"
FOR I=1 TO 1:VSYNC 1:I=BUTTON(3):NEXT
END
@MOVE
FOR I=1 TO 1:VSYNC 1:B=BUTTON(3):I=B:NEXT
BEEP 66
IF B AND 32 THEN G$=SUBST$(G$,CY*16+CX,1,"0")
IF B AND 16 THEN G$=SUBST$(G$,CY*16+CX,1,"1")
IF B AND 1 THEN CY=CY+15
IF B AND 2 THEN CY=CY+1
IF B AND 4 THEN CX=CX+15
IF B AND 8 THEN CX=CX+1
CX=CX%16:CY=CY%16
SPOFS 1,CX*8,CY*8
RETURN
@PANE
FOR I=0 TO 15:FOR J=0 TO 15
LOCATE J,I
IF "0"==MID$(G$,I*16+J,1) THEN ?CHR$(32) ELSE ?CHR$(224)
NEXT:NEXT
RETURN
@DISP
CLS
FOR I=0 TO 255 STEP 16
H$="":C=0
FOR J=0 TO 15
IF "1"==MID$(P$,I+J,1) THEN C=C+1
IF "0"==MID$(p$,I+J,1) THEN IF C THEN H$=H$+STR$(C):C=0
NEXT
IF C THEN H$=H$+STR$(C)
LOCATE 17,I/16:?H$
NEXT
FOR I=0 TO 15
H$="":C=0
FOR J=0 TO 15
IF "1"==MID$(P$,I+(J*16),1) THEN C=C+1
IF "0"==MID$(p$,I+(J*16),1) THEN IF C THEN H$=H$+STR$(C):C=0
NEXT
IF C THEN H$=H$+STR$(C)
FOR J=0 TO LEN(H$)
LOCATE I,16+J:?MID$(H$,J,1);
NEXT
NEXT
RETURN
@INIT
P[0]=2016
P[1]=14364
P[2]=16770
P[3]=33729
P[4]=35793
P[5]=40377
P[6]=24186
P[7]=19506
P[8]=12300
P[9]=21066
P[10]=18834
P[11]=38505
P[12]=44085
P[13]=25638
P[14]=24582
P[15]=4104
FOR I=0 TO 15
V=P[I]:T$=""
FOR J=0 TO 15
T$=STR$(V%2)+T$:V=FLOOR(V/2)
NEXT
P$=P$+T$
NEXT
G$="0"*LEN(P$)
SPSET 1,156,0,0,0,0,16,16
SPANIM 1,4,15
SPSCALE 1,50
CX=0:CY=0
RETURN

Saturday, November 24, 2012

Black Friday Shopping

I just bought Modernist Cuisine at Home. It was on sale for 50% off, so I paid just $70. Good deal! I also bought Make Magazine 3D Printing special. I sure would like to print some in the future. Will be visiting Hackerspace nearby when I have time. I guess I'm a maker at heart.

Friday, November 23, 2012

Review The 4 Hour CHEF


I've just finished reading Timothy Ferriss' book The 4-Hour CHEF. It's a strange cooking book, to say the least. It's the only book I've read so far that includes not only recipes for various meals, but also catching food in the wild or even pigeons in the park. Also mentioned in there is memorization techniques good for developing vocabulary and random numbers.

I suppose it all dove in with his goal of "Meta Learning." As a jack of all trades myself, I approve. True that the book will not make you a Master Chef, as opposed to Joy of Cooking, but that's what the references are for. There's a lot of links in the book that goes back to his website. I have yet to check them all.

There's a lot of interesting tid bits, including how to win eating contest (he didn't, not exactly) and still be thin (he is). I'm not too happy about the fried rice meal. My favorite is the chicken fried rice, and although he has chicken, and fried rice, the featured fried rice featured mealworms, instead of chicken. Well, I suppose adapting his recipes is a given.

I am also not too happy with his tool selection. Way too many tools for what is supposedly a simple chef. I suppose now I have to get myself a 7 inch chef knife. That and a thermometer. Not to mention Modernist Cuisine book by Nathan Myhrvold. He also didn't mention my favorite cooking implement: Rice cooker. Using rice cooker effectively is what enables me to write 1000 words essay while the food is cooking. To be fair, he did mention 2 hour chicken meal, and I can fit in more than 1000 words with that kind of meal, but I digress.

A section of surviving catastrophe, like San Francisco earthquake is very interesting. Apparently, you need to be able to survive on your own for about 7-10 days without electricity or water. Get out a generator, because that is what you need to power your refrigerator! Very interesting and important knowledge to have. I just didn't expect to find it in a cookbook!

If Timothy Ferriss would come down my way sometimes, I'd like to ask him if he's interested in learning computer languages. After all, I learned my first computer language, the Applesoft Basic, in 3 hours. That's one hour shorter than his 4 hours learning. Admittedly, the subsequent computer language took longer than that, at about 2 days to one week. It's still relatively fast approach, though, compared to the traditional computer programming learning.

Maybe I'm an outlier. My life has never been an easy conformance. I sure want to know whether that 5 minutes per week weight-lifting exercise is any good. I suppose I'll need to add his other book to my library as well. Sigh. I'm supposed to be writing Nanowrimo right now! I wonder if Barry Ross wrote a book? Will have to check that out.

This book review was written while my food is cooking on rice cooker. It just beeped me, so I’m done!

Monday, November 19, 2012

No post this week

I'm way behind 8000 words with Nanowrimo. I guess life does have an interference. But if I make it, I know I can do anything! So, here's to keep trying. Hang in there!