2012-08-08 3 views
0

게임 tictactoe에 대한 가장 간단한 솔루션을 구현했습니다.이를 표현하기 위해 GUI를 사용했습니다. 그래서 좀 더 우아하게 만드는 방법을 제안하고 아이디어를 줄 수 있는지 묻고 있습니다. 개선하려는 부분은 TicTacToe 클래스와 메서드 mousePressed 및 mouseEntered에 있습니다. 여기에 코드가 있습니다.tictactoe 구현

/**The TicTacToe class with the GUI 
*/ 
import java.awt.Color; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.border.Border; 
import javax.swing.border.LineBorder; 

public class TicTacToe extends JFrame implements MouseListener 
{ 
    JLabel[] jl ={ 
      new JLabel(), 
      new JLabel(), 
      new JLabel(), 
      new JLabel(), 
      new JLabel(), 
      new JLabel(), 
      new JLabel(), 
      new JLabel(), 
      new JLabel()} ; 

    ImageIcon[] ox = { 
      new ImageIcon("tic/o.gif"), 
      new ImageIcon("tic/x.gif") 
    }; 

    static boolean isFirst; 
    static int counter = 1; 

    public TicTacToe() 
    { 

     JPanel panel = new JPanel(new GridLayout(3,3)); 

     Border border = new LineBorder(Color.red, 2); 
     Border border2 = new LineBorder(Color.blue, 2); 



     for(int i=0; i<jl.length; i++) 
     { 
      jl[i].setBorder(border2); 
      jl[i].addMouseListener(this); 
      panel.add(jl[i]); 
     } 
     panel.setBorder(border); 
     this.add(panel); 




    } 

    public static String count() 
    { 
     counter++; 

     if(counter % 2 != 0) 
     { 
      isFirst = true; 
      return "First"; 
     } 
      isFirst = false; 
      return "Second"; 

    } 


    @Override 
    public void mouseClicked(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 


    @Override 
    public void mousePressed(MouseEvent me) 
    { 
     int count = 0; 
     count(); 


     for(int i=0; i<jl.length; i++) 
     { 

      if(jl[0].getIcon() == ox[0] && jl[1].getIcon() == ox[0] && jl[2].getIcon() == ox[0]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with 0 won "); 
       System.exit(0); 
      } 

      else if(jl[3].getIcon() == ox[0] && jl[4].getIcon() == ox[0] && jl[5].getIcon() == ox[0]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with 0 won "); 
       System.exit(0); 
      } 

      else if(jl[6].getIcon() == ox[0] && jl[7].getIcon() == ox[0] && jl[8].getIcon() == ox[0]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with 0 won "); 
       System.exit(0); 
      } 


      else if(jl[0].getIcon() == ox[0] && jl[4].getIcon() == ox[0] && jl[8].getIcon() == ox[0]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with 0 won with the major diagonal "); 
       System.exit(0); 
      } 

      else if(jl[2].getIcon() == ox[0] && jl[4].getIcon() == ox[0] && jl[6].getIcon() == ox[0]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with 0 won with the subdiagonal "); 
       System.exit(0); 
      } 

      else if(jl[0].getIcon() == ox[0] && jl[3].getIcon() == ox[0] && jl[6].getIcon() == ox[0]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with 0 won "); 
       System.exit(0); 
      } 

      else if(jl[1].getIcon() == ox[0] && jl[4].getIcon() == ox[0] && jl[7].getIcon() == ox[0]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with 0 won "); 
       System.exit(0); 
      } 

      else if(jl[2].getIcon() == ox[0] && jl[5].getIcon() == ox[0] && jl[8].getIcon() == ox[0]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with 0 won "); 
       System.exit(0); 
      } 

      else if(jl[0].getIcon() == ox[1] && jl[1].getIcon() == ox[1] && jl[2].getIcon() == ox[1]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with X won "); 
       System.exit(0); 
      } 

      else if(jl[3].getIcon() == ox[1] && jl[4].getIcon() == ox[1] && jl[5].getIcon() == ox[1]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with X won "); 
       System.exit(0); 
      } 

      else if(jl[6].getIcon() == ox[1] && jl[7].getIcon() == ox[1] && jl[8].getIcon() == ox[1]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with X won "); 
       System.exit(0); 
      } 


      else if(jl[0].getIcon() == ox[1] && jl[4].getIcon() == ox[1] && jl[8].getIcon() == ox[1]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with X won with the major diagonal "); 
       System.exit(0); 
      } 

      else if(jl[2].getIcon() == ox[1] && jl[4].getIcon() == ox[1] && jl[6].getIcon() == ox[1]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with X won with the subdiagonal "); 
       System.exit(0); 
      } 

      else if(jl[0].getIcon() == ox[1] && jl[3].getIcon() == ox[1] && jl[6].getIcon() == ox[1]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with X won "); 
       System.exit(0); 
      } 

      else if(jl[1].getIcon() == ox[1] && jl[4].getIcon() == ox[1] && jl[7].getIcon() == ox[1]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with X won "); 
       System.exit(0); 
      } 

      else if(jl[2].getIcon() == ox[1] && jl[5].getIcon() == ox[1] && jl[8].getIcon() == ox[1]) 
      { 
       JOptionPane.showMessageDialog(null, 
         "Player with X won "); 
       System.exit(0); 
      } 




      if(me.getSource() == jl[i]) 
      { 

       if(jl[i].getIcon() == ox[0] || jl[i].getIcon() == ox[1]) 
       { 
        JOptionPane.showMessageDialog(null, 
          "You can't insert at " + (i + 1) + " the place is already taken "); 
        break; 
       } 


       if(isFirst) 
       { 
        jl[i].setIcon(ox[0]); 
       } 
       else 
       { 
        jl[i].setIcon(ox[1]); 
       } 
      } 
     } 



    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseEntered(MouseEvent e) 
    { 
     if((jl[0].getIcon() == ox[0] || jl[0].getIcon() == ox[1]) && (jl[1].getIcon() == ox[0] || jl[1].getIcon() == ox[1]) && 
       (jl[2].getIcon() == ox[0] || jl[2].getIcon() == ox[1]) && (jl[3].getIcon() == ox[0] || jl[3].getIcon() == ox[1]) && 
        (jl[4].getIcon() == ox[0] || jl[4].getIcon() == ox[1]) && (jl[5].getIcon() == ox[0] || jl[5].getIcon() == ox[1]) && 
        (jl[6].getIcon() == ox[0] || jl[6].getIcon() == ox[1]) && (jl[7].getIcon() == ox[0] || jl[7].getIcon() == ox[1]) 
        && (jl[8].getIcon() == ox[0] || jl[8].getIcon() == ox[1])) 
      { 

        JOptionPane.showMessageDialog(null, 
          "Draw"); 
        System.exit(0); 

      } 

    } 

    @Override 
    public void mouseExited(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 

} 

/** The tester class with the main method and the frame 
*/ 
import javax.swing.JFrame; 
public class TestTicTacToe 
{ 
    public static void main(String[] args) 
    { 
//  JFrame application = new JFrame(); 
     TicTacToe frame = new TicTacToe(); 
     frame.setLocationByPlatform(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(400,400); 
     frame.setVisible(true); 
    } 

} 
+4

http://codereview.stackexchange.com에 더 적합 – nullpotent

+0

나는이 사이트에 대해 몰랐다. –

답변

2

상태로 아이콘을 사용하지 마십시오! 필드 (해당 배열이 비어있는 경우 보드에 대한 적절한 (숫자) 모델과 필드에 대한 마크, 플레이어가 필드를 클릭 그렇다면

int[] board = new int[3][3]; 
final static int X_MARK = 1; 
final static int O_MARK = -1; 

같은

  1. 검사를 사용하여 셀이 값 0을 가짐)
  2. 셀 값을 1 또는 -1로 변경
  3. 우승자 또는 추첨자가 있는지 계산합니다 (합계가 3 또는 -3 인 경우 단순히 행, 열 및 대각선의 합계를 계산 함). , 그렇다면 우승자가 있고 그렇지 않은 필드가 0이 아니면 우리는 무승부가됩니다)

그런 다음 모델을 사용하여 보드보기를 업데이트하십시오.