Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.vutbr.feec.hracisla;
- public class HraciPole {
- private int[][] pole = new int[][] { { 7, 4, 6 }, { 3, 0, 1 }, { 8, 9, 2 } };
- private int Y = 1;
- private int X = 1;
- public HraciPole() {
- }
- public HraciPole(HraciPole h1) {
- // kopirovani dat z h1.pole -> this.pole
- for (int x = 0; x < 3; x++) {
- for (int y = 0; y < 3; y++) {
- this.pole[x][y] = h1.pole[x][y];
- }
- }
- this.X = h1.X;
- this.Y = h1.Y;
- }
- public void posun(int smer) {
- switch (smer) {
- case 4:
- if (Y == 0) {
- break;
- } else {
- this.pole[X][Y] = this.pole[X][Y-1];
- Y--;
- this.pole[X][Y] = 0;
- break;
- }
- case 6:
- if (Y ==2 ){
- break;
- } else {
- this.pole[X][Y] = this.pole[X][Y+1];
- Y++;
- this.pole[X][Y] = 0;
- break;
- }
- case 8:
- if (X == 0) {
- break;
- } else {
- this.pole[X][Y] = this.pole[X-1][Y];
- X--;
- this.pole[X][Y] = 0;
- break;
- }
- case 2:
- if (X == 2) {
- break;
- } else {
- this.pole[X][Y] = this.pole[X+1][Y];
- X++;
- this.pole[X][Y] = 0;
- break;
- }
- }
- }
- public void vykresli() {
- for (int x = 0; x < 3; x++) {
- for (int y = 0; y < 3; y++) {
- System.out.print(pole[x][y] + " ");
- }
- System.out.println();
- }
- System.out.println();
- }
- }
- MAIN
- package cz.vutbr.feec.hracisla;
- import java.util.Scanner;
- public class Spustitelna {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- HraciPole h1 = new HraciPole();
- h1.vykresli();
- int run = 1;
- while(run == 1){
- h1.posun(sc.nextInt());
- h1.vykresli();
- }
- HraciPole h2 = new HraciPole(h1);
- h2.vykresli();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement