Troubleshooting putting something in a list

I am trying to make a randomly generated map, that is 20 x 20, that is then sent to an I2C OLED that is 128 x 64, and then you can move around it with a joystick. The problem is that you die even when there isn't a dot there. When I Serial.print ed the list that had all the positions with dots in it, it showed the list as having 70 things in it. If there are any questions, please ask.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
int x = 0;
int y = 0;
int q;
int p;
int pp;
int o;
int oo;
int xe;
int playable = 0;
int xnumero;
int xpos[35];
int ypos[35];
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void m(){//this is the function that checks where the joystick is on a xy plane
  if(analogRead(0) < 450 && analogRead(1) < 440 && y>= 18 && playable == 0){//the default position when you aren't pushing it is 520, 510
    //top left
    y--;//up
    display.drawPixel(x, y+1, BLACK);//this erases the pixel where you moved from
  display.drawPixel(x, y, WHITE);//this draws the pixel where you are moving to
  display.display();//this updates the screen
  poscheck();
  wincheck();
    delay(50);//this delay is here so you don't move way too far
  } else if (analogRead(0) > 590 && analogRead(1) < 440 && x<=38 && playable == 0){
    //top right
    x++;//right
    display.drawPixel(x-1, y, BLACK);
  display.drawPixel(x, y, WHITE);
  display.display();
  delay(50);
  poscheck();
  wincheck();
  } else if (analogRead(0) < 450 && analogRead(1) > 580 && x>=18 && playable == 0){
    //bottom left
    x--;//left
    display.drawPixel(x+1,y,BLACK);
  display.drawPixel(x,y,WHITE);
  display.display();
  delay(50);
  poscheck();
  wincheck();
  } else if(analogRead(0)>590 && analogRead(1)>580 && y <= 38 && playable == 0){
    //bottom right
    y++; //down
    display.drawPixel(x,y-1,BLACK);
  display.drawPixel(x,y,WHITE);
  display.display();
  delay(50);
  poscheck();
  wincheck();
  } else if (analogRead(0)==520 && analogRead(1)==510 && playable == 0){
    display.drawPixel(x, y, WHITE);
    display.display();
    delay(40);//this makes the player blink if it isn't moving
    display.drawPixel(x, y, BLACK);
    display.display();
    delay(10);
    wincheck();
  }
}



void poscheck(){//this checks the places where there is a dot
  for (xe = sizeof(xpos); xe >= 0; xe--){//this loops so you go down the line checking all the spots in the list that have a value
    if(x == xpos[xe] && y == ypos[xe]){//this checks if you are in a spot where there is a dot 
      display.clearDisplay();
      display.setCursor(50, 50);
      display.println("YOU LOST");
      display.display();
      playable = 1;//makes it so the player can't play anymore
    }
    Serial.print("X "); Serial.print(xe); Serial.print(" "); Serial.println(xpos[xe]);//These 2 lines are telling me where all the positions are that have a dot
    Serial.print("Y "); Serial.print(xe); Serial.print(" "); Serial.println(ypos[xe]);
  }
  Serial.println("Done");
Serial.println(sizeOf(xpos);//Because theoretically xpos and ypos should be the same length
}

void wincheck(){
  if(x == 48 && y == 48){
    display.clearDisplay();
    display.setCursor(0, 40);
    display.println("CONGRATULATIONS YOU");
    display.setCursor(0, 50);
    display.println("WON");
    display.display();
    playable = 1;
  } 
  display.drawPixel(48, 48, WHITE);
  display.display();
  delay(30);
  display.drawPixel(48, 48, BLACK);
  display.display();
  delay(10);
}


void setup() {
  Serial.begin(115200);//I am not sure why it is 115200, but when I tried to print something without the 9600 being there it only printed question marks
  Serial.begin(9600);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.clearDisplay();
  x = 18;
  y = 18;
  for(p = 0; p <= 20; p++){
    for(o = 0; o <= 20; o++){
      q = random(17);
      if(q==0){
        oo = o + 18;
        pp = p + 18;
        display.drawPixel(oo, pp, WHITE);
        display.display();
        xpos[xnumero] = oo;
        ypos[xnumero] = pp;
        xnumero++;
        delay(50);
      }
    }
  }
}


void loop() {
    m();
  }

Print out the information used in this comparison and check against your expectations.

    if(x == xpos[xe] && y == ypos[xe]){//this checks if you are in a spot where there is a dot 
Serial.begin(115200);//I am not sure why it is 115200, but when I tried to print something without the 9600 being there it only printed question marks

The baud rates in the serial monitor and the Serial.begin statement must match.

I thought I was when I printed out xpos[xe] and ypos[xe]. If not, can you clarify a bit more?

What is printed, and does that agree with your expectations?

TFT_Pong5.zip (4.7 KB)

When I'm doing something like this, I create a notes.h file using Notepad (or IDE tab) and a monospace font. Map out the character positions just like graph paper. Then, once the visual is ready, go back and start building the program.

In the game attached, I started by just making the joystick values map to the ILI9341 display. When I got that, I did a file-save-as _Pong1 and later to _Pong2, through the final version TFT_Pong5. Today, disk space is cheap enough to never loose your previous version.

/*
// These are the pins used for the UNO

              UNO LEO  // J2 header pin # on GLCD
#define _sclk 13   15  // 7
#define _miso 12   14  // 9
#define _mosi 11   16  // 6
#define _cs   10   10  // 3
#define _dc    9    9  // 5
#define _rst   8    8  // 4



                                      +-\/-+
Reset                           PC6  1|    |28  PC5 (AI 5)
Rx                        (D 0) PD0  2|    |27  PC4 (AI 4)
Tx                        (D 1) PD1  3|    |26  PC3 (AI 3)
                          (D 2) PD2  4|    |25  PC2 (AI 2)
                     PWM+ (D 3) PD3  5|    |24  PC1 (AI 1)
lDebug Low=true           (D 4) PD4  6|    |23  PC0 (AI 0)
+5                              VCC  7|    |22  GND             GND
GND                             GND  8|    |21  AREF            +5 and bypass cap
XTL1                            PB6  9|    |20  AVCC            +5
XTL2                            PB7 10|    |19  PB5 (D 13)      J2#7/SCK  -----------> D15 32U4
                          (D 5) PD5 11|    |18  PB4 (D 12)      J2#9/MISO -----------> D14 32U4
BAUD (low=4800)      PWM+ (D 6) PD6 12|    |17  PB3 (D 11) PWM  J2#6/MOSI (SDI) -----> D16 32U4
                          (D 7) PD7 13|    |16  PB2 (D 10) PWM  J2#3/CS
J2#4/Reset                (D 8) PB0 14|    |15  PB1 (D  9) PWM  J2#5/DC
                                      +----+

/--------------------------------------------------------------------/ 
/             Color definitions for TFT SPI 2.2" Display             /
/                    ILI9340_BLACK   0x0000                          /
/                    ILI9340_BLUE    0x001F                          /
/                    ILI9340_RED     0xF800                          /
/                    ILI9340_GREEN   0x07E0                          /
/                    ILI9340_CYAN    0x07FF                          /
/                    ILI9340_MAGENTA 0xF81F                          /
/                    ILI9340_YELLOW  0xFFE0                          /
/                    ILI9340_WHITE   0xFFFF                          /
/--------------------------------------------------------------------/ 

0        1         2         =  26 x 15 =   390 char/display    
12345678901234567890123456   = 240 x 320 (landscape) = 76,800 pixels
___display characters_____/ line#  character#
12345678901234567890123456    1     1 -  26
12345678901234567890123456    2    27 -  52
12345678901234567890123456    3    53 -  78
12345678901234567890123456    4    79 - 104
12345678901234567890123456    5   105 - 130
12345678901234567890123456    6   131 - 156
12345678901234567890123456    7   157 - 182
12345678901234567890123456    8   183 - 208
12345678901234567890123456    9   209 - 234
12345678901234567890123456    10  235 - 260
12345678901234567890123456    11  261 - 286
12345678901234567890123456    12  287 - 312
12345678901234567890123456    13  313 - 338
12345678901234567890123456    14  339 - 364
12345678901234567890123456    15  365 - 390
--------------------------/

    //tft.drawLine( x1,  y1,  x2,   y2, color);
    lcd.drawLine(    0,  0,  319,    0, ILI9340_YELLOW);
    lcd.drawLine(    0, 15,  319,   15, ILI9340_YELLOW);
    lcd.drawLine(    0,  0,    0,  239, ILI9340_YELLOW);
    lcd.drawLine(   11,  0,   11,  239, ILI9340_YELLOW);
    //lcd.setTextColor(ILI9340_WHITE, ILI9340_BLACK) ;
    
    (character# % 26 ) * 16 == (000,yyy)
    
    ***** Graphic text appears to be 12w x 16h font for .setTextSize(2) ***** Landscape == 15 Lines x 26 Characters Wide  == 390 Characters per Screen
___________________________________/1st/_______________________________________________________     ___________________________________/26th/_______________________________________________________
_______/_______/_______/_______/_______/_______/_______/_______/_______/_______/_______/_______/    _______/_______/_______/_______/_______/_______/_______/_______/_______/_______/_______/_______/
000,000 001,000 002,000 003,000 004,000 005,000 006,000 007,000 008,000 009,000 010,000 011,000     308,000 309,000 310,000 311,000 312,000 313,000 314,000 315,000 316,000 317,000 318,000 319,000 
000,001 001,001 001,002 001,003 001,004 001,005 001,006 001,007 001,008 001,009 001,010 001,011     308,001 309,001
000,002                                                                                             308,002
000,003                                                                                             308,003
000,004                                                                                             308,004
000,005                                                                                             308,005
000,006                                                                                             308,006
000,007                                                                                             308,007
000,008                                                                                             308,008
000,009                                                                                             308,009
000,010                                                                                             308,010
000,011                                                                                             308,011
000,012                                                                                             308,012
000,013                                                                                             308,013
000,014                                                                                             308,014
000,015 001,015 002,015 003,015 004,015 005,015 006,015 007,015 008,015 009,015 010,015 011,015     308,015 309,015 310,015 311,015 312,015 313,015 314,015 315,015 316,015 317,015 318,015 319,015

000,016  .line 2    000,080  .line 6    000,144  .line 10    000,208  .line 14
000,032  .line 3    000,096  .line 7    000,160  .line 11    000,224  .line 15
000,048  .line 4    000,112  .line 8    000,176  .line 12    000,240 --- > 000,000
000,064  .line 5    000,128  .line 9    000,192  .line 13

___________________________________/365th/_____________________________________________________     ___________________________________/390th/_____________________________________________________
_______/_______/_______/_______/_______/_______/_______/_______/_______/_______/_______/_______/    _______/_______/_______/_______/_______/_______/_______/_______/_______/_______/_______/_______/
000,224 001,224 002,224 003,224 004,224 005,224 006,224 007,224 008,224 009,224 010,224 011,224     308,224 309,224 310,224 311,224 312,224 313,224 314,224 315,224 316,224 317,224 318,224 319,224
000,225                                                                                             308,225
000,226                                                                                             308,226
000,227                                                                                             308,227
000,228                                                                                             308,228
000,229                                                                                             308,229
000,230                                                                                             308,230
000,231                                                                                             308,231
000,232                                                                                             308,232
000,233                                                                                             308,233
000,234                                                                                             308,234
000,235                                                                                             308,235
000,236                                                                                             308,236
000,237                                                                                             308,237
000,238                                                                                             308,238
000,239 001,239 002,239 003,239 004,239 005,239 006,239 007,239 008,239 009,239 010,239 011,239     308,239 309,239 310,239 311,239 312,239 313,239 314,239 315,239 316,239 317,239 318,239 319,239

 */



How did you do that Show that code.

Was there room for 70 points or positions or "dots"?

    int xpos[35];
    int ypos[35];

This

The problem is that you die even when there isn't a dot there.

sounds intriguing, perhaps you would take a few moments to explain the whole game; it would certainly make slogging though your code a bit easier!

a7

The game is a randomly generated map that you have to navigate without touching the edges to get to a spot where you then win.

I fixed it by checking if xnumero, which defined what spot in the list I was setting a position to was less than the length I wanted it to be.

Cool. Post the code that works so we can play too.

a7

WARNING! The 'sizeof' operator returns the size IN BYTES. Since each of the 35 elements of the array take up two bytes the 'sizeof xpos' is 70. You are going WAY off the end of your array.