-3

这是java中的井字游戏,有人可以解释如何将获胜次数X和次数保存O到文本文件中,我查看了类似的线程,但我不知道在这种情况下如何制作。抱歉重复,不知道如何更新最后一个问题。

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

public class XO2 implements ActionListener {
private int[][] winningCombination = new int[][] {
        {0, 1, 2},
                    {3, 4, 5},
                    {6, 7, 8}, 
        {0, 3, 6},
                    {1, 4, 7},
                    {2, 5, 8}, 
        {0, 4, 8},
                    {3, 4, 6}            
};
private JFrame window = new JFrame("Tic Tac Toe");
private JButton buttons[] = new JButton[9];
private int count = 0;
private String letter = "";
private boolean win = false;

public XO2(){

    window.setSize(300,300);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setLayout(new GridLayout(3,3));


    for(int i=0; i<9; i++){
        buttons[i] = new JButton();
        window.add(buttons[i]);
        buttons[i].addActionListener(this);
    }


    window.setVisible(true);
}


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


    if(count % 2 == 0){
        letter = "O";
    }
    else {
        letter = "X";
    }


    JButton pressedButton = (JButton)a.getSource();
    pressedButton.setText(letter);
    pressedButton.setEnabled(false);


    for(int i=0; i<8; i++){
        if( buttons[winningCombination[i][0]].getText().equals(buttons[winningCombination[i][1]].getText()) &&
                buttons[winningCombination[i][1]].getText().equals(buttons[winningCombination[i][2]].getText()) &&
                !buttons[winningCombination[i][0]].getText().equals("")){
            win = true;
        }
    }


    if(win == true){
        JOptionPane.showMessageDialog(null, letter + " Won!");
        System.exit(0);
    } else if(count == 9 && win == false){
        JOptionPane.showMessageDialog(null, "Draw!");
        System.exit(0);
    }
}

public static void main(String[] args){
    XO2 starter = new XO2();
}

}

4

1 回答 1

0

将您的代码更改为此

编辑代码 3

import java.awt.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.swing.*;

        public class XO2 implements ActionListener {
        private int[][] winningCombination = new int[][] {
                {0, 1, 2},
                            {3, 4, 5},
                            {6, 7, 8}, 
                {0, 3, 6},
                            {1, 4, 7},
                            {2, 5, 8}, 
                {0, 4, 8},
                            {3, 4, 6}            
        };
        private JFrame window = new JFrame("Tic Tac Toe");
        private JButton buttons[] = new JButton[9];
        private int count = 0;
        private String letter = "";
        private boolean win = false;

        public XO2(){

            window.setSize(300,300);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setLayout(new GridLayout(3,3));


            for(int i=0; i<9; i++){
                buttons[i] = new JButton();
                window.add(buttons[i]);
                buttons[i].addActionListener(this);
            }


            window.setVisible(true);
        }


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


            if(count % 2 == 0){
                letter = "O";
            }
            else {
                letter = "X";
            }


            JButton pressedButton = (JButton)a.getSource();
            pressedButton.setText(letter);
            pressedButton.setEnabled(false);


            for(int i=0; i<8; i++){
                if( buttons[winningCombination[i][0]].getText().equals(buttons[winningCombination[i][1]].getText()) &&
                        buttons[winningCombination[i][1]].getText().equals(buttons[winningCombination[i][2]].getText()) &&
                        !buttons[winningCombination[i][0]].getText().equals("")){
                    win = true;
                }
            }


            if(win == true){
                JOptionPane.showMessageDialog(null, letter + " Won!");
                try {
                    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter( "c:\\calc\\output.txt", true)));
                    out.println(letter + "Won!");
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.exit(0);
            } else if(count == 9 && win == false){
                JOptionPane.showMessageDialog(null, "Draw!");
                try {
                    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter( "c:\\calc\\output.txt", true)));
                    out.println("Draw!");
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                System.exit(0);
            }
        }

        public static void main(String[] args){
            XO2 starter = new XO2();
        }

}

并确保在添加之前创建了要写入的文本文件

我添加的代码用双星号标记

希望这可以帮助

于 2013-04-20T17:03:05.580 回答