fork download
  1. import java.util.Random;
  2.  
  3. class Cell {
  4. public int addSpaces;
  5.  
  6. public Cell() {
  7. addSpaces = 0;
  8. }
  9.  
  10. public Cell(int x) {
  11. addSpaces = x;
  12. }
  13.  
  14. public String toString() {
  15. String print;
  16. if (addSpaces == -10)
  17. print = "C10";
  18. else
  19. print = "---";
  20. return print;
  21. }
  22. }
  23.  
  24. class ChutesAndLadders {
  25. Cell[] board = new Cell[100]; // Set array of Cell object
  26. Random ran = new Random();
  27. Cell s = new Cell();
  28. public int Chut, Ladd;
  29.  
  30. public ChutesAndLadders() {
  31. }
  32.  
  33. public ChutesAndLadders(int numChutes, int numLadders) {
  34. Chut = numChutes;
  35. Ladd = numLadders;
  36. }
  37.  
  38. public void setBoard() {
  39. for (int i = 0; i < board.length; i++)
  40. board[i] = new Cell(); // board now has 100 Cell with toString "---"
  41. int chutCount = 0;
  42. while (chutCount < Chut)
  43. {
  44. int randomNum = ran.nextInt(board.length);
  45. if (board[randomNum].addSpaces == 0) // uninitialized
  46. {
  47. board[randomNum] = new Cell(-10);
  48. chutCount++;
  49. }
  50. }
  51. }
  52.  
  53. public void printBoard() { // method to print out board
  54. int count = 0;
  55. for (int i = 0; i < board.length; i++) {
  56. count++;
  57. System.out.print("|" + board[i]);
  58. if (count == 10) {
  59. System.out.print("|");
  60. System.out.println();
  61. count = 0;
  62. }
  63. }
  64. }
  65. }
  66.  
  67. public class Main
  68. {
  69. public static void main(String[] args) {
  70. ChutesAndLadders cl = new ChutesAndLadders(10, 10);
  71. cl.setBoard();
  72. cl.printBoard();
  73. }
  74. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
|C10|---|---|---|---|---|C10|---|---|---|
|---|---|C10|---|C10|---|---|---|C10|---|
|---|---|---|---|---|---|---|---|---|C10|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|C10|---|---|---|---|---|---|---|---|
|C10|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|C10|---|---|C10|---|