-3

here is the tic tac toe game in java, can somebody explain how to save how many times win X and how many times O into the text file, I looked at similar threads but I don't know how to make it in this case. Sorry for the duplicate, didnt know how to update last question.

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 Answers1

0

Change your code to this

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

}

And make sure you have created the text file you want to write to before adding this

The code i added is marked by a double asterisk

Hope this helps

06needhamt
  • 1,555
  • 2
  • 19
  • 38
  • Did you make sure you created the file first and added the correct path in the program cause it worked for me – 06needhamt Apr 20 '13 at 17:27
  • yes i changed location to desctop and i made output.txt file – user2302407 Apr 20 '13 at 17:30
  • Did you add double backslashes between each folder like this "c:\\calc\\output.txt" because if you do single nothing happens – 06needhamt Apr 20 '13 at 17:32
  • oh its working now, but when you play again it deletes last game result, can you keep all results in column? – user2302407 Apr 20 '13 at 17:38
  • Yes you can to do so replace your code with the code in my edited answer above – 06needhamt Apr 20 '13 at 17:46
  • thank you so much for your time, i know im asking too much but it woud be hard to include draw too? and why if i write >>> out.println(letter + " won!"); >>> it writes just X not X won! ? >>>thanks again you helped me a lot – user2302407 Apr 20 '13 at 18:01
  • how can you make to play against computer? – user2302407 Apr 21 '13 at 10:19
  • I can't do it myself but i found this tutorial http://progressivejava.blogspot.co.uk/2012/11/How-to-make-a-Tic-Tac-Toe-game-in-Java.html – 06needhamt Apr 21 '13 at 12:00