Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.46 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package lekce109;
  7.  
  8. /**
  9. *
  10. * @author CoolPrase
  11. */
  12. /*
  13. * To change this license header, choose License Headers in Project Properties.
  14. * To change this template file, choose Tools | Templates
  15. * and open the template in the editor.
  16. // */
  17. //
  18. import cz.gyarab.util.event.EventListener;
  19. import cz.gyarab.util.light.BasicLightMatrix;
  20. import cz.gyarab.util.light.InteractiveLightPanel;
  21. import cz.gyarab.util.light.LightColor;
  22. import cz.gyarab.util.light.LightPanel;
  23. import cz.gyarab.util.light.LightPanelFactory;
  24. import cz.gyarab.util.light.SwingDiamondLightPanel;
  25. import cz.gyarab.util.light.LightSelectEvent;
  26. import cz.gyarab.util.light.Matrix;
  27. import java.util.Arrays;
  28.  
  29. /**
  30. *
  31. * @author kahoun
  32. */
  33. public class Sachovnice {
  34.  
  35. public enum Barva {
  36. BILY(LightColor.CHESSBOARD_WHITE, +1, true, false),
  37. CERNY(LightColor.CHESSBOARD_BLACK, -1, false, false),
  38. DAMA_B(LightColor.LIGHT_GRAY, +1, true, true),
  39. DAMA_C(LightColor.DARK_GRAY, -1, false, true);
  40. private final LightColor barva;
  41.  
  42. //true = white; false = black
  43. private final boolean white;
  44.  
  45. private final int smer;
  46. private final boolean dama;
  47.  
  48. public static Barva valueOf(LightColor lc) {
  49. for (Barva barva : values()) {
  50. if (barva.getBarva().equals(lc)) {
  51. return barva;
  52. }
  53. }
  54. return null;
  55. }
  56.  
  57. private Barva(LightColor barva, int smer, boolean white, boolean dama) {
  58. this.barva = barva;
  59. this.smer = smer;
  60. this.dama = dama;
  61. this.white = white;
  62. }
  63.  
  64. public LightColor getBarva() {
  65. return barva;
  66. }
  67.  
  68. public int getSmer() {
  69. return smer;
  70. }
  71.  
  72. public boolean isWhite() {
  73. return white;
  74. }
  75.  
  76. private boolean isDama() {
  77. return dama;
  78. }
  79.  
  80. }
  81.  
  82. public static final int POCET_RAD = 3;
  83.  
  84. public static void main(String[] args) {
  85. Sachovnice sachovnice = new Sachovnice();
  86. sachovnice.poloz(0, 2, Barva.BILY);
  87. }
  88. private final Matrix matrix = Matrix.createMatrix(8, 8);
  89. private Barva hrac = Barva.BILY;
  90. private int sloupec = -1;
  91. private int radek = -1;
  92.  
  93. public Sachovnice() {
  94.  
  95. matrix.showWindow();
  96. matrix.setBackground(LightColor.CYAN);
  97. // vytvarime sachovou desku
  98. for (int radek = 0; radek < matrix.getHeight(); radek++) {
  99. for (int sloupec = 0; sloupec < matrix.getWidth(); sloupec++) {
  100. matrix.setBackground(sloupec, radek,
  101. (sloupec + radek) % 2 == 0
  102. ? LightColor.CHESSBOARD_DARK
  103. : LightColor.CHESSBOARD_LIGHT);
  104. }
  105. }
  106.  
  107. //vytvarime 12 bilych kamenu
  108. for (int radek = 0; radek < POCET_RAD; radek++) {
  109. for (int sloupec = radek % 2; sloupec < matrix.getWidth(); sloupec += 2) {
  110. poloz(sloupec, radek, Barva.BILY);
  111. }
  112. }
  113.  
  114. //vytvarime 12 cernych kamenu
  115. for (int radek = matrix.getHeight() - 1; radek >= matrix.getHeight() - POCET_RAD; radek--) {
  116. for (int sloupec = radek % 2; sloupec < matrix.getWidth(); sloupec += 2) {
  117. poloz(sloupec, radek, Barva.CERNY);
  118. }
  119. }
  120.  
  121. matrix.getInteractiveLightPanel().setUserSelectionMode(
  122. InteractiveLightPanel.UserSelectionMode.PRESS);
  123. matrix.getInteractiveLightPanel().setSelectionVisible(true);
  124. matrix.getInteractiveLightPanel().addSelectListener(new EventListener<LightSelectEvent>() {
  125. @Override
  126. public void event(LightSelectEvent event) {
  127. if (event.getCol() < 0) {
  128. return;
  129. }
  130. if (event.getRow() < 0) {
  131. return;
  132. }
  133.  
  134. LightColor lc = matrix.getColor(event.getCol(), event.getRow());
  135. if (lc == null) { //pokud je policko, kam chci tahnout prazdne
  136. if ((sloupec < 0) || (radek < 0)) {
  137. matrix.getInteractiveLightPanel().unselect();
  138. System.out.println("mimo");
  139. return;
  140. } else {
  141. if ((event.getRow() - radek == hrac.getSmer())
  142. && (Math.abs(sloupec - event.getCol()) == 1)) { //normalni tah
  143. if (event.getRow() != 0 && event.getRow() != matrix.getHeight() - 1) {
  144. tah(sloupec, radek, event.getCol(), event.getRow(), matrix.getColor(sloupec, radek));
  145. } else {
  146.  
  147. if (hrac.isWhite()) {
  148. tah(sloupec, radek, event.getCol(), event.getRow(), Barva.DAMA_B.getBarva());
  149. } else {
  150. tah(sloupec, radek, event.getCol(), event.getRow(), Barva.DAMA_C.getBarva());
  151. }
  152. }
  153. stridaniHracu();
  154. } else if (hrac.isDama()) { //tah damy
  155. if (Math.abs(sloupec - event.getCol()) == Math.abs(radek - event.getRow())) {
  156. int b = 0;
  157. int x = -1;
  158. int y = -1;
  159. for (int a = 1; a < Math.abs(sloupec - event.getCol()) + 1; a++) {
  160. if (matrix.getColor(
  161. ((event.getCol() - sloupec) * a) / Math.abs(event.getCol() - sloupec) + sloupec,
  162. ((event.getRow() - radek) * a) / Math.abs(event.getRow() - radek) + radek) != null) {
  163. if (b == 0) {
  164. x = ((event.getCol() - sloupec) * a) / Math.abs(event.getCol() - sloupec) + sloupec;
  165. y = ((event.getRow() - radek) * a) / Math.abs(event.getRow() - radek) + radek;
  166. }
  167. b += 1;
  168. if (b > 1) {
  169. System.out.println("neplatny tah");
  170. return;
  171. }
  172. }
  173. }
  174. if (x != -1 && y != -1) {
  175. smazat(x, y);
  176. }
  177. tah(sloupec, radek, event.getCol(), event.getRow(), hrac.getBarva());
  178. //check stridani
  179. boolean stridani = true;
  180. x = event.getCol();
  181. y = event.getRow();
  182. //nahoru vpravo
  183. while (x < matrix.getWidth() - 1 && y < matrix.getHeight() - 1) {
  184.  
  185. if (matrix.getColor(x, y) != null && Barva.valueOf(matrix.getColor(x, y)).isWhite() != hrac.isWhite()) {
  186. if (matrix.getColor(x + 1, y + 1) == null) {
  187. stridani = false;
  188. }
  189. }
  190. x++;
  191. y++;
  192. }
  193. //dolu vlevo
  194. while (x > 0 && y > 0) {
  195.  
  196. if (matrix.getColor(x, y) != null && Barva.valueOf(matrix.getColor(x, y)).isWhite() != hrac.isWhite()) {
  197. if (matrix.getColor(x - 1, y - 1) == null) {
  198. stridani = false;
  199. }
  200. }
  201. x--;
  202. y--;
  203. }
  204. x = event.getCol();
  205. y = event.getRow();
  206. //dolu vpravo
  207. while (x < matrix.getWidth() - 1 && y > 0) {
  208.  
  209. if (matrix.getColor(x, y) != null && Barva.valueOf(matrix.getColor(x, y)).isWhite() != hrac.isWhite()) {
  210. if (matrix.getColor(x + 1, y - 1) == null) {
  211. stridani = false;
  212. }
  213. }
  214. x++;
  215. y--;
  216. }
  217. //nahoru vlevo
  218. while (x > 0 && y < matrix.getHeight() - 1) {
  219.  
  220. if (matrix.getColor(x, y) != null && Barva.valueOf(matrix.getColor(x, y)).isWhite() != hrac.isWhite()) {
  221. if (matrix.getColor(x - 1, y + 1) == null) {
  222. stridani = false;
  223. }
  224. }
  225. x--;
  226. y++;
  227. }
  228. if (stridani) {
  229. stridaniHracu();
  230. }
  231. }
  232. } else { //skok
  233. int skokRadek = event.getRow() - 2 * hrac.getSmer();
  234. if (skokRadek == radek) {
  235. int sloup = -1;
  236. if (event.getCol() + 2 == sloupec) {
  237. sloup = event.getCol() + 1;
  238. } else if (event.getCol() - 2 == sloupec) {
  239. sloup = event.getCol() - 1;
  240. }
  241. if (sloup != -1) {
  242. LightColor l = matrix.getColor(sloup, skokRadek + hrac.getSmer());
  243. Barva tempB = Barva.valueOf(l);
  244. if (!hrac.isWhite() == tempB.isWhite()) {
  245. if (event.getRow() != 0 && event.getRow() != matrix.getHeight() - 1) {
  246. tah(sloupec, radek, event.getCol(), event.getRow(), matrix.getColor(sloupec, radek));
  247. } else {
  248.  
  249. if (hrac.isWhite()) {
  250. tah(sloupec, radek, event.getCol(), event.getRow(), Barva.DAMA_B.getBarva());
  251. } else {
  252. tah(sloupec, radek, event.getCol(), event.getRow(), Barva.DAMA_C.getBarva());
  253. }
  254. }
  255. smazat(sloup, skokRadek + hrac.getSmer());
  256. //Kontroluje zda by se melo stridat
  257. int x = event.getCol() - 1 >= 0 ? event.getCol() - 1 : -1;
  258. int x2 = event.getCol() + 1 <= matrix.getWidth() ? event.getCol() + 1 : -1;
  259. int y = event.getRow() + hrac.getSmer() >= matrix.getHeight() || event.getRow() + hrac.getSmer() < 0 ? -1 : event.getRow() + hrac.getSmer();
  260. boolean stridani = true;
  261. if (y != -1) {
  262. //doleva
  263. if (x != -1) {
  264. LightColor lc_leva = matrix.getColor(x, y);
  265. if (lc_leva == null || Barva.valueOf(lc_leva).isWhite() == hrac.isWhite()) {
  266. stridani = true;
  267. } else {
  268. int x3 = x - 1 <= matrix.getWidth() ? x - 1 : -1;
  269. int y2 = (y + hrac.getSmer() >= matrix.getHeight() || y + hrac.getSmer() < 0) ? -1 : y + hrac.getSmer();
  270. if (x3 != -1 && y2 != -1) {
  271. LightColor lc_next = matrix.getColor(x3, y2);
  272. if (lc_next == null) {
  273. stridani = false;
  274. }
  275. }
  276.  
  277. }
  278. }
  279. //doprava
  280. if (x2 != -1) {
  281. LightColor lc_prava = matrix.getColor(x2, y);
  282. if (lc_prava == null || Barva.valueOf(lc_prava).isWhite() == hrac.isWhite()) {
  283. stridani = true;
  284. } else {
  285. int x3 = x2 + 1 <= matrix.getWidth() ? x2 + 1 : -1;
  286. int y2 = (y + hrac.getSmer() >= matrix.getHeight() || y + hrac.getSmer() < 0) ? -1 : y + hrac.getSmer();
  287. if (x3 != -1 && y2 != -1) {
  288. LightColor lc_next = matrix.getColor(x3, y2);
  289. if (lc_next == null) {
  290. stridani = false;
  291. }
  292. }
  293.  
  294. }
  295. }
  296. }
  297. if (stridani) {
  298. stridaniHracu();
  299. }
  300. return;
  301. }
  302. }
  303. }
  304. matrix.getInteractiveLightPanel().unselect();
  305. System.out.println("neplatny tah");
  306. }
  307. return;
  308. }
  309. }
  310. Barva barva = Barva.valueOf(lc);
  311. if (barva != hrac) {
  312. if (barva.isWhite() == hrac.isWhite()) {
  313. hrac = barva;
  314. } else {
  315. matrix.getInteractiveLightPanel().unselect();
  316. System.out.println("hraje " + hrac);
  317. return;
  318. }
  319. }
  320. sloupec = event.getCol();
  321. radek = event.getRow();
  322. }
  323. });
  324. }
  325.  
  326. private void stridaniHracu() {
  327. hrac = hrac.isWhite() ? Barva.CERNY : Barva.BILY;
  328. }
  329.  
  330. private void poloz(int sloupec, int radek, Barva barva) {
  331. matrix.setColor(sloupec, radek, barva.getBarva());
  332. }
  333.  
  334. public void tah(int sloupecOd, int radekOd, int sloupecKam, int radekKam, LightColor b) {
  335. matrix.setColor(sloupecKam, radekKam, b);
  336. matrix.setOff(sloupecOd, radekOd);
  337. }
  338.  
  339. public void smazat(int sloupec, int radek) {
  340. matrix.setOff(sloupec, radek);
  341. }
  342.  
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement