Problems with creating a slot machine.

Started by Twisted, Wed 27/10/2010 13:26:10

Previous topic - Next topic

Twisted

I am trying to make a slot machine with AGS but for some reason the reward system isn't working.
my script looks somewhat like this, what is wrong with it?

int cash;           // your money
int reward1 =20;   // the prize you win for three cherry's in a row
int slot1;      // the first slot
int slot2;      // the second slot
int slot3;      // the third slot

slot1 = random(3);  //gives the slot a number in wich 0 = bar, 1 = cherry, 2 = lime etc.
slot2 = random(3);
slot3 = random(3);

if(slot1 == 1 && slot2 == 1 && slot3 ==1) //three cherry's in a row
{
cash = (cash + reward1); // the prize added to your money
return cash;       // your new amount of money
}

Matti

Instead of
cash = (cash + reward1);
return cash;
you can write
cash += reward1;
Also, you can add your variables in a row like this:
int slot1, slot2, slot3;


But I don't really see a problem here, are you sure it doesn't work?

Twisted

I'm not at home right now so i used this script as an example, maybe i just got it right this tie by luck  :).

The actual script is a little bit more complex than this one, i'll browse through it again when i get home. If i still find that i'm having problems, would it be ok to post the actual script? (it's a bit long)

Matti

Quote from: Twisted on Wed 27/10/2010 13:39:23
would it be ok to post the actual script? (it's a bit long)

That's not only ok but the best way to track down the bug.  ;)

Twisted

Okay, i'll take a look at it. If i find that it won't work i'll rename my int's and comments to english to make it more understandable for you because it's written in dutch  ;). At least i won't be bored today.

edit: And if it does work i'll post the script anyway incase you guys might want to use a slot machine in one of your games.

Twisted

Sorry for the double post. I've got it up and running after screwing with it for a while. I've translated it to english so you guys can use it if you want (it still needs some tweaking and i still have to find out how to actually put my money in the inventory)

// room script file
// room = Slot Machine

int cash = 100; // your ammount of money
int reward; //the prize that you've won
int slot1; //the first slot
int slot2; //the second slot
int slot3; //the third slot
int costs = 1; //the cost to play slots

function hHotspot1_Interact()
{
slot1 = Random(3); //assigns random number to slot in wich 0 = bar,  1 = cherry, 2 = seven and 3 = orange.
slot2 = Random(3);
slot3 = Random(3);
cash = (cash - costs); //puts a coin in the slot machine


  if(cash < 0)
  {
    Display("Sorry,  you're out of cash!");
  }
  else
  {
  if (slot1 == 0) //slot 1 = bar
    {
      object[0].SetView(2, 0, 0); //sets animation of slot 1 to bar
    }

  if (slot1 == 1) //slot 1 = cherry
  {
      object[0].SetView(2, 0, 2); //sets animation of slot 1 to cherry
  }

  if (slot1 == 2) //slot 1 = seven
  {
      object[0].SetView(2, 0, 1); //sets animation of slot 1 to seven
  }

  if (slot1 == 3) //slot 1 = orange
  {
      object[0].SetView(2, 0, 3); //sets animation of slot 1 to orange 
  }

  if (slot2 == 0)  //slot 2 = bar
  {
      object[1].SetView(2, 0, 0); //sets animation of slot 2 to bar
  }

  if (slot2 == 1)  //slot 2 = cherry
  {
      object[1].SetView(2, 0, 2); //sets animation of slot 2 to cherry
  }

  if (slot2 == 2) //slot 2 = seven
  {
      object[1].SetView(2, 0, 1); //sets animation of slot 2 to seven
  }

  if (slot2 == 3) //slot 2 = orange
  {
      object[1].SetView(2, 0, 3); //sets animation of slot 2 to orange
  }

  if (slot3 == 0) //slot 3 = bar
  {
      object[2].SetView(2, 0, 0); //sets animation of slot 3 to bar
  }

  if (slot3 == 1) //slot 3 = cherry
  {
    object[2].SetView(2, 0, 2); //sets animation of slot 3 to cherry
  }

  if (slot3 == 2) //slot 3 = seven
  {
    object[2].SetView(2, 0, 1); //sets animation of slot 3 to seven
  }

  if (slot3 == 3) //slot 3 = orange
  {
    object[2].SetView(2, 0, 3); //sets animation of slot 3 to orange
  }
 
  if (slot1 == 1 && slot2 == 1 && slot3 == 1) // three bars in a row
  {
    reward = 100; //the prize that is won
    cash = (cash+reward); //the new ammount of cash
    return cash;
  }
   
   if (slot1 == 3 && slot2 == 3 && slot3 == 3) // three sevens in a row
  {
    reward = 50; //the prize that is won
    cash = (cash+reward); //the new ammount of cash
    return cash;
  }
 
   if (slot1 == 3 && slot2 == 3 && slot3 == 3) // three oranges in a row
  {
    reward = 40; //the prize that is won
    cash = (cash+reward); //the new ammount of cash
    return cash;
  } 
   
    if (slot1 == 2 && slot2 == 2 && slot3 == 2) // three cherry's in a row
  {
    reward = 30; //the prize that is won
    cash = (cash+reward); //the new ammount of cash
    return cash;
  }
   
   if (slot1 == 2 && slot2 == 2) // two cherry's in a row
  {
    reward = 20; //the prize that is won
    cash = (cash+reward); //the new ammount of cash
    return cash;
  }
 
  }
  DisplayAt(155,  33,  300, "Your current cash is %d dollar", cash);
 
}

Khris

#6
You can greatly simplify and shorten that code.

First of all you don't animate the slot objects, thus all you need to do is change the object's .Graphic.
You don't even need views.
Plus, if you use consecutive sprite slot numbers, all you need to do is "object[0].Graphic = slot1 + X;"

So let's assume the bar sprite is sprite #15, cherry #16, seven #17 and orange #18.

Code: ags
// room script file
// room = Slot Machine

int cash = 100; // your ammount of money
int reward; //the prize that you've won
int slot[3]; // create slot[0], slot[1] and slot[2]
int costs = 1; //the cost to play slots

#define BAR_SPRITE 15                             <---- SET FIRST SPRITE SLOT HERE

function hHotspot1_Interact() {

  int i;
  while (i < 3) {
    slot[t] = Random(3); //assigns random number to slot in wich 0 = bar,  1 = cherry, 2 = seven and 3 = orange.
    i++;
  }

  if (cash == 0) {
    Display("Sorry,  you're out of cash!");
    return; // exit function
  }

  cash -= costs;  //puts a coin in the slot machine

  // set objects' graphic property
  i = 0;
  while (i < 3) {
    object[i].Graphic = slot[i] + BAR_SPRITE;
    i++;
  }

  if (slot[0] == slot[1] && slot[0] == slot[2]) {  // all three slots the same?

    if (slot[0] == 0) reward = 100; // bars
    if (slot[0] == 1) reward = 30;  // cherries
    if (slot[0] == 2) reward = 50;  // sevens
    if (slot[0] == 3) reward = 40;  // oranges

    cash += reward;
  }

  DisplayAt(155,  33,  300, "Your current cash is %d dollar", cash);
 
}


Just saw that your rewards are completely mixed up.
You're checking for 3 twice and not for 0, and the comment doesn't fit the "0 = bar, etc." from earlier.

EDIT: You might have to put a "Wait(1);" before the DisplayAt in case the objects' graphics aren't updated before the text is displayed.

Twisted

Thanks a lot for pointing out the reward mix up. Also thanks for showing the "while" option. I'm pretty new to this. I picked AGS two days ago so i still have to learn alot.  Anyway i am planning to make animations for the slots but I thought, before i begin with animating i want the basic script to work so that way i would be easier for me to see what's wrong if it doesn't work.

SMF spam blocked by CleanTalk