Hi,need a little help here
i want to create enemies that pop out 5 random questions out of 6 and can't be taken twice
while the enemies number and position is fix in the world class
i already use random number method but still can be taken twice
how do i do this? any help would be appreciated
this is my world class
this is my ProEnemy class
1 2 3 4 5 | addObject( new ProEnemy( 1 ), 350 , 510 ); addObject( new ProEnemy( 2 ), 780 , 510 ); addObject( new ProEnemy( 3 ), 666 , 294 ); addObject( new ProEnemy( 4 ), 968 , 210 ); addObject( new ProEnemy( 5 ), 155 , 210 ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.*; import javax.swing.JOptionPane; import java.awt.Point; import javax.swing.JDialog; import javax.swing.JFrame; /** * Write a description of class Addition here. * * @author (your name) * @version (a version number or a date) */ public class ProEnemy extends Enemies { ... ... private int number = getRandomNumber( 1 , 6 ); public int newSpot1 = getRandomNumber( 120 , 370 ); public int newSpot2 = getRandomNumber( 640 , 850 ); public int newSpot3 = getRandomNumber( 656 , 868 ); public int newSpot4 = getRandomNumber( 440 , 550 ); public int newSpot5 = getRandomNumber( 179 , 390 ); JFrame frame; public ProEnemy( int voucher) { ticket = voucher; } public void addedToWorld(World World) { getPosition(); } public void getPosition() { if (ticket== 1 ) setMove( 120 , 370 , 1 ,- 1 ); if (ticket== 2 ) setMove( 640 , 850 , 1 ,- 1 ); if (ticket== 3 ) setMove( 656 , 868 , 1 , 1 ); if (ticket== 4 ) setMove( 968 , 968 , 0 ,- 1 ); if (ticket== 5 ) setMove( 179 , 390 , 1 , 1 ); } public void respawn() { if (ticket== 1 ) getWorld().addObject ( new ProEnemy ( 1 ), (newSpot1), 510 ); if (ticket== 2 ) getWorld().addObject ( new ProEnemy ( 2 ), (newSpot2), 510 ); if (ticket== 3 ) getWorld().addObject ( new ProEnemy ( 3 ), (newSpot3), 294 ); if (ticket== 4 ) getWorld().addObject ( new ProEnemy ( 4 ), 968 , 210 ); if (ticket== 5 ) getWorld().addObject ( new ProEnemy ( 5 ), (newSpot5), 210 ); } setMove() .. .. public void act() { animate(); movement .. .. touchingPlayer(); } animate() .. .. public boolean touchingPlayer() { Actor touchingPlayer = getOneObjectAtOffset( 0 , 0 , Player. class ); if (touchingPlayer != null ) { throwQuestion(); getWorld().removeObject( this ); } return touchingPlayer!= null ; } public int getRandomNumber( int start, int end) { int normal = Greenfoot.getRandomNumber(end-start+ 1 ); return normal+start; } public void throwQuestion() { if (number == 1 ) { int answer = 0 ; String input = JOptionPane.showInputDialog( null , "1+1 = ?" ); if (input == null ) { errorQuestion(); } else { try { answer = Integer.parseInt(input); if (answer == 2 ) { JOptionPane.showMessageDialog(frame, "good" ); } else { int reply = JOptionPane.showConfirmDialog( null , "do you want to retry ?" , "wrong answer" , JOptionPane.YES_NO_OPTION); if (reply == JOptionPane.YES_OPTION) { throwQuestion(); } else { respawn(); } } } catch (Exception ex) { errorQuestion(); } } } .. .. repeat until number == 6 } public void errorQuestion() { try { respawn(); } catch (Exception ex) { } } } |