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

2014/12/11

Non Repeating Random Generator

BrownBoii333 BrownBoii333

2014/12/11

#
HI I'm trying to make a non repeating random generator for my flip tiles game. This is a memorization game which involves a set amount of tiles that are hiding images of smiley face and images of sad faces, At the start of the level all of the tiles disappear so that the player can see where the faces are and then when they re-appear they need to click the tiles with the smiley faces. But i need a random generator that adds a certain amount of smileys in a random place. Greenfoot.getRandomNumber sometimes adds like 4 sometimes adds none. I have something that may work but don't know how to use it. here is my code. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.*; /** * Write a description of class TWorld here. * * @author (your name) * @version (a version number or a date) */ public class TWorld extends World { private Tiles tiles = new Tiles; private MPDS mpds = new MPDS; int worldWidth = 3; int worldHeight = 3; int cellSize = 100; private int CELL_SIZE = 100; public TWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(3,3,100); CELL_SIZE = new Tiles().cellSize +5; GreenfootImage img = new GreenfootImage(CELL_SIZE,CELL_SIZE); img.drawRect(0,0,CELL_SIZE-1,CELL_SIZE-1); img.setColor(new Color(197,162,120)); img.fill(); setBackground(img); int x=0, y=0; setPaintOrder(Tiles.class,MPDS.class); for(int h=0; h<9; h++) { tiles = new Tiles(); addObject( tiles, x, y ); mpds = new MPDS(false,CELL_SIZE); addObject( mpds,x,y); x++; if( x==3 ) { x=0; y++; } } } public TWorld(int worldWidth,int worldHeight,int CELL_SIZE){ super(worldWidth,worldWidth,CELL_SIZE); CELL_SIZE = new Tiles().cellSize +5; GreenfootImage img = new GreenfootImage(CELL_SIZE,CELL_SIZE); img.drawRect(0,0,CELL_SIZE-1,CELL_SIZE-1); img.setColor(Color.WHITE); setBackground(img); int x=0, y=0; setPaintOrder(Tiles.class,MPDS.class); for(int h=0; h<9; h++) { tiles = new Tiles(); addObject( tiles, x, y ); mpds = new MPDS(false,CELL_SIZE); addObject( mpds,x,y); x++; if( x==3 ) { x=0; y++; } } } public void random(){ int tileLeft = worldWidth; mpds = new MPDS; while(tileLeft>0){ int randomCard; do { randomCard = Greenfoot.getRandomNumber(worldWidth *worldWidth); } while(mpds.correct == true); mpds = new MPDS (true, CELL_SIZE); tileLeft--; } } } My actor subclasses: (note in the first class there is some experimental code) import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MPDS here. * * @author (your name) * @version (a version number or a date) */ public class MPDS extends Actor { public GreenfootImage img1 = new GreenfootImage("GOOD SMILE.png"); public GreenfootImage img2 = new GreenfootImage("BAD SMILE.png"); int x = 10; public boolean check = false; public boolean check2 = false; int y = 0; public boolean correct; public int Smaller; //197,162,120 public MPDS(boolean isRight,int SD){ correct = isRight; Smaller = SD; if(correct){ setImage(img1); }else if (!correct) { setImage(img2); } } public void act() { getImage().scale(Tiles.cellSize,Tiles.cellSize); touching(); } public void touching(){ Actor d = (Actor) getOneIntersectingObject(Tiles.class); if(d==null){ if(getImage()==img1){ check=true; if(check==true){ y++; check =false; } } } if(y>=1){ y=1; } } public boolean goUP(){ y++; return true; } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.*; /** * Write a description of class TWorld here. * * @author (your name) * @version (a version number or a date) */ public class TWorld extends World { private Tiles tiles = new Tiles; private MPDS mpds = new MPDS; int worldWidth = 3; int worldHeight = 3; int cellSize = 100; private int CELL_SIZE = 100; public TWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(3,3,100); CELL_SIZE = new Tiles().cellSize +5; GreenfootImage img = new GreenfootImage(CELL_SIZE,CELL_SIZE); img.drawRect(0,0,CELL_SIZE-1,CELL_SIZE-1); img.setColor(new Color(197,162,120)); img.fill(); setBackground(img); int x=0, y=0; setPaintOrder(Tiles.class,MPDS.class); for(int h=0; h<9; h++) { tiles = new Tiles(); addObject( tiles, x, y ); mpds = new MPDS(false,CELL_SIZE); addObject( mpds,x,y); x++; if( x==3 ) { x=0; y++; } } } public TWorld(int worldWidth,int worldHeight,int CELL_SIZE){ super(worldWidth,worldWidth,CELL_SIZE); CELL_SIZE = new Tiles().cellSize +5; GreenfootImage img = new GreenfootImage(CELL_SIZE,CELL_SIZE); img.drawRect(0,0,CELL_SIZE-1,CELL_SIZE-1); img.setColor(Color.WHITE); setBackground(img); int x=0, y=0; setPaintOrder(Tiles.class,MPDS.class); for(int h=0; h<9; h++) { tiles = new Tiles(); addObject( tiles, x, y ); mpds = new MPDS(false,CELL_SIZE); addObject( mpds,x,y); x++; if( x==3 ) { x=0; y++; } } } public void random(){ int tileLeft = worldWidth; mpds = new MPDS; while(tileLeft>0){ int randomCard; do { randomCard = Greenfoot.getRandomNumber(worldWidth *worldWidth); } while(mpds.correct == true); mpds = new MPDS (true, CELL_SIZE); tileLeft--; } } } sooooo ya, help plz?
danpost danpost

2014/12/12

#
You can produce a range of numbers with the following:
1
2
3
int minimumValue = 5;
int maximumValue = 10;
int rand = minimumValue+Greenfoot.getRandomNumber(maximumValue-minimumValue+1);
of course, that can be simplified to this:
1
int rand = 5+Greenfoot.getRandomNumber(6);
I just needed to show how the numbers were determined. The value of 'rand' will be a number between 5 and 10 inclusive. Would that be something like what you were looking for?
You need to login to post a reply.