TicTacToe win check doesn't work

时间:2016-02-12 21:41:44

标签: java

I'm trying to get my TicTacToe working, but my win or lose checker doesn't work.

this is my startup frame.

@foreach (var item in Model.Select(x => new //here you count your total
                                            {
                                            Rid = x.Rid,
                                            Total = x.Total * x.Cantidad
                                            })
                                            .GroupBy(l => l.Rid) //and then grouping
                                            .Select(z => new
                                            {
                                            Turno = z.Key,
                                            Total = z.Sum(l => l.Total)
                                            }))
                                        {
                                        <input value="@item" />
                                        }

and these are my buttons and panel. package tictactoe;

package tictactoe;
import javax.swing.*;

public class TTT extends JFrame {


    public static void main( String args[] ) {
    JFrame frame = new TTT();

    frame.setSize( 695, 620 );
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setTitle( "TicTacToe" );
    JPanel paneel = new paneel();
    frame.setContentPane( paneel );
    frame.setVisible( true );
    frame.setLocation( 400, 300 );
    }
}

and this is my Win or lose checker.

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

class paneel extends JPanel {


    private JButton start, exit, TTT1, TTT2, TTT3, TTT4, TTT5, TTT6, TTT7, TTT8, TTT9;
    private JTextField roundInt;

    static String x = "X";
    static String o = "O";

    static int round = 1; 
    static boolean playerTurn = true;

    public paneel() {
        setLayout(null);
        setBackground(Color.DARK_GRAY);

    start = new JButton("Start");
    start.addActionListener(new startHandler());
    start.setBounds(100,100,400,100);
    start.setBackground(Color.GRAY);
    start.setForeground(Color.black);
    start.setVisible(true);
    start.setOpaque(true);

    exit = new JButton("exit");
    exit.addActionListener(new exitHandler());
    exit.setBounds(100,300,400,100);
    exit.setBackground(Color.GRAY);
    exit.setForeground(Color.BLACK);
    exit.setVisible(true);
    exit.setOpaque(true);

    roundInt = new JTextField(round);
    roundInt.setBackground(Color.DARK_GRAY);
    roundInt.setForeground(Color.WHITE);
    roundInt.setBounds(590,20,70,40);
    roundInt.setVisible(false);

    TTT1 = new JButton();
    TTT2 = new JButton();
    TTT3 = new JButton();
    TTT4 = new JButton();
    TTT5 = new JButton();
    TTT6 = new JButton();
    TTT7 = new JButton();
    TTT8 = new JButton();
    TTT9 = new JButton();

    TTT1.addActionListener(new ttt1());
    TTT2.addActionListener(new ttt2());
    TTT3.addActionListener(new ttt3());
    TTT4.addActionListener(new ttt4());
    TTT5.addActionListener(new ttt5());
    TTT6.addActionListener(new ttt6());
    TTT7.addActionListener(new ttt7());
    TTT8.addActionListener(new ttt8());
    TTT9.addActionListener(new ttt9());

    TTT1.setBounds(20,20,170,170);
    TTT2.setBounds(200,20,170,170);
    TTT3.setBounds(380,20,170,170);
    TTT4.setBounds(20,200,170,170);
    TTT5.setBounds(200,200,170,170);
    TTT6.setBounds(380,200,170,170);
    TTT7.setBounds(20,380,170,170);
    TTT8.setBounds(200,380,170,170);
    TTT9.setBounds(380,380,170,170);

    TTT1.setBackground(Color.decode("0x999999"));
    TTT2.setBackground(Color.decode("0x999999"));
    TTT3.setBackground(Color.decode("0x999999"));
    TTT4.setBackground(Color.decode("0x999999"));
    TTT5.setBackground(Color.decode("0x999999"));
    TTT6.setBackground(Color.decode("0x999999"));
    TTT7.setBackground(Color.decode("0x999999"));
    TTT8.setBackground(Color.decode("0x999999"));
    TTT9.setBackground(Color.decode("0x999999"));


    TTT1.setVisible(false);
    TTT2.setVisible(false);
    TTT3.setVisible(false);
    TTT4.setVisible(false);
    TTT5.setVisible(false);
    TTT6.setVisible(false);
    TTT7.setVisible(false);
    TTT8.setVisible(false);
    TTT9.setVisible(false);

    TTT1.setForeground(Color.decode("0x000000"));
    TTT2.setForeground(Color.decode("0x000000"));
    TTT3.setForeground(Color.decode("0x000000"));
    TTT4.setForeground(Color.decode("0x000000"));
    TTT5.setForeground(Color.decode("0x000000"));
    TTT6.setForeground(Color.decode("0x000000"));
    TTT7.setForeground(Color.decode("0x000000"));
    TTT8.setForeground(Color.decode("0x000000"));
    TTT9.setForeground(Color.decode("0x000000"));

    add( start );
    add( exit );
    add( TTT1 );
    add( TTT2 );
    add( TTT3 );
    add( TTT4 );
    add( TTT5 );
    add( TTT6 );
    add( TTT7 );
    add( TTT8 );
    add( TTT9 );
    add( roundInt );
    }

    class exitHandler implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
          System.exit(0);
        }
      }

    class startHandler implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
          start.setVisible(false);
          exit.setVisible(false);
          roundInt.setVisible(true);

          TTT1.setVisible(true);
          TTT2.setVisible(true);
          TTT3.setVisible(true);
          TTT4.setVisible(true);
          TTT5.setVisible(true);
          TTT6.setVisible(true);
          TTT7.setVisible(true);
          TTT8.setVisible(true);
          TTT9.setVisible(true);
        }
      }

    class ttt1 implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            if(playerTurn){
                TTT1.setText("X");
                playerTurn = false;
                wincheck.knop1C = 1;
                round++;
              }else{
                TTT1.setText("O");
                playerTurn = true;
                wincheck.knop1C = 2;
                round++;
              }
        }
      }

    class ttt2 implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            if(playerTurn){
                TTT2.setText("X");
                playerTurn = false;
                wincheck.knop2C = 1;
                round++;
              }else{
                TTT2.setText("O");
                playerTurn = true;
                wincheck.knop2C = 2;
                round++;
              }
        }
      }

    class ttt3 implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            if(playerTurn){
                TTT3.setText("X");
                playerTurn = false;
                wincheck.knop3C = 1;
                round++;
              }else{
                TTT3.setText("O");
                playerTurn = true;
                wincheck.knop3C = 2;
                round++;
              }
        }
      }

    class ttt4 implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            if(playerTurn){
                TTT4.setText("X");
                playerTurn = false;
                wincheck.knop4C = 1;
                round++;
              }else{
                TTT4.setText("O");
                playerTurn = true;
                wincheck.knop4C = 2;
                round++;
              }
        }
      }

    class ttt5 implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            if(playerTurn){
                TTT5.setText("X");
                playerTurn = false;
                wincheck.knop5C = 1;
                round++;
              }else{
                TTT5.setText("O");
                playerTurn = true;
                wincheck.knop5C = 2;
                round++;
              }
        }
      }

    class ttt6 implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            if(playerTurn){
                TTT6.setText("X");
                playerTurn = false;
                wincheck.knop6C = 1;
                round++;
              }else{
                TTT6.setText("O");
                playerTurn = true;
                wincheck.knop6C = 2;
                round++;
              }
        }
      }

    class ttt7 implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            if(playerTurn){
                TTT7.setText("X");
                playerTurn = false;
                wincheck.knop7C = 1;
                round++;
              }else{
                TTT7.setText("O");
                playerTurn = true;
                wincheck.knop7C = 2;
                round++;
              }
        }
      }

    class ttt8 implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            if(playerTurn){
                TTT8.setText("X");
                playerTurn = false;
                wincheck.knop8C = 1;
                round++;
              }else{
                TTT8.setText("O");
                playerTurn = true;
                wincheck.knop8C = 2;
                round++;
              }
        }
      }

    class ttt9 implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            if(playerTurn){
                TTT9.setText("X");
                playerTurn = false;
                wincheck.knop9C = 1;
                round++;
              }else{
                TTT9.setText("O");
                playerTurn = true;
                wincheck.knop9C = 2;
                round++;
              }
        }
      }
}

Can anyone please help? I need this for a school project.

(by the way, I'm dutch. Please don't mind my bad grammar)

1 个答案:

答案 0 :(得分:3)

我不会为你做你的工作,但我将向你展示如何通过使用数组,布局管理器和单个ActionListener来替换90%的过多代码来极大地简化它。请注意代码中的注释:

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

@SuppressWarnings("serial")
public class TicTacToePanel extends JPanel {
    private static final int SIDES = 3;
    private static final Font FONT = new Font(Font.SANS_SERIF, Font.BOLD, 60);

    // 2D array of all buttons
    private JButton[][] grid = new JButton[SIDES][SIDES];

    public TicTacToePanel() {
        // use a single ActionListener for all buttons
        ButtonListener listener = new ButtonListener();

        // use a GridLayout to easily create your 3x3 grid
        setLayout(new GridLayout(SIDES, SIDES));

        // use a nested for loop to create your "grid" of
        // JButtons. 
        for (int row = 0; row < grid.length; row++) {
            for (int column = 0; column < grid[row].length; column++) {

                // create your button, and its properties
                JButton btn = new JButton("   "); // give it some width
                btn.setFont(FONT); // make button bigger by using large Font

                // add an ActionListener to it
                btn.addActionListener(listener);

                // add it to the GUI -- the current JPanel
                add(btn);

                // place it in the 2D grid array of JButton
                grid[row][column] = btn;
            }
        }
    }

    // single listener for all buttons
    private class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // get which button was pushed
            JButton sourceButton = (JButton) e.getSource();

            // Now use nested for loops to find row and column button pressed

            // change text in that button if not already set

            // check for win here. Since you know the row and column of the 
            // last button pressed
            // you can search for win associated with the row, column, and possibly
            // diagonal that involves just that button. No need to look at all possible
            // wins.

        }
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Tic Tac Toe");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new TicTacToePanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}