Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author zPain
- */
- public class Jogo extends javax.swing.JFrame {
- /**
- * Creates new form Jogo
- * @param player1
- * @param player2
- */
- public Jogo(String player1, String player2) {
- initComponents();
- jTable_Scoreboard.getColumnModel().getColumn(0).setHeaderValue((playerName[0] = player1));
- jTable_Scoreboard.getColumnModel().getColumn(1).setHeaderValue((playerName[1] = player2));
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jPanel1 = new javax.swing.JPanel();
- jPanel2 = new javax.swing.JPanel();
- jButton1 = new javax.swing.JButton();
- jButton2 = new javax.swing.JButton();
- jButton3 = new javax.swing.JButton();
- jButton4 = new javax.swing.JButton();
- jButton5 = new javax.swing.JButton();
- jButton6 = new javax.swing.JButton();
- jButton7 = new javax.swing.JButton();
- jButton8 = new javax.swing.JButton();
- jButton9 = new javax.swing.JButton();
- jPanel5 = new javax.swing.JPanel();
- jLabel7 = new javax.swing.JLabel();
- jLabel8 = new javax.swing.JLabel();
- jLabel9 = new javax.swing.JLabel();
- jLabel10 = new javax.swing.JLabel();
- jLabel11 = new javax.swing.JLabel();
- jLabel12 = new javax.swing.JLabel();
- jPanel6 = new javax.swing.JPanel();
- jScrollPane1 = new javax.swing.JScrollPane();
- jTable_Scoreboard = new javax.swing.JTable();
- jLabel_Info = new javax.swing.JLabel();
- jSeparator2 = new javax.swing.JSeparator();
- jButton_Match = new javax.swing.JButton();
- jButton_Round = new javax.swing.JButton();
- jButton_Change = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("Jogo da velha");
- setResizable(false);
- jPanel1.setBackground(new java.awt.Color(102, 204, 255));
- jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jPanel2.setBackground(new java.awt.Color(102, 153, 255));
- jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jButton1.setBackground(new java.awt.Color(102, 102, 255));
- jButton1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
- jButton1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jButton1.setEnabled(false);
- jButton1.setFocusable(false);
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jButton2.setBackground(new java.awt.Color(102, 102, 255));
- jButton2.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
- jButton2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jButton2.setEnabled(false);
- jButton2.setFocusable(false);
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- jButton3.setBackground(new java.awt.Color(102, 102, 255));
- jButton3.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
- jButton3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jButton3.setEnabled(false);
- jButton3.setFocusable(false);
- jButton3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton3ActionPerformed(evt);
- }
- });
- jButton4.setBackground(new java.awt.Color(102, 102, 255));
- jButton4.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
- jButton4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jButton4.setEnabled(false);
- jButton4.setFocusable(false);
- jButton4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton4ActionPerformed(evt);
- }
- });
- jButton5.setBackground(new java.awt.Color(102, 102, 255));
- jButton5.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
- jButton5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jButton5.setEnabled(false);
- jButton5.setFocusable(false);
- jButton5.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton5ActionPerformed(evt);
- }
- });
- jButton6.setBackground(new java.awt.Color(102, 102, 255));
- jButton6.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
- jButton6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jButton6.setEnabled(false);
- jButton6.setFocusable(false);
- jButton6.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton6ActionPerformed(evt);
- }
- });
- jButton7.setBackground(new java.awt.Color(102, 102, 255));
- jButton7.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
- jButton7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jButton7.setEnabled(false);
- jButton7.setFocusable(false);
- jButton7.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton7ActionPerformed(evt);
- }
- });
- jButton8.setBackground(new java.awt.Color(102, 102, 255));
- jButton8.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
- jButton8.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jButton8.setEnabled(false);
- jButton8.setFocusable(false);
- jButton8.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton8ActionPerformed(evt);
- }
- });
- jButton9.setBackground(new java.awt.Color(102, 102, 255));
- jButton9.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
- jButton9.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jButton9.setEnabled(false);
- jButton9.setFocusable(false);
- jButton9.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton9ActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
- jPanel2.setLayout(jPanel2Layout);
- jPanel2Layout.setHorizontalGroup(
- jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel2Layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel2Layout.createSequentialGroup()
- .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton2)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton3))
- .addGroup(jPanel2Layout.createSequentialGroup()
- .addComponent(jButton4)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton5)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton6))
- .addGroup(jPanel2Layout.createSequentialGroup()
- .addComponent(jButton7)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton8)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton9)))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jButton3, jButton4, jButton5, jButton6, jButton7, jButton8, jButton9});
- jPanel2Layout.setVerticalGroup(
- jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel2Layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton2)
- .addComponent(jButton3))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jButton4)
- .addComponent(jButton5)
- .addComponent(jButton6))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jButton7)
- .addComponent(jButton8)
- .addComponent(jButton9))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jButton2, jButton3, jButton4, jButton5, jButton6, jButton7, jButton8, jButton9});
- jPanel5.setBackground(new java.awt.Color(102, 153, 255));
- jPanel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jLabel7.setText("Sobre o jogo");
- jLabel8.setText("1. Vence aquele que preencher uma sequência com seu símbolo.");
- jLabel9.setText("2. A sequência pode ser preenchida em uma linha, coluna ou diagonal.");
- jLabel10.setText("3. O jogador vencedor será o primeiro a jogar na próxima partida.");
- jLabel11.setText("4. Em caso de empate, será sorteado quem iniciará a partida seguinte.");
- jLabel12.setText("5. O placar é zerado ao iniciar uma nova rodada.");
- javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
- jPanel5.setLayout(jPanel5Layout);
- jPanel5Layout.setHorizontalGroup(
- jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel5Layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel7)
- .addComponent(jLabel8)
- .addComponent(jLabel9)
- .addComponent(jLabel10)
- .addComponent(jLabel11)
- .addComponent(jLabel12))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- jPanel5Layout.setVerticalGroup(
- jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel5Layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jLabel7)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jLabel8)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jLabel9)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jLabel10)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jLabel11)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jLabel12)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- jPanel6.setBackground(new java.awt.Color(102, 153, 255));
- jPanel6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jTable_Scoreboard.setModel(new javax.swing.table.DefaultTableModel(
- new Object [][] {
- { new Integer(0), new Integer(0), new Integer(0)}
- },
- new String [] {
- "", "", "Empates"
- }
- ) {
- Class[] types = new Class [] {
- java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
- };
- boolean[] canEdit = new boolean [] {
- false, false, false
- };
- public Class getColumnClass(int columnIndex) {
- return types [columnIndex];
- }
- public boolean isCellEditable(int rowIndex, int columnIndex) {
- return canEdit [columnIndex];
- }
- });
- jTable_Scoreboard.setRowHeight(30);
- jScrollPane1.setViewportView(jTable_Scoreboard);
- jLabel_Info.setText("Iniciem uma nova rodada.");
- jButton_Match.setBackground(new java.awt.Color(102, 102, 255));
- jButton_Match.setText("Nova partida");
- jButton_Match.setToolTipText("Inicia uma nova partida");
- jButton_Match.setEnabled(false);
- jButton_Match.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton_MatchActionPerformed(evt);
- }
- });
- jButton_Round.setBackground(new java.awt.Color(102, 102, 255));
- jButton_Round.setText("Nova rodada");
- jButton_Round.setToolTipText("Inicia uma nova rodada");
- jButton_Round.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton_RoundActionPerformed(evt);
- }
- });
- jButton_Change.setBackground(new java.awt.Color(102, 102, 255));
- jButton_Change.setText("Trocar jogadores");
- jButton_Change.setToolTipText("Troca os jogadores que vão se enfrentar");
- jButton_Change.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton_ChangeActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
- jPanel6.setLayout(jPanel6Layout);
- jPanel6Layout.setHorizontalGroup(
- jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel6Layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
- .addComponent(jSeparator2)
- .addGroup(jPanel6Layout.createSequentialGroup()
- .addComponent(jLabel_Info)
- .addGap(0, 0, Short.MAX_VALUE))
- .addGroup(jPanel6Layout.createSequentialGroup()
- .addComponent(jButton_Match, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton_Round, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton_Change, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
- .addContainerGap())
- );
- jPanel6Layout.setVerticalGroup(
- jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel6Layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jLabel_Info)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(jButton_Change, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
- .addComponent(jButton_Round, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jButton_Match, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
- jPanel1.setLayout(jPanel1Layout);
- jPanel1Layout.setHorizontalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- jPanel1Layout.setVerticalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(0, 0, Short.MAX_VALUE))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
- .addContainerGap())
- );
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- );
- pack();
- setLocationRelativeTo(null);
- }// </editor-fold>
- // Placar
- private int lastWinner = -1;
- private int totalTies = 0;
- private int[] totalWins = {0, 0};
- // Jogo
- private final int B1 = 1;
- private final int B2 = 2;
- private final int B3 = 4;
- private final int B4 = 8;
- private final int B5 = 16;
- private final int B6 = 32;
- private final int B7 = 64;
- private final int B8 = 128;
- private final int B9 = 256;
- private final String[] playerSymbol = {"X", "O"};
- private int currentMove = 1;
- private int playingNow;
- private String[] playerName = new String[2];
- private int[] playerMoves = {0, 0};
- // Processamento do jogo
- private void processMove() {
- int index = playingNow - 1;
- // linha 1
- if(((playerMoves[index] & B1) == B1) && ((playerMoves[index] & B2) == B2) && ((playerMoves[index] & B3) == B3)) {
- playerWin(playingNow);
- }
- // linha 2
- else if(((playerMoves[index] & B4) == B4) && ((playerMoves[index] & B5) == B5) && ((playerMoves[index] & B6) == B6)) {
- playerWin(playingNow);
- }
- // linha 3
- else if(((playerMoves[index] & B7) == B7) && ((playerMoves[index] & B8) == B8) && ((playerMoves[index] & B9) == B9)) {
- playerWin(playingNow);
- }
- // coluna 1
- else if(((playerMoves[index] & B1) == B1) && ((playerMoves[index] & B4) == B4) && ((playerMoves[index] & B7) == B7)) {
- playerWin(playingNow);
- }
- // coluna 2
- else if(((playerMoves[index] & B2) == B2) && ((playerMoves[index] & B5) == B5) && ((playerMoves[index] & B8) == B8)) {
- playerWin(playingNow);
- }
- // coluna 3
- else if(((playerMoves[index] & B3) == B3) && ((playerMoves[index] & B6) == B6) && ((playerMoves[index] & B9) == B9)) {
- playerWin(playingNow);
- }
- // diagonal (esquerda > direita)
- else if(((playerMoves[index] & B1) == B1) && ((playerMoves[index] & B5) == B5) && ((playerMoves[index] & B9) == B9)) {
- playerWin(playingNow);
- }
- // diagonal (direita > esquerda
- else if(((playerMoves[index] & B3) == B3) && ((playerMoves[index] & B5) == B5) && ((playerMoves[index] & B7) == B7)) {
- playerWin(playingNow);
- }
- //
- else {
- // Empate
- if(++currentMove == 10) {
- jButton1.setEnabled(false);
- jButton2.setEnabled(false);
- jButton3.setEnabled(false);
- jButton4.setEnabled(false);
- jButton5.setEnabled(false);
- jButton6.setEnabled(false);
- jButton7.setEnabled(false);
- jButton8.setEnabled(false);
- jButton9.setEnabled(false);
- lastWinner = -1;
- jTable_Scoreboard.setValueAt(++totalTies, 0, 2);
- jLabel_Info.setText("Iniciem uma nova partida.");
- JOptionPane.showMessageDialog(null, "A partida acabou empatada.");
- }
- // Jogo segue
- else {
- playingNow = playingNow == 1 ? 2 : 1;
- jLabel_Info.setText("É a vez de "+playerName[playingNow - 1]+".");
- }
- }
- }
- private void playerWin(int player) {
- jButton1.setEnabled(false);
- jButton2.setEnabled(false);
- jButton3.setEnabled(false);
- jButton4.setEnabled(false);
- jButton5.setEnabled(false);
- jButton6.setEnabled(false);
- jButton7.setEnabled(false);
- jButton8.setEnabled(false);
- jButton9.setEnabled(false);
- lastWinner = player--;
- jLabel_Info.setText("Iniciem uma nova partida.");
- jTable_Scoreboard.setValueAt(++totalWins[player], 0, player);
- JOptionPane.showMessageDialog(null, playerName[player]+" venceu a partida.");
- }
- // Métodos dos componentes
- private void jButton_MatchActionPerformed(java.awt.event.ActionEvent evt) {
- jButton1.setText(""); jButton1.setEnabled(true);
- jButton2.setText(""); jButton2.setEnabled(true);
- jButton3.setText(""); jButton3.setEnabled(true);
- jButton4.setText(""); jButton4.setEnabled(true);
- jButton5.setText(""); jButton5.setEnabled(true);
- jButton6.setText(""); jButton6.setEnabled(true);
- jButton7.setText(""); jButton7.setEnabled(true);
- jButton8.setText(""); jButton8.setEnabled(true);
- jButton9.setText(""); jButton9.setEnabled(true);
- if(lastWinner == -1) {
- playingNow = new java.util.Random().nextInt(2) + 1;
- jLabel_Info.setText("Foi sorteado que "+playerName[playingNow - 1]+" fará a primeira jogada.");
- }
- else {
- jLabel_Info.setText(playerName[playingNow - 1]+" fará a primeira jogada, já que venceu a última partida.");
- }
- currentMove = 1;
- playerMoves[0] = playerMoves[1] = 0;
- JOptionPane.showMessageDialog(null, "Uma nova partida foi iniciada.");
- }
- private void jButton_RoundActionPerformed(java.awt.event.ActionEvent evt) {
- jButton1.setText(""); jButton1.setEnabled(false);
- jButton2.setText(""); jButton2.setEnabled(false);
- jButton3.setText(""); jButton3.setEnabled(false);
- jButton4.setText(""); jButton4.setEnabled(false);
- jButton5.setText(""); jButton5.setEnabled(false);
- jButton6.setText(""); jButton6.setEnabled(false);
- jButton7.setText(""); jButton7.setEnabled(false);
- jButton8.setText(""); jButton8.setEnabled(false);
- jButton9.setText(""); jButton9.setEnabled(false);
- jButton_Match.setEnabled(true);
- jTable_Scoreboard.setValueAt((totalTies = 0), 0, 2);
- jTable_Scoreboard.setValueAt((totalWins[1] = 0), 0, 1);
- jTable_Scoreboard.setValueAt((totalWins[0] = 0), 0, 0);
- jLabel_Info.setText("Iniciem uma nova partida.");
- JOptionPane.showMessageDialog(null, "Uma nova rodada foi iniciada.");
- }
- private void jButton_ChangeActionPerformed(java.awt.event.ActionEvent evt) {
- this.dispose();
- new Intro().setVisible(true);
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- if(jButton1.isEnabled()) {
- jButton1.setEnabled(false);
- jButton1.setText(playerSymbol[playingNow - 1]);
- playerMoves[playingNow - 1] |= B1;
- processMove();
- }
- }
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- if(jButton2.isEnabled()) {
- jButton2.setEnabled(false);
- jButton2.setText(playerSymbol[playingNow - 1]);
- playerMoves[playingNow - 1] |= B2;
- processMove();
- }
- }
- private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
- if(jButton3.isEnabled()) {
- jButton3.setEnabled(false);
- jButton3.setText(playerSymbol[playingNow - 1]);
- playerMoves[playingNow - 1] |= B3;
- processMove();
- }
- }
- private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
- if(jButton4.isEnabled()) {
- jButton4.setEnabled(false);
- jButton4.setText(playerSymbol[playingNow - 1]);
- playerMoves[playingNow - 1] |= B4;
- processMove();
- }
- }
- private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
- if(jButton5.isEnabled()) {
- jButton5.setEnabled(false);
- jButton5.setText(playerSymbol[playingNow - 1]);
- playerMoves[playingNow - 1] |= B5;
- processMove();
- }
- }
- private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
- if(jButton6.isEnabled()) {
- jButton6.setEnabled(false);
- jButton6.setText(playerSymbol[playingNow - 1]);
- playerMoves[playingNow - 1] |= B6;
- processMove();
- }
- }
- private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
- if(jButton7.isEnabled()) {
- jButton7.setEnabled(false);
- jButton7.setText(playerSymbol[playingNow - 1]);
- playerMoves[playingNow - 1] |= B7;
- processMove();
- }
- }
- private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
- if(jButton8.isEnabled()) {
- jButton8.setEnabled(false);
- jButton8.setText(playerSymbol[playingNow - 1]);
- playerMoves[playingNow - 1] |= B8;
- processMove();
- }
- }
- private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
- if(jButton9.isEnabled()) {
- jButton9.setEnabled(false);
- jButton9.setText(playerSymbol[playingNow - 1]);
- playerMoves[playingNow - 1] |= B9;
- processMove();
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(Jogo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(Jogo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(Jogo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Jogo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Jogo("Jogador 1", "Jogador 2").setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton2;
- private javax.swing.JButton jButton3;
- private javax.swing.JButton jButton4;
- private javax.swing.JButton jButton5;
- private javax.swing.JButton jButton6;
- private javax.swing.JButton jButton7;
- private javax.swing.JButton jButton8;
- private javax.swing.JButton jButton9;
- private javax.swing.JButton jButton_Change;
- private javax.swing.JButton jButton_Match;
- private javax.swing.JButton jButton_Round;
- private javax.swing.JLabel jLabel10;
- private javax.swing.JLabel jLabel11;
- private javax.swing.JLabel jLabel12;
- private javax.swing.JLabel jLabel7;
- private javax.swing.JLabel jLabel8;
- private javax.swing.JLabel jLabel9;
- private javax.swing.JLabel jLabel_Info;
- private javax.swing.JPanel jPanel1;
- private javax.swing.JPanel jPanel2;
- private javax.swing.JPanel jPanel5;
- private javax.swing.JPanel jPanel6;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JSeparator jSeparator2;
- private javax.swing.JTable jTable_Scoreboard;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement