How to save results from the game to text file [closed]

爷,独闯天下 提交于 2019-12-14 03:38:08

问题


here is the tic tac toe game in java, can somebody complete the program that it will save how many times win X and how many times O into the text file, please :)

    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();
    }
}

回答1:


I've created a basic class for you that will save the wins to a text file and retrieve them from a text file.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class WinsFileManager {

    String path = System.getProperty("user.home").replace("\\", "\\\\") + "\\";

    public int getWins(String fileName) {

        String fullPath = path + fileName;
        int wins = 0;

        try {
            wins = Integer.parseInt(new Scanner(new File(fullPath)).nextLine());
        } catch (NumberFormatException e) {} 
          catch (FileNotFoundException e) {
            wins = 0;
        }

        return wins;
    }

    public void saveWins(String fileName, int wins) {
        PrintWriter out = null;

        try {
            out = new PrintWriter(path + fileName, "UTF-8");
            out.println(wins);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Here's how you'd use an object of the above class from a different location.

public class test {

    public static void main(String[] args) {
        WinsFileManager manager = new WinsFileManager();
        String fileName = "O Wins.txt";

        manager.saveWins(fileName, 5);
        System.out.println(manager.getWins(fileName));
    }
}

The console output in the above example would be '5'.

I've done the hard part. Now it's up to you to implement this into your program. To start off, whenever someone wins, I would retrieve the stored number of wins via the getWins method, add one to that value, and then use the saveWins method to store the incremented value. Keep in mind that even once you exit your program, the win values will be saved.

Note: There are much easier ways to do this if all you want to do is keep track of the wins within the lifespan of the program.




回答2:


Take a look at the following resources.

Reading and writing a text file.

How to write console output to a txt file.

How do I create and write data into a text file?



来源:https://stackoverflow.com/questions/16121934/how-to-save-results-from-the-game-to-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!