Alternatively, join us on Discord.

FloodIt [TI 84+ CE] [C] (need C help)

Started by Unicorn, February 24, 2016, 11:52:53 pm

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Unicorn

February 24, 2016, 11:52:53 pm Last Edit: February 25, 2016, 04:09:27 am by DJ Omnimaga
I'm figuring out how to use C with the CE, so I decided to port this game.

I've got the displaying part down, with the absence of buttons to change the color you want, but that should be easy to implement.



I've been trying to figure out how to check the colors of the squares adjacent and how to change their colors. Currently I have this in mind:

Select color
Change top left color
If the color of an adjacent is color of last color, change to current color
increase turns
repeat

Do you think this works? I'm going to check using getpixel, and I'm starting to believe that I'm gonna need to have each square mapped using a variable. Suggestions would be welcome on how to do this!



??? ??? ??? ??? ???

DJ Omnimaga

Glad to see you are learning C. I hope you have fun with that language :). Also the game seems interesting. I hope you can pull it off and hopefully can get help for your problem.

Scipi

I would use an array to hold all the color data, and then index into it. This article may help you: http://www.geeksforgeeks.org/flood-fill-algorithm-implement-fill-paint/




aeTIos

I was going to suggest what @Scipi suggested. That would make it a lot easier to check color data. Are you going to use F1 .. F5 for choosing the colors?
ceci n'est pas une signature

Unicorn

Quote from: aeTIos on February 25, 2016, 09:37:50 amAre you going to use F1 .. F5 for choosing the colors?

Yes, I will also probably have a selection using the arrow keys.

Quote from: Scipi on February 25, 2016, 07:18:57 am
I would use an array to hold all the color data, and then index into it. This article may help you: http://www.geeksforgeeks.org/flood-fill-algorithm-implement-fill-paint/

Yeah, that is what Cemetech recommended as well, I'll try to get that working, though I'm not sure how well I will be able to...

And for the curious, some code:


/* Keep these headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <ti84pce.h>

/* Standard headers - it's recommended to leave them included */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* CE Graphics C Library */
#include <graphc.h>
int x = 61;
int y = 4;
int color;
int r;
/* Main Function */
void main(void)
{
    /* Some variable intialization */

    gc_InitGraph();
    gc_SetColorIndex(255);
    gc_FillScrn(0);
    gc_NoClipRectangleOutline(60,2,258,195);
    while (1) {
        r = rand() % 6;
        if (r == 0)
            color = 224; //RED
        if (r == 1)
            color = 16; //BLUE
        if (r == 2)
            color = 231; //YELLOW
        if (r == 3)
            color = 5; //GREEN
        if (r == 4)
            color = 112; //PURPLE
        if (r == 5)
            color = 227; //ORANGE
           
        gc_SetColorIndex(color);   
        gc_NoClipRectangle(x,y,16,16);
        x += 16;
            if (x >= 305) {
                x = 61;
                y += 16;
            }
        if (y >= 192)
            break;
    }


    /* Print a string of stuff */
    gc_PrintStringXY("CircleStuff",1,250);
     
    /* This is a really bad function to use. Don't use it in actual things */
    _OS( GetKey() );

    /* Close the graphics canvas and return to the OS */
    gc_CloseGraph();
_OS( asm("call _DrawStatusBar"));
}



??? ??? ??? ??? ???

Scipi

Quote from: Unicorn on February 25, 2016, 11:38:04 pm
Quote from: Scipi on February 25, 2016, 07:18:57 am
I would use an array to hold all the color data, and then index into it. This article may help you: http://www.geeksforgeeks.org/flood-fill-algorithm-implement-fill-paint/

Yeah, that is what Cemetech recommended as well, I'll try to get that working, though I'm not sure how well I will be able to...


The code on that site looks like valid C, so you could use it more or less verbatim, provided all your color data is in a 2D array of ints.




Unicorn

I'm gonna try something myself first.

So I've added boxes and for selection, but my keyboard routine is way to fast. How would I go about slowing it down? I also need help with my randomizing. Mateo told me to seed it with srand(*(unsigned long*)0xF30044); but when I put that above the rand() % 6; code, it fills the box with one color that changes each time. What am I doing wrong?


Keyboard code:

while (kb_ScanGroup(kb_group_6) != kb_Clear) {
gc_SetColorIndex(214);
gc_NoClipRectangle(selectorX,233,25,3);
key = kb_ScanGroup(kb_group_7);
if (key & kb_Left && selectorX > 65+12) {
gc_SetColorIndex(0);
gc_NoClipRectangle(selectorX,233,25,3);
selectorX -= 10;
}
if (key & kb_Right && selectorX < 215+62) {
gc_SetColorIndex(0);
gc_NoClipRectangle(selectorX,233,25,3);
selectorX += 10;
}
kb_Reset();
}


Entire Code

/* Keep these headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <ti84pce.h>

/* Standard headers - it's recommended to leave them included */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/* CE Graphics C Library */
#include <graphc.h>
#include <keypadc.h>
int x = 61;
int y = 4;
int color;
int r;
int lastColor;
int key;
int selectorX = 65+11;
unsigned int speed;
void delay(unsigned int);
/* Main Function */
void main(void)
{
    /* Some variable intialization */

    gc_InitGraph();
    gc_SetColorIndex(255);
    gc_FillScrn(0);
gc_NoClipRectangleOutline(60,2,258,195);
gc_NoClipRectangleOutline(65+11,205,25,25);
gc_NoClipRectangleOutline(95+21,205,25,25);
gc_NoClipRectangleOutline(125+31,205,25,25);
gc_NoClipRectangleOutline(155+41,205,25,25);
gc_NoClipRectangleOutline(185+51,205,25,25);
gc_NoClipRectangleOutline(215+61,205,25,25);
gc_SetColorIndex(224);
gc_NoClipRectangle(65+12,206,23,23);
gc_SetColorIndex(16);
gc_NoClipRectangle(95+22,206,23,23);
gc_SetColorIndex(231);
gc_NoClipRectangle(125+32,206,23,23);
gc_SetColorIndex(5);
gc_NoClipRectangle(155+42,206,23,23);
gc_SetColorIndex(112);
gc_NoClipRectangle(185+52,206,23,23);
gc_SetColorIndex(227);
gc_NoClipRectangle(215+62,206,23,23);
while (1) {
r = rand() % 6;
        if (r == 0)
            color = 224; //RED
        if (r == 1)
            color = 16; //BLUE
        if (r == 2)
            color = 231; //YELLOW
        if (r == 3)
            color = 5; //GREEN
        if (r == 4)
            color = 112; //PURPLE
        if (r == 5)
            color = 227; //ORANGE
           
        gc_SetColorIndex(color);   
        gc_NoClipRectangle(x,y,16,16);
        x += 16;
            if (x >= 305) {
                x = 61;
                y += 16;
            }
        if (y >= 192)
break;
}
while (kb_ScanGroup(kb_group_6) != kb_Clear) {
gc_SetColorIndex(214);
gc_NoClipRectangle(selectorX,233,25,3);
key = kb_ScanGroup(kb_group_7);
if (key & kb_Left && selectorX > 65+12) {
gc_SetColorIndex(0);
gc_NoClipRectangle(selectorX,233,25,3);
selectorX -= 10;
}
if (key & kb_Right && selectorX < 215+62) {
gc_SetColorIndex(0);
gc_NoClipRectangle(selectorX,233,25,3);
selectorX += 10;
}
kb_Reset();
}


    /* Print a string of stuff */
    gc_PrintStringXY("CircleStuff",1,250);
     
    /* This is a really bad function to use. Don't use it in actual things */
    _OS( GetKey() );

    /* Close the graphics canvas and return to the OS */
    gc_CloseGraph();
_OS( asm("call _DrawStatusBar"));
}




??? ??? ??? ??? ???

DJ Omnimaga

I can't help you with the code since I don't know C, but would using a variable as a key delay work? For example, when pressing a key, it would set the variable to 10 and then it would count down to zero every loop and when it reaches zero then you can press a key again. You would probably need to make it so that the first delay is longer, though.

Unicorn

Quote from: DJ Omnimaga on February 27, 2016, 05:43:24 am
I can't help you with the code since I don't know C, but would using a variable as a key delay work? For example, when pressing a key, it would set the variable to 10 and then it would count down to zero every loop and when it reaches zero then you can press a key again. You would probably need to make it so that the first delay is longer, though.

That is what I ended up doing, Kerm had written a description for PT_ on cemetech, and that helped me get things going. So now, I'm gonna start getting my flood-fill algorithm going, I may need some help with that at some point.



??? ??? ??? ??? ???

Unicorn

bump.

I have fixed the above issues, I had the seed in a loop, and the key-presses were pretty easy to fix.

Anyways, I've gotten my array filled with the numbers of my colors (I hope), and I am about to move on to the flood-fill.
Check it out!

/* Keep these headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <ti84pce.h>

/* Standard headers - it's recommended to leave them included */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/* CE Graphics C Library */
#include <graphc.h>
#include <keypadc.h>
int x = 61;
int y = 4;
int r, seed = 13;
int arrayColumn, arrayRow;
int lastColor, color;
int key, key1, key2;
int selectorX = 65+11;
unsigned int speed = 4, keyPress;
int floodArray[12][16];
/* Main Function */
void main(void)
{
    /* Some variable intialization */

    gc_InitGraph();
    gc_SetColorIndex(255);
    gc_FillScrn(0);
gc_NoClipRectangleOutline(60,2,258,195);
gc_NoClipRectangleOutline(65+11,205,25,25);
gc_NoClipRectangleOutline(95+21,205,25,25);
gc_NoClipRectangleOutline(125+31,205,25,25);
gc_NoClipRectangleOutline(155+41,205,25,25);
gc_NoClipRectangleOutline(185+51,205,25,25);
gc_NoClipRectangleOutline(215+61,205,25,25);
gc_SetColorIndex(224);
gc_NoClipRectangle(65+12,206,23,23);
gc_SetColorIndex(16);
gc_NoClipRectangle(95+22,206,23,23);
gc_SetColorIndex(231);
gc_NoClipRectangle(125+32,206,23,23);
gc_SetColorIndex(5);
gc_NoClipRectangle(155+42,206,23,23);
gc_SetColorIndex(112);
gc_NoClipRectangle(185+52,206,23,23);
gc_SetColorIndex(227);
gc_NoClipRectangle(215+62,206,23,23);
gc_SetTextColor((0<<8)|255);
gc_PrintStringXY("FloodIt",1,1);
gc_PrintStringXY("by",1,15);
gc_PrintStringXY("Unicorn",1,30);
srand(*(unsigned long*)0xF30044);
while (1) {
r = rand() % 6;
        if (r == 0)
            color = 224; //RED
        if (r == 1)
            color = 16; //BLUE
        if (r == 2)
            color = 231; //YELLOW
        if (r == 3)
            color = 5; //GREEN
        if (r == 4)
            color = 112; //PURPLE
        if (r == 5)
            color = 227; //ORANGE
           
        gc_SetColorIndex(color);   
        gc_NoClipRectangle(x,y,16,16);
x += 16;
floodArray[arrayColumn][arrayRow] = color;
if (x >= 305) {
arrayRow = 0;
            x = 61;
y += 16;
if (y >= 192)
break;
arrayColumn++;
}
arrayRow++;
}
while (kb_ScanGroup(kb_group_6) != kb_Clear) {
if (keyPress % 45 == 0) {
if (key & kb_Left && selectorX > 65+12) {
gc_SetColorIndex(0);
gc_NoClipRectangle(selectorX,233,25,3);
selectorX -= 40;
}
if (key & kb_Right && selectorX < 215+58) {
gc_SetColorIndex(0);
gc_NoClipRectangle(selectorX,233,25,3);
selectorX += 40;
}
if (key1 & kb_2nd && selectorX == 76) {
//check algorithm - RED
lastColor = color;
color = 224;

}
if (key1 & kb_2nd && selectorX == 116) {
//check algorithm - BLUE
lastColor = color;
color = 16;
}
if (key1 & kb_2nd && selectorX == 156) {
//check algorithm - YELLOW
lastColor = color;
color = 231;
}
if (key1 & kb_2nd && selectorX == 196) {
//check algorithm - GREEN
lastColor = color;
color = 5;
}
if (key1 & kb_2nd && selectorX == 236) {
//check algorithm - PURPLE
lastColor = color;
color = 112;
}
if (key1 & kb_2nd && selectorX == 276) {
//check algorithm - ORANGE
lastColor = color;
color = 227;
}
}

gc_SetColorIndex(214);
gc_NoClipRectangle(selectorX,233,25,3);
key = kb_ScanGroup(kb_group_7);
key1 = kb_ScanGroup(kb_group_1);
gc_SetTransparentColor(30);
gc_SetTextColor(255);
gc_PrintStringXY("FloodIt",1,1);
gc_PrintStringXY("by",20,15);
gc_PrintStringXY("Unicorn",1,30);
keyPress++;
}

    /* Close the graphics canvas and return to the OS */
    gc_CloseGraph();
_OS( asm("call _DrawStatusBar"));
}





??? ??? ??? ??? ???

Dudeman313

Nice! Looks like this is running much faster than the BASIC one I just tested. Can't wait for a release! ;D
Does this qualify as a signature? 
The answer is "Sure."


DJ Omnimaga

Good to hear, and looks nice Unicorn. By the way, does this key delay cause key detection issues? (keypresses not registering all the time) Or is it fine?

Unicorn

Quote from: DJ Omnimaga on February 29, 2016, 07:04:56 am
Good to hear, and looks nice Unicorn. By the way, does this key delay cause key detection issues? (keypresses not registering all the time) Or is it fine?

Not really, and if it becomes a problem, I can shorted the delay easily.



??? ??? ??? ??? ???

Unicorn

March 01, 2016, 05:59:50 am #13 Last Edit: March 01, 2016, 08:36:47 pm by Unicorn
So, right now, I have something of an algorithm, though it doesn't do the job correctly. (screenshot below) Any tips on how to fix it up? I'll be taking more of a look at it tomorrow, so hopefully I can get more done with a fresh mind :)



Pseudocode of what it is supposed to do:

If 2nd pressed and Selector at RED and top left color doesn't equal red
     Set color RED
     Loop
          If last color equals the color of the squares in the row
               Set color RED
               Change color of those squares
          If last color is not the same color next square in the row
               Change columns
               Set row to first one
          Increment row
          If row is 16
               go to next column
               Set row to first one
          If column is 12
               exit loop
     Repeat loop


/* Keep these headers */
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <ti84pce.h>

/* Standard headers - it's recommended to leave them included */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/* CE Graphics C Library */
#include <graphc.h>
#include <keypadc.h>
int x = 61;
int y = 4;
int r, seed = 13;
int arrayColumn, arrayRow;
int lastColor, color;
int key, key1, key2;
int selectorX = 65+11;
unsigned int speed = 4, keyPress;
int floodArray[12][16], coordXArray[16], coordYArray[12];
/* Main Function */
void main(void)
{
coordYArray[0] = 4;
coordYArray[1] = 20;
coordYArray[2] = 36;
coordYArray[3] = 52;
coordYArray[4] = 68;
coordYArray[5] = 84;
coordYArray[6] = 100;
coordYArray[8] = 116;
coordYArray[9] = 132;
coordYArray[10] = 148;
coordYArray[11] = 164;
coordYArray[12] = 180;
coordXArray[0] = 61;
coordXArray[1] = 77;
coordXArray[2] = 93;
coordXArray[3] = 109;
coordXArray[4] = 125;
coordXArray[5] = 141;
coordXArray[6] = 157;
coordXArray[7] = 173;
coordXArray[8] = 189;
coordXArray[9] = 205;
coordXArray[10] = 221;
coordXArray[11] = 237;
coordXArray[12] = 253;
coordXArray[13] = 269;
coordXArray[14] = 285;
coordXArray[15] = 301;
coordXArray[16] = 317;

    gc_InitGraph();
    gc_SetColorIndex(255);
    gc_FillScrn(0);
gc_NoClipRectangleOutline(60,2,258,195);
gc_NoClipRectangleOutline(65+11,205,25,25);
gc_NoClipRectangleOutline(95+21,205,25,25);
gc_NoClipRectangleOutline(125+31,205,25,25);
gc_NoClipRectangleOutline(155+41,205,25,25);
gc_NoClipRectangleOutline(185+51,205,25,25);
gc_NoClipRectangleOutline(215+61,205,25,25);
gc_SetColorIndex(224);
gc_NoClipRectangle(65+12,206,23,23);
gc_SetColorIndex(16);
gc_NoClipRectangle(95+22,206,23,23);
gc_SetColorIndex(231);
gc_NoClipRectangle(125+32,206,23,23);
gc_SetColorIndex(5);
gc_NoClipRectangle(155+42,206,23,23);
gc_SetColorIndex(112);
gc_NoClipRectangle(185+52,206,23,23);
gc_SetColorIndex(227);
gc_NoClipRectangle(215+62,206,23,23);
gc_SetTextColor((0<<8)|255);
gc_PrintStringXY("FloodIt",1,1);
gc_PrintStringXY("by",1,15);
gc_PrintStringXY("Unicorn",1,30);
srand(*(unsigned long*)0xF30044);
while (1) {
r = rand() % 6;
        if (r == 0)
            color = 224; //RED
        if (r == 1)
            color = 16; //BLUE
        if (r == 2)
            color = 231; //YELLOW
        if (r == 3)
            color = 5; //GREEN
        if (r == 4)
            color = 112; //PURPLE
        if (r == 5)
            color = 227; //ORANGE
           
        gc_SetColorIndex(color);   
        gc_NoClipRectangle(x,y,16,16);
x += 16;
floodArray[arrayColumn][arrayRow] = color;
coordXArray[arrayRow] = x;
coordYArray[arrayColumn] = y;
if (x >= 305) {
arrayRow = 0;
            x = 61;
y += 16;
if (y >= 192)
break;
arrayColumn++;
}
arrayRow++;
}
arrayRow = 0;
arrayColumn = 0;
while (kb_ScanGroup(kb_group_6) != kb_Clear) {
if (keyPress % 45 == 0) {
if (key & kb_Left && selectorX > 65+12) {
gc_SetColorIndex(0);
gc_NoClipRectangle(selectorX,233,25,3);
selectorX -= 40;
}
if (key & kb_Right && selectorX < 215+58) {
gc_SetColorIndex(0);
gc_NoClipRectangle(selectorX,233,25,3);
selectorX += 40;
}
lastColor = color;
if (key1 & kb_2nd && selectorX == 76 && lastColor != 224) {
//check algorithm - RED
color = 224;
arrayColumn = 0;
gc_SetColorIndex(color);
gc_NoClipRectangle(61,4,16,16);
while (1) {
if (lastColor == floodArray[arrayColumn][arrayRow]) { 
gc_NoClipRectangle(coordXArray[arrayRow],coordYArray[arrayColumn],16,16);
}
arrayRow++;
if (lastColor != floodArray[arrayColumn][arrayRow]) {
arrayColumn++;
arrayRow = 0;
}
if (arrayRow == 15) {
arrayRow = 0;
arrayColumn++;
}
if (arrayColumn == 11)
break;
}
}
if (key1 & kb_2nd && selectorX == 116 && lastColor != 16) {
//check algorithm - BLUE
color = 16;
arrayColumn = 0;
gc_SetColorIndex(color);
gc_NoClipRectangle(61,4,16,16);
while (1) {
if (lastColor == floodArray[arrayColumn][arrayRow]) { 
gc_NoClipRectangle(coordXArray[arrayRow],coordYArray[arrayColumn],16,16);
}
arrayRow++;
if (lastColor != floodArray[arrayColumn][arrayRow]) {
arrayColumn++;
arrayRow = 0;
}
if (arrayRow == 15) {
arrayRow = 0;
arrayColumn++;
}
if (arrayColumn == 11)
break;
}
}
if (key1 & kb_2nd && selectorX == 156 && lastColor != 231) {
//check algorithm - YELLOW
color = 231;
arrayColumn = 0;
gc_SetColorIndex(color);
gc_NoClipRectangle(61,4,16,16);
while (1) {
if (lastColor == floodArray[arrayColumn][arrayRow]) { 
gc_NoClipRectangle(coordXArray[arrayRow],coordYArray[arrayColumn],16,16);
}
arrayRow++;
if (lastColor != floodArray[arrayColumn][arrayRow]) {
arrayColumn++;
arrayRow = 0;
}
if (arrayRow == 15) {
arrayRow = 0;
arrayColumn++;
}
if (arrayColumn == 11)
break;
}
}
if (key1 & kb_2nd && selectorX == 196 && lastColor != 5) {
//check algorithm - GREEN
color = 5;
arrayColumn = 0;
gc_SetColorIndex(color);
gc_NoClipRectangle(61,4,16,16);
while (1) {
if (lastColor == floodArray[arrayColumn][arrayRow]) { 
gc_NoClipRectangle(coordXArray[arrayRow],coordYArray[arrayColumn],16,16);
}
arrayRow++;
if (lastColor != floodArray[arrayColumn][arrayRow]) {
arrayColumn++;
arrayRow = 0;
}
if (arrayRow == 15) {
arrayRow = 0;
arrayColumn++;
}
if (arrayColumn == 11)
break;
}
}
if (key1 & kb_2nd && selectorX == 236 && lastColor != 112) {
//check algorithm - PURPLE
color = 112;
arrayColumn = 0;
gc_SetColorIndex(color);
gc_NoClipRectangle(61,4,16,16);
while (1) {
if (lastColor == floodArray[arrayColumn][arrayRow]) { 
gc_NoClipRectangle(coordXArray[arrayRow],coordYArray[arrayColumn],16,16);
}
arrayRow++;
if (lastColor != floodArray[arrayColumn][arrayRow]) {
arrayColumn++;
arrayRow = 0;
}
if (arrayRow == 15) {
arrayRow = 0;
arrayColumn++;
}
if (arrayColumn == 11)
break;
}
}
if (key1 & kb_2nd && selectorX == 276 && lastColor != 227) {
//check algorithm - ORANGE
color = 227;
arrayColumn = 0;
gc_SetColorIndex(color);
gc_NoClipRectangle(61,4,16,16);
while (1) {
if (lastColor == floodArray[arrayColumn][arrayRow]) { 
gc_NoClipRectangle(coordXArray[arrayRow],coordYArray[arrayColumn],16,16);
}
arrayRow++;
if (lastColor != floodArray[arrayColumn][arrayRow]) {
arrayColumn++;
arrayRow = 0;
}
if (arrayRow == 15) {
arrayRow = 0;
arrayColumn++;
}
if (arrayColumn == 11)
break;
}
}
}
gc_SetColorIndex(214);
gc_NoClipRectangle(selectorX,233,25,3);
key = kb_ScanGroup(kb_group_7);
key1 = kb_ScanGroup(kb_group_1);
gc_SetTransparentColor(30);
gc_SetTextColor(255);
gc_PrintStringXY("FloodIt",1,1);
gc_PrintStringXY("by",20,15);
gc_PrintStringXY("Unicorn",1,30);
keyPress++;
arrayRow = 0;
arrayColumn = 0;
}

    /* Close the graphics canvas and return to the OS */
    gc_CloseGraph();
_OS( asm("call _DrawStatusBar"));
}

EDIT: Updated code with a few fixes, but it still does pretty much the same thing.



??? ??? ??? ??? ???

DJ Omnimaga

@Unicorn have you gotten any luck solving this so far? It seems that not many people here are able to help or maybe you should make a separate C topic about flood filling, but have you chedked tutorials on flood filling in the meantime?

Powered by EzPortal