U E D R , A S I H C RSS

Tic Tac Toe/조재화,신소영

~cpp 
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter.*;
import java.awt.event.MouseEvent.*;
import javax.swing.*;

public class FirstJava extends JFrame{
	int x,y;
	int counter=0;
	int array[][] = new int [3][3];
	
	
    public FirstJava() 
    {
    	for(int j=0;j<3; j++)
    	{
    		for(int i=0; i<3;i++)
    			array[i][j] = -1;
    	}
    	
    	addMouseListener(new MouseAdapter() {  
        public void mouseClicked(MouseEvent e) { 
            x = e.getX(); 
            y = e.getY(); 
             
            System.out.println("x 좌표 : " + x);
            
            System.out.println("y 좌표 : " + y); 
            repaint();
        } 

    }); 
    } 
    public static void main(String args[]) { 
            FirstJava helloWorld = new FirstJava(); 
            helloWorld.setBounds(100,100,600,600); 
            helloWorld.show();
            

    }
    public void paint(final Graphics g)
    {
    	g.drawLine(100,0,100,300);
    	g.drawLine(200,0,200,300);
       	g.drawLine(0,100,300,100);
    	g.drawLine(0,200,300,200);

    	
      	if (0<x&&x<100  &&  0<y&&y<100)
      	{
      		if(counter%2 == 0)
      			g.drawOval(25,25,50,50);
      		     		
      		else//(counter%2 == 1)
      			g.drawLine(25,25,75,75);
      			
      		array[0][0] = counter%2;
      		counter ++;
      	}
      	else if (100<x&&x<200  &&  0<y&&y<100)
        {
      		if(counter%2 == 0)
      			g.drawOval(125,25,50,50);
      	else//(counter%2 == 1)
      			g.drawLine(125,25,175,75);    
      		array[0][1] = counter%2;
      		counter ++;
      	}
      	else if (200<x&&x<300  &&  0<y&&y<100)
      	{
      		if(counter%2 == 0)
      			g.drawOval(225,25,50,50);
      		else//(counter%2 == 1)
      			g.drawLine(225,25,275,75);    
      		array[0][2] = counter%2;
      		counter ++;
      	}

      	else if (0<x&&x<100  &&  100<y&&y<200)
      	{
      		if(counter%2 == 0)
      			g.drawOval(25,125,50,50);
      		else//(counter%2 == 1)
      			g.drawLine(25,125,75,175);    
      		array[1][0] = counter%2;
      		counter ++;
      	}

      	else if (100<x&&x<200  &&  100<y&&y<200)
      	{
      		if(counter%2 == 0)
      			g.drawOval(125,125,50,50);
      		else//(counter%2 == 1)
      			g.drawLine(125,125,175,175);    
      		array[1][1] = counter%2;
      		counter ++;
      	}

      	else if (200<x&&x<300  &&  100<y&&y<200)
      	{
      		if(counter%2 == 0)
      			g.drawOval(225,125,50,50);
      		else//(counter%2 == 1)
      			g.drawLine(225,125,275,175);    
      		array[1][2] = counter%2;
      		counter ++;
      	}

      	else if (0<x&&x<100  &&  200<y&&y<300)
      	{
      		if(counter%2 == 0)
      			g.drawOval(25,225,50,50);
      		else//(counter%2 == 1)
      			g.drawLine(25,225,75,275);    
      		array[2][0] = counter%2;
      		counter ++;
      	}

      	else if (100<x&&x<200  && 200<y&&y<300)
      	{
      		if(counter%2 == 0)
      			g.drawOval(125,225,50,50);
      		else//(counter%2 == 1)
      			g.drawLine(125,225,175,275);    
      		array[2][1] = counter%2;
      		counter ++;
      	}

      	else if(200<x&&x<300  &&  200<y&&y<300)
      	{
      		if(counter%2 == 0)
      			g.drawOval(225,225,50,50);
      		else//(counter%2 == 1)
      			g.drawLine(225,225,275,275);    
      		array[2][2] = counter%2;
      		counter ++;
      	}
      	for(int i=0; i<3; i++)
      	{
      		for(int j=0; j<3; j++)
      			System.out.print(array[i][j]);
      		System.out.println();				
      	}
      	
      	//승리조건검사
      	
      	boolean flag = true;
      	
      	if(flag ==true)
      	{
	      	for(int i=0; i<3; i++)
	      	{
		      		
	      		if(array[i][0] == array[i][1] && array[i][1] == array[i][2] &&  array[i][0] != -1)
	      		{
	      			JOptionPane.showMessageDialog(null, Integer.toString(array[i][0])+"가 이겼습니다.!!");
	      			flag = false;
	      			break;
	      		}
	      	}
	      	for(int i=0; i<3; i++)
	      	{
		      		
	      		if(array[0][i] == array[1][i] && array[1][i] == array[2][i] &&  array[0][i] != -1)
	      		{
	      			JOptionPane.showMessageDialog(null, Integer.toString(array[0][i])+"가 이겼습니다.!!");
	      			flag = false;
	      			break;
	      		}
	      	}
      	}
        if(flag == true)
        {
	      	if(array[0][0] == array[1][1] && array[1][1] == array[2][2] &&  array[0][0] != -1)
	      		JOptionPane.showMessageDialog(null, Integer.toString(array[0][0])+"가 이겼습니다.!!");
	      	if(array[0][2] == array[1][1] && array[1][1] == array[2][0] &&  array[0][2] != -1)
	      		JOptionPane.showMessageDialog(null, Integer.toString(array[0][2])+"가 이겼습니다.!!");
	      	else if(counter == 9)
	      		JOptionPane.showMessageDialog(null, "0과 1은 비겼습니다.");
	      	flag = false;
        }
   	
    }
     
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:14
Processing time 0.0103 sec