Author Topic: Lights Out  (Read 2914 times)

0 Members and 1 Guest are viewing this topic.

Offline juha.kivekas

  • LV0 Newcomer (Next: 5)
  • Posts: 4
  • Rating: +2/-0
    • View Profile
Lights Out
« on: April 10, 2013, 02:37:52 pm »
Over the weekend i finished my first real assembly program!
It's a game replica of the Lights Out handheld game released by Tiger Toys in 1995. The objective is to turn all lights off (to black). Since i didn't make a scrambling routine yet you have to scramble it yourself. Use the arrow keys move and enter to turn on/off the lights. I've tried it on TI-83+ (Emulated) and TI-84+ and it runs just right.

I really feel good. Learning assembly has taught me a lot, and given me a good bit of brainteasers. Now that i've got the basics i just want to do more and more by myself! Dithcing those B_CALLs and making my own routines, making something amazingly complex, or just optimize routines.

This code is not optimzed, it's more like soulutionized, wich means the solutions work.

Source:
http://guttula.com/LightsOut.asm.txt
.8xp:
http://guttula.com/LightsOut.8xp

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Lights Out
« Reply #1 on: April 10, 2013, 03:46:26 pm »
I made one of these a while ago, it was a good time and now I know how to solve them.


Anyways, since it looks like you're new to programming, I have some suggestions:

1. bcall(_runIndicOff) - just add this to the top of your program and you'll be happy

2. A scrambler won't be too difficult to make . Since I believe there are unsolvable positions in this game, you should just make a scrambler that simply fakes a bunch of random key presses. I think I used 25. This way, the board is randomized, but still solvable. (Since you probably learned using 28 days, why not give the "r" register a go for your randomizer)

3. If you end up playing your game a lot, you might get tired of moving the cursor around. So as programming practice, and just to make the game easier to play, you should link each square to a button on the keyboard. Just pick a 4x4 grid of buttons and hardwire them to squares.

4. Also Light's out had a 5x5 grid I believe :P


Also, congratulations on making your first assembly game. Making games is the best way to get better.
« Last Edit: April 10, 2013, 03:47:11 pm by thepenguin77 »
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline juha.kivekas

  • LV0 Newcomer (Next: 5)
  • Posts: 4
  • Rating: +2/-0
    • View Profile
Re: Lights Out
« Reply #2 on: April 10, 2013, 04:07:17 pm »
I learned from z80 assembly for absolute beginners, so I have not read about generating random numbers. Thanks for the tip, I'll take a looksies. I left the run indicator there because then i feel safe about my program running and not having crashed (has happened way often during developing).

Why 4x4? because 16x16 sprite routines are easy to make, and it nicely divides the screen. :D  But maybe the extra challenge is the reason to make a 5x5.

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Lights Out
« Reply #3 on: April 10, 2013, 04:14:18 pm »
Oh, ok. No need to look, I'll just explain it. There's this register called r, and it actually has a really complex use, but don't worry about what that is.

You can read it with "ld a, r". The low 7 bits (numbers 0 - 127) will be essentially random. The upper bit will be whatever was last written to r. But the idea is that as long as you aren't in a tightly looped routine, r will be kind of random. (Though, there might be a pattern)
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: Lights Out
« Reply #4 on: April 11, 2013, 02:25:33 am »
And if you only want 16 values, you can just "and $F" (and %00001111) and your "random" number will be limited to 0-15 :)

If you did 5x5, you could also just use 8x8 sprites and add some other things around the border. Also, i'm not sure how you store the current status of a square, but if you used 2 bytes where each bit represented one square, you could easily check to see if the player has beaten the game or not just be checking if both bytes = 0.

And another idea: leave the cursor as it is, but also add in thepenguin's idea where you use a 4x4 (or 5x5) grid on the calculator keypad. Then, when you press that button the cursor will jump there and change the lights accordingly.

I just loaded it up to my calc and it's really nice! I would recommend not using _GetKey, though. Try pressing [2nd]+[On] while running your program and see what happens ;) Generally you would use direct input, but here _GetCSC would also be quick enough. If you use direct input, you'll need to add a delay, otherwise the cursor will move too quickly!

Also, if you notice, you really don't even need sprites here. You're only drawing two different bytes to the screen: %10101010 and %01010101. You could just load %10101010 into a, draw that twice, move down a row, cpl (a faster and smaller way of doing xor $FF, so it will turn %10101010 into %01010101) and repeat 15 times. The main loop of your sprite routine will probably be smaller than the size of your 16x16 sprites!

Another tip is that often times if you're going to be editing a byte in RAM it'll be faster and smaller to load it into hl first. If more than one area will use it, you can load hl before jumping to that routine, for example your key checking routine:
Code: [Select]
DoKeyAction:
LD HL,Cursor
CP kLeft
JR NZ,RightKeyAction

LeftKeyAction:
LD A,(HL) ;load cursor column
DEC A ;smaller than cp 1, and also serves to decrease a :)
RET Z ;if Column=1 dont got left, it'll go off the grid
LD (HL),A ;1 byte, 7 T-States. Loading into an address is 3 bytes (1 byte for the opcode and 2 bytes for the address)
RET ;KeyAction is done: return to main loop

RightKeyAction:
CP kRight
JR NZ,UpKeyAction
LD A,(HL) ;load cursor column (we loaded Cursor into HL above)
CP 4
RET Z ;if Column=4 dont got right, it'll go off the grid
INC A
LD (HL),A
RET ;KeyAction is done: return to main loop

And in your main loop, you push HL which is a nice idea to save a little space, but why not do it like this:
Code: [Select]
LD HL,Cursor ;xorDraw active Sprite
LD B, (HL)
INC HL
LD C, (HL)
LD HL,ActiveSprite
PUSH HL ;save HL (sprite pointer) and BC (sprite coordinates)
PUSH BC
CALL XorDrawSprite ;aguments in BC and HL, as supposed to

B_CALL _GrBufCpy ;draw the current status and active square
B_CALL _getKey
CP kClear
JR Z,GameEnd
CALL DoKeyAction
POP BC
POP HL ;xorDraw active Sprite (on old location, so its ereased)
CALL XorDrawSprite
? :) All in all your code is nice and well organized, though i might switch ix and hl around in your sprite routine just 'cuz ix is so slow and takes up more space. Either way, it's a nice first project and i look forward to seeing what else you've got planned!
« Last Edit: April 11, 2013, 02:43:04 am by chickendude »

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Lights Out
« Reply #5 on: April 11, 2013, 02:32:58 am »
Just some random input from me in response to chickendude: if your program doesn't need to do anything while waiting for a key press, _GetKey is actually pretty great. And if you want to avoid the ON issue, you can thankfully get around it pretty easily. Just change the call to _GetKeyRetOff (you won't find it in TI's include file, I recommend using BrandonW's much more complete file) which returns kOff instead of actually turning the calculator off.

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: Lights Out
« Reply #6 on: April 11, 2013, 02:46:16 am »
I guess it'll help out with your battery life. I never knew about the _GetKeyRetOff B_CALL before. I don't think i'll ever actually use it, but it's nice to know :)