Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. package cz.vutbr.feec.hracisla;
  2.  
  3. public class HraciPole {
  4. private int[][] pole = new int[][] { { 7, 4, 6 }, { 3, 0, 1 }, { 8, 9, 2 } };
  5. private int Y = 1;
  6. private int X = 1;
  7.  
  8. public HraciPole() {
  9. }
  10.  
  11. public HraciPole(HraciPole h1) {
  12. // kopirovani dat z h1.pole -> this.pole
  13. for (int x = 0; x < 3; x++) {
  14. for (int y = 0; y < 3; y++) {
  15. this.pole[x][y] = h1.pole[x][y];
  16. }
  17. }
  18. this.X = h1.X;
  19. this.Y = h1.Y;
  20. }
  21.  
  22. public void posun(int smer) {
  23. switch (smer) {
  24. case 4:
  25. if (Y == 0) {
  26. break;
  27. } else {
  28. this.pole[X][Y] = this.pole[X][Y-1];
  29. Y--;
  30. this.pole[X][Y] = 0;
  31. break;
  32. }
  33. case 6:
  34. if (Y ==2 ){
  35. break;
  36. } else {
  37. this.pole[X][Y] = this.pole[X][Y+1];
  38. Y++;
  39. this.pole[X][Y] = 0;
  40. break;
  41. }
  42. case 8:
  43. if (X == 0) {
  44. break;
  45. } else {
  46. this.pole[X][Y] = this.pole[X-1][Y];
  47. X--;
  48. this.pole[X][Y] = 0;
  49. break;
  50. }
  51. case 2:
  52. if (X == 2) {
  53. break;
  54. } else {
  55. this.pole[X][Y] = this.pole[X+1][Y];
  56. X++;
  57. this.pole[X][Y] = 0;
  58. break;
  59. }
  60. }
  61. }
  62.  
  63. public void vykresli() {
  64. for (int x = 0; x < 3; x++) {
  65. for (int y = 0; y < 3; y++) {
  66. System.out.print(pole[x][y] + " ");
  67. }
  68. System.out.println();
  69. }
  70. System.out.println();
  71.  
  72. }
  73.  
  74. }
  75.  
  76.  
  77. MAIN
  78.  
  79. package cz.vutbr.feec.hracisla;
  80.  
  81. import java.util.Scanner;
  82.  
  83. public class Spustitelna {
  84.  
  85. public static void main(String[] args) {
  86. Scanner sc = new Scanner(System.in);
  87. HraciPole h1 = new HraciPole();
  88. h1.vykresli();
  89. int run = 1;
  90. while(run == 1){
  91. h1.posun(sc.nextInt());
  92. h1.vykresli();
  93. }
  94.  
  95. HraciPole h2 = new HraciPole(h1);
  96. h2.vykresli();
  97.  
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement