Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. //Lukka
  2. import java.util.*;
  3. public class SnakeGame {
  4. public static char[][] gameBoard = new char[8][8];
  5. public static void printBoard(){
  6. int i,j;
  7. for(i=0;i<8;++i){
  8. for(j=0;j<8;++j){
  9. System.out.print(gameBoard[i][j]+" ");
  10. }
  11. System.out.print("\n");
  12. }
  13. System.out.print("\n");
  14. }
  15. public static boolean checkCollision(Snake snake1, Snake snake2){
  16. for(int i=0;i<snake1.getLength();++i){
  17. for(int j=0;j<snake2.getLength();++j){
  18. if(snake1.snakeBody.get(i).equals(snake2.snakeBody.get(j)))
  19. return true;
  20. }
  21. }
  22. return false;
  23. }
  24. public static void updateBoard(Snake snake){
  25. gameBoard[snake.snakeBody.get(0).x][snake.snakeBody.get(0).y]='@';
  26. for(int i=1;i<snake.getLength();++i){
  27. gameBoard[snake.snakeBody.get(i).x][snake.snakeBody.get(i).y]='o';
  28. }
  29. }
  30. public static void clearBoard(){
  31. int i,j;
  32. for(i=0;i<8;++i){
  33. for(j=0;j<8;++j){
  34. gameBoard[i][j] = '.';
  35. }
  36. }
  37. }
  38. public static void main(String args[]){
  39. clearBoard();
  40. Snake snake1 = new Snake(new Point(2,2),new Point(2,1),new Point(2,0));
  41. Snake snake2 = new Snake(new Point(5,5),new Point(5,6),new Point(5,7));
  42. updateBoard(snake1);
  43. updateBoard(snake2);
  44. while(true){
  45. printBoard();
  46. try{
  47. Thread.sleep(3000);
  48. }
  49. catch (InterruptedException e){};
  50. if(!snake1.newMove()){
  51. System.out.println("0-1");
  52. break;
  53. };
  54. if(!snake2.newMove()){
  55. System.out.println("1-0");
  56. break;
  57. }
  58. if(checkCollision(snake1,snake2)){
  59. System.out.println("0-0");
  60. break;
  61. }
  62. clearBoard();
  63. updateBoard(snake1);
  64. updateBoard(snake2);
  65. }
  66. }
  67. }
  68. class Point{
  69. public int x,y;
  70. public Point(){
  71. this.x = 0;
  72. this.y = 0;
  73. }
  74. public Point(int x, int y){
  75. this.x = x;
  76. this.y = y;
  77. }
  78. public void makeEqual(Point a){
  79. this.x = a.x;
  80. this.y = a.y;
  81. }
  82. }
  83. class Snake{
  84. ArrayList<Point> snakeBody = new ArrayList<>(3);
  85. private int moveNumber = 0;
  86. public Snake(Point a, Point b, Point c){
  87. this.snakeBody.add(a);
  88. this.snakeBody.add(b);
  89. this.snakeBody.add(c);
  90. }
  91. public int getLength(){
  92. return this.snakeBody.size();
  93. }
  94. public boolean timeToIncrease(){
  95. return (this.moveNumber % 3 == 0)? true: false;
  96. }
  97. private boolean checkPointCollision(Point a){
  98. if(a.x<0 || a.x>7 || a.y<0 ||a.y>7)
  99. return true;
  100. for(int i=0;i<this.getLength();++i){
  101. if(a.equals(this.snakeBody.get(i)))
  102. return true;
  103. }
  104. return false;
  105. }
  106. public boolean newMove(){
  107. if(!timeToIncrease()) {
  108. (this.snakeBody.get(this.getLength())).makeEqual(new Point(-1,-1));
  109. }
  110. Point newHeadPosition = new Point();
  111. int spareDirections = 4;
  112. int[] Directions = new int[4];
  113. do {
  114. newHeadPosition.makeEqual(this.snakeBody.get(0));
  115. Random rnd = new Random();
  116. int moveNumber = rnd.nextInt(4);
  117. switch (moveNumber) {
  118. case 0:
  119. newHeadPosition.x--;
  120. break;
  121. case 1:
  122. newHeadPosition.y--;
  123. break;
  124. case 2:
  125. newHeadPosition.x++;
  126. break;
  127. case 3:
  128. newHeadPosition.y++;
  129. break;
  130. }
  131. if(Directions[moveNumber]==0){
  132. spareDirections--;
  133. Directions[moveNumber]=1;
  134. }
  135. if(spareDirections==0)
  136. return false;
  137. }while(this.checkPointCollision(newHeadPosition));
  138. if(timeToIncrease()){
  139. this.snakeBody.add(new Point(-1,-1));
  140. }
  141. for(int i=this.getLength()-1;i>0;--i){
  142. this.snakeBody.get(i).makeEqual(this.snakeBody.get(i-1));
  143. }
  144. (this.snakeBody.get(0)).makeEqual(newHeadPosition);
  145. return true;
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement