Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Poker - Game.java

Game.java

Caricato da: Bonny
Scarica il programma completo

  1. package jpoker;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6.  
  7. public class Game extends JFrame implements ActionListener {
  8.  
  9.     public Game() {
  10.  
  11.         super("JPoker");
  12.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13.      
  14. // pannello centrale
  15.         JPanel pcard = new JPanel();//carte
  16.         pcard.setBackground(new Color(0, 128, 0));
  17.         pcard.setLayout(new GridLayout(1, 5, 5, 0));
  18.         for (int i = 0; i < vl.length; i++) {
  19.             pcard.add(vl[i] = new JLabel());
  20.             vl[i].setIcon(new ImageIcon("cards\\sfondo.jpg"));
  21.         }
  22.         JPanel pbut = new JPanel();//bottoni di selezione
  23.         pbut.setBackground(new Color(0, 128, 0));
  24.         pbut.setLayout(new GridLayout(1, 5, 5, 0));
  25.         for (int i = 0; i < but.length; i++) {
  26.             pbut.add(but[i] = new JButton("Hold " + (i + 1)));
  27.             but[i].setPreferredSize(new Dimension(175, 40));
  28.             but[i].addActionListener(this);
  29.             but[i].setForeground(Color.BLACK);
  30.         }
  31.         centro = new JPanel();
  32.         centro.setBackground(new Color(0, 128, 0));
  33.         centro.add(pcard);
  34.         centro.add(pbut);
  35.         this.getContentPane().add(centro, "Center");
  36. // pannello sud
  37.         sud = new JPanel();
  38.         sud.setBackground(new Color(0, 128, 0));
  39.         sud.add(start = new JButton("Start"));
  40.         start.setPreferredSize(new Dimension(150, 40));
  41.         start.addActionListener(this);
  42.         this.getContentPane().add(sud, "South");
  43. // pannello nord
  44.         nord = new JPanel();
  45.         nord.setBackground(new Color(0, 128, 0));
  46.         JLabel titolo = new JLabel("My Poker");
  47.         titolo.setFont(new Font("Arial", Font.BOLD, 30));
  48.         titolo.setForeground(Color.red);
  49.         nord.add(titolo);
  50.         this.getContentPane().add(nord, "North");
  51. // pannello destro
  52.         dx = new JPanel();
  53.         dx.setBackground(new Color(0, 128, 0));
  54.         dx.setLayout(new GridLayout(10, 1, 5, 0));
  55.         for (int i = 0; i < vincite.length; i++) {
  56.             vincite[i] = new JLabel(titoliVincite[i]);
  57.             vincite[i].setFont(new Font("Arial", Font.BOLD, 18));
  58.             vincite[i].setForeground(Color.BLUE);
  59.             dx.add(vincite[i]);
  60.         }
  61.         dx.add(lwin);
  62.         lwin.setForeground(Color.YELLOW);
  63.         lwin.setFont(new Font("Arial", Font.BOLD, 24));
  64.         this.getContentPane().add(dx, "East");
  65. // pannello sinistro
  66.         sx = new JPanel();
  67.         sx.setBackground(new Color(0, 128, 0));
  68.         sx.setLayout(new GridLayout(4, 1, 5, 65));
  69.         nome.setFont(new Font("Arial", Font.BOLD, 22));
  70.         nome.setForeground(Color.ORANGE);
  71.         coin.setFont(new Font("Arial", Font.BOLD, 22));
  72.         coin.setForeground(Color.ORANGE);
  73.         lbet.setFont(new Font("Arial", Font.BOLD, 22));
  74.         lbet.setForeground(Color.ORANGE);
  75.         bet.setPreferredSize(new Dimension(150, 40));
  76.         bet.addActionListener(this);
  77.         sx.add(nome);
  78.         sx.add(coin);
  79.         sx.add(lbet);
  80.         sx.add(bet);
  81.         this.getContentPane().add(sx, "West");
  82. //
  83.         pack();
  84.         setResizable(false);
  85.         setVisible(true);
  86.     }
  87.  
  88.     public void actionPerformed(ActionEvent e) {
  89.  
  90.         String b = e.getActionCommand();
  91.  
  92.         if (b.equals("Bet") && stato == 1) {
  93.  
  94.             if (denaro > 0) {
  95.  
  96.                 lbet.setText(" Bet  " + (puntata += 100));
  97.                 coin.setText(" Coin  " + (denaro -= 100) + "$");
  98.                 flagBet = true;
  99.  
  100.             } else {
  101.                 denaro = puntata;
  102.                 lbet.setText(" Bet  0");
  103.                 coin.setText(" Coin  " + denaro + "$");
  104.                 puntata = 0;
  105.                 flagBet = true;
  106.             }
  107.  
  108.         } else if (b.equals("Hold 1") && stato == 2) {
  109.             setHold(0);
  110.         } else if (b.equals("Hold 2") && stato == 2) {
  111.             setHold(1);
  112.         } else if (b.equals("Hold 3") && stato == 2) {
  113.             setHold(2);
  114.         } else if (b.equals("Hold 4") && stato == 2) {
  115.             setHold(3);
  116.         } else if (b.equals("Hold 5") && stato == 2) {
  117.             setHold(4);
  118.         } else if (b.equals("Start")) {
  119.  
  120.             if (puntata > 0) {
  121.                 dai_carte();
  122.             } else {
  123.                 JOptionPane.showMessageDialog(null, "Devi puntare!!!", "msg", JOptionPane.INFORMATION_MESSAGE);
  124.             }
  125.         }
  126.     }
  127.  
  128.     public void dai_carte() {
  129.  
  130.         if (stato == 1) {
  131.  
  132.             if (!flagBet) {
  133.  
  134.                 if (denaro > 0) {
  135.  
  136.                     if (denaro >= puntata) {
  137.  
  138.                         coin.setText(" Coin  " + (denaro -= puntata) + "$");
  139.                     } else {
  140.  
  141.                         puntata = denaro;
  142.                         denaro = 0;
  143.                         lbet.setText(" Bet  " + puntata);
  144.                         coin.setText(" Coin  " + denaro + "$");
  145.                     }
  146.                 } else {
  147.                     JOptionPane.showMessageDialog(null, "GAME OVER", "msg", JOptionPane.INFORMATION_MESSAGE);
  148.                     System.exit(0);
  149.                 }
  150.             }
  151.             cards[0] = String.valueOf((int) (Math.round(Math.random() * 12) + 1)) + JCard.estraiSEME();
  152.             cards[1] = JCard.estraicarta(cards[0], String.valueOf((int) (Math.round(Math.random() * 12) + 1)) + JCard.estraiSEME());
  153.             cards[2] = JCard.estraicarta(cards[0], cards[1], String.valueOf((int) (Math.round(Math.random() * 12) + 1)) + JCard.estraiSEME());
  154.             cards[3] = JCard.estraicarta(cards[0], cards[1], cards[2], String.valueOf((int) (Math.round(Math.random() * 12) + 1)) + JCard.estraiSEME());
  155.             cards[4] = JCard.estraicarta(cards[0], cards[1], cards[2], cards[3], String.valueOf((int) (Math.round(Math.random() * 12) + 1)) + JCard.estraiSEME());
  156.  
  157.             for (int d = 0; d < 5; d++) {
  158.                 vl[d].setIcon(new ImageIcon("cards\\" + cards[d] + ".jpg"));
  159.             }
  160.             stato = 2;
  161.  
  162.         } else if (stato == 2) {
  163.  
  164.             for (int d = 0; d < 5; d++) {
  165.                 if (!H[d]) {
  166.                     switch (d) {
  167.                         case 0:
  168.                             cards[0] = JCard.estraicarta(cards[1], cards[2], cards[3], cards[4], String.valueOf((int) (Math.round(Math.random() * 12) + 1)) + JCard.estraiSEME());
  169.                             break;
  170.                         case 1:
  171.                             cards[1] = JCard.estraicarta(cards[0], cards[2], cards[3], cards[4], String.valueOf((int) (Math.round(Math.random() * 12) + 1)) + JCard.estraiSEME());
  172.                             break;
  173.                         case 2:
  174.                             cards[2] = JCard.estraicarta(cards[0], cards[1], cards[3], cards[4], String.valueOf((int) (Math.round(Math.random() * 12) + 1)) + JCard.estraiSEME());
  175.                             break;
  176.                         case 3:
  177.                             cards[3] = JCard.estraicarta(cards[0], cards[1], cards[2], cards[4], String.valueOf((int) (Math.round(Math.random() * 12) + 1)) + JCard.estraiSEME());
  178.                             break;
  179.                         case 4:
  180.                             cards[4] = JCard.estraicarta(cards[0], cards[1], cards[2], cards[3], String.valueOf((int) (Math.round(Math.random() * 12) + 1)) + JCard.estraiSEME());
  181.                             break;
  182.                     }
  183.                 }
  184.             }
  185.             for (int d = 0; d < 5; d++) {
  186.                 vl[d].setIcon(new ImageIcon("cards\\" + cards[d] + ".jpg"));
  187.                 H[d] = false;
  188.             }
  189.             stato = 3;
  190.             //////////////////////////////////////////////////////////////////////////
  191.             JWin.setcard(cards);
  192.             if (JWin.scalaR()) {
  193.                 vinto(0);
  194.             } else if (JWin.scalaColore()) {
  195.                 vinto(1);
  196.             } else if (JWin.poker()) {
  197.                 vinto(2);
  198.             } else if (JWin.full()) {
  199.                 vinto(3);
  200.             } else if (JWin.colore()) {
  201.                 vinto(4);
  202.             } else if (JWin.scala()) {
  203.                 vinto(5);
  204.             } else if (JWin.tris()) {
  205.                 vinto(6);
  206.             } else if (JWin.doppiaCoppia()) {
  207.                 vinto(7);
  208.             } else if (JWin.coppia()) {
  209.                 vinto(8);
  210.             }
  211.             /////////////////////////////////////////////////////////////////////////////////7
  212.         } else if (stato == 3) {
  213.  
  214.             for (int d = 0; d < 5; d++) {
  215.                 vl[d].setIcon(new ImageIcon("cards\\sfondo.jpg"));
  216.                 but[d].setForeground(Color.BLACK);
  217.                 cards[d] = null;
  218.             }
  219.             for (int y = 0; y < 9; y++) {
  220.                 vincite[y].setForeground(Color.BLUE);
  221.             }
  222.             coin.setText(" Coin " + denaro + "$");
  223.             lwin.setText("");
  224.             puntataTemp = puntata;
  225.             flagBet = false;
  226.  
  227.             stato = 1;
  228.         }
  229.     }//
  230.  
  231.     public void setHold(int x) {
  232.         if (but[x].getForeground() == Color.YELLOW) {
  233.             but[x].setForeground(Color.BLACK);
  234.             H[x] = false;
  235.         } else if (but[x].getForeground() == Color.BLACK) {
  236.             but[x].setForeground(Color.YELLOW);
  237.             H[x] = true;
  238.         }
  239.     }
  240.  
  241.     public void vinto(int c) {
  242.  
  243.         vincite[c].setForeground(Color.YELLOW);
  244.  
  245.         switch (c) {
  246.  
  247.             case 0:
  248.                 denaro += puntata * 100;
  249.                 lwin.setText("   Win " + puntata * 100 + "$");
  250.                 break;
  251.             case 1:
  252.                 denaro += puntata * 50;
  253.                 lwin.setText("   Win " + puntata * 50 + "$");
  254.                 break;
  255.             case 2:
  256.                 denaro += puntata * 40;
  257.                 lwin.setText("   Win " + puntata * 40 + "$");
  258.                 break;
  259.             case 3:
  260.                 denaro += puntata * 30;
  261.                 lwin.setText("   Win " + puntata * 30 + "$");
  262.                 break;
  263.             case 4:
  264.                 denaro += puntata * 20;
  265.                 lwin.setText("   Win " + puntata * 20 + "$");
  266.                 break;
  267.             case 5:
  268.                 denaro += puntata * 15;
  269.                 lwin.setText("   Win " + puntata * 15 + "$");
  270.                 break;
  271.             case 6:
  272.                 denaro += puntata * 10;
  273.                 lwin.setText("   Win " + puntata * 10 + "$");
  274.                 break;
  275.             case 7:
  276.                 denaro += puntata * 5;
  277.                 lwin.setText("   Win " + puntata * 5 + "$");
  278.                 break;
  279.             case 8:
  280.                 denaro += puntata ;
  281.                 lwin.setText("   Win " + puntata * 1 + "$");
  282.                 break;
  283.         }
  284.     }
  285.  
  286.     private static boolean flagBet = false;
  287.     private static String[] cards = new String[5];
  288.     private String titoliVincite[] = {"Scala Reale x 100", "Scala colore x 50", "Poker x 40", "Full x 30", "Colore x 20", "Scala x 15", "Tris x 10", "Doppia Coppia x 5", "Coppia x 1"};
  289.     private static boolean[] H = new boolean[5];
  290.     private static int puntata = 0,
  291.             denaro = 1000,
  292.             puntataTemp = 0,
  293.             stato = 1;
  294.     private JLabel[] vl = new JLabel[5],
  295.             vincite = new JLabel[9];
  296.     private JButton[] but = new JButton[5];
  297.     private JButton start;
  298.     private JPanel centro, sx, dx, nord, sud;
  299.     private JLabel nome = new JLabel(" Nome: luca");
  300.     private JLabel coin = new JLabel(" Coin  " + denaro + "$");
  301.     private JLabel lbet = new JLabel(" Bet  0");
  302.     private JLabel lwin = new JLabel("");
  303.     private JButton bet = new JButton("Bet");
  304. }//fine class game
  305. //int s = JOptionPane.showConfirmDialog(null, "GAME OVER \n\n Ricomincia una nuova partita?", "Messaggio", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);