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:

Code:

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!
Unicorn wrote:
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:

Code:

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!


1. Sounds awesome! This'll certainly be a fun game.
2. Use arrays (if they exist in +CE C, which I assume they do).
3. For the flood-fill, I recommend some form of recursive algorithm.
You can use arrays in C just like you can use matrices in TI-BASIC, which would probably be the best way to store the color of each block. Of course you'd have a single array element for each block. Are you trying to implement a flood-fill algorithm? If you're sure that you won't recurse too far down (I believe the TI-84+CE's stack is 400 bytes for the OS; not sure the size that the C SDK configures), you can use the standard flood fill algorithm, treating each box as a pixel.

Edit: Mateo said the stack is 4KB on SAX, so you should be okay.
Awesome! I may try and do this in pure basic, that would be fun Smile Not so sure about how fast I could draw the new color though. We'll see

Also, what's your plan for the cursor, to click the squares?
That's a cool game. Good luck recreating it! Smile
Hactar wrote:

1. Sounds awesome! This'll certainly be a fun game.
2. Use arrays (if they exist in +CE C, which I assume they do).
3. For the flood-fill, I recommend some form of recursive algorithm.


Thanks!
I was thinking about using such a thing, a matrix, like Kerm said.

Quote:

You can use arrays in C just like you can use matrices in TI-BASIC, which would probably be the best way to store the color of each block. Of course you'd have a single array element for each block. Are you trying to implement a flood-fill algorithm? If you're sure that you won't recurse too far down (I believe the TI-84+CE's stack is 400 bytes for the OS; not sure the size that the C SDK configures), you can use the standard flood fill algorithm, treating each box as a pixel.


Ok, I'll check that out!

Quote:

Awesome! I may try and do this in pure basic, that would be fun Smile Not so sure about how fast I could draw the new color though. We'll see

Also, what's your plan for the cursor, to click the squares?


I wonder if that is possible... And thanks! The color will be chosen by 6 squares that will be highlighted.
Quote:

That's a cool game. Good luck recreating it! Smile

Thanks!

Forgot to post this, but heres the code for those interested.

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"));
}
Here's my algorithm, I am 99% sure it works, but it is majorly slow in ti-basic. Taken from my thread.
Michael2_3B wrote:
Pseudocode:

Code:

If clicked_color is the same as top_left_color
    return

Else
Scan each element of [F]
    If that element is the same as the top_left_color and is adjacent to a 1 in matrix [G]
    Store a 1 in the 2nd matrix at the same spot the current element being scanned is at
Repeat this scan once (to make sure to get spots that hadnt had a 1 adjacent to them yet)
Replace all elements of [F] where a 1 is in [G] with clicked_color, and redraw table at same time


Code:

Code:
If K=105 and [F](1,1)!=[F](B+1,A+1:Then   //continue only if clicked color is not the same as top left color
      [F](1,1->C    //top left color stored into c
      [F](B+1,A+1->D   //clicked color stored into d
      DelVar [G]{14,14->dim([G]
      1->[G](1,1
      For(Z,0,1   //scan matrix twice, for the reason stated in the pseudocode
         For(I,1,14
            For(J,1+(I=1),14
               If [F](J,I)=C and ((J>1)[G](J-(J>1),I) or (I>1)[G](J,I-(I>1)) or (J<14)[G](J+(J<14),I) or (I<14)[G](J,I+(I<14   //if element equals top left color and there is a 1 adjacent to it in [G]
               1->[G](J,I
            End
         End
      End
      For(I,1,14
         For(J,1,14
            If [G](J,I:Then
               D->[F](J,I   //replace values in matrix
               For(Z,52+10I,61+10I,2
                  Line(Z,~2-10J,Z,~11-10J,1,D   //recolor necessary boxes
               End
            End
         End
      End
      DelVar [G]   //save memory
   End

Whoa! That's a surprisingly addicting game! Very Happy Cool algorithm too!
Michael: Wow, great job getting that working in such a short time!

Dianzi tian: Yeah, its pretty addicting Razz

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:

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

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"));
}
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!

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 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"));
}


Did you add some debouncing on the key input routines, or was slowing them down sufficient? This looks really great so far!
KermMartian wrote:
Did you add some debouncing on the key input routines, or was slowing them down sufficient? This looks really great so far!

Slowing them down was sufficient, thanks for the description Smile

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 Smile



Pseudocode of what it is supposed to do:

Code:

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



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 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.
I won't work on the UI until the game engine works. I think I might implement a cursor, but I really need to work on that algorithm...
From Wikipedia, which knows all:


Code:
Flood-fill(node, target-color, replacement-color):
 1. If target-color is equal to replacement-color, return.
 2. If the color of node is not equal to target-color, return.
 3. Set the color of node to replacement-color.
 4. Perform Flood-fill (one step to the south of node, target-color, replacement-color).
    Perform Flood-fill (one step to the north of node, target-color, replacement-color).
    Perform Flood-fill (one step to the west of node, target-color, replacement-color).
    Perform Flood-fill (one step to the east of node, target-color, replacement-color).
 5. Return.


Added to this, don'y forget to do bounds checking so you stay within the array itself. Also note that passing a pointer to the target and replacement colors rather than pass-by-value will help ensure your stack depth doesn't get too large.
Thanks for that,and just to clarify, node is the square I'm checking? So in my case, always the top left. Target color is the last color th user wanted changed? And replacement is the color the user wants now, correct?
I found the equation for the maximum amount of game clicks on the site:

Code:

maxclick= Math.floor(25*((nrows+ncols)*ncolors)/((14+14)*6))

However in some cases, such as a 2x2 board with 3 colors, that gets evaluated to 1, which would be impossible. I added 3 to ncolors and I think that will work.

The (14+14)*6 can be replaced with 168. Here's my modified TI-Basic version:

Code:

//F = width, height
//M = number of colors

int(50F(M+3)/168
Interesting...
I would have thought it wa hard coded, as when you had 3 colors and were onthe small board you only had one move...
Any ideas why this square appears? The console in CEmu shows that all of the coords in coordXArray and coordYArray are correct...



Here's my code...

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>
#include <debug.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) {
   gc_InitGraph();
   gc_SetColorIndex(255);
   gc_FillScrn(0);
   gc_NoClipRectangleOutline(60,3,258,194);
   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",3,1);
   gc_PrintStringXY("by",8,15);
   gc_PrintStringXY("Unicorn",2,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;
      dbg_printf(dbgout, "Coord X Array and X\n");
      dbg_printf(dbgout, "%d", coordXArray[arrayRow]);
      dbg_printf(dbgout, "%d", x);
      if (x >= 305) {
         arrayRow = 0;
                  x = 61;
         y += 16;
         if (y >= 192)
            break;
         arrayColumn++;
         coordYArray[arrayColumn] = y;
         dbg_printf(dbgout, "Coord Y Array and Y ");
         dbg_printf(dbgout, "%d", coordYArray[arrayColumn]);
         dbg_printf(dbgout, "%d", y);
      }
      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;
         }
         if (key1 & kb_2nd && selectorX == 76) {
            //check algorithm - RED
            lastColor = color;
            color = 224;
            arrayRow = 0;
            arrayColumn = 0;
            gc_SetColorIndex(color);
            gc_NoClipRectangle(coordXArray[0],coordYArray[0],16,16);
            while (1) {
               if (lastColor == color || floodArray[arrayColumn][arrayRow] != lastColor)
                  break;
               floodArray[arrayColumn][arrayRow] = color;
               if (floodArray[arrayColumn][arrayRow] == lastColor || floodArray[arrayColumn][arrayRow] == color)
               gc_NoClipRectangle(coordXArray[arrayRow],coordYArray[arrayColumn],16,16);

               arrayRow++;
               if (arrayRow == 16) {
                  arrayRow = 0;
                  arrayColumn++;
               }
               if (arrayColumn == 12)
                  break;
            }
         }
         if (key1 & kb_2nd && selectorX == 116) {
            //check algorithm - BLUE
            lastColor = color;
            color = 16;
            arrayRow = 0;
            arrayColumn = 0;
            gc_SetColorIndex(color);
            gc_NoClipRectangle(coordXArray[0],coordYArray[0],16,16);
            while (1) {
               if (lastColor == color || floodArray[arrayColumn][arrayRow] != lastColor)
                  break;
               floodArray[arrayColumn][arrayRow] = color;
               if (floodArray[arrayColumn][arrayRow] == lastColor || floodArray[arrayColumn][arrayRow] == color)
               gc_NoClipRectangle(coordXArray[arrayRow],coordYArray[arrayColumn],16,16);

               arrayRow++;
               if (arrayRow == 16) {
                  arrayRow = 0;
                  arrayColumn++;
               }
               if (arrayColumn == 12)
                  break;
            }
         }
         if (key1 & kb_2nd && selectorX == 156) {
            //check algorithm - YELLOW
            lastColor = color;
            color = 231;
            arrayRow = 0;
            arrayColumn = 0;
            gc_SetColorIndex(color);
            gc_NoClipRectangle(coordXArray[0],coordYArray[0],16,16);
            while (1) {
               if (lastColor == color || floodArray[arrayColumn][arrayRow] != lastColor)
                  break;
               floodArray[arrayColumn][arrayRow] = color;
               if (floodArray[arrayColumn][arrayRow] == lastColor || floodArray[arrayColumn][arrayRow] == color)
               gc_NoClipRectangle(coordXArray[arrayRow],coordYArray[arrayColumn],16,16);

               arrayRow++;
               if (arrayRow == 16) {
                  arrayRow = 0;
                  arrayColumn++;
               }
               if (arrayColumn == 12)
                  break;
            }
         }
         if (key1 & kb_2nd && selectorX == 196) {
            //check algorithm - GREEN
            lastColor = color;
            color = 5;
            arrayRow = 0;
            arrayColumn = 0;
            gc_SetColorIndex(color);
            gc_NoClipRectangle(coordXArray[0],coordYArray[0],16,16);
            while (1) {
               if (lastColor == color || floodArray[arrayColumn][arrayRow] != lastColor)
                  break;
               floodArray[arrayColumn][arrayRow] = color;
               if (floodArray[arrayColumn][arrayRow] == lastColor || floodArray[arrayColumn][arrayRow] == color)
               gc_NoClipRectangle(coordXArray[arrayRow],coordYArray[arrayColumn],16,16);

               arrayRow++;
               if (arrayRow == 16) {
                  arrayRow = 0;
                  arrayColumn++;
               }
               if (arrayColumn == 12)
                  break;
            }
         }
         if (key1 & kb_2nd && selectorX == 236) {
            //check algorithm - PURPLE
            lastColor = color;
            color = 112;
            arrayRow = 0;
            arrayColumn = 0;
            gc_SetColorIndex(color);
            gc_NoClipRectangle(coordXArray[0],coordYArray[0],16,16);
            while (1) {
               if (lastColor == color || floodArray[arrayColumn][arrayRow] != lastColor)
                  break;
               floodArray[arrayColumn][arrayRow] = color;
               if (floodArray[arrayColumn][arrayRow] == lastColor || floodArray[arrayColumn][arrayRow] == color)
               gc_NoClipRectangle(coordXArray[arrayRow],coordYArray[arrayColumn],16,16);

               arrayRow++;
               if (arrayRow == 16) {
                  arrayRow = 0;
                  arrayColumn++;
               }
               if (arrayColumn == 12)
                  break;
            }
         }
         if (key1 & kb_2nd && selectorX == 276) {
            //check algorithm - ORANGE
            lastColor = color;
            color = 227;
            arrayRow = 0;
            arrayColumn = 0;
            gc_SetColorIndex(color);
            gc_NoClipRectangle(coordXArray[0],coordYArray[0],16,16);
            while (1) {
               if (lastColor == color || floodArray[arrayColumn][arrayRow] != lastColor)
                  break;
               floodArray[arrayColumn][arrayRow] = color;
               if (floodArray[arrayColumn][arrayRow] == lastColor || floodArray[arrayColumn][arrayRow] == color)
               gc_NoClipRectangle(coordXArray[arrayRow],coordYArray[arrayColumn],16,16);

               arrayRow++;
               if (arrayRow == 16) {
                  arrayRow = 0;
                  arrayColumn++;
               }
               if (arrayColumn == 12)
                  break;
            }
         }
      }
      arrayRow = 0;
      arrayColumn = 0;
      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",3,1);
      gc_PrintStringXY("by",20,15);
      gc_PrintStringXY("Unicorn",3,30);
      keyPress++;
      arrayRow = 0;
      arrayColumn = 0;
   }

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


I know mateo said to have only one flood-fill loop for easier debugging, but I just needed to get something going.
I feel like you are making this way more difficult than it has to be. Why don't you just have a single array for the board of type uint8_t, and then just make a function that draws the board rather than trying to draw each box individually? Just a single function to draw the board, another function to perform the flood fill, and another function which gets your inputs. This modular programming technique will solve all of your problems, and make it easier to debug.
How's this coming? Got anything new?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement