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