If statements are being ignored and loop one print

I’m having trouble with the code in an RPG game I’m trying to display onto a 2.2" ILI9341 screen. I’m using an Arduino Mega 2560. It seems as if the if statements are being ignored and skipping right to the calling the battle function and writing the text in it. How do I get it to stop this and work the way I intended to. I’m new to coding for an Arduino so the solution may be obvious. Any ideas on where it is going wrong?

#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"

#define TFT_RST 8
#define TFT_DC 9
#define TFT_CS 10
#define TFT_MOSI 11
#define TFT_MISO 12
#define TFT_CLK 13

//Determines monster numbers
int mn = 1;//rand()%(2+1)+1;

//Determines monster type
int mt = rand() % (5 + 1) + 1;

//Determines encounter chance
int ec = 1; //rand()%(2+1)+1;

//Player1 damage stat
int p1d = 10;

//Player2 damage stat
int p2d = 5;

//Player1 health stat
int p1h = 25;

//Player2 health stat
int p2h = 15;

//Player1 mana stat
int p1m = 10;

//Player2 mana stat
int p2m = 20;

//Board level
int bl = 1;

//Random damage number
int rd = rand() % (10 + 1) + (-5);

//Random player 1 damage number
int p1dr = p1d + rd;

//Random player 2 damage number
int p2dr = p2d + rd;

//Healing amount player 1
int hl1 = (p1h / 2) + rd;

//Healing amount player 2
int hl2 = (p2h / 2) + rd;

//Monster Health 1
int mh1 = 0;

//Monster Health 2
int mh2 = 0;

//Monster Health 3
int mh3 = 0;

//Monster Damage 1
int md1 = 0;

//Monster Damage 2
int md2 = 0;

//Monster Damage 3
int md3 = 0;

//Button placement
int bp = 0; // 0 = left, 1 = center, 2 = right

//Selection Location
int sl = 0; // 0 = out of battle, 1 = in battle, 2 = triple battle fight menu, 3 = specials p1, 4 = specials p2

//green slime stats
int mth1 = 10;
int mtd1 = 10;

//leafling stats
int mth2 = 5;
int mtd2 = 15;

//log ent stats
int mth3 = 20;
int mtd3 = 5;

//acorn boar stats
int mth4 = 13;
int mtd4 = 13;

//dire squirrel stats (maybe make solo battles only)
int mth5 = 20;
int mtd5 = 25;

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);

//set up screen
void setup() {
  tft.begin();
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  tft.fillScreen(ILI9341_BLACK);
}
void loop() {
  screen();
  button1();
  button2();
  button3();
}
void screen() {
  tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
  //read the pushbutton value into a variable
  int button1 = digitalRead(2);
  int button2 = digitalRead(3);
  int button3 = digitalRead(4);
  // Low is pressed
}
//one monster battle
void battle() {
  if (sl == 1)
    tft.fillScreen(ILI9341_RED);
      if (button2 == LOW)
        if (bp == 0)
          mh1 = mh1 - p1dr;
            if (mh1 > 0)
              tft.print("Attack Successful");
              sl = 0;
            if (mh1 <= 0)
              tft.print("Enemy dies");
              sl = 0;
}
void button1() {
  if (button1 == LOW) {
    bp++;
    tft.fillScreen(ILI9341_BLUE);
    if (bp > 2)
      bp = 0;
  }
}
void button2() {
  if (button2 == LOW) {
    if (sl == 0)
      if (bp == 0)
        if (bl == 1)
          if (ec == 1)
            sl = sl + 1;
    if (mn == 1)
      if (mt == 1)
        mh1 = mth1;
    md1 = mtd1;
    battle();
  }
  else if (mt == 2) {
    mh1 = mth2;
    mh1 = mtd2;
    battle();
  }
  else if (mt == 3) {
    mh1 = mth3;
    mh1 = mtd3;
    battle();
  }
  else if (mt == 4) {
    mh1 = mth4;
    mh1 = mtd4;
    battle();
  }
  else if (mt == 5) {
    mh1 = mth5;
    mh1 = mtd5;
    battle();
  }
  else if (mn == 2) {
    if (mt == 1)
      mh1 = mth1;
    md1 = mtd1;
    //else if (bl=1)
    //call loop based on in or out of battle
    //else if (bl=2)
    //call loop based on in or out of battle
  }
}
void button3() {
  if (button3 == LOW) {
    bp--;
    tft.fillScreen(ILI9341_GREEN);
    if (bp < 0)
      bp = 2;
  }
}
            if (mh1 > 0)
              tft.print("Attack Successful");
              sl = 0;

Multiple statements must be enclosed in { } for if()

A very helpful troubleshooting tool is the Auto Format feature (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor). If you do an Auto Format and then compare the resulting indentation to your intended program structure, it will quickly point you to where there is a missing or extra brace.

Your battle function after auto-format:

void battle() {
  if (sl == 1)
    tft.fillScreen(ILI9341_RED);
  if (button2 == LOW)
    if (bp == 0)
      mh1 = mh1 - p1dr;
  if (mh1 > 0)
    tft.print("Attack Successful");
  sl = 0;
  if (mh1 <= 0)
    tft.print("Enemy dies");
  sl = 0;
}

You have a number of places where you set the variable 'mh1' to one value and then immediately set it to a different value, like this:

  else if (mt == 2) {
    mh1 = mth2;
    mh1 = mtd2;
    battle();
  }

What did you intend that to do?!?

arduino_new:
Multiple statements must be enclosed in { } for if()

AKA "You're not in Python any more!" :slight_smile:

MarkMLl