Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.96 KB | None | 0 0
  1. package kursProject;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Point;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.util.Date;
  10. import java.util.Random;
  11.  
  12. import javax.swing.ImageIcon;
  13. import javax.swing.JButton;
  14. import javax.swing.JComponent;
  15. import javax.swing.JDialog;
  16. import javax.swing.JFrame;
  17. import javax.swing.JLabel;
  18. import javax.swing.JLayeredPane;
  19. import javax.swing.JMenu;
  20. import javax.swing.JMenuBar;
  21. import javax.swing.JMenuItem;
  22. import javax.swing.JPanel;
  23. import javax.swing.WindowConstants;
  24.  
  25. class polyLeft{
  26. boolean free = false;//true - если в данной ячейке , лежит карта. иначе false
  27. JButton btnE = new JButton();//своя, родная кнопка
  28.  
  29. poly pp;//инфо о прикрепляемой карте
  30. }
  31.  
  32. class polyRight{
  33. int q;
  34. int mast;//черви,ромбики,крести,пики
  35. JButton btnS = new JButton();
  36. }
  37. class poly{
  38. JButton btnM = new JButton();
  39. ImageIcon iconn;
  40. boolean coloR;
  41. int mast;
  42. int rang;
  43. int row;//i
  44. int line;
  45. int tikto(int kart){
  46. switch(kart){
  47. case 12:return 2;
  48. case 10:return 3;
  49. case 9:return 4;
  50. case 8:return 5;
  51. case 7:return 6;
  52. case 11:return 7;
  53. case 6:return 8;
  54. case 5:return 9;
  55. case 4:return 10;
  56. case 3:return 11;
  57. case 2:return 12;
  58. case 1:return 13;
  59. case 0:return 14;
  60. default: return -1;}
  61. }
  62. void whoo(int i,int j,int w){
  63. row = i;
  64. line = j;
  65. if(w < 13)//черви
  66. {
  67. coloR = true;
  68. mast = 0;
  69. rang = tikto(12-w);
  70. }
  71. if((12 < w) && (w < 26))//бубны
  72. {
  73. coloR = true;
  74. mast = 1;
  75. rang = tikto(25-w);
  76. }
  77. if((25< w) && (w < 39))//крести
  78. {
  79. coloR = false;
  80. mast = 2;
  81. rang = tikto(38-w);
  82. }
  83. if((38< w) && (w < 52))//пики
  84. {
  85. coloR = false;
  86. mast = 3;
  87. rang = tikto(51-w);
  88. }
  89. }
  90. }
  91.  
  92. public class Soliter extends JPanel{
  93. static boolean costil;//при пересоздании класса, запускается конструктор Soliter -> с помощью этой переменной мы забудем об этой проблеме
  94. static JLayeredPane panel;//на эту панель собственно всё и добавляется от кнопок до лейблов(записей)
  95. static polyLeft btnHelp[] = new polyLeft[4];//переменная созданная для *вспомогательной* колоды
  96. static polyRight btnFinal[] = new polyRight[4];//переменная для *финальной* колоды
  97. static int nFinal = 0;//счётчик *сколько карт уже в *финальной* колоде*
  98. static JLabel UIinfo = new JLabel("карт осталось:52");//запись, в которую выводим *сколько осталось карт в игре*
  99. static int lastHelp;//возвращает индекс последней нажатой кнопки из *вспомогательной* колоды
  100. static poly btnMwho[][] = new poly[8][13];//характеристики карт(тип poly), всего 8 стопок и 13макс.в одной стопке
  101. static JButton zapac[] = new JButton[8];//кнопки,которые появляются после того, как в стопе станет 0 карт
  102. static String whoI = null;//переменная типа String, обращаясь к ней, можно узнать, по какому типу кнопок мы нажимали в последний раз
  103. static ImageIcon icon[] = new ImageIcon[53]; //массив картинок для карт
  104. static int endRow[] = {7,7,7,7,6,6,6,6};//характеристика каждого столбца(сколько сейчас в нём карт)
  105. static int mas[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
  106. 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,
  107. 38,39,40,41,42,43,44,45,46,47,48,49,50,51};//у нас всего 52 карты,изначально они не перемешаны
  108.  
  109. static int lastI=0,lastJ=0;//предпоследний нажатый столбец/строчка
  110. static boolean click = false; //click - false означает, что первая карта не выбрана, true - выбрана
  111.  
  112.  
  113. static void addCard(int ot,int q,int from)//q - столбец в который переместим,ot - позиция по y карты принимающей
  114. {
  115. int p = endRow[lastI];
  116. System.out.println(endRow[lastI]-1);
  117. if (endRow[lastI]-1 == 0 ) {zapac[lastI].setVisible(true);System.out.println("закончилось");}
  118. for(int i = ot; i <p;i++ )
  119. {
  120. endRow[q]++;
  121. if(from == 0)
  122. btnMwho[q][endRow[q]-1] = btnMwho[lastI][i];
  123. else
  124. btnMwho[q][endRow[q]-1] = btnHelp[lastHelp].pp;
  125. btnMwho[q][endRow[q]-1].row = q;
  126. btnMwho[q][endRow[q]-1].line = endRow[q]-1;
  127. btnMwho[q][endRow[q]-1].btnM.setBounds(30+100*q,200+endRow[q]*40-40,75,100);
  128. panel.add(btnMwho[q][endRow[q]-1].btnM,new Integer(btnMwho[q][endRow[q]-2].line + 10));
  129. }
  130.  
  131. for(int i = ot; i <p;i++ )
  132. {
  133. if(from == 0)
  134. endRow[lastI]--;
  135. btnMwho[lastI][endRow[lastI]] = null;
  136. }
  137.  
  138. }
  139.  
  140. /*static void addCard2(int ot,int q)
  141. {
  142. if (endRow[lastI]-1 == 0 ) {zapac[lastI].setVisible(true);System.out.println("закончилось");}
  143. int p = endRow[lastI];
  144. endRow[q]++;
  145. btnMwho[q][endRow[q]-1] = btnHelp[lastHelp].pp;
  146. btnMwho[q][endRow[q]-1].row = q;
  147. btnMwho[q][endRow[q]-1].line = endRow[q]-1;
  148. btnMwho[q][endRow[q]-1].btnM.setBounds(30+100*q,200+endRow[q]*40-40,75,100);
  149. panel.add(btnMwho[q][endRow[q]-1].btnM,new Integer(btnMwho[q][endRow[q]-2].line + 1));
  150.  
  151. btnMwho[lastI][endRow[lastI]] = null;
  152. }*/
  153.  
  154. static void createMenu(JFrame frame){
  155. JMenuBar menuBar = new JMenuBar();
  156. JMenu menu1 = new JMenu("меню");
  157. JMenuItem s1 = new JMenuItem("новая игра");
  158. JMenuItem s2 = new JMenuItem("справка");
  159. JMenuItem s3 = new JMenuItem("выход");
  160. s1.addActionListener(new ActionListener() {
  161. public void actionPerformed(ActionEvent event) {
  162. Soliter s1 = new Soliter();
  163. String arg[] = new String[2];
  164. costil = false;
  165. endRow[0]= endRow[1] = endRow[2] = endRow[3] = 7;
  166. endRow[4]= endRow[5] = endRow[6] = endRow[7] = 6;
  167. frame.dispose();
  168. s1.main(arg);}});
  169. s2.addActionListener(new ActionListener() {
  170. public void actionPerformed(ActionEvent event) {
  171. JDialog dialog = new AboutDialog(frame);
  172. dialog.setVisible(true);}});
  173. s3.addActionListener(new ActionListener() {
  174. public void actionPerformed(ActionEvent event) {System.exit(0);}});
  175. menu1.add(s1);
  176. menu1.add(s2);
  177. menu1.add(s3);
  178. menuBar.add(menu1);
  179. frame.setJMenuBar(menuBar);
  180. }
  181. static void createPole(){
  182. Point xx2 = new Point(20,480);//x, x2
  183.  
  184. String nameKart[] = new String[4];
  185. nameKart[0] = "черви";
  186. nameKart[1] = "ромбики";
  187. nameKart[2] = "крести";
  188. nameKart[3] = "пики";
  189. for(int i= 0; i < 4; i++)
  190. {
  191. btnHelp[i]= new polyLeft();
  192. btnHelp[i].btnE.setLocation(xx2.x,40);
  193. btnHelp[i].btnE.setBounds(xx2.x,40,75,100);
  194. xx2.x = xx2.x+ 70 + 20;
  195. btnHelp[i].btnE.setBackground(Color.WHITE);
  196. btnHelp[i].btnE.addActionListener(new leftColod(i));
  197. panel.add(btnHelp[i].btnE);
  198.  
  199. btnFinal[i] = new polyRight();
  200. btnFinal[i].mast = i;
  201. btnFinal[i].q = 2;
  202. btnFinal[i].btnS.setLocation(xx2.y,40);
  203. btnFinal[i].btnS.setBounds(xx2.y,40,75,100);
  204. xx2.y = xx2.y + 70 + 20;
  205. btnFinal[i].btnS.setBackground(Color.WHITE);
  206. btnFinal[i].btnS.addActionListener(new rightColod(i));
  207. panel.add(btnFinal[i].btnS);
  208.  
  209. JLabel gdeChervitd = new JLabel(nameKart[i]);
  210. gdeChervitd.setBounds(xx2.y-75, 0, 50, 50);
  211. panel.add(gdeChervitd);
  212. }
  213. }
  214. static void blockRow(){
  215.  
  216. for(int i = 0 ; i < 8;i++)
  217. {
  218. for(int j = endRow[i]-1 ; j > 0 ; j--)
  219. {
  220. if(((btnMwho[i][j-1].rang - btnMwho[i][j].rang) == 1) && (btnMwho[i][j].coloR != btnMwho[i][j-1].coloR))
  221. {
  222. btnMwho[i][j-1].btnM.setEnabled(true);
  223. btnMwho[i][j].btnM.setEnabled(true);
  224. }
  225. else break;
  226. }
  227. if(endRow[i]-1 >= 0)
  228. btnMwho[i][endRow[i]-1].btnM.setEnabled(true);
  229. }
  230.  
  231.  
  232. }
  233. static void createMainPole()
  234. {
  235. int k = 0;
  236. int st = 30;
  237. for(int i = 0 ; i < 8 ; i ++)
  238. {
  239. zapac[i] = new JButton();
  240. zapac[i].setIcon(icon[52]);
  241. zapac[i].setBounds(st,200,75,100);
  242. zapac[i].setVisible(false);
  243. panel.add(zapac[i]);
  244. st +=100;
  245. for(int j = 0; j < endRow[i];j++)
  246. {
  247. btnMwho[i][j] = new poly();
  248. btnMwho[i][j].btnM = new JButton();
  249. btnMwho[i][j].iconn = icon[mas[k]];
  250. btnMwho[i][j].whoo(i,j,mas[k]);
  251. btnMwho[i][j].btnM.addActionListener(new MoveAction(btnMwho[i][j]));
  252. k++;
  253. }
  254. }
  255.  
  256. UIinfo.setBounds(750, 530, 100, 100);
  257. panel.add(UIinfo);
  258. drawCards();
  259.  
  260. }
  261.  
  262. static void drawCards(){
  263. int max = 0;//находим максимальное кол-во карт в одной из восьми стопок
  264. for(int i = 0 ; i < 8;i++)//зачем находим ? что бы правильно расчитать откуда начинать отрисовывать по y
  265. if(endRow[i] > max) max = endRow[i];
  266.  
  267. Point origin = new Point(30, 0);
  268. for(int i = 0 ; i < 8;i++)
  269. {
  270. origin.y = 200;
  271. for(int j = 0 ; j < endRow[i];j++)
  272. {
  273. btnMwho[i][j].btnM.setIcon(btnMwho[i][j].iconn);//устанавливаем картинку на кнопку
  274. btnMwho[i][j].btnM.setBounds(origin.x,origin.y,75,100);//расположение кнопки
  275. if(endRow[i]-1 != j)
  276. btnMwho[i][j].btnM.setEnabled(false);
  277. origin.y+=40;
  278. panel.add(btnMwho[i][j].btnM,new Integer(j));
  279. }
  280. origin.x += 100;
  281. }
  282. blockRow();
  283. }
  284.  
  285. static void generIcon(){
  286. icon[0] = new ImageIcon("src/image/2S.png");
  287. icon[1] = new ImageIcon("src/image/7S.png");
  288. icon[2]= new ImageIcon("src/image/3S.png");
  289. icon[3] = new ImageIcon("src/image/4S.png");
  290. icon[4] = new ImageIcon("src/image/5S.png");
  291. icon[5] = new ImageIcon("src/image/6S.png");
  292. icon[6] = new ImageIcon("src/image/8S.png");
  293. icon[7] = new ImageIcon("src/image/9S.png");
  294. icon[8] = new ImageIcon("src/image/10S.png");
  295. icon[9] = new ImageIcon("src/image/jS.png");
  296. icon[10] = new ImageIcon("src/image/qS.png");
  297. icon[11] = new ImageIcon("src/image/kS.png");
  298. icon[12] = new ImageIcon("src/image/tS.png");
  299.  
  300. icon[13] = new ImageIcon("src/image/2R.png");
  301. icon[14] = new ImageIcon("src/image/7R.png");
  302. icon[15]= new ImageIcon("src/image/3R.png");
  303. icon[16] = new ImageIcon("src/image/4R.png");
  304. icon[17] = new ImageIcon("src/image/5R.png");
  305. icon[18] = new ImageIcon("src/image/6R.png");
  306. icon[19] = new ImageIcon("src/image/8R.png");
  307. icon[20] = new ImageIcon("src/image/9R.png");
  308. icon[21] = new ImageIcon("src/image/10R.png");
  309. icon[22] = new ImageIcon("src/image/jR.png");
  310. icon[23] = new ImageIcon("src/image/qR.png");
  311. icon[24] = new ImageIcon("src/image/kR.png");
  312. icon[25] = new ImageIcon("src/image/tR.png");
  313.  
  314. icon[26] = new ImageIcon("src/image/2K.png");
  315. icon[27] = new ImageIcon("src/image/7K.png");
  316. icon[28]= new ImageIcon("src/image/3K.png");
  317. icon[29] = new ImageIcon("src/image/4K.png");
  318. icon[30] = new ImageIcon("src/image/5K.png");
  319. icon[31] = new ImageIcon("src/image/6K.png");
  320. icon[32] = new ImageIcon("src/image/8K.png");
  321. icon[33] = new ImageIcon("src/image/9K.png");
  322. icon[34] = new ImageIcon("src/image/10K.png");
  323. icon[35] = new ImageIcon("src/image/jK.png");
  324. icon[36] = new ImageIcon("src/image/qK.png");
  325. icon[37] = new ImageIcon("src/image/kK.png");
  326. icon[38] = new ImageIcon("src/image/tK.png");
  327.  
  328. icon[39] = new ImageIcon("src/image/2L.png");
  329. icon[40] = new ImageIcon("src/image/7L.png");
  330. icon[41]= new ImageIcon("src/image/3L.png");
  331. icon[42] = new ImageIcon("src/image/4L.png");
  332. icon[43] = new ImageIcon("src/image/5L.png");
  333. icon[44] = new ImageIcon("src/image/6L.png");
  334. icon[45] = new ImageIcon("src/image/8L.png");
  335. icon[46] = new ImageIcon("src/image/9L.png");
  336. icon[47] = new ImageIcon("src/image/10L.png");
  337. icon[48] = new ImageIcon("src/image/jL.png");
  338. icon[49] = new ImageIcon("src/image/qL.png");
  339. icon[50] = new ImageIcon("src/image/kL.png");
  340. icon[51] = new ImageIcon("src/image/tL.png");
  341.  
  342. icon[52] = new ImageIcon("src/image/pusto.jpg");
  343. }
  344. public static void reshuffle(int[] pack) {
  345. if(pack != null) {
  346. int length = pack.length;
  347. Random generator = new Random(new Date().getTime());
  348. for(int i = 0; i < length; i++) {
  349. int newPos = generator.nextInt(length);
  350. int curCard = pack[i];
  351. pack[i] = pack[newPos];
  352. pack[newPos] = curCard;
  353. if(i%(length/4) == 0) {
  354. int pause = generator.nextInt(20);
  355. try {
  356. Thread.currentThread().sleep(pause);
  357. }
  358. catch (InterruptedException ex) {}
  359. generator.setSeed(new Date().getTime());
  360. }
  361. }
  362. }
  363. }
  364.  
  365. Soliter(){
  366. if(!costil)
  367. {
  368. setLayout(new BorderLayout());
  369. panel = new JLayeredPane();
  370. panel.setPreferredSize(new Dimension(900, 600));
  371. add(panel,BorderLayout.CENTER);
  372. costil = true;
  373. }
  374. }
  375.  
  376.  
  377. public static void main(String[] args) {
  378. JFrame frame = new JFrame("LayeredPaneDemo");
  379. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  380. generIcon();//создаём картинки
  381. reshuffle(mas);//перемешиваем карты при создании
  382. JComponent newContentPane = new Soliter();
  383. createPole();
  384. createMainPole();
  385. frame.setContentPane(newContentPane);
  386. createMenu(frame);
  387. frame.pack();
  388. frame.setVisible(true);
  389. }
  390. }
  391.  
  392. class AboutDialog extends JDialog
  393. {
  394. public AboutDialog(JFrame owner)
  395. {
  396. super(owner, "Справка", true);
  397. add(new JLabel(" курсовой проект сделалалала"),BorderLayout.CENTER);
  398. JButton ok = new JButton("ok");
  399. ok.addActionListener(new ActionListener() {
  400. public void actionPerformed(ActionEvent event) {setVisible(false);}});
  401. JPanel panel = new JPanel();
  402. panel.add(ok);
  403. add(panel, BorderLayout.SOUTH);
  404. setSize(260, 160);
  405. }
  406. }
  407.  
  408.  
  409. class rightColod implements ActionListener{
  410. int u2;
  411. public rightColod(int u)
  412. {
  413. u2 = u;
  414. }
  415.  
  416.  
  417. public void actionPerformed(ActionEvent e) {
  418. Soliter s1 = new Soliter();
  419. if(s1.click)
  420. {
  421. if((s1.btnMwho[s1.lastI][s1.lastJ].mast == s1.btnFinal[u2].mast) && (s1.btnMwho[s1.lastI][s1.lastJ].rang == s1.btnFinal[u2].q) && (s1.btnMwho[s1.lastI][s1.lastJ].line == s1.endRow[s1.lastI]-1))
  422. {
  423. s1.nFinal++;
  424. s1.UIinfo.setText("карт осталось:" + (52-s1.nFinal));
  425. s1.btnFinal[u2].q++;
  426. s1.btnFinal[u2].btnS.setIcon(s1.btnMwho[s1.lastI][s1.lastJ].iconn);
  427. s1.btnMwho[s1.lastI][s1.lastJ].btnM.setVisible(false);
  428. s1.btnMwho[s1.lastI][s1.lastJ] = null;
  429. s1.endRow[s1.lastI]--;
  430. s1.click = false;
  431. s1.blockRow();
  432.  
  433. if(s1.nFinal == 52)
  434. {
  435. JLabel endGame = new JLabel("ВЫ ВЫИГРАЛИ!");
  436. endGame.setBounds(500, 400, 100, 100);
  437. s1.panel.add(endGame);
  438. }
  439. }
  440. }
  441. }
  442.  
  443.  
  444. }
  445.  
  446.  
  447. class leftColod implements ActionListener{
  448. int u2;
  449. public leftColod(int u)
  450. {
  451. u2 = u;
  452. }
  453. public void actionPerformed(ActionEvent e)
  454. {
  455. Soliter s1 = new Soliter();
  456.  
  457. poly p2 = new poly();
  458. if(s1.click)//если доп.ячейка - 2 клик(приносим)
  459. {
  460. if(!s1.whoI.equals("ядоп"))
  461. {
  462. if(s1.btnMwho[s1.lastI][s1.lastJ].line == (s1.endRow[s1.lastI]-1))
  463. {
  464. if(!s1.btnHelp[u2].free)
  465. {
  466. s1.btnHelp[u2].btnE.setIcon(s1.btnMwho[s1.lastI][s1.lastJ].iconn);
  467. s1.btnMwho[s1.lastI][s1.lastJ].btnM.setVisible(false);
  468. s1.endRow[s1.lastI]--;
  469. s1.btnHelp[u2].pp = s1.btnMwho[s1.lastI][s1.lastJ];
  470. s1.btnHelp[u2].pp.btnM.setBackground(Color.WHITE);
  471. s1.btnMwho[s1.lastI][s1.endRow[s1.lastI]] = null;
  472. s1.btnHelp[u2].free = true;
  473. s1.blockRow();
  474. }
  475. }
  476. }
  477. s1.click = false;
  478. s1.btnHelp[u2].btnE.setBackground(Color.WHITE);
  479. }else{//если доп ячейка - 1 клик(выносим)
  480. if(s1.btnHelp[u2].pp != null)
  481. {
  482. s1.lastI = s1.btnHelp[u2].pp.row;
  483. s1.lastJ = s1.btnHelp[u2].pp.line;
  484. s1.btnHelp[u2].free = false;
  485. s1.btnHelp[u2].btnE.setBackground(Color.GREEN);
  486. s1.lastHelp = u2;
  487. s1.whoI = "ядоп";
  488. s1.click = true;
  489. }
  490. }
  491. }
  492.  
  493. }
  494.  
  495.  
  496. class MoveAction implements ActionListener{//класс для нижних колод
  497. poly pol2 = new poly();
  498. public MoveAction(poly btnM){
  499. pol2 = btnM;
  500. }
  501.  
  502. public void actionPerformed(ActionEvent e) {
  503. Soliter s1 = new Soliter();
  504. if(!s1.click)
  505. {
  506. s1.whoI = "яосн";
  507. s1.lastI= pol2.row;
  508. s1.lastJ= pol2.line;
  509. s1.btnMwho[pol2.row][pol2.line].btnM.setBackground(Color.GREEN);
  510. s1.click = true;
  511. }else{
  512. if((pol2.line == (s1.endRow[pol2.row]-1)) || (s1.btnMwho[s1.lastI][s1.lastJ].rang == pol2.rang))
  513. {
  514. if(s1.whoI.equals("яосн"))//из основных стопок //!изменить!
  515. {
  516. if((pol2.coloR != s1.btnMwho[s1.lastI][s1.lastJ].coloR) && (pol2.rang - s1.btnMwho[s1.lastI][s1.lastJ].rang == 1) && (s1.btnMwho[s1.lastI][s1.lastJ].row != pol2.row))
  517. {
  518. s1.addCard(s1.lastJ,pol2.row,0);//куда и откуда
  519. s1.btnMwho[pol2.row][s1.endRow[pol2.row]-1].btnM.setBackground(Color.WHITE);
  520. s1.blockRow();
  521. }else
  522. s1.btnMwho[s1.lastI][s1.lastJ].btnM.setBackground(Color.WHITE);
  523. }else{
  524. if((s1.btnHelp[s1.lastHelp].pp.coloR != pol2.coloR) && (pol2.rang - s1.btnHelp[s1.lastHelp].pp.rang == 1))
  525. {
  526. s1.addCard(s1.lastJ,pol2.row,1);//куда и откуда
  527. s1.btnHelp[s1.lastHelp].pp.btnM.setVisible(true);
  528. s1.btnHelp[s1.lastHelp].btnE.setIcon(null);
  529. s1.blockRow();
  530. }
  531. s1.btnHelp[s1.lastHelp].btnE.setBackground(Color.WHITE);
  532. s1.btnHelp[s1.lastHelp].pp = null;
  533. }
  534. s1.click = false;
  535. }
  536.  
  537. }
  538. }
  539.  
  540. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement