Advertisement
Guest User

Ab23.2

a guest
Feb 11th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. import java.util.Random;
  2. /**
  3. * @author Kevin George
  4. * @version 2/10/16
  5. */
  6. public class Game
  7. {
  8. int [][] board;
  9. int posX,posY,deltaX,deltaY;
  10. int spot,x,y,fail;
  11. public Game()
  12. {
  13. board = new int[9][9];
  14. posX = posY = 1;
  15. spot = 2;
  16. fail = 0;
  17. }
  18.  
  19. private void createBoard()
  20. {
  21.  
  22. for(y=0;y<9;y++)
  23. for(x=0;x<9;x++)
  24. board[y][x] = 0;
  25. /*
  26. for(y=1;y<9;y++)
  27. {
  28. for(x=1;x<9;x++)
  29. System.out.print(board[y][x] +" ");
  30. System.out.println();
  31. }
  32. System.out.println();*/
  33. }
  34.  
  35. public void gamePlay()
  36. {
  37. board[1][1] = 1;
  38. int till64 = 0;
  39. boolean conti = true;
  40. do
  41. {
  42. createBoard();
  43. while(spot < 64)
  44. {
  45. move();
  46. fail++;
  47. if(fail>500)
  48. break;
  49. }
  50. System.out.println(spot);
  51. if(spot == 64)
  52. conti = false;
  53. else
  54. spot=2;
  55. //board = new int[9][9];
  56. posX = posY = 1;
  57. spot = 2;
  58. fail = 0;
  59. till64++;
  60. }while(conti);
  61. System.out.println();
  62. System.out.println(till64+" tries");
  63. }
  64.  
  65. private void move()
  66. {
  67. boolean j = true;
  68. deltaX = randomNum();
  69. while(j)
  70. {
  71. deltaY = randomNum();
  72. if(Math.abs(deltaY) == Math.abs(deltaX))
  73. j=true;
  74. else
  75. j=false;
  76. }
  77.  
  78. if((posX+deltaX)<=8&&(posY+deltaY)<=8&&(posX+deltaX)>=1&&(posY+deltaY)>=1&&board[posY+deltaY][posX+deltaX]==0)
  79. {
  80. posY+=deltaY;
  81. posX+=deltaX;
  82. board[posY][posX] = spot;
  83. spot++;
  84.  
  85. }
  86.  
  87. }
  88.  
  89. private int randomNum()
  90. {
  91. int n=0;
  92. Random rand = new Random();
  93. while(n==0)
  94. {
  95. n = rand.nextInt(5) - 2;
  96. }
  97. return n;
  98. }
  99.  
  100. public void printBoard()
  101. {
  102. for(y=1;y<9;y++)
  103. {
  104. for(x=1;x<9;x++)
  105. System.out.printf("%4d",board[y][x]);
  106. System.out.println();
  107. }
  108.  
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement