Advertisement
painz

(Java) Jogo da Velha - Jogo.java

May 1st, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 34.87 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8.  
  9. /**
  10.  *
  11.  * @author zPain
  12.  */
  13. public class Jogo extends javax.swing.JFrame {
  14.  
  15.     /**
  16.      * Creates new form Jogo
  17.      * @param player1
  18.      * @param player2
  19.      */
  20.     public Jogo(String player1, String player2) {
  21.         initComponents();
  22.         jTable_Scoreboard.getColumnModel().getColumn(0).setHeaderValue((playerName[0] = player1));
  23.         jTable_Scoreboard.getColumnModel().getColumn(1).setHeaderValue((playerName[1] = player2));
  24.     }
  25.  
  26.     /**
  27.      * This method is called from within the constructor to initialize the form.
  28.      * WARNING: Do NOT modify this code. The content of this method is always
  29.      * regenerated by the Form Editor.
  30.      */
  31.     @SuppressWarnings("unchecked")
  32.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  33.     private void initComponents() {
  34.  
  35.         jPanel1 = new javax.swing.JPanel();
  36.         jPanel2 = new javax.swing.JPanel();
  37.         jButton1 = new javax.swing.JButton();
  38.         jButton2 = new javax.swing.JButton();
  39.         jButton3 = new javax.swing.JButton();
  40.         jButton4 = new javax.swing.JButton();
  41.         jButton5 = new javax.swing.JButton();
  42.         jButton6 = new javax.swing.JButton();
  43.         jButton7 = new javax.swing.JButton();
  44.         jButton8 = new javax.swing.JButton();
  45.         jButton9 = new javax.swing.JButton();
  46.         jPanel5 = new javax.swing.JPanel();
  47.         jLabel7 = new javax.swing.JLabel();
  48.         jLabel8 = new javax.swing.JLabel();
  49.         jLabel9 = new javax.swing.JLabel();
  50.         jLabel10 = new javax.swing.JLabel();
  51.         jLabel11 = new javax.swing.JLabel();
  52.         jLabel12 = new javax.swing.JLabel();
  53.         jPanel6 = new javax.swing.JPanel();
  54.         jScrollPane1 = new javax.swing.JScrollPane();
  55.         jTable_Scoreboard = new javax.swing.JTable();
  56.         jLabel_Info = new javax.swing.JLabel();
  57.         jSeparator2 = new javax.swing.JSeparator();
  58.         jButton_Match = new javax.swing.JButton();
  59.         jButton_Round = new javax.swing.JButton();
  60.         jButton_Change = new javax.swing.JButton();
  61.  
  62.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  63.         setTitle("Jogo da velha");
  64.         setResizable(false);
  65.  
  66.         jPanel1.setBackground(new java.awt.Color(102, 204, 255));
  67.         jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  68.  
  69.         jPanel2.setBackground(new java.awt.Color(102, 153, 255));
  70.         jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  71.  
  72.         jButton1.setBackground(new java.awt.Color(102, 102, 255));
  73.         jButton1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
  74.         jButton1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  75.         jButton1.setEnabled(false);
  76.         jButton1.setFocusable(false);
  77.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  78.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  79.                 jButton1ActionPerformed(evt);
  80.             }
  81.         });
  82.  
  83.         jButton2.setBackground(new java.awt.Color(102, 102, 255));
  84.         jButton2.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
  85.         jButton2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  86.         jButton2.setEnabled(false);
  87.         jButton2.setFocusable(false);
  88.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  89.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  90.                 jButton2ActionPerformed(evt);
  91.             }
  92.         });
  93.  
  94.         jButton3.setBackground(new java.awt.Color(102, 102, 255));
  95.         jButton3.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
  96.         jButton3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  97.         jButton3.setEnabled(false);
  98.         jButton3.setFocusable(false);
  99.         jButton3.addActionListener(new java.awt.event.ActionListener() {
  100.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  101.                 jButton3ActionPerformed(evt);
  102.             }
  103.         });
  104.  
  105.         jButton4.setBackground(new java.awt.Color(102, 102, 255));
  106.         jButton4.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
  107.         jButton4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  108.         jButton4.setEnabled(false);
  109.         jButton4.setFocusable(false);
  110.         jButton4.addActionListener(new java.awt.event.ActionListener() {
  111.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  112.                 jButton4ActionPerformed(evt);
  113.             }
  114.         });
  115.  
  116.         jButton5.setBackground(new java.awt.Color(102, 102, 255));
  117.         jButton5.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
  118.         jButton5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  119.         jButton5.setEnabled(false);
  120.         jButton5.setFocusable(false);
  121.         jButton5.addActionListener(new java.awt.event.ActionListener() {
  122.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  123.                 jButton5ActionPerformed(evt);
  124.             }
  125.         });
  126.  
  127.         jButton6.setBackground(new java.awt.Color(102, 102, 255));
  128.         jButton6.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
  129.         jButton6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  130.         jButton6.setEnabled(false);
  131.         jButton6.setFocusable(false);
  132.         jButton6.addActionListener(new java.awt.event.ActionListener() {
  133.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  134.                 jButton6ActionPerformed(evt);
  135.             }
  136.         });
  137.  
  138.         jButton7.setBackground(new java.awt.Color(102, 102, 255));
  139.         jButton7.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
  140.         jButton7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  141.         jButton7.setEnabled(false);
  142.         jButton7.setFocusable(false);
  143.         jButton7.addActionListener(new java.awt.event.ActionListener() {
  144.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  145.                 jButton7ActionPerformed(evt);
  146.             }
  147.         });
  148.  
  149.         jButton8.setBackground(new java.awt.Color(102, 102, 255));
  150.         jButton8.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
  151.         jButton8.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  152.         jButton8.setEnabled(false);
  153.         jButton8.setFocusable(false);
  154.         jButton8.addActionListener(new java.awt.event.ActionListener() {
  155.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  156.                 jButton8ActionPerformed(evt);
  157.             }
  158.         });
  159.  
  160.         jButton9.setBackground(new java.awt.Color(102, 102, 255));
  161.         jButton9.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
  162.         jButton9.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  163.         jButton9.setEnabled(false);
  164.         jButton9.setFocusable(false);
  165.         jButton9.addActionListener(new java.awt.event.ActionListener() {
  166.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  167.                 jButton9ActionPerformed(evt);
  168.             }
  169.         });
  170.  
  171.         javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
  172.         jPanel2.setLayout(jPanel2Layout);
  173.         jPanel2Layout.setHorizontalGroup(
  174.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  175.             .addGroup(jPanel2Layout.createSequentialGroup()
  176.                 .addContainerGap()
  177.                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  178.                     .addGroup(jPanel2Layout.createSequentialGroup()
  179.                         .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
  180.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  181.                         .addComponent(jButton2)
  182.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  183.                         .addComponent(jButton3))
  184.                     .addGroup(jPanel2Layout.createSequentialGroup()
  185.                         .addComponent(jButton4)
  186.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  187.                         .addComponent(jButton5)
  188.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  189.                         .addComponent(jButton6))
  190.                     .addGroup(jPanel2Layout.createSequentialGroup()
  191.                         .addComponent(jButton7)
  192.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  193.                         .addComponent(jButton8)
  194.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  195.                         .addComponent(jButton9)))
  196.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  197.         );
  198.  
  199.         jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jButton3, jButton4, jButton5, jButton6, jButton7, jButton8, jButton9});
  200.  
  201.         jPanel2Layout.setVerticalGroup(
  202.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  203.             .addGroup(jPanel2Layout.createSequentialGroup()
  204.                 .addContainerGap()
  205.                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  206.                     .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
  207.                     .addComponent(jButton2)
  208.                     .addComponent(jButton3))
  209.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  210.                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  211.                     .addComponent(jButton4)
  212.                     .addComponent(jButton5)
  213.                     .addComponent(jButton6))
  214.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  215.                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  216.                     .addComponent(jButton7)
  217.                     .addComponent(jButton8)
  218.                     .addComponent(jButton9))
  219.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  220.         );
  221.  
  222.         jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jButton2, jButton3, jButton4, jButton5, jButton6, jButton7, jButton8, jButton9});
  223.  
  224.         jPanel5.setBackground(new java.awt.Color(102, 153, 255));
  225.         jPanel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  226.  
  227.         jLabel7.setText("Sobre o jogo");
  228.  
  229.         jLabel8.setText("1. Vence aquele que preencher uma sequência com seu símbolo.");
  230.  
  231.         jLabel9.setText("2. A sequência pode ser preenchida em uma linha, coluna ou diagonal.");
  232.  
  233.         jLabel10.setText("3. O jogador vencedor será o primeiro a jogar na próxima partida.");
  234.  
  235.         jLabel11.setText("4. Em caso de empate, será sorteado quem iniciará a partida seguinte.");
  236.  
  237.         jLabel12.setText("5. O placar é zerado ao iniciar uma nova rodada.");
  238.  
  239.         javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
  240.         jPanel5.setLayout(jPanel5Layout);
  241.         jPanel5Layout.setHorizontalGroup(
  242.             jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  243.             .addGroup(jPanel5Layout.createSequentialGroup()
  244.                 .addContainerGap()
  245.                 .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  246.                     .addComponent(jLabel7)
  247.                     .addComponent(jLabel8)
  248.                     .addComponent(jLabel9)
  249.                     .addComponent(jLabel10)
  250.                     .addComponent(jLabel11)
  251.                     .addComponent(jLabel12))
  252.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  253.         );
  254.         jPanel5Layout.setVerticalGroup(
  255.             jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  256.             .addGroup(jPanel5Layout.createSequentialGroup()
  257.                 .addContainerGap()
  258.                 .addComponent(jLabel7)
  259.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  260.                 .addComponent(jLabel8)
  261.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  262.                 .addComponent(jLabel9)
  263.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  264.                 .addComponent(jLabel10)
  265.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  266.                 .addComponent(jLabel11)
  267.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  268.                 .addComponent(jLabel12)
  269.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  270.         );
  271.  
  272.         jPanel6.setBackground(new java.awt.Color(102, 153, 255));
  273.         jPanel6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  274.  
  275.         jTable_Scoreboard.setModel(new javax.swing.table.DefaultTableModel(
  276.             new Object [][] {
  277.                 { new Integer(0),  new Integer(0),  new Integer(0)}
  278.             },
  279.             new String [] {
  280.                 "", "", "Empates"
  281.             }
  282.         ) {
  283.             Class[] types = new Class [] {
  284.                 java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
  285.             };
  286.             boolean[] canEdit = new boolean [] {
  287.                 false, false, false
  288.             };
  289.  
  290.             public Class getColumnClass(int columnIndex) {
  291.                 return types [columnIndex];
  292.             }
  293.  
  294.             public boolean isCellEditable(int rowIndex, int columnIndex) {
  295.                 return canEdit [columnIndex];
  296.             }
  297.         });
  298.         jTable_Scoreboard.setRowHeight(30);
  299.         jScrollPane1.setViewportView(jTable_Scoreboard);
  300.  
  301.         jLabel_Info.setText("Iniciem uma nova rodada.");
  302.  
  303.         jButton_Match.setBackground(new java.awt.Color(102, 102, 255));
  304.         jButton_Match.setText("Nova partida");
  305.         jButton_Match.setToolTipText("Inicia uma nova partida");
  306.         jButton_Match.setEnabled(false);
  307.         jButton_Match.addActionListener(new java.awt.event.ActionListener() {
  308.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  309.                 jButton_MatchActionPerformed(evt);
  310.             }
  311.         });
  312.  
  313.         jButton_Round.setBackground(new java.awt.Color(102, 102, 255));
  314.         jButton_Round.setText("Nova rodada");
  315.         jButton_Round.setToolTipText("Inicia uma nova rodada");
  316.         jButton_Round.addActionListener(new java.awt.event.ActionListener() {
  317.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  318.                 jButton_RoundActionPerformed(evt);
  319.             }
  320.         });
  321.  
  322.         jButton_Change.setBackground(new java.awt.Color(102, 102, 255));
  323.         jButton_Change.setText("Trocar jogadores");
  324.         jButton_Change.setToolTipText("Troca os jogadores que vão se enfrentar");
  325.         jButton_Change.addActionListener(new java.awt.event.ActionListener() {
  326.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  327.                 jButton_ChangeActionPerformed(evt);
  328.             }
  329.         });
  330.  
  331.         javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
  332.         jPanel6.setLayout(jPanel6Layout);
  333.         jPanel6Layout.setHorizontalGroup(
  334.             jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  335.             .addGroup(jPanel6Layout.createSequentialGroup()
  336.                 .addContainerGap()
  337.                 .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  338.                     .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
  339.                     .addComponent(jSeparator2)
  340.                     .addGroup(jPanel6Layout.createSequentialGroup()
  341.                         .addComponent(jLabel_Info)
  342.                         .addGap(0, 0, Short.MAX_VALUE))
  343.                     .addGroup(jPanel6Layout.createSequentialGroup()
  344.                         .addComponent(jButton_Match, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
  345.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  346.                         .addComponent(jButton_Round, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
  347.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  348.                         .addComponent(jButton_Change, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  349.                 .addContainerGap())
  350.         );
  351.         jPanel6Layout.setVerticalGroup(
  352.             jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  353.             .addGroup(jPanel6Layout.createSequentialGroup()
  354.                 .addContainerGap()
  355.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
  356.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  357.                 .addComponent(jLabel_Info)
  358.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  359.                 .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
  360.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  361.                 .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  362.                     .addComponent(jButton_Change, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
  363.                     .addComponent(jButton_Round, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  364.                     .addComponent(jButton_Match, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  365.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  366.         );
  367.  
  368.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  369.         jPanel1.setLayout(jPanel1Layout);
  370.         jPanel1Layout.setHorizontalGroup(
  371.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  372.             .addGroup(jPanel1Layout.createSequentialGroup()
  373.                 .addContainerGap()
  374.                 .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  375.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  376.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  377.                     .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  378.                     .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  379.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  380.         );
  381.         jPanel1Layout.setVerticalGroup(
  382.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  383.             .addGroup(jPanel1Layout.createSequentialGroup()
  384.                 .addContainerGap()
  385.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  386.                     .addGroup(jPanel1Layout.createSequentialGroup()
  387.                         .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  388.                         .addGap(0, 0, Short.MAX_VALUE))
  389.                     .addGroup(jPanel1Layout.createSequentialGroup()
  390.                         .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  391.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  392.                         .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  393.                 .addContainerGap())
  394.         );
  395.  
  396.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  397.         getContentPane().setLayout(layout);
  398.         layout.setHorizontalGroup(
  399.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  400.             .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  401.         );
  402.         layout.setVerticalGroup(
  403.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  404.             .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  405.         );
  406.  
  407.         pack();
  408.         setLocationRelativeTo(null);
  409.     }// </editor-fold>                        
  410.  
  411.     // Placar
  412.     private int lastWinner = -1;
  413.     private int totalTies = 0;
  414.     private int[] totalWins = {0, 0};
  415.    
  416.     // Jogo
  417.     private final int B1 = 1;
  418.     private final int B2 = 2;
  419.     private final int B3 = 4;
  420.     private final int B4 = 8;
  421.     private final int B5 = 16;
  422.     private final int B6 = 32;
  423.     private final int B7 = 64;
  424.     private final int B8 = 128;
  425.     private final int B9 = 256;
  426.    
  427.     private final String[] playerSymbol = {"X", "O"};
  428.    
  429.     private int currentMove = 1;
  430.     private int playingNow;
  431.    
  432.     private String[] playerName = new String[2];
  433.     private int[] playerMoves = {0, 0};
  434.    
  435.     // Processamento do jogo
  436.     private void processMove() {
  437.         int index = playingNow - 1;
  438.        
  439.         // linha 1
  440.         if(((playerMoves[index] & B1) == B1) && ((playerMoves[index] & B2) == B2) && ((playerMoves[index] & B3) == B3))  {
  441.             playerWin(playingNow);
  442.         }
  443.        
  444.         // linha 2
  445.         else if(((playerMoves[index] & B4) == B4) && ((playerMoves[index] & B5) == B5) && ((playerMoves[index] & B6) == B6))  {
  446.             playerWin(playingNow);
  447.         }
  448.        
  449.         // linha 3
  450.         else if(((playerMoves[index] & B7) == B7) && ((playerMoves[index] & B8) == B8) && ((playerMoves[index] & B9) == B9))  {
  451.             playerWin(playingNow);
  452.         }
  453.        
  454.         // coluna 1
  455.         else if(((playerMoves[index] & B1) == B1) && ((playerMoves[index] & B4) == B4) && ((playerMoves[index] & B7) == B7))  {
  456.             playerWin(playingNow);
  457.         }
  458.        
  459.         // coluna 2
  460.         else if(((playerMoves[index] & B2) == B2) && ((playerMoves[index] & B5) == B5) && ((playerMoves[index] & B8) == B8))  {
  461.             playerWin(playingNow);
  462.         }
  463.        
  464.         // coluna 3
  465.         else if(((playerMoves[index] & B3) == B3) && ((playerMoves[index] & B6) == B6) && ((playerMoves[index] & B9) == B9))  {
  466.             playerWin(playingNow);
  467.         }
  468.        
  469.         // diagonal (esquerda > direita)
  470.         else if(((playerMoves[index] & B1) == B1) && ((playerMoves[index] & B5) == B5) && ((playerMoves[index] & B9) == B9))  {
  471.             playerWin(playingNow);
  472.         }
  473.        
  474.         // diagonal (direita > esquerda
  475.         else if(((playerMoves[index] & B3) == B3) && ((playerMoves[index] & B5) == B5) && ((playerMoves[index] & B7) == B7))  {
  476.             playerWin(playingNow);
  477.         }
  478.        
  479.         //
  480.         else {
  481.             // Empate
  482.             if(++currentMove == 10) {
  483.                 jButton1.setEnabled(false);
  484.                 jButton2.setEnabled(false);
  485.                 jButton3.setEnabled(false);
  486.                 jButton4.setEnabled(false);
  487.                 jButton5.setEnabled(false);
  488.                 jButton6.setEnabled(false);
  489.                 jButton7.setEnabled(false);
  490.                 jButton8.setEnabled(false);
  491.                 jButton9.setEnabled(false);
  492.                
  493.                 lastWinner = -1;
  494.                 jTable_Scoreboard.setValueAt(++totalTies, 0, 2);
  495.                 jLabel_Info.setText("Iniciem uma nova partida.");
  496.                 JOptionPane.showMessageDialog(null, "A partida acabou empatada.");
  497.             }
  498.            
  499.             // Jogo segue
  500.             else {
  501.                 playingNow = playingNow == 1 ? 2 : 1;
  502.                 jLabel_Info.setText("É a vez de "+playerName[playingNow - 1]+".");
  503.             }
  504.         }
  505.     }
  506.    
  507.     private void playerWin(int player) {
  508.         jButton1.setEnabled(false);
  509.         jButton2.setEnabled(false);
  510.         jButton3.setEnabled(false);
  511.         jButton4.setEnabled(false);
  512.         jButton5.setEnabled(false);
  513.         jButton6.setEnabled(false);
  514.         jButton7.setEnabled(false);
  515.         jButton8.setEnabled(false);
  516.         jButton9.setEnabled(false);
  517.        
  518.         lastWinner = player--;
  519.         jLabel_Info.setText("Iniciem uma nova partida.");
  520.         jTable_Scoreboard.setValueAt(++totalWins[player], 0, player);
  521.         JOptionPane.showMessageDialog(null, playerName[player]+" venceu a partida.");
  522.     }
  523.    
  524.     // Métodos dos componentes
  525.     private void jButton_MatchActionPerformed(java.awt.event.ActionEvent evt) {                                              
  526.         jButton1.setText(""); jButton1.setEnabled(true);
  527.         jButton2.setText(""); jButton2.setEnabled(true);
  528.         jButton3.setText(""); jButton3.setEnabled(true);
  529.         jButton4.setText(""); jButton4.setEnabled(true);
  530.         jButton5.setText(""); jButton5.setEnabled(true);
  531.         jButton6.setText(""); jButton6.setEnabled(true);
  532.         jButton7.setText(""); jButton7.setEnabled(true);
  533.         jButton8.setText(""); jButton8.setEnabled(true);
  534.         jButton9.setText(""); jButton9.setEnabled(true);
  535.        
  536.         if(lastWinner == -1) {
  537.             playingNow = new java.util.Random().nextInt(2) + 1;
  538.             jLabel_Info.setText("Foi sorteado que "+playerName[playingNow - 1]+" fará a primeira jogada.");
  539.         }
  540.        
  541.         else {
  542.             jLabel_Info.setText(playerName[playingNow - 1]+" fará a primeira jogada, já que venceu a última partida.");
  543.         }
  544.        
  545.         currentMove = 1;
  546.         playerMoves[0] = playerMoves[1] = 0;
  547.         JOptionPane.showMessageDialog(null, "Uma nova partida foi iniciada.");
  548.     }                                            
  549.  
  550.     private void jButton_RoundActionPerformed(java.awt.event.ActionEvent evt) {                                              
  551.         jButton1.setText(""); jButton1.setEnabled(false);
  552.         jButton2.setText(""); jButton2.setEnabled(false);
  553.         jButton3.setText(""); jButton3.setEnabled(false);
  554.         jButton4.setText(""); jButton4.setEnabled(false);
  555.         jButton5.setText(""); jButton5.setEnabled(false);
  556.         jButton6.setText(""); jButton6.setEnabled(false);
  557.         jButton7.setText(""); jButton7.setEnabled(false);
  558.         jButton8.setText(""); jButton8.setEnabled(false);
  559.         jButton9.setText(""); jButton9.setEnabled(false);
  560.        
  561.         jButton_Match.setEnabled(true);
  562.         jTable_Scoreboard.setValueAt((totalTies = 0), 0, 2);
  563.         jTable_Scoreboard.setValueAt((totalWins[1] = 0), 0, 1);
  564.         jTable_Scoreboard.setValueAt((totalWins[0] = 0), 0, 0);
  565.        
  566.         jLabel_Info.setText("Iniciem uma nova partida.");
  567.         JOptionPane.showMessageDialog(null, "Uma nova rodada foi iniciada.");
  568.     }                                            
  569.  
  570.     private void jButton_ChangeActionPerformed(java.awt.event.ActionEvent evt) {                                              
  571.         this.dispose();
  572.         new Intro().setVisible(true);
  573.     }                                              
  574.  
  575.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  576.         if(jButton1.isEnabled()) {
  577.             jButton1.setEnabled(false);
  578.             jButton1.setText(playerSymbol[playingNow - 1]);
  579.            
  580.             playerMoves[playingNow - 1] |= B1;
  581.             processMove();
  582.         }
  583.     }                                        
  584.  
  585.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  586.         if(jButton2.isEnabled()) {
  587.             jButton2.setEnabled(false);
  588.             jButton2.setText(playerSymbol[playingNow - 1]);
  589.            
  590.             playerMoves[playingNow - 1] |= B2;
  591.             processMove();
  592.         }
  593.     }                                        
  594.  
  595.     private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  596.         if(jButton3.isEnabled()) {
  597.             jButton3.setEnabled(false);
  598.             jButton3.setText(playerSymbol[playingNow - 1]);
  599.            
  600.             playerMoves[playingNow - 1] |= B3;
  601.             processMove();
  602.         }
  603.     }                                        
  604.  
  605.     private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  606.         if(jButton4.isEnabled()) {
  607.             jButton4.setEnabled(false);
  608.             jButton4.setText(playerSymbol[playingNow - 1]);
  609.            
  610.             playerMoves[playingNow - 1] |= B4;
  611.             processMove();
  612.         }
  613.     }                                        
  614.  
  615.     private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  616.         if(jButton5.isEnabled()) {
  617.             jButton5.setEnabled(false);
  618.             jButton5.setText(playerSymbol[playingNow - 1]);
  619.            
  620.             playerMoves[playingNow - 1] |= B5;
  621.             processMove();
  622.         }
  623.     }                                        
  624.  
  625.     private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  626.         if(jButton6.isEnabled()) {
  627.             jButton6.setEnabled(false);
  628.             jButton6.setText(playerSymbol[playingNow - 1]);
  629.            
  630.             playerMoves[playingNow - 1] |= B6;
  631.             processMove();
  632.         }
  633.     }                                        
  634.  
  635.     private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  636.         if(jButton7.isEnabled()) {
  637.             jButton7.setEnabled(false);
  638.             jButton7.setText(playerSymbol[playingNow - 1]);
  639.            
  640.             playerMoves[playingNow - 1] |= B7;
  641.             processMove();
  642.         }
  643.     }                                        
  644.  
  645.     private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  646.         if(jButton8.isEnabled()) {
  647.             jButton8.setEnabled(false);
  648.             jButton8.setText(playerSymbol[playingNow - 1]);
  649.            
  650.             playerMoves[playingNow - 1] |= B8;
  651.             processMove();
  652.         }
  653.     }                                        
  654.  
  655.     private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  656.         if(jButton9.isEnabled()) {
  657.             jButton9.setEnabled(false);
  658.             jButton9.setText(playerSymbol[playingNow - 1]);
  659.            
  660.             playerMoves[playingNow - 1] |= B9;
  661.             processMove();
  662.         }
  663.     }                                        
  664.  
  665.     /**
  666.      * @param args the command line arguments
  667.      */
  668.     public static void main(String args[]) {
  669.         /* Set the Nimbus look and feel */
  670.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  671.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  672.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  673.          */
  674.         try {
  675.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  676.                 if ("Nimbus".equals(info.getName())) {
  677.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  678.                     break;
  679.                 }
  680.             }
  681.         } catch (ClassNotFoundException ex) {
  682.             java.util.logging.Logger.getLogger(Jogo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  683.         } catch (InstantiationException ex) {
  684.             java.util.logging.Logger.getLogger(Jogo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  685.         } catch (IllegalAccessException ex) {
  686.             java.util.logging.Logger.getLogger(Jogo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  687.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  688.             java.util.logging.Logger.getLogger(Jogo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  689.         }
  690.         //</editor-fold>
  691.  
  692.         /* Create and display the form */
  693.         java.awt.EventQueue.invokeLater(new Runnable() {
  694.             public void run() {
  695.                 new Jogo("Jogador 1", "Jogador 2").setVisible(true);
  696.             }
  697.         });
  698.     }
  699.  
  700.     // Variables declaration - do not modify                    
  701.     private javax.swing.JButton jButton1;
  702.     private javax.swing.JButton jButton2;
  703.     private javax.swing.JButton jButton3;
  704.     private javax.swing.JButton jButton4;
  705.     private javax.swing.JButton jButton5;
  706.     private javax.swing.JButton jButton6;
  707.     private javax.swing.JButton jButton7;
  708.     private javax.swing.JButton jButton8;
  709.     private javax.swing.JButton jButton9;
  710.     private javax.swing.JButton jButton_Change;
  711.     private javax.swing.JButton jButton_Match;
  712.     private javax.swing.JButton jButton_Round;
  713.     private javax.swing.JLabel jLabel10;
  714.     private javax.swing.JLabel jLabel11;
  715.     private javax.swing.JLabel jLabel12;
  716.     private javax.swing.JLabel jLabel7;
  717.     private javax.swing.JLabel jLabel8;
  718.     private javax.swing.JLabel jLabel9;
  719.     private javax.swing.JLabel jLabel_Info;
  720.     private javax.swing.JPanel jPanel1;
  721.     private javax.swing.JPanel jPanel2;
  722.     private javax.swing.JPanel jPanel5;
  723.     private javax.swing.JPanel jPanel6;
  724.     private javax.swing.JScrollPane jScrollPane1;
  725.     private javax.swing.JSeparator jSeparator2;
  726.     private javax.swing.JTable jTable_Scoreboard;
  727.     // End of variables declaration                  
  728. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement