2013-04-20 3 views
-3

여기에 자바의 발가락 정강이 발가락 게임이 있습니다. 얼마나 많은 시간을 얼마나 많은 시간을 절약 할 수 있는지 설명 할 수 있습니까? 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(); 
} 

}

+0

100000000000 % 중복 버튼이 있었으면합니다. – Maroun

+0

죄송합니다. maroun maroun,이 포럼의 새로운 내용 – user2302407

답변

0

변경에 코드이

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

} 

그리고 있는지 확인이

를 추가하기 전에 당신이 쓰고 싶은 텍스트 파일을 만들었습니다

내가 추가 한 코드는 두 개의 별표로 표시됩니다.

희망 하시겠습니까?

+0

파일을 먼저 만들고 프로그램에 올바른 경로를 추가했는지 확인 했습니까? –

+0

예. 위치를 desctop으로 변경했으며 output.txt 파일을 만들었습니다. – user2302407

+0

"c : \\ calc \\ output.txt"와 같은 각 폴더 사이에 이중 백 슬래시를 추가하십시오. 아무 일도 일어나지 않으면 –

관련 문제