Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Test {
  4. public static void main(String[] args) {
  5.  
  6. Random rnd = new Random();
  7. int[][] board = new int[8][8];
  8. int [] ant = new int [10];
  9.  
  10. int positionX = rnd.nextInt(8); //x-coordinate for the ant
  11. int positionY = rnd.nextInt(8); //y-coordinate for the ant
  12. int count = 0;
  13. int square = 0; //squares that have been stepped on
  14. boolean full = true;
  15. for (int i = 0; i < 10; i++) {
  16.  
  17. positionX = rnd.nextInt(8); //x-coordinate for the ant
  18. positionY = rnd.nextInt(8);
  19.  
  20. count = 0;
  21. square = 0;
  22.  
  23. full = true;;
  24.  
  25.  
  26.  
  27. for (int rows = 0; rows < 8; rows++) {
  28. for (int columns = 0; columns < 8; columns++) {
  29. board[rows][columns] = 0;
  30.  
  31.  
  32. while (full) {
  33. int step = rnd.nextInt(4)+1;
  34. if (step == 1) {
  35. positionX++; //move right
  36. count++;
  37. }
  38.  
  39. if (step == 2) {
  40. positionX--; // move left
  41. count++;
  42. }
  43.  
  44. if (step == 3) {
  45. positionY++; // move up
  46. count++;
  47. }
  48.  
  49. if (step == 4) {
  50. positionY--; // move down
  51. count++;
  52. }
  53.  
  54. if (positionX < 0 || positionY < 0 || positionX > 7 || positionY > 7) {
  55. count++;
  56.  
  57.  
  58. positionX = 0; //x-coordinate for the ant
  59. positionY = 0;
  60.  
  61. //System.out.println("help");
  62. }
  63.  
  64. //if (board[positionX][positionX] == 64) {
  65. //System.out.println(count);
  66. // full = true;
  67. //}
  68. //System.out.println("Y: " + positionY);
  69. // System.out.println("X: " + positionX);
  70. board[positionY][positionX] = 1;
  71. //System.out.println(count);
  72. for (int j = 0; j < 8; j++) {
  73. for (int k = 0; k < 8 ; k++) {
  74. if (board[j][k] == 1){
  75. square++;
  76. //System.out.println(square);
  77. }
  78. }
  79. if (square >= 64) {
  80. full = false;
  81.  
  82. //System.out.println("TEST:" + full);
  83. }
  84.  
  85.  
  86. }
  87. // System.out.println(i);
  88.  
  89. }
  90.  
  91.  
  92. }
  93.  
  94. }
  95.  
  96. System.out.println("Steps in simulation " + (i + 1) + " : " + count);
  97. }
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement