Tic Tac Toe java

时间:2012-06-09 14:20:32

标签: java

我是学习Java编程的初学者,而且我正在做tic tac toe游戏。

当我完成游戏时,我无法继续玩游戏,因为该程序将退出。我应该在这段代码中添加什么。由于我不使用paint方法,因此无法使用repaint()。

import java.awt.*;  
import java.awt.event.*;
import javax.swing.*;

public class TicTacToeV1 implements ActionListener {
    /*Instance Variables*/
    private JFrame window = new JFrame("Tic-Tac-Toe");
    private JButton button1 = new JButton("");
    private JButton button2 = new JButton("");
    private JButton button3 = new JButton("");
    private JButton button4 = new JButton("");
    private JButton button5 = new JButton("");
    private JButton button6 = new JButton("");
    private JButton button7 = new JButton("");
    private JButton button8 = new JButton("");
    private JButton button9 = new JButton("");

    private String letter = "";
    public static int count = 0;
    public TicTacToeV1(){           
        /*Create Window*/
        window.setSize(300,300);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLayout(new GridLayout(3,3));

        /*Add Buttons To The Window*/
        window.add(button1);
        window.add(button2);
        window.add(button3);
        window.add(button4);
        window.add(button5);
        window.add(button6);
        window.add(button7);
        window.add(button8);
        window.add(button9);

        /*Add The Action Listener To The Buttons*/
        button1.addActionListener(this);
        button2.addActionListener(this);
        button3.addActionListener(this);
        button4.addActionListener(this);
        button5.addActionListener(this);
        button6.addActionListener(this);
        button7.addActionListener(this);
        button8.addActionListener(this);
        button9.addActionListener(this);

        /*Make The Window Visible*/
        window.setVisible(true);

        String input = JOptionPane.showInputDialog("Please select ur pawn: \n1) X\n2) O");
        int pawn = Integer.parseInt(input);
        if ( input.equals("2")){
              setCount(1);
        }
    }

    public static void setCount (int co){
        count = co;            
    }

    public void actionPerformed(ActionEvent a) {    
        count++;

        /*Calculate Who's Turn It Is*/
        if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9|| count == 11){
            letter = "X";

        } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
            letter = "O";
        }

        /*Display X's or O's on the buttons*/
        if(a.getSource() == button1){
            button1.setText(letter);
            button1.setEnabled(false);
        } else if(a.getSource() == button2){
            button2.setText(letter);
            button2.setEnabled(false);
        } else if(a.getSource() == button3){
            button3.setText(letter);
            button3.setEnabled(false);
        } else if(a.getSource() == button4){
             button4.setText(letter);
             button4.setEnabled(false);
        } else if(a.getSource() == button5){
             button5.setText(letter);
             button5.setEnabled(false);
        } else if(a.getSource() == button6){
             button6.setText(letter);
             button6.setEnabled(false);
        } else if(a.getSource() == button7){
             button7.setText(letter);
             button7.setEnabled(false);
        } else if(a.getSource() == button8){
             button8.setText(letter);
             button8.setEnabled(false);
        } else if(a.getSource() == button9){
             button9.setText(letter);
             button9.setEnabled(false);
        }       
    }

    public static void main(String[] args){           
            new TicTacToeV1();
    }
}

2 个答案:

答案 0 :(得分:1)

创建一个名为reset(或类似的东西)的方法,并使其执行以下操作:

  1. 重置每个文本值。
  2. 将计数重置为0.
  3. 您可能想要为按钮创建一个数组[](或2D阵列,如果您知道>> [] []),并且更容易管理。这样可以更好地管理许多按钮,并删除当前代码中的大量无用重复。

    示例代码:

    public static void reset() {
        button1.setText("");
        button1.setEnabled(true);
        //etc...
        count = 0;
    }
    

    然后在游戏结束时调用reset()(你可能还需要检查谁赢了)。

    希望这有助于和DFTBA。 :)

答案 1 :(得分:1)

a)您的整个代码适合50行。 b)你没有办法检测胜利者或完成的游戏,所以我只是在计数== 9之后添加了代码。

import java.awt.*;  
import java.awt.event.*;
import javax.swing.*;

public class TicTacToeV1 extends JFrame implements ActionListener {
    private JButton [] button  = new JButton [9];
    private int count = 0;

    public TicTacToeV1 () {           
        super ("Tic-Tac-Toe");
        setSize (300, 300);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setLayout (new GridLayout (3, 3));
        init ();
    }

    private void init () {           
        count = 0;
        for (int i = 0; i < 9; ++i) {
            button [i] = new JButton ("");
            button [i].addActionListener (this);
            add (button [i]);
        }
        setVisible (true);
    }

    public void actionPerformed (ActionEvent a) {    
        String letter = (++count % 2 == 1) ? "X" : "O";
        /*Display X's or O's on the buttons*/
        for (JButton jb : button) 
        if (a.getSource () == jb) {
            jb.setText (letter);
            jb.setEnabled (false);
        }
        if (count == 9) {
            for (JButton jb : button) 
                remove (jb) ;
            init ();
        }
    }

    public static void main (String [] args) {           
        new TicTacToeV1 ();
    }
}

我把变量部分放入它自己的方法(init)中,并从Ctor或游戏结束方法中调用它。

当然你也可以从那里打电话给新的Ctor。或者只是重置按钮状态和计数器。罗马有很多方法。虽然它不应该发挥作用,但是在内存中使用所有按钮避免100个结束的游戏可能会节省10kb的内存。

相关问题