Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kursProject;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Point;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Date;
- import java.util.Random;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JComponent;
- import javax.swing.JDialog;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JLayeredPane;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JPanel;
- import javax.swing.WindowConstants;
- class polyLeft{
- boolean free = false;//true - если в данной ячейке , лежит карта. иначе false
- JButton btnE = new JButton();//своя, родная кнопка
- poly pp;//инфо о прикрепляемой карте
- }
- class polyRight{
- int q;
- int mast;//черви,ромбики,крести,пики
- JButton btnS = new JButton();
- }
- class poly{
- JButton btnM = new JButton();
- ImageIcon iconn;
- boolean coloR;
- int mast;
- int rang;
- int row;//i
- int line;
- int tikto(int kart){
- switch(kart){
- case 12:return 2;
- case 10:return 3;
- case 9:return 4;
- case 8:return 5;
- case 7:return 6;
- case 11:return 7;
- case 6:return 8;
- case 5:return 9;
- case 4:return 10;
- case 3:return 11;
- case 2:return 12;
- case 1:return 13;
- case 0:return 14;
- default: return -1;}
- }
- void whoo(int i,int j,int w){
- row = i;
- line = j;
- if(w < 13)//черви
- {
- coloR = true;
- mast = 0;
- rang = tikto(12-w);
- }
- if((12 < w) && (w < 26))//бубны
- {
- coloR = true;
- mast = 1;
- rang = tikto(25-w);
- }
- if((25< w) && (w < 39))//крести
- {
- coloR = false;
- mast = 2;
- rang = tikto(38-w);
- }
- if((38< w) && (w < 52))//пики
- {
- coloR = false;
- mast = 3;
- rang = tikto(51-w);
- }
- }
- }
- public class Soliter extends JPanel{
- static boolean costil;//при пересоздании класса, запускается конструктор Soliter -> с помощью этой переменной мы забудем об этой проблеме
- static JLayeredPane panel;//на эту панель собственно всё и добавляется от кнопок до лейблов(записей)
- static polyLeft btnHelp[] = new polyLeft[4];//переменная созданная для *вспомогательной* колоды
- static polyRight btnFinal[] = new polyRight[4];//переменная для *финальной* колоды
- static int nFinal = 0;//счётчик *сколько карт уже в *финальной* колоде*
- static JLabel UIinfo = new JLabel("карт осталось:52");//запись, в которую выводим *сколько осталось карт в игре*
- static int lastHelp;//возвращает индекс последней нажатой кнопки из *вспомогательной* колоды
- static poly btnMwho[][] = new poly[8][13];//характеристики карт(тип poly), всего 8 стопок и 13макс.в одной стопке
- static JButton zapac[] = new JButton[8];//кнопки,которые появляются после того, как в стопе станет 0 карт
- static String whoI = null;//переменная типа String, обращаясь к ней, можно узнать, по какому типу кнопок мы нажимали в последний раз
- static ImageIcon icon[] = new ImageIcon[53]; //массив картинок для карт
- static int endRow[] = {7,7,7,7,6,6,6,6};//характеристика каждого столбца(сколько сейчас в нём карт)
- static int mas[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
- 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,
- 38,39,40,41,42,43,44,45,46,47,48,49,50,51};//у нас всего 52 карты,изначально они не перемешаны
- static int lastI=0,lastJ=0;//предпоследний нажатый столбец/строчка
- static boolean click = false; //click - false означает, что первая карта не выбрана, true - выбрана
- static void addCard(int ot,int q,int from)//q - столбец в который переместим,ot - позиция по y карты принимающей
- {
- int p = endRow[lastI];
- System.out.println(endRow[lastI]-1);
- if (endRow[lastI]-1 == 0 ) {zapac[lastI].setVisible(true);System.out.println("закончилось");}
- for(int i = ot; i <p;i++ )
- {
- endRow[q]++;
- if(from == 0)
- btnMwho[q][endRow[q]-1] = btnMwho[lastI][i];
- else
- btnMwho[q][endRow[q]-1] = btnHelp[lastHelp].pp;
- btnMwho[q][endRow[q]-1].row = q;
- btnMwho[q][endRow[q]-1].line = endRow[q]-1;
- btnMwho[q][endRow[q]-1].btnM.setBounds(30+100*q,200+endRow[q]*40-40,75,100);
- panel.add(btnMwho[q][endRow[q]-1].btnM,new Integer(btnMwho[q][endRow[q]-2].line + 10));
- }
- for(int i = ot; i <p;i++ )
- {
- if(from == 0)
- endRow[lastI]--;
- btnMwho[lastI][endRow[lastI]] = null;
- }
- }
- /*static void addCard2(int ot,int q)
- {
- if (endRow[lastI]-1 == 0 ) {zapac[lastI].setVisible(true);System.out.println("закончилось");}
- int p = endRow[lastI];
- endRow[q]++;
- btnMwho[q][endRow[q]-1] = btnHelp[lastHelp].pp;
- btnMwho[q][endRow[q]-1].row = q;
- btnMwho[q][endRow[q]-1].line = endRow[q]-1;
- btnMwho[q][endRow[q]-1].btnM.setBounds(30+100*q,200+endRow[q]*40-40,75,100);
- panel.add(btnMwho[q][endRow[q]-1].btnM,new Integer(btnMwho[q][endRow[q]-2].line + 1));
- btnMwho[lastI][endRow[lastI]] = null;
- }*/
- static void createMenu(JFrame frame){
- JMenuBar menuBar = new JMenuBar();
- JMenu menu1 = new JMenu("меню");
- JMenuItem s1 = new JMenuItem("новая игра");
- JMenuItem s2 = new JMenuItem("справка");
- JMenuItem s3 = new JMenuItem("выход");
- s1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent event) {
- Soliter s1 = new Soliter();
- String arg[] = new String[2];
- costil = false;
- endRow[0]= endRow[1] = endRow[2] = endRow[3] = 7;
- endRow[4]= endRow[5] = endRow[6] = endRow[7] = 6;
- frame.dispose();
- s1.main(arg);}});
- s2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent event) {
- JDialog dialog = new AboutDialog(frame);
- dialog.setVisible(true);}});
- s3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent event) {System.exit(0);}});
- menu1.add(s1);
- menu1.add(s2);
- menu1.add(s3);
- menuBar.add(menu1);
- frame.setJMenuBar(menuBar);
- }
- static void createPole(){
- Point xx2 = new Point(20,480);//x, x2
- String nameKart[] = new String[4];
- nameKart[0] = "черви";
- nameKart[1] = "ромбики";
- nameKart[2] = "крести";
- nameKart[3] = "пики";
- for(int i= 0; i < 4; i++)
- {
- btnHelp[i]= new polyLeft();
- btnHelp[i].btnE.setLocation(xx2.x,40);
- btnHelp[i].btnE.setBounds(xx2.x,40,75,100);
- xx2.x = xx2.x+ 70 + 20;
- btnHelp[i].btnE.setBackground(Color.WHITE);
- btnHelp[i].btnE.addActionListener(new leftColod(i));
- panel.add(btnHelp[i].btnE);
- btnFinal[i] = new polyRight();
- btnFinal[i].mast = i;
- btnFinal[i].q = 2;
- btnFinal[i].btnS.setLocation(xx2.y,40);
- btnFinal[i].btnS.setBounds(xx2.y,40,75,100);
- xx2.y = xx2.y + 70 + 20;
- btnFinal[i].btnS.setBackground(Color.WHITE);
- btnFinal[i].btnS.addActionListener(new rightColod(i));
- panel.add(btnFinal[i].btnS);
- JLabel gdeChervitd = new JLabel(nameKart[i]);
- gdeChervitd.setBounds(xx2.y-75, 0, 50, 50);
- panel.add(gdeChervitd);
- }
- }
- static void blockRow(){
- for(int i = 0 ; i < 8;i++)
- {
- for(int j = endRow[i]-1 ; j > 0 ; j--)
- {
- if(((btnMwho[i][j-1].rang - btnMwho[i][j].rang) == 1) && (btnMwho[i][j].coloR != btnMwho[i][j-1].coloR))
- {
- btnMwho[i][j-1].btnM.setEnabled(true);
- btnMwho[i][j].btnM.setEnabled(true);
- }
- else break;
- }
- if(endRow[i]-1 >= 0)
- btnMwho[i][endRow[i]-1].btnM.setEnabled(true);
- }
- }
- static void createMainPole()
- {
- int k = 0;
- int st = 30;
- for(int i = 0 ; i < 8 ; i ++)
- {
- zapac[i] = new JButton();
- zapac[i].setIcon(icon[52]);
- zapac[i].setBounds(st,200,75,100);
- zapac[i].setVisible(false);
- panel.add(zapac[i]);
- st +=100;
- for(int j = 0; j < endRow[i];j++)
- {
- btnMwho[i][j] = new poly();
- btnMwho[i][j].btnM = new JButton();
- btnMwho[i][j].iconn = icon[mas[k]];
- btnMwho[i][j].whoo(i,j,mas[k]);
- btnMwho[i][j].btnM.addActionListener(new MoveAction(btnMwho[i][j]));
- k++;
- }
- }
- UIinfo.setBounds(750, 530, 100, 100);
- panel.add(UIinfo);
- drawCards();
- }
- static void drawCards(){
- int max = 0;//находим максимальное кол-во карт в одной из восьми стопок
- for(int i = 0 ; i < 8;i++)//зачем находим ? что бы правильно расчитать откуда начинать отрисовывать по y
- if(endRow[i] > max) max = endRow[i];
- Point origin = new Point(30, 0);
- for(int i = 0 ; i < 8;i++)
- {
- origin.y = 200;
- for(int j = 0 ; j < endRow[i];j++)
- {
- btnMwho[i][j].btnM.setIcon(btnMwho[i][j].iconn);//устанавливаем картинку на кнопку
- btnMwho[i][j].btnM.setBounds(origin.x,origin.y,75,100);//расположение кнопки
- if(endRow[i]-1 != j)
- btnMwho[i][j].btnM.setEnabled(false);
- origin.y+=40;
- panel.add(btnMwho[i][j].btnM,new Integer(j));
- }
- origin.x += 100;
- }
- blockRow();
- }
- static void generIcon(){
- icon[0] = new ImageIcon("src/image/2S.png");
- icon[1] = new ImageIcon("src/image/7S.png");
- icon[2]= new ImageIcon("src/image/3S.png");
- icon[3] = new ImageIcon("src/image/4S.png");
- icon[4] = new ImageIcon("src/image/5S.png");
- icon[5] = new ImageIcon("src/image/6S.png");
- icon[6] = new ImageIcon("src/image/8S.png");
- icon[7] = new ImageIcon("src/image/9S.png");
- icon[8] = new ImageIcon("src/image/10S.png");
- icon[9] = new ImageIcon("src/image/jS.png");
- icon[10] = new ImageIcon("src/image/qS.png");
- icon[11] = new ImageIcon("src/image/kS.png");
- icon[12] = new ImageIcon("src/image/tS.png");
- icon[13] = new ImageIcon("src/image/2R.png");
- icon[14] = new ImageIcon("src/image/7R.png");
- icon[15]= new ImageIcon("src/image/3R.png");
- icon[16] = new ImageIcon("src/image/4R.png");
- icon[17] = new ImageIcon("src/image/5R.png");
- icon[18] = new ImageIcon("src/image/6R.png");
- icon[19] = new ImageIcon("src/image/8R.png");
- icon[20] = new ImageIcon("src/image/9R.png");
- icon[21] = new ImageIcon("src/image/10R.png");
- icon[22] = new ImageIcon("src/image/jR.png");
- icon[23] = new ImageIcon("src/image/qR.png");
- icon[24] = new ImageIcon("src/image/kR.png");
- icon[25] = new ImageIcon("src/image/tR.png");
- icon[26] = new ImageIcon("src/image/2K.png");
- icon[27] = new ImageIcon("src/image/7K.png");
- icon[28]= new ImageIcon("src/image/3K.png");
- icon[29] = new ImageIcon("src/image/4K.png");
- icon[30] = new ImageIcon("src/image/5K.png");
- icon[31] = new ImageIcon("src/image/6K.png");
- icon[32] = new ImageIcon("src/image/8K.png");
- icon[33] = new ImageIcon("src/image/9K.png");
- icon[34] = new ImageIcon("src/image/10K.png");
- icon[35] = new ImageIcon("src/image/jK.png");
- icon[36] = new ImageIcon("src/image/qK.png");
- icon[37] = new ImageIcon("src/image/kK.png");
- icon[38] = new ImageIcon("src/image/tK.png");
- icon[39] = new ImageIcon("src/image/2L.png");
- icon[40] = new ImageIcon("src/image/7L.png");
- icon[41]= new ImageIcon("src/image/3L.png");
- icon[42] = new ImageIcon("src/image/4L.png");
- icon[43] = new ImageIcon("src/image/5L.png");
- icon[44] = new ImageIcon("src/image/6L.png");
- icon[45] = new ImageIcon("src/image/8L.png");
- icon[46] = new ImageIcon("src/image/9L.png");
- icon[47] = new ImageIcon("src/image/10L.png");
- icon[48] = new ImageIcon("src/image/jL.png");
- icon[49] = new ImageIcon("src/image/qL.png");
- icon[50] = new ImageIcon("src/image/kL.png");
- icon[51] = new ImageIcon("src/image/tL.png");
- icon[52] = new ImageIcon("src/image/pusto.jpg");
- }
- public static void reshuffle(int[] pack) {
- if(pack != null) {
- int length = pack.length;
- Random generator = new Random(new Date().getTime());
- for(int i = 0; i < length; i++) {
- int newPos = generator.nextInt(length);
- int curCard = pack[i];
- pack[i] = pack[newPos];
- pack[newPos] = curCard;
- if(i%(length/4) == 0) {
- int pause = generator.nextInt(20);
- try {
- Thread.currentThread().sleep(pause);
- }
- catch (InterruptedException ex) {}
- generator.setSeed(new Date().getTime());
- }
- }
- }
- }
- Soliter(){
- if(!costil)
- {
- setLayout(new BorderLayout());
- panel = new JLayeredPane();
- panel.setPreferredSize(new Dimension(900, 600));
- add(panel,BorderLayout.CENTER);
- costil = true;
- }
- }
- public static void main(String[] args) {
- JFrame frame = new JFrame("LayeredPaneDemo");
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- generIcon();//создаём картинки
- reshuffle(mas);//перемешиваем карты при создании
- JComponent newContentPane = new Soliter();
- createPole();
- createMainPole();
- frame.setContentPane(newContentPane);
- createMenu(frame);
- frame.pack();
- frame.setVisible(true);
- }
- }
- class AboutDialog extends JDialog
- {
- public AboutDialog(JFrame owner)
- {
- super(owner, "Справка", true);
- add(new JLabel(" курсовой проект сделалалала"),BorderLayout.CENTER);
- JButton ok = new JButton("ok");
- ok.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent event) {setVisible(false);}});
- JPanel panel = new JPanel();
- panel.add(ok);
- add(panel, BorderLayout.SOUTH);
- setSize(260, 160);
- }
- }
- class rightColod implements ActionListener{
- int u2;
- public rightColod(int u)
- {
- u2 = u;
- }
- public void actionPerformed(ActionEvent e) {
- Soliter s1 = new Soliter();
- if(s1.click)
- {
- 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))
- {
- s1.nFinal++;
- s1.UIinfo.setText("карт осталось:" + (52-s1.nFinal));
- s1.btnFinal[u2].q++;
- s1.btnFinal[u2].btnS.setIcon(s1.btnMwho[s1.lastI][s1.lastJ].iconn);
- s1.btnMwho[s1.lastI][s1.lastJ].btnM.setVisible(false);
- s1.btnMwho[s1.lastI][s1.lastJ] = null;
- s1.endRow[s1.lastI]--;
- s1.click = false;
- s1.blockRow();
- if(s1.nFinal == 52)
- {
- JLabel endGame = new JLabel("ВЫ ВЫИГРАЛИ!");
- endGame.setBounds(500, 400, 100, 100);
- s1.panel.add(endGame);
- }
- }
- }
- }
- }
- class leftColod implements ActionListener{
- int u2;
- public leftColod(int u)
- {
- u2 = u;
- }
- public void actionPerformed(ActionEvent e)
- {
- Soliter s1 = new Soliter();
- poly p2 = new poly();
- if(s1.click)//если доп.ячейка - 2 клик(приносим)
- {
- if(!s1.whoI.equals("ядоп"))
- {
- if(s1.btnMwho[s1.lastI][s1.lastJ].line == (s1.endRow[s1.lastI]-1))
- {
- if(!s1.btnHelp[u2].free)
- {
- s1.btnHelp[u2].btnE.setIcon(s1.btnMwho[s1.lastI][s1.lastJ].iconn);
- s1.btnMwho[s1.lastI][s1.lastJ].btnM.setVisible(false);
- s1.endRow[s1.lastI]--;
- s1.btnHelp[u2].pp = s1.btnMwho[s1.lastI][s1.lastJ];
- s1.btnHelp[u2].pp.btnM.setBackground(Color.WHITE);
- s1.btnMwho[s1.lastI][s1.endRow[s1.lastI]] = null;
- s1.btnHelp[u2].free = true;
- s1.blockRow();
- }
- }
- }
- s1.click = false;
- s1.btnHelp[u2].btnE.setBackground(Color.WHITE);
- }else{//если доп ячейка - 1 клик(выносим)
- if(s1.btnHelp[u2].pp != null)
- {
- s1.lastI = s1.btnHelp[u2].pp.row;
- s1.lastJ = s1.btnHelp[u2].pp.line;
- s1.btnHelp[u2].free = false;
- s1.btnHelp[u2].btnE.setBackground(Color.GREEN);
- s1.lastHelp = u2;
- s1.whoI = "ядоп";
- s1.click = true;
- }
- }
- }
- }
- class MoveAction implements ActionListener{//класс для нижних колод
- poly pol2 = new poly();
- public MoveAction(poly btnM){
- pol2 = btnM;
- }
- public void actionPerformed(ActionEvent e) {
- Soliter s1 = new Soliter();
- if(!s1.click)
- {
- s1.whoI = "яосн";
- s1.lastI= pol2.row;
- s1.lastJ= pol2.line;
- s1.btnMwho[pol2.row][pol2.line].btnM.setBackground(Color.GREEN);
- s1.click = true;
- }else{
- if((pol2.line == (s1.endRow[pol2.row]-1)) || (s1.btnMwho[s1.lastI][s1.lastJ].rang == pol2.rang))
- {
- if(s1.whoI.equals("яосн"))//из основных стопок //!изменить!
- {
- 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))
- {
- s1.addCard(s1.lastJ,pol2.row,0);//куда и откуда
- s1.btnMwho[pol2.row][s1.endRow[pol2.row]-1].btnM.setBackground(Color.WHITE);
- s1.blockRow();
- }else
- s1.btnMwho[s1.lastI][s1.lastJ].btnM.setBackground(Color.WHITE);
- }else{
- if((s1.btnHelp[s1.lastHelp].pp.coloR != pol2.coloR) && (pol2.rang - s1.btnHelp[s1.lastHelp].pp.rang == 1))
- {
- s1.addCard(s1.lastJ,pol2.row,1);//куда и откуда
- s1.btnHelp[s1.lastHelp].pp.btnM.setVisible(true);
- s1.btnHelp[s1.lastHelp].btnE.setIcon(null);
- s1.blockRow();
- }
- s1.btnHelp[s1.lastHelp].btnE.setBackground(Color.WHITE);
- s1.btnHelp[s1.lastHelp].pp = null;
- }
- s1.click = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement