Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.27 KB | None | 0 0
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. package luola;
  92.  
  93. import java.util.*;
  94.  
  95. public class Hirvio {
  96.  
  97. private int x;
  98. private int y;
  99.  
  100. public Hirvio(int leveys, int korkeus) {
  101. this.x = leveys;
  102. this.y = korkeus;
  103.  
  104. }
  105.  
  106. public int palautaX() {
  107. return this.x;
  108. }
  109.  
  110. public int palautaY() {
  111. return this.y;
  112. }
  113.  
  114. public void siirry(String komento, int leveys, int korkeus, List<Hirvio> hirviot) {
  115. if (komento.equals("w")) {
  116. int onkoRuudussaKaksi = 0;
  117. this.y--;
  118. for (Hirvio hirvio : hirviot) {
  119. if (hirvio.palautaX() == this.x && hirvio.palautaY() == this.y) {
  120. onkoRuudussaKaksi++;
  121. }
  122. }
  123. if (onkoRuudussaKaksi > 1) {
  124. this.y++;
  125. }
  126.  
  127. if (this.y < 0) {
  128. this.y = 0;
  129.  
  130. }
  131. }
  132. if (komento.equals("s")) {
  133. int onkoRuudussaKaksi = 0;
  134. this.y++;
  135. for (Hirvio hirvio : hirviot) {
  136. if (hirvio.palautaX() == this.x && hirvio.palautaY() == this.y) {
  137. onkoRuudussaKaksi++;
  138. }
  139. }
  140. if (onkoRuudussaKaksi > 1) {
  141. this.y--;
  142. }
  143. if (this.y > korkeus) {
  144. this.y = korkeus - 1;
  145. }
  146. }
  147.  
  148. if (komento.equals("a")) {
  149. int onkoRuudussaKaksi = 0;
  150. this.x--;
  151. for (Hirvio hirvio : hirviot) {
  152. if (hirvio.palautaX() == this.x && hirvio.palautaY() == this.y) {
  153. onkoRuudussaKaksi++;
  154. }
  155. }
  156. if (onkoRuudussaKaksi > 1) {
  157. this.x++;
  158. }
  159. if (this.x < 0) {
  160. this.x = 0;
  161.  
  162. }
  163. }
  164. if (komento.equals("d")) {
  165. int onkoRuudussaKaksi = 0;
  166. this.x++;
  167. for (Hirvio hirvio : hirviot) {
  168. if (hirvio.palautaX() == this.x && hirvio.palautaY() == this.y) {
  169. onkoRuudussaKaksi++;
  170. }
  171. }
  172. if (onkoRuudussaKaksi > 1) {
  173. this.x--;
  174. }
  175. if (this.x > leveys) {
  176. this.x = leveys;
  177. }
  178. }
  179. }
  180. }
  181. src/luola/Luola.java
  182. ?
  183. 1
  184. 2
  185. 3
  186. 4
  187. 5
  188. 6
  189. 7
  190. 8
  191. 9
  192. 10
  193. 11
  194. 12
  195. 13
  196. 14
  197. 15
  198. 16
  199. 17
  200. 18
  201. 19
  202. 20
  203. 21
  204. 22
  205. 23
  206. 24
  207. 25
  208. 26
  209. 27
  210. 28
  211. 29
  212. 30
  213. 31
  214. 32
  215. 33
  216. 34
  217. 35
  218. 36
  219. 37
  220. 38
  221. 39
  222. 40
  223. 41
  224. 42
  225. 43
  226. 44
  227. 45
  228. 46
  229. 47
  230. 48
  231. 49
  232. 50
  233. 51
  234. 52
  235. 53
  236. 54
  237. 55
  238. 56
  239. 57
  240. 58
  241. 59
  242. 60
  243. 61
  244. 62
  245. 63
  246. 64
  247. 65
  248. 66
  249. 67
  250. 68
  251. 69
  252. 70
  253. 71
  254. 72
  255. 73
  256. 74
  257. 75
  258. 76
  259. 77
  260. 78
  261. 79
  262. 80
  263. 81
  264. 82
  265. 83
  266. 84
  267. 85
  268. 86
  269. 87
  270. package luola;
  271.  
  272. import java.util.*;
  273.  
  274. public class Luola {
  275.  
  276. private int leveys;
  277. private int korkeus;
  278. private int hirvioita;
  279. private int siirtoja;
  280. private boolean hirviotLiikkuvat;
  281. private List<Hirvio> hirviot;
  282.  
  283. public Luola(int leveys, int korkeus, int hirvioita, int siirtoja, boolean hirviotLiikkuvat) {
  284. this.leveys = leveys;
  285. this.korkeus = korkeus;
  286. this.hirvioita = hirvioita;
  287. this.siirtoja = siirtoja;
  288. this.hirviotLiikkuvat = hirviotLiikkuvat;
  289. this.hirviot = new ArrayList<Hirvio>();
  290. }
  291.  
  292. public void luoHirviot(Pelaaja pelaaja) {
  293. int i = 0;
  294. boolean samakoordinaatti = false;
  295.  
  296. while (i < this.hirvioita) {
  297.  
  298. int hirvionY = new Random().nextInt(this.korkeus);
  299. int hirvionX = new Random().nextInt(this.leveys);
  300.  
  301. for (Hirvio hirvio : this.hirviot) {
  302.  
  303. if (hirvionX == hirvio.palautaX() && hirvionY == hirvio.palautaY()) {
  304. samakoordinaatti = true;
  305. break;
  306. }
  307. }
  308. if (samakoordinaatti == false) {
  309. this.hirviot.add(new Hirvio(hirvionX, hirvionY));
  310. i++;
  311. } else {
  312. continue;
  313. }
  314. }
  315. }
  316.  
  317. public void getHirviot() {
  318. for (Hirvio hirvio : this.hirviot) {
  319. System.out.println("h " + hirvio.palautaX() + " " + hirvio.palautaY());
  320. }
  321. }
  322.  
  323. public void run() {
  324. Scanner lukija = new Scanner(System.in);
  325.  
  326. Pelikentta pelikentta = new Pelikentta(this.leveys, this.korkeus, this.hirvioita, this.hirviotLiikkuvat);
  327. Pelaaja pelaaja = new Pelaaja(this.leveys, this.korkeus, this.hirviotLiikkuvat);
  328. luoHirviot(pelaaja);
  329.  
  330. while (this.siirtoja > 0) {
  331. this.hirviot.removeAll(pelaaja.palautaPoistettavat());
  332. if (this.hirviot.isEmpty()) {
  333. System.out.println("VOITIT");
  334. break;
  335. }
  336. System.out.println(this.siirtoja);
  337. System.out.println("");
  338. System.out.println("@ " + pelaaja.palautaX() + " " + pelaaja.palautaY());
  339.  
  340. if (this.hirviot.isEmpty()) {
  341. System.out.println("VOITIT");
  342. break;
  343. }
  344. getHirviot();
  345. System.out.println("");
  346. System.out.println(pelikentta.getKentta(pelaaja, hirviot));
  347. this.siirtoja--;
  348. pelikentta.teeSiirto(lukija, pelaaja, this.hirviot);
  349. if (this.siirtoja == 0) {
  350. System.out.println("HÄVISIT");
  351. break;
  352. }
  353.  
  354. }
  355. }
  356. }
  357. src/luola/Paaohjelma.java
  358. ?
  359. 1
  360. 2
  361. 3
  362. 4
  363. 5
  364. 6
  365. 7
  366. 8
  367. package luola;
  368.  
  369.  
  370. public class Paaohjelma {
  371. public static void main(String[] args) {
  372. new Luola(10,7,5,5,false).run();
  373. }
  374. }
  375. src/luola/Pelaaja.java
  376. ?
  377. 1
  378. 2
  379. 3
  380. 4
  381. 5
  382. 6
  383. 7
  384. 8
  385. 9
  386. 10
  387. 11
  388. 12
  389. 13
  390. 14
  391. 15
  392. 16
  393. 17
  394. 18
  395. 19
  396. 20
  397. 21
  398. 22
  399. 23
  400. 24
  401. 25
  402. 26
  403. 27
  404. 28
  405. 29
  406. 30
  407. 31
  408. 32
  409. 33
  410. 34
  411. 35
  412. 36
  413. 37
  414. 38
  415. 39
  416. 40
  417. 41
  418. 42
  419. 43
  420. 44
  421. 45
  422. 46
  423. 47
  424. 48
  425. 49
  426. 50
  427. 51
  428. 52
  429. 53
  430. 54
  431. 55
  432. 56
  433. 57
  434. 58
  435. 59
  436. 60
  437. 61
  438. 62
  439. 63
  440. 64
  441. 65
  442. 66
  443. 67
  444. 68
  445. 69
  446. 70
  447. 71
  448. 72
  449. 73
  450. 74
  451. 75
  452. 76
  453. 77
  454. 78
  455. 79
  456. 80
  457. 81
  458. 82
  459. 83
  460. 84
  461. 85
  462. 86
  463. 87
  464. 88
  465. 89
  466. 90
  467. 91
  468. 92
  469. 93
  470. 94
  471. 95
  472. 96
  473. 97
  474. 98
  475. 99
  476. 100
  477. 101
  478. 102
  479. 103
  480. 104
  481. 105
  482. 106
  483. 107
  484. 108
  485. 109
  486. 110
  487. 111
  488. 112
  489. 113
  490. 114
  491. 115
  492. 116
  493. 117
  494. 118
  495. 119
  496. 120
  497. 121
  498. 122
  499. 123
  500. 124
  501. 125
  502. 126
  503. 127
  504. 128
  505. 129
  506. 130
  507. 131
  508. 132
  509. 133
  510. 134
  511. 135
  512. 136
  513. 137
  514. 138
  515. 139
  516. 140
  517. 141
  518. 142
  519. 143
  520. 144
  521. 145
  522. 146
  523. 147
  524. 148
  525. 149
  526. 150
  527. 151
  528. 152
  529. 153
  530. 154
  531. 155
  532. 156
  533. 157
  534. 158
  535. 159
  536. 160
  537. 161
  538. 162
  539. 163
  540. 164
  541. 165
  542. 166
  543. 167
  544. 168
  545. 169
  546. 170
  547. 171
  548. package luola;
  549.  
  550. import java.util.ArrayList;
  551. import java.util.List;
  552. import java.util.Random;
  553.  
  554. public class Pelaaja {
  555.  
  556. private int x;
  557. private int y;
  558. private int kentanLeveys;
  559. private int kentanKorkeus;
  560. private ArrayList<Hirvio> poistettavat;
  561. private boolean hirviotLiikkuvat;
  562.  
  563. public Pelaaja(int leveys, int korkeus, boolean hirviotLiikkuvat) {
  564. this.x = 0;
  565. this.y = 0;
  566. this.kentanLeveys = leveys - 1;
  567. this.kentanKorkeus = korkeus - 1;
  568. this.poistettavat = new ArrayList<Hirvio>();
  569. this.hirviotLiikkuvat = hirviotLiikkuvat;
  570. }
  571.  
  572. public List palautaPoistettavat() {
  573. return this.poistettavat;
  574. }
  575.  
  576. public int palautaX() {
  577. return this.x;
  578. }
  579.  
  580. public int palautaY() {
  581. return this.y;
  582. }
  583.  
  584. public void siirry(String komento, List<Hirvio> hirviot) {
  585. String suoritettavaKomento = "";
  586. double komento1 = 0.0;
  587.  
  588. if (komento.equals("w")) {
  589.  
  590. this.y--;
  591. if (this.y < 0) {
  592. this.y = 0;
  593.  
  594. }
  595. if (hirviotLiikkuvat == true) {
  596. komento1 = new Random().nextDouble();
  597. if (komento1 > 0 && komento1 < 0.26) {
  598. suoritettavaKomento = "w";
  599. }
  600. if (komento1 > 0.25 && komento1 < 0.51) {
  601. suoritettavaKomento = "s";
  602. }
  603. if (komento1 > 0.50 && komento1 < 0.76) {
  604. suoritettavaKomento = "a";
  605. }
  606. if (komento1 > 0.75 && komento1 < 1.01) {
  607. suoritettavaKomento = "d";
  608. }
  609.  
  610. for (Hirvio hirvio1 : hirviot) {
  611. hirvio1.siirry(suoritettavaKomento, this.kentanLeveys, this.kentanKorkeus, hirviot);
  612. }
  613.  
  614. }
  615. }
  616. for (Hirvio hirvio : hirviot) {
  617. if (hirvio.palautaX() == this.x && hirvio.palautaY() == this.y) {
  618. this.poistettavat.add(hirvio);
  619. }
  620. }
  621. if (komento.equals("s")) {
  622.  
  623. this.y++;
  624. if (this.y > this.kentanKorkeus) {
  625. this.y = this.kentanKorkeus - 1;
  626. }
  627. if (hirviotLiikkuvat == true) {
  628. komento1 = new Random().nextDouble();
  629. if (komento1 > 0 && komento1 < 0.26) {
  630. suoritettavaKomento = "w";
  631. }
  632. if (komento1 > 0.25 && komento1 < 0.51) {
  633. suoritettavaKomento = "s";
  634. }
  635. if (komento1 > 0.50 && komento1 < 0.76) {
  636. suoritettavaKomento = "a";
  637. }
  638. if (komento1 > 0.75 && komento1 < 1.01) {
  639. suoritettavaKomento = "d";
  640. }
  641. for (Hirvio hirvio1 : hirviot) {
  642. hirvio1.siirry(suoritettavaKomento, this.kentanLeveys, this.kentanKorkeus, hirviot);
  643. }
  644.  
  645. }
  646. }
  647. for (Hirvio hirvio : hirviot) {
  648. if (hirvio.palautaX() == this.x && hirvio.palautaY() == this.y) {
  649. this.poistettavat.add(hirvio);
  650. }
  651. }
  652. if (komento.equals("a")) {
  653.  
  654. this.x--;
  655. if (this.x < 0) {
  656. this.x = 0;
  657.  
  658. }
  659. if (hirviotLiikkuvat == true) {
  660. komento1 = new Random().nextDouble();
  661. if (komento1 > 0 && komento1 < 0.26) {
  662. suoritettavaKomento = "w";
  663. }
  664. if (komento1 > 0.25 && komento1 < 0.51) {
  665. suoritettavaKomento = "s";
  666. }
  667. if (komento1 > 0.50 && komento1 < 0.76) {
  668. suoritettavaKomento = "a";
  669. }
  670. if (komento1 > 0.75 && komento1 < 1.01) {
  671. suoritettavaKomento = "d";
  672. }
  673. for (Hirvio hirvio1 : hirviot) {
  674. hirvio1.siirry(suoritettavaKomento, this.kentanLeveys, this.kentanKorkeus, hirviot);
  675. }
  676.  
  677. }
  678. }
  679. for (Hirvio hirvio : hirviot) {
  680. if (hirvio.palautaX() == this.x && hirvio.palautaY() == this.y) {
  681. this.poistettavat.add(hirvio);
  682. }
  683. }
  684. if (komento.equals("d")) {
  685.  
  686. this.x++;
  687.  
  688. if (this.x > this.kentanLeveys) {
  689. this.x = this.kentanLeveys;
  690. }
  691. if (hirviotLiikkuvat == true) {
  692. komento1 = new Random().nextDouble();
  693. if (komento1 > 0 && komento1 < 0.26) {
  694. suoritettavaKomento = "w";
  695. }
  696. if (komento1 > 0.25 && komento1 < 0.51) {
  697. suoritettavaKomento = "s";
  698. }
  699. if (komento1 > 0.50 && komento1 < 0.76) {
  700. suoritettavaKomento = "a";
  701. }
  702. if (komento1 > 0.75 && komento1 < 1.01) {
  703. suoritettavaKomento = "d";
  704. }
  705. for (Hirvio hirvio1 : hirviot) {
  706. hirvio1.siirry(suoritettavaKomento, this.kentanLeveys, this.kentanKorkeus, hirviot);
  707. }
  708.  
  709. }
  710. }
  711. for (Hirvio hirvio : hirviot) {
  712. if (hirvio.palautaX() == this.x && hirvio.palautaY() == this.y) {
  713. this.poistettavat.add(hirvio);
  714. }
  715. }
  716.  
  717. }
  718. }
  719. src/luola/Pelikentta.java
  720. ?
  721. 1
  722. 2
  723. 3
  724. 4
  725. 5
  726. 6
  727. 7
  728. 8
  729. 9
  730. 10
  731. 11
  732. 12
  733. 13
  734. 14
  735. 15
  736. 16
  737. 17
  738. 18
  739. 19
  740. 20
  741. 21
  742. 22
  743. 23
  744. 24
  745. 25
  746. 26
  747. 27
  748. 28
  749. 29
  750. 30
  751. 31
  752. 32
  753. 33
  754. 34
  755. 35
  756. 36
  757. 37
  758. 38
  759. 39
  760. 40
  761. 41
  762. 42
  763. 43
  764. 44
  765. 45
  766. 46
  767. 47
  768. 48
  769. 49
  770. 50
  771. 51
  772. 52
  773. 53
  774. 54
  775. 55
  776. 56
  777. 57
  778. 58
  779. 59
  780. 60
  781. 61
  782. 62
  783. 63
  784. 64
  785. 65
  786. 66
  787. 67
  788. 68
  789. 69
  790. 70
  791. 71
  792. 72
  793. 73
  794. 74
  795. 75
  796. 76
  797. 77
  798. 78
  799. 79
  800. 80
  801. 81
  802. 82
  803. 83
  804. 84
  805. 85
  806. 86
  807. 87
  808. 88
  809. 89
  810. 90
  811. 91
  812. 92
  813. 93
  814. 94
  815. 95
  816. 96
  817. 97
  818. 98
  819. 99
  820. package luola;
  821.  
  822. import java.util.*;
  823.  
  824. public class Pelikentta {
  825.  
  826. private int leveys;
  827. private int korkeus;
  828. private int hirvioita;
  829. private String siirto;
  830.  
  831. private boolean hirviotLiikkuvat;
  832.  
  833. public Pelikentta(int leveys, int korkeus, int hirvioita, boolean hirviotLiikkuvat) {
  834. this.leveys = leveys;
  835. this.korkeus = korkeus;
  836. this.hirvioita = hirvioita;
  837.  
  838. this.hirviotLiikkuvat = hirviotLiikkuvat;
  839. }
  840.  
  841. public String getKentta(Pelaaja pelaaja, List<Hirvio> hirviot) {
  842. String kenttaMerkkiJonona = "";
  843.  
  844. int x = 0;
  845. int y = 0;
  846.  
  847. int b = 0;
  848. while (true) {
  849.  
  850. if (kenttaMerkkiJonona.length() == this.leveys * this.korkeus + this.korkeus) {
  851.  
  852. return kenttaMerkkiJonona;
  853. }
  854. if (x == this.leveys) {
  855. y++;
  856. kenttaMerkkiJonona += "\n";
  857. x = 0;
  858.  
  859. }
  860.  
  861. if (pelaaja.palautaX() == x && pelaaja.palautaY() == y) {
  862. kenttaMerkkiJonona += "@";
  863. x++;
  864. } else {
  865. for (Hirvio hirvio : hirviot) {
  866. if (hirvio.palautaX() == x && hirvio.palautaY() == y) {
  867. kenttaMerkkiJonona += "h";
  868. if (x == this.leveys) {
  869. kenttaMerkkiJonona += "\n";
  870. }
  871. x++;
  872. }
  873. }
  874.  
  875. kenttaMerkkiJonona += ".";
  876. x++;
  877. }
  878.  
  879. if (x == this.leveys) {
  880. y++;
  881. kenttaMerkkiJonona += "\n";
  882. x = 0;
  883.  
  884. }
  885.  
  886. }
  887.  
  888. }
  889.  
  890. public void teeSiirto(Scanner lukija, Pelaaja pelaaja, List<Hirvio> hirviot) {
  891. String siirrot = "wsad";
  892. String komento = "";
  893.  
  894. this.siirto = lukija.nextLine();
  895. int i = 0;
  896. while (i < this.siirto.length()) {
  897.  
  898. if (this.siirto.charAt(i) == siirrot.charAt(0)) {
  899. komento = "w";
  900. pelaaja.siirry(komento, hirviot);
  901. }
  902. if (this.siirto.charAt(i) == siirrot.charAt(1)) {
  903. komento = "s";
  904. pelaaja.siirry(komento, hirviot);
  905. }
  906. if (this.siirto.charAt(i) == siirrot.charAt(2)) {
  907. komento = "a";
  908. pelaaja.siirry(komento, hirviot);
  909. }
  910. if (this.siirto.charAt(i) == siirrot.charAt(3)) {
  911. komento = "d";
  912. pelaaja.siirry(komento, hirviot);
  913. }
  914. i++;
  915. }
  916.  
  917. }
  918. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement