Advertisement
cankocanko

SoftUni java Advanced Exam 02.Snake

Jul 24th, 2020
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.58 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) {
  8.  
  9. Scanner sc = new Scanner(System.in);
  10.  
  11. int size = Integer.parseInt(sc.nextLine());
  12.  
  13. String[][] matrix = new String[size][size];
  14.  
  15. for (int row = 0; row < size; row++) {
  16. String[] arr = sc.nextLine().split("");
  17. matrix[row] = arr;
  18. }
  19. int food = 0;
  20.  
  21. String command = sc.nextLine();
  22. int snakeRow = 0;
  23. int snakeCol = 0;
  24. while (food < 10) {
  25.  
  26. for (int rows = 0; rows < matrix.length; rows++) {
  27. for (int cols = 0; cols < matrix[rows].length; cols++) {
  28. if (matrix[rows][cols].equals("S")) {
  29. snakeRow = rows;
  30. snakeCol = cols;
  31. }
  32. }
  33. }
  34. switch (command) {
  35. case "up":
  36. if (snakeRow > 0) {
  37. //check if food
  38. if (matrix[snakeRow - 1][snakeCol].equals("*")) {
  39. matrix[snakeRow][snakeCol] = ".";
  40. matrix[snakeRow - 1][snakeCol] = "S";
  41. snakeRow -= 1;
  42. food += 1;
  43. break;
  44. }
  45. if (matrix[snakeRow - 1][snakeCol].equals("B")) {
  46. matrix[snakeRow][snakeCol] = ".";
  47. matrix[snakeRow - 1][snakeCol] = ".";
  48. for (int rows = 0; rows < matrix.length; rows++) {
  49. for (int cols = 0; cols < matrix[rows].length; cols++) {
  50. if (matrix[rows][cols].equals("B")) {
  51. matrix[rows][cols] = "S";
  52. snakeRow = rows;
  53. snakeCol = cols;
  54. }
  55. }
  56. }
  57. } else {
  58. matrix[snakeRow][snakeCol] = ".";
  59. matrix[snakeRow - 1][snakeCol] = "S";
  60. snakeRow -= 1;
  61. }
  62. } else {
  63. System.out.println("Game over!");
  64. System.out.println(String.format("Food eaten: %d", food));
  65. matrix[snakeRow][snakeCol] = ".";
  66. for (int rows = 0; rows < matrix.length; rows++) {
  67. for (int cols = 0; cols < matrix[rows].length; cols++) {
  68. System.out.print(matrix[rows][cols]);
  69. }
  70. System.out.println();
  71. }
  72. }
  73. break;
  74. case "down":
  75. if (snakeRow < matrix.length - 1) {
  76. //check if food
  77. if (matrix[snakeRow + 1][snakeCol].equals("*")) {
  78. matrix[snakeRow][snakeCol] = ".";
  79. matrix[snakeRow + 1][snakeCol] = "S";
  80. snakeRow += 1;
  81. food += 1;
  82. break;
  83. }
  84. if (matrix[snakeRow + 1][snakeCol].equals("B")) {
  85. matrix[snakeRow][snakeCol] = ".";
  86. matrix[snakeRow + 1][snakeCol] = ".";
  87. for (int rows = 0; rows < matrix.length; rows++) {
  88. for (int cols = 0; cols < matrix[rows].length; cols++) {
  89. if (matrix[rows][cols].equals("B")) {
  90. matrix[rows][cols] = "S";
  91. snakeRow = rows;
  92. snakeCol = cols;
  93. }
  94. }
  95. }
  96. } else {
  97. matrix[snakeRow][snakeCol] = ".";
  98. matrix[snakeRow + 1][snakeCol] = "S";
  99. snakeRow += 1;
  100. }
  101. } else {
  102. System.out.println("Game over!");
  103. System.out.println(String.format("Food eaten: %d", food));
  104. matrix[snakeRow][snakeCol] = ".";
  105. for (int rows = 0; rows < matrix.length; rows++) {
  106. for (int cols = 0; cols < matrix[rows].length; cols++) {
  107. System.out.print(matrix[rows][cols]);
  108. }
  109. System.out.println();
  110. }
  111. }
  112. break;
  113. case "left":
  114. if (snakeCol > 0) {
  115. //check if food
  116. if (matrix[snakeRow][snakeCol - 1].equals("*")) {
  117. matrix[snakeRow][snakeCol] = ".";
  118. matrix[snakeRow][snakeCol - 1] = "S";
  119. snakeCol -= 1;
  120. food += 1;
  121. break;
  122. }
  123. if (matrix[snakeRow][snakeCol - 1].equals("B")) {
  124. matrix[snakeRow][snakeCol] = ".";
  125. matrix[snakeRow][snakeCol - 1] = ".";
  126. for (int rows = 0; rows < matrix.length; rows++) {
  127. for (int cols = 0; cols < matrix[rows].length; cols++) {
  128. if (matrix[rows][cols].equals("B")) {
  129. matrix[rows][cols] = "S";
  130. snakeRow = rows;
  131. snakeCol = cols;
  132. }
  133. }
  134. }
  135. } else {
  136. matrix[snakeRow][snakeCol] = ".";
  137. matrix[snakeRow][snakeCol - 1] = "S";
  138. snakeCol -= 1;
  139. }
  140. } else {
  141. System.out.println("Game over!");
  142. System.out.println(String.format("Food eaten: %d", food));
  143. matrix[snakeRow][snakeCol] = ".";
  144. for (int rows = 0; rows < matrix.length; rows++) {
  145. for (int cols = 0; cols < matrix[rows].length; cols++) {
  146. System.out.print(matrix[rows][cols]);
  147. }
  148. System.out.println();
  149. }
  150. }
  151. break;
  152. case "right":
  153. if (snakeCol < matrix.length - 1) {
  154. //check if food
  155. if (matrix[snakeRow][snakeCol + 1].equals("*")) {
  156. matrix[snakeRow][snakeCol] = ".";
  157. matrix[snakeRow][snakeCol + 1] = "S";
  158. snakeCol += 1;
  159. food += 1;
  160. break;
  161. }
  162. if (matrix[snakeRow][snakeCol + 1].equals("B")) {
  163. matrix[snakeRow][snakeCol] = ".";
  164. matrix[snakeRow][snakeCol + 1] = ".";
  165. for (int rows = 0; rows < matrix.length; rows++) {
  166. for (int cols = 0; cols < matrix[rows].length; cols++) {
  167. if (matrix[rows][cols].equals("B")) {
  168. matrix[rows][cols] = "S";
  169. snakeRow = rows;
  170. snakeCol = cols;
  171. }
  172. }
  173. }
  174. } else {
  175. matrix[snakeRow][snakeCol] = ".";
  176. matrix[snakeRow += 1][snakeCol] = "S";
  177. snakeCol += 1;
  178. }
  179. } else {
  180. System.out.println("Game over!");
  181. System.out.println(String.format("Food eaten: %d", food));
  182. matrix[snakeRow][snakeCol] = ".";
  183. for (int rows = 0; rows < matrix.length; rows++) {
  184. for (int cols = 0; cols < matrix[rows].length; cols++) {
  185. System.out.print(matrix[rows][cols]);
  186. }
  187. System.out.println();
  188. }
  189. }
  190. break;
  191. }
  192. if (food >= 10){
  193. System.out.println("You won! You fed the snake.");
  194. System.out.println(String.format("Food eaten: %d",food));
  195. for (int rows = 0; rows < matrix.length; rows++) {
  196. for (int cols = 0; cols < matrix[rows].length; cols++) {
  197. System.out.print(matrix[rows][cols]);
  198. }
  199. System.out.println();
  200. }
  201. break;
  202. }
  203. command = sc.nextLine();
  204. }
  205.  
  206. }
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement