如何将游戏结果保存到文本文件 [英] How to save results from the game to text file

查看:129
本文介绍了如何将游戏结果保存到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Java中的tic tac toe游戏,可以有人完成该程序,将文本中保存多少次X赢取和O一次,请:)

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

推荐答案

我为您创建了一个基础类,该基础类会将胜利保存到文本文件中并从文本文件中检索出来.

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

以上示例中的控制台输出为"5".

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

我已经完成了困难的部分.现在由您决定将其实现到程序中.首先,每当有人获胜时,我都将通过getWins方法检索存储的获胜次数,将该值加一,然后使用saveWins方法来存储递增的值.请记住,即使退出程序,胜利值也会被保存.

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.

这篇关于如何将游戏结果保存到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆