/** sets the amoebes to random squares, two for each player */ private void setAmoebesToSquares() { Die die = dieProvider.get(); for (int i = 0; i < this.players.length; i++) { Game.Color color = this.players[i].getColor(); boolean statusOK = false; // AK this sounds a bit over-dramatic, "freeSquare" seems more appropriate int randomRow, randomCol; do { randomRow = die.roll(1, 5) - 1; // AK could you maybe implement a `choose(List<Square> from) : Square` function // that randomCol = die.roll(1, 5) - 1; // guarantees that even if I'm unlucky (or have badly mocked), always halts? statusOK = Board.board[randomRow][randomCol].getAmoebesList().size() == 0; } while (!statusOK); Amoebe amo1 = AmoebeFactory.get(this.compassProvider, color); board[randomRow][randomCol].enterSquare(amo1); statusOK = false; // AK don't repeat yourselves, you could have done the same with a simple for-loop // here. do { // remember that more code is more place for error randomRow = die.roll(1, 5) - 1; randomCol = die.roll(1, 5) - 1; statusOK = Board.board[randomRow][randomCol].getAmoebesList().size() == 0; } while (!statusOK); Amoebe amo2 = AmoebeFactory.get(this.compassProvider, color); board[randomRow][randomCol].enterSquare(amo2); } }
public static void main(String[] args) { Scanner userInput = new Scanner(System.in); Die die1 = new Die(); Die die2 = new Die(); die1.roll(); die1.print(); die2.roll(); die2.print(); }
@Test public void testRollDiceIsRandom() { RandomMock r = new RandomMock(); Die d = new Die(r); r.setFace(2); d.roll(); assertEquals(2, d.getValue()); r.setFace(3); d.roll(); assertEquals(3, d.getValue()); }
public static void main(String[] args) { int antal; Scanner scan = new Scanner(System.in); System.out.print("Number of sides: "); antal = scan.nextInt(); Die Dice = new Die(antal); Die Dice2 = new Die(antal); System.out.println("slag ifrån tärning1:" + Dice.roll()); System.out.println("slag ifrån tärning2:" + Dice2.roll()); System.out.println(Dice); System.out.println(Dice2); }
public void rethrow() { if (canRethrow()) for (Die die : getDice()) { if (!isSelected(die)) die.roll(); } myCurrentAttempts++; }
/** The beginnings of a super cool dungeon hack simulator */ public static void main(String[] args) { Weapon s = new Weapon("Broad Sword", "slashes", 10); Weapon c = new Weapon("Arm", "Claws", 6); Actor player = new Actor("Conan the Barbarian", 5, 10, s); Actor monster = new Actor("Grumpy Troll", 5, 8, c); Die d20 = new Die(20); // player gets first attack :-) int roll = d20.roll(); System.out.printf("[Roll=%d|AC=%d] ", roll, monster.armorClass()); System.out.printf( "%s %s his %s at the %s and ", player.name(), player.weapon().action(), player.weapon().name(), monster.name()); if (roll >= monster.armorClass()) { int damage = player.weapon().hitDamage(); monster.takeDamage(damage); System.out.printf("hits for %d damage", damage); if (monster.isDead()) { System.out.printf(", killing it"); } } else { System.out.printf("misses"); } System.out.println("!"); }
public static void main(String [] args) { Scanner sc = new Scanner(System.in); System.out.print("Number of sides: "); int sides = sc.nextInt() Die d = new Die(sides); System.out.println("Alea iacta est: " + d.roll()); }
// Starts this die rolling public void roll() { super.roll(); int width = tableRight - tableLeft; int height = tableBottom - tableTop; xCenter = tableLeft; yCenter = tableTop + height / 2; xSpeed = width * (Math.random() + 1) * speedFactor; ySpeed = height * (Math.random() - .5) * 2. * speedFactor; }
// Draws this die when rolling with a random number of dots private void drawRolling(Graphics g) { int x = xCenter - dieSize / 2 + (int) (3 * Math.random()) - 1; int y = yCenter - dieSize / 2 + (int) (3 * Math.random()) - 1; g.setColor(Color.RED); if (x % 2 != 0) g.fillRoundRect(x, y, dieSize, dieSize, dieSize / 4, dieSize / 4); else g.fillOval(x - 2, y - 2, dieSize + 4, dieSize + 4); Die die = new Die(); die.roll(); drawDots(g, x, y, die.getNumDots()); }
public void throwDie(Die die) { int currentThrow; presskey = new Scanner(System.in); System.out.print("Press Enter to throw your die!"); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } presskey.nextLine(); currentThrow = die.roll(); // this.point += currentThrow; this.roundPoint = currentThrow; System.out.println("You get " + currentThrow + " points."); System.out.println(); }
public static void main(String[] args) { Die die = new Die(); int p1 = 0; int p2 = 0; while (p1 <= 100 && p2 <= 100) { die.roll(); p1 += die.getResult(); if (p1 <= 100) { p2 += die.getResult(); } } if (p1 > 100) { System.out.println("Spelare ett vann!"); } else { System.out.println("Spelare två vann!"); } }
public int roll() { return die1.roll() + die2.roll(); }