CODE CRASHED PLEASE HELP (21 sticks game / AI CRASHES)

So far here is my code
Pretty Much there is 21 sticks in the beginning the player gets choice between picking 1 or 2 sticks and the AI gets to pick the Sticks when its turn,The one who is forced to pick the last stick loses.
so far everything good but the AI gets stuck when its the last move not sure why

int sticks=21;
int take=0;// 1 or 2
int playerScore=0;
int AIScore=0;
int turn=0; // 0=init  1= player 2=AI 3=state
int mode=1; // 1=easy 2=Med 3= hard 4=Impossible  
int scene = 0; // 0=menu 1=play 2=win 3=lose
int alarm=-1; // alarm- undefined
PImage menu, game, win, lose, start, ring; //Background
PImage stick; //character
void setup()
{
  size(1080, 720);
  menu=loadImage("menu.jpg");
  game=loadImage("back.png");
  win=loadImage("win.jpg");
  lose=loadImage("lose.jpg");
  stick=loadImage("stick.png");
  start=loadImage("start.png");
  ring=loadImage("ring.png");
}
void showMenu()
{
  image (menu, 0, 0, width, height);
  textSize(90);
  text("21 Sticks Game", 210, 340);
  image(start, 410, 370);
  start.resize(250, 220);
  ////////////////////////////
  ring.resize(75, 75);
  image(ring, 50, 120 );
  image(ring, 50, 185 );
  image(ring, 50, 250 );
  image(ring, 50, 310 );
  //////////////////////////////////
  textSize(45);
  text("Modes", 75, 75);
  ///////////////////////////
  text("1", 75, 170 );  
  text("2", 75, 240 ); 
  text("3", 75, 300 );  
  text("4", 75, 360 );  
  ///////////////////////////////////
  textSize(30);
  text("Easy", 125, 170 );
  text("Med", 125, 240);
  text("Hard", 125, 300);  
  text("Impossible", 125, 365 );
}
void draw()
{  
  if (scene==2)
  {
    image(win, 0, 0, width, height);
    println(scene, mode);
  }

  if (scene == 0)
  {
    showMenu();
    mode=1;
  }
  //////////////////////////////////////////////  

  if (scene >= 1)
  {
    showGame();
    if (turn ==0)
      initGame();
    if (turn==2)
      AIturn();
  }
  if (scene>2)
  {
    showEnd();
  }
}
void mouseClicked()
{
  if (scene==0)
  {
    // image(start, 410, 370);
    int ax=410;
    int ay=370;
    if (mouseX>ax && mouseX<ax+200 && mouseY>ay && mouseY<ay+200 )
    {

      scene = 1;
      println(scene, mode);
    }///////////////////////////ARROW
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
    //     image(ring, 50, 185 );
    int bx=50;
    int by=185;
    if (mouseX>bx && mouseX<bx+50 && mouseY>by && mouseY<by+50 )
    {
      scene = 1;
      mode=2;
      println(scene, mode);
    }
    /////////////////////////////////////////////////////////////////////////////////
    //     image(ring, 50, 250 );


    int cx=50;
    int cy=250;
    if (mouseX>cx && mouseX<cx+50 && mouseY>cy && mouseY<cy+50 )
    {
      scene = 1;
      mode=3;
      println(scene, mode);
    }
    ////////////////////////////////////////////////////////////////////////////////
    //  image(ring, 50, 310 );

    int dx=50;
    int dy=310;
    if (mouseX>dx && mouseX<dx+50 && mouseY>dy && mouseY<dy+50 )
    {
      scene = 1;
      mode  = 4;
      println(scene, mode);
    }
    ////////////////////////////////////////////////////////////////////////////
  }
  if (scene==1 && turn ==1) // game
  {
    playerTurn();
  }
  if (scene>1)
  {
    //win/lose screen
  }
}
void showGame()
{
  image(game, 0, 0, width, height);

  fill(255);
  rect(270, 0, 550, 75);
  //////////////////////////////////////////////////  
  fill(0);
  textSize(80);
  text("Pick Up Sticks", 270, 60);
  ///////////////////////////////////////////////
  fill(#904800);
  rect(215, 550, 190, 60);
  rect(570, 550, 190, 60);
  ///////////////////////////////////////////////
  fill(255);
  textSize(35);
  text("Pick Up 1", 235, 590);
  text("Pick Up 2", 590, 590);
  //////////////////////////////////////////////
  for (int i=0; i<sticks; i++)
  {
    image(stick, 200+i%7*80, 90+i/7*160, 150, 130);
  }
}
void initGame()
{
  turn=1;
}
void playerTurn()
{
  if (mouseX> 215 && mouseX<215+140  &&/////RECT1
    mouseY > 550 && mouseY <550+60)
  {    
    sticks--;
    turn=2;
  }
  if (mouseX>570 && mouseX<570+140  &&//////RECT2
    mouseY > 550 && mouseY <550+60)
  {
    sticks-=2;
    checkWin();
    turn=2;
  }
}
void AIturn()
{
  if (alarm==-1)
  {
    alarm=20;
  } else if (alarm==0)
  {
    take = bestChoice(sticks);
    int ranNum = int (random (1, 101));
    if ((mode == 1&& ranNum>25)
      || (mode == 2&& ranNum >55)
      || (mode == 3&& ranNum >75))
      if (take == 1)
        take = 2;
      else take = 1;
    sticks-=take;
    checkWin();
    turn=1;
    alarm = -1;
  } else alarm--;
}

void showEnd()
{
  if (scene==3)
  {
    image(lose, 0, 0, width, height);
    println(scene, mode);
  }
}
int bestChoice(int s)
{
  if (s%3==2)
    return(1);
  if (s%3==0)
    return(2);
  return int(random(1, 3));
}
void checkWin()
{
  if ((turn == 1 && sticks==1)
    || (turn == 2 && sticks<=0))
    scene=2;////WIN
  println(scene, mode);
  if ((turn == 2 && sticks==1)
    || (turn == 1 && sticks<=0))
    scene=3;/////LOSE
  println(scene, mode);
}

1 Like

In draw()

>= is better

2 Likes

Oh Ok thanks
Now the showEnd() Doesnt Seem to work thou
everything runs perfectly except for the lose and win screen

Does scene 2 versus 3 store who has won?

2 Likes

The checkWin() checks the Win and turns the scene and scenes show win or lose, i think did couple months ago

Sorry, I posted wrong

I meant

>= is better

3 Likes

Then scene must be either 2 OR 3

to show if you win or loose

1 Like

Yeah Correct but the void created to control the scenes doesn’t work i think or something that controls the scene doesnt work

Use print(scene); in draw() to find out

2 Likes

Played Around with it I edited the code and now the lose screen shows up but the win(scene2) doesnt show up. It might be problem with the void checkWin() please help, i made a println(scene, mode); here is the latest compile when I won

1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1

Erros was becouse of this it should be == otherwise everytime I won it restarts the game

I have not checked the code since I can’t run the sketch without the images but I can see an error in the bestChoice method it should be

int bestChoice(int s)
{
  int a = s % 3;  // 0, 1 or 2
  if(a == 0) {  // No best solution so pick one at random
    return int(random(1, 3));
  }
  // Best solution is to make the number chosen add up to 3
  return 3 - a;
}
2 Likes

Also, why does screen switch between 1 and 2 ?

Or why 2 numbers in one line?

I see screen and mode (edited)

1 Like

Try with >=

Thank you

1 Like