KRITSADA

Untitled

Aug 22nd, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. enum smer { nic, hore, dole, vpravo, vlavo };
  2. enum bloky { volno, stena, start, ciel }
  3.  
  4. class Bludisko {
  5.     private mapa: bloky[][];
  6.     private poziciaX: number;
  7.     private poziciaY: number;
  8.     private mozemIst: boolean;
  9.     private poslednySmer: smer;
  10.  
  11.     constructor(mojLevel: bloky[][]) {
  12.         this.mapa = mojLevel;
  13.     }
  14.  
  15.     private dajCoJeNaSuradnici(x: number, y: number): bloky {
  16.         if (y < 0 || y >= this.mapa.length) { return bloky.stena; }
  17.         if (x < 0 || x >= this.mapa[y].length) { return bloky.stena; }
  18.  
  19.         return this.mapa[y][x];
  20.     }
  21.  
  22.     private somNaKonci(): boolean {
  23.         return this.dajCoJeNaSuradnici(this.poziciaX, this.poziciaY) == bloky.ciel;
  24.     }
  25.  
  26.     private najdiStart() {
  27.         this.poziciaX = 0;
  28.         this.poziciaY = 0;
  29.  
  30.         for (let y = 0; y < this.mapa.length; y++) {
  31.             for (let x = 0; x < this.mapa[y].length; x++) {
  32.                 if (this.dajCoJeNaSuradnici(x, y) == bloky.start) {
  33.                     this.poziciaX = x;
  34.                     this.poziciaY = y;
  35.                     return;
  36.                 }
  37.             }
  38.         }
  39.     }
  40.  
  41.     public start() {
  42.         this.najdiStart();
  43.         this.odblokujPohyb();
  44.     }
  45.  
  46.     private mozemIstHore(): boolean {
  47.         return this.dajCoJeNaSuradnici(
  48.             this.poziciaX,
  49.             this.poziciaY - 1
  50.         ) != 1;
  51.     }
  52.  
  53.     private mozemIstDole(): boolean {
  54.         return this.dajCoJeNaSuradnici(
  55.             this.poziciaX,
  56.             this.poziciaY + 1
  57.         ) != 1;
  58.     }
  59.  
  60.     private mozemIstVPravo(): boolean {
  61.         return this.dajCoJeNaSuradnici(
  62.             this.poziciaX + 1,
  63.             this.poziciaY
  64.         ) != 1;
  65.     }
  66.  
  67.     private mozemIstVLavo(): boolean {
  68.         return this.dajCoJeNaSuradnici(
  69.             this.poziciaX - 1,
  70.             this.poziciaY
  71.         ) != 1;
  72.     }
  73.  
  74.     private kresliStenuHore() {
  75.         if (this.mozemIstHore()) {
  76.             led.unplot(1, 0)
  77.             led.unplot(2, 0)
  78.             led.unplot(3, 0)
  79.         } else {
  80.             led.plot(1, 0)
  81.             led.plot(2, 0)
  82.             led.plot(3, 0)
  83.         }
  84.     }
  85.  
  86.     private kresliStenuDole() {
  87.         if (this.mozemIstDole()) {
  88.             led.unplot(1, 4)
  89.             led.unplot(2, 4)
  90.             led.unplot(3, 4)
  91.         } else {
  92.             led.plot(1, 4)
  93.             led.plot(2, 4)
  94.             led.plot(3, 4)
  95.         }
  96.     }
  97.  
  98.     private kresliStenuVPravo() {
  99.         if (this.mozemIstVPravo()) {
  100.             led.unplot(4, 1)
  101.             led.unplot(4, 2)
  102.             led.unplot(4, 3)
  103.         } else {
  104.             led.plot(4, 1)
  105.             led.plot(4, 2)
  106.             led.plot(4, 3)
  107.         }
  108.     }
  109.  
  110.     private kresliStenuVLavo() {
  111.         if (this.mozemIstVLavo()) {
  112.             led.unplot(0, 1)
  113.             led.unplot(0, 2)
  114.             led.unplot(0, 3)
  115.         } else {
  116.             led.plot(0, 1)
  117.             led.plot(0, 2)
  118.             led.plot(0, 3)
  119.         }
  120.     }
  121.  
  122.     private vykresliSipku() {
  123.         switch (this.poslednySmer) {
  124.             case smer.hore:
  125.                 led.plot(2, 1)
  126.                 led.plot(1, 2)
  127.                 led.plot(3, 2)
  128.                 led.unplot(2, 3)
  129.                 break;
  130.             case smer.dole:
  131.                 led.plot(2, 3)
  132.                 led.plot(1, 2)
  133.                 led.plot(3, 2)
  134.                 led.unplot(2, 1)
  135.                 break;
  136.             case smer.vpravo:
  137.                 led.plot(2, 3)
  138.                 led.plot(2, 1)
  139.                 led.plot(3, 2)
  140.                 led.unplot(1, 2)
  141.                 break;
  142.             case smer.vlavo:
  143.                 led.plot(2, 3)
  144.                 led.plot(2, 1)
  145.                 led.plot(1, 2)
  146.                 led.unplot(3, 2)
  147.                 break;
  148.             default:
  149.                 led.unplot(2, 1)
  150.                 led.unplot(1, 2)
  151.                 led.unplot(3, 2)
  152.                 led.unplot(2, 3)
  153.                 break;
  154.         }
  155.     }
  156.  
  157.     public vykresliBludisko() {
  158.         if (this.somNaKonci()) {
  159.             basic.showIcon(IconNames.Diamond);
  160.             return;
  161.         }
  162.         led.plot(0, 0);
  163.         led.plot(0, 4);
  164.         led.plot(4, 0);
  165.         led.plot(4, 4);
  166.         this.kresliStenuHore();
  167.         this.kresliStenuDole();
  168.         this.kresliStenuVPravo();
  169.         this.kresliStenuVLavo();
  170.         this.vykresliSipku();
  171.     }
  172.  
  173.     public chodHore() {
  174.         if (this.somNaKonci()) { return; }
  175.         if (!this.mozemIst) { return; }
  176.         if (!this.mozemIstHore()) { return; }
  177.         this.poziciaY--;
  178.         this.mozemIst = false;
  179.         this.poslednySmer = smer.hore;
  180.     }
  181.  
  182.     public chodDole() {
  183.         if (this.somNaKonci()) { return; }
  184.         if (!this.mozemIst) { return; }
  185.         if (!this.mozemIstDole()) { return; }
  186.         this.poziciaY++;
  187.         this.mozemIst = false;
  188.         this.poslednySmer = smer.dole;
  189.     }
  190.  
  191.     public chodVPravo() {
  192.         if (this.somNaKonci()) { return; }
  193.         if (!this.mozemIst) { return; }
  194.         if (!this.mozemIstVPravo()) { return; }
  195.         this.poziciaX++;
  196.         this.mozemIst = false;
  197.         this.poslednySmer = smer.vpravo;
  198.     }
  199.  
  200.     public chodVLavo() {
  201.         if (this.somNaKonci()) { return; }
  202.         if (!this.mozemIst) { return; }
  203.         if (!this.mozemIstVLavo()) { return; }
  204.         this.poziciaX--;
  205.         this.mozemIst = false;
  206.         this.poslednySmer = smer.vlavo;
  207.     }
  208.  
  209.     public odblokujPohyb() {
  210.         this.mozemIst = true;
  211.         this.poslednySmer = smer.nic;
  212.     }
  213. }
  214.  
  215. let level: bloky[][] = [
  216.     [1, 1, 1, 1, 1, 1, 1, 1],
  217.     [1, 2, 0, 0, 0, 0, 0, 1],
  218.     [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
  219.     [1, 3, 0, 0, 0, 0, 0, 1],
  220.     [1, 1, 1, 1, 1, 1, 1, 1]
  221. ];
  222.  
  223. let bludisko = new Bludisko(level);
  224. bludisko.start();
  225.  
  226. basic.forever(function () {
  227.     bludisko.vykresliBludisko();
  228. })
  229.  
  230. input.onGesture(Gesture.LogoDown, function () {
  231.     bludisko.chodHore();
  232. })
  233.  
  234. input.onGesture(Gesture.LogoUp, function () {
  235.     bludisko.chodDole();
  236. })
  237.  
  238. input.onGesture(Gesture.TiltRight, function () {
  239.     bludisko.chodVPravo();
  240. })
  241.  
  242. input.onGesture(Gesture.TiltLeft, function () {
  243.     bludisko.chodVLavo();
  244. })
  245.  
  246. input.onGesture(Gesture.ScreenUp, function () {
  247.     bludisko.odblokujPohyb();
  248. })
  249.  
  250. input.onButtonPressed(Button.A, function () {
  251.     bludisko.start();
  252. })
Add Comment
Please, Sign In to add comment