Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1.  
  2. //we are maintaining a snake board of [5][5] in which snake would grow and move in the directions specified by user
  3.  
  4. class SnakeBoard{
  5. int [][] Board;
  6. int moves;
  7. int [][] position=new int[100][2];
  8. int direction;
  9. //up 0,left 1,down 2,right 3
  10. public SnakeBoard(){
  11. //we are initializing the array to teh starting position when the constructed is called initially
  12. Board=new int[][]{
  13. { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  14. { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  15. { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  16. { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  17. { 0, 0, 0, 0, 1, 0, 0, 0, 0 },
  18. { 0, 0, 0, 0, 2, 0, 0, 0, 0 },
  19. { 0, 0, 0, 0, 3, 0, 0, 0, 0 },
  20. { 0, 0, 0, 0, 4, 0, 0, 0, 0 },
  21. { 0, 0, 0, 0, 5, 0, 0, 0, 0 }
  22. };
  23.  
  24. //this keeps the track of moves the snake has taken till present state
  25. moves=0;
  26. direction=0;
  27. //we keep the position of the snake in teh position array,we store teh coordinates of it
  28. for(int i=0;i<100;i++){
  29. position[i][0]=-1;position[i][1]=-1;
  30. }
  31. position[0][0]= 4 ;position[0][1]=4;
  32. position[1][0]= 5 ;position[1][1]=4;
  33. position[2][0]= 6 ;position[2][1]=4;
  34. position[3][0]= 7 ;position[3][1]=4;
  35. position[4][0]= 8 ;position[4][1]=4;
  36. }
  37.  
  38. public static int move(string A){
  39. moves++;
  40. //if the snake want to move in upward direction
  41. if(A.equlas("u")){
  42. if(position[0][0]==0) return -1;
  43. board[position[0][0]-1][position[0][1]]=1;
  44. int start=0;
  45. while(position[start+1][0]!=-1){
  46. board[position[start][0]][position[start][1]]=start+2;start++;
  47. }
  48. if(moves%5==0)
  49. {
  50. board[position[start][0]][position[start][1]]=start+2;start++;
  51. }
  52. else{
  53. position[start][0]=-1;position[start][1]=-1;
  54. }
  55.  
  56. //update the coordinates of snake
  57. while(start>0){
  58. position[start][0]=position[start-1][0];
  59. start--;
  60. }
  61. position[0][0]=position[0][0]-1;
  62. }
  63.  
  64.  
  65. if(A.equlas("d")){
  66. if(position[0][0]==8) return -1;
  67. board[position[0][0]+1][position[0][1]]=1;
  68. int start=0;
  69. while(position[start+1][0]!=-1){
  70. board[position[start][0]][position[start][1]]=start+2;start++;
  71. }
  72. if(moves%5==0)
  73. {
  74. board[position[start][0]][position[start][1]]=start+2;start++;
  75. }
  76. else{
  77. position[start][0]=-1;position[start][1]=-1;
  78. }
  79.  
  80. //update the coordinates of snake
  81. while(start>0){
  82. position[start][0]=position[start-1][0];
  83. start--;
  84. }
  85. position[0][0]=position[0][0]+1;
  86. }
  87.  
  88. if(A.equlas("l")){
  89. if(position[0][1]==0) return -1;
  90. board[position[0][0]][position[0][1]-1]=1;
  91. int start=0;
  92. while(position[start+1][0]!=-1){
  93. board[position[start][0]][position[start][1]]=start+2;start++;
  94. }
  95. if(moves%5==0)
  96. {
  97. board[position[start][0]][position[start][1]]=start+2;start++;
  98. }
  99. else{
  100. position[start][0]=-1;position[start][1]=-1;
  101. }
  102.  
  103. //update the coordinates of snake
  104. while(start>0){
  105. position[start][0]=position[start-1][0];
  106. start--;
  107. }
  108. position[0][1]=position[0][0]-1;
  109. }
  110.  
  111. if(A.equlas("r")){
  112. if(position[0][1]==8) return -1;
  113. board[position[0][0]][position[0][1]+1]=1;
  114. int start=0;
  115. while(position[start+1][0]!=-1){
  116. board[position[start][0]][position[start][1]]=start+2;start++;
  117. }
  118. if(moves%5==0)
  119. {
  120. board[position[start][0]][position[start][1]]=start+2;start++;
  121. }
  122. else{
  123. position[start][0]=-1;position[start][1]=-1;
  124. }
  125.  
  126. //update the coordinates of snake
  127. while(start>0){
  128. position[start][0]=position[start-1][0];
  129. start--;
  130. }
  131. position[0][1]=position[0][1]+1;
  132. }
  133. return 0;
  134. }
  135.  
  136.  
  137. void print(){
  138. for(int i=0;i<9;i++){
  139. for(int j=0;j<9;j++){
  140. System.Out.Print("Board[i][j] ");
  141. if(Board[i][j]<10)
  142. System.Out.Print(" ");
  143. }
  144. System.Out.Println("");
  145. }
  146. }
  147.  
  148. public static void main(){
  149. SnakeBoard temp=new SnakeBoard();
  150. while(1){
  151. Scanner in = new Scanner(System.in);
  152. System.println("enter choice:");
  153. s = in.nextLine();
  154. if(s.equals("u")||s.equals("d")||s.equals("r")||s.equals("l")||s.equals("q")){
  155. int t=temp.move(s);
  156. if(s.equals("q")){
  157. System.println("Quit , moves:"+temp.moves);return;
  158. }
  159. if(t==-1) {
  160. System.println("Game over , moves:"+temp.moves);return;
  161. }
  162. temp.print();
  163. }
  164. else {
  165. System.println("invalid argument");
  166. }
  167. }
  168.  
  169. return;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement