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

**URL:** <https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802>\
**Category:** Coding Questions\
**Created:** [January 2, 2020, 8:14am UTC](https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802 "2020-01-02T08:14:36Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![pothead](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@pothead](https://discourse.processing.org/u/pothead)\
**Post date:** [January 2, 2020, 8:14am UTC](https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802/1 "2020-01-02T08:14:36Z")

</div>

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

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 2, 2020, 8:33am UTC](https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802/2 "2020-01-02T08:33:03Z")

</div>

In draw()

> [@pothead](#):
>
> if (scene == 1)

`>=` is better

---

<div class="post-metadata">

**Author:** ![pothead](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@pothead](https://discourse.processing.org/u/pothead)\
**Post date:** [January 2, 2020, 8:45am UTC](https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802/3 "2020-01-02T08:45:04Z")

</div>

> [@Chrisir](#):
>
> = is better

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 2, 2020, 9:05am UTC](https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802/4 "2020-01-02T09:05:08Z")

</div>

Does scene 2 versus 3 store who has won?

---

<div class="post-metadata">

**Author:** ![pothead](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@pothead](https://discourse.processing.org/u/pothead)\
**Post date:** [January 2, 2020, 9:08am UTC](https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802/5 "2020-01-02T09:08:51Z")

</div>

> [@Chrisir](#):
>
> 2 versus 3 store who has won?

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 2, 2020, 9:33am UTC](https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802/6 "2020-01-02T09:33:15Z")

</div>

Sorry, I posted wrong

I meant

`>=` is better

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 2, 2020, 9:34am UTC](https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802/7 "2020-01-02T09:34:10Z")

</div>

> [@pothead](#):
>
> turns the scene and scenes show win or lose

Then scene must be either 2 OR 3

to show if you win or loose

---

<div class="post-metadata">

**Author:** ![pothead](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@pothead](https://discourse.processing.org/u/pothead)\
**Post date:** [January 2, 2020, 9:47am UTC](https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802/8 "2020-01-02T09:47:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 2, 2020, 9:48am UTC](https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802/9 "2020-01-02T09:48:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pothead](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@pothead](https://discourse.processing.org/u/pothead)\
**Post date:** [January 2, 2020, 10:54am UTC](https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802/10 "2020-01-02T10:54:41Z")

</div>

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![pothead](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@pothead](https://discourse.processing.org/u/pothead)\
**Post date:** [January 2, 2020, 10:58am UTC](https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802/11 "2020-01-02T10:58:37Z")

</div>

> [@Chrisir](#):
>
> Sorry, I posted wrong
> 
> I meant
> 
> `>=` is better

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

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [January 2, 2020, 11:07am UTC](https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802/12 "2020-01-02T11:07:20Z")

</div>

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

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 2, 2020, 11:26am UTC](https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802/13 "2020-01-02T11:26:42Z")

</div>

Also, why does screen switch between 1 and 2 ?

Or why 2 numbers in one line?

I see screen and mode (edited)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 2, 2020, 11:53am UTC](https://discourse.processing.org/t/code-crashed-please-help-21-sticks-game-ai-crashes/16802/14 "2020-01-02T11:53:59Z")

</div>

> [@pothead](#):
>
> Erros was becouse of this it should be ==

Try with \>=

Thank you
