This site requires JavaScript, please enable it in your browser!
Greenfoot back
JPEG
JPEG wrote ...

2019/2/10

Random Function

JPEG JPEG

2019/2/10

#
Hi there! So I'm making Rock, Paper & Scissors on Greenfoot. So I have 3 actors:Rock, paper and scissors.I'm on the part where the player has to click on an actor when...... 'How can I code the program to generate one of the actor's randomly (as it's the Computer playing) and to show it on MyWorld'? So far below is the coding I'm using at the moment on My world. As you can tell, I already set up 3 actors in place. I want the program to choose a random actor after the user has clicked any of the 3 actors shown on the screen. After they click on an actor, one random actor out of the three should be shown above them (to show what the player has chosen)-hence the switch case. I know I have loads of mistakes below and was hoping if anyone could help me out? public class MyWorld extends greenfoot.World { public MyWorld() { super(650, 500,1); prepare(); } private void prepare() { Paper paper=new Paper(); addObject(paper, 200,400); paper.setLocation(150,350); Rock rock=new Rock(); addObject(rock, 250,400); rock.setLocation(325,350); Scissors scissors=new Scissors(); addObject(scissors, 200,400); scissors.setLocation(500,350); } private Actor getRandomActor() { if (Greenfoot.mouseClicked(this)) switch (Greenfoot.getRandomNumber(3)) { case 0: Rock rock=new Rock(); addObject(rock, 250,400); rock.setLocation(325,100);//return new Rock(); case 1: Paper paper=new Paper(); addObject(paper, 200,400); paper.setLocation(325,100); case 2: Scissors scissors=new Scissors(); addObject(scissors, 200,400); scissors.setLocation(325,100); } return null; } }
danpost danpost

2019/2/10

#
JPEG wrote...
I know I have loads of mistakes below and was hoping if anyone could help me out?
One big one is this line:
if (Greenfoot.mouseClicked(this))
'this' in your MyWorld class will refer to your MyWorld object. The line will not return a true condition if an Actor object is clicked on. If you use 'null' instead of 'this', any click will be detected, in which case you will then have to determine what was clicked on. This is possible using a bunch of checks, but I am sure there must be a better way. You could make references to the three actors and check for clicks on all three individually (placing the references in an array and using a loop to iterate through it would be feasible). This brings the number of checks to a nominal level (possibly the minimum). Another way to handle it is to use an intermediate class between Actor and the 3 subclasses you currently have. It can contain the codes that would be repeated in all 3 classes so you only have to code it once. It could also contain two fields -- one field for an identifier (assigned differently by its subclasses) and one class field (static) that can be assigned one of the identifiers after the actor was determined to have been clicked on. Another problem is that nowhere are you calling the getRandomActor method. It must be called to execute and, if you are to wait on a mouse click, it must be called repeatedly to catch the click when it happens. You will need an act method to call this method. The switch block is missing break statements between the cases; however, if you add the appropriate return statements at the end of each case, break statements would be unnecessary. The getRandomActor method returns the actor chosen by the computer; however, as your code is now, the calling method will not know which actor was clicked on. If I were to code this, I would place the actors in an array in an order like -- { rock, paper, scissors }, where, allowing wrapping, an actor wins to the one on its left. Being in an array, they are automatically assigned identifiers -- the index at which it is located in the array. Then, when the computer makes a choice, you can just compare the random number with the index of the actor chosen by the player:
switch (compChoice+3-playerChoice)%3)
{
    case 0: tied(); break;
    case 1: compWon(); break;
    case 2: playerWon(); break;
}
I might just draw the images of the choices onto the background image of the world before this switch block and add the appropriate text in the methods called within the switch block. It might be a good idea to add a started method in your MyWorld class to create and set active a new MyWorld object. That way you can use Greenfoot.stop(); after the switch block. Then, when the player clicks Run, a new game will start automatically.
JPEG JPEG

2019/2/12

#
Thanks, it worked!
You need to login to post a reply.