U E D R , A S I H C RSS

Tic Tac Toe/김홍선

Tic Tac Toe 하는중;;;;

~cpp 
import java.awt.Graphics; 
import javax.swing.*; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
 
public class FirstJava extends JFrame{
        int board[][];
        int x,y;
        int z[];
        int count =1 ;
        int i;
        int a,b,winner;
        public FirstJava() 
        { 
			board =new int [6][6];
			z = new int [9];
			
			
        	addMouseListener(new MouseAdapter() {  
                public void mouseClicked(MouseEvent e) { 
                   	x = e.getX(); 
                    y = e.getY();
                    check();
                    win();
                    repaint();
                    
                    System.out.println("승자는 " + winner + "입니다");
                    //getGraphics().drawRect(0,0,900,900);
                    if(count==1)
                    	count=2;
                    else count=1;
                    
                    for(int w=0;w<3;w++)
                    for(int q=0;q<3;q++)
                    board[q][w]=0;
                    repaint();
                    
                }
        	});
        } 
        public static void main(String args[]) { 
                FirstJava helloWorld = new FirstJava(); 
                helloWorld.setBounds(100,100,500,500); 
                helloWorld.show(); 
                 
                 
        } 
        public void check()
        {
        	if( 100< x && x <200 && 100< y && y <200 )
        	{x=100;y=100;  i=0;    board[0][0]=1;
        	if(count==2)
        		board[0][0]=2;}
        	if( 200< x && x <300 && 100< y && y <200 ){        		
        	  		x=200;y=100;i=1;board[1][0]=1;
                	if(count==2)
                		board[1][0]=2;  }
        	if( 300< x && x <400 && 100< y && y <200 ){
        		x=300;y=100;i=2;board[2][0]=1;
            	if(count==2)
            		board[2][0]=2;  }
        	if( 100< x && x <200 && 200< y && y <300 ){
            	x=100;y=200;i=3;board[0][1]=1;
            	if(count==2)
            		board[0][1]=2; }
        	if( 200< x && x <300 && 200< y && y <300  ){
            	x=200;y=200;i=4;board[1][1]=1;
            	if(count==2)
            		board[1][1]=2;  }
        	if( 300< x && x <400 && 200< y && y <300  ){
            	x=300;y=200;i=5;board[2][1]=1;
            	if(count==2)
            		board[2][1]=2;  }
        	if( 100< x && x <200 && 300< y && y <400 ){
            	x=100;y=300;i=6;board[0][2]=1;
            	if(count==2)
            		board[0][2]=2;  }
        	if( 200< x && x <300 && 300< y && y <400 ){
            	x=200;y=300;i=7;board[1][2]=1;
            	if(count==2)
            		board[1][2]=2;  }
        	if(300< x && x <400 && 300< y && y <400 ){
            	x=300;y=300;i=8;board[2][2]=1;
            	if(count==2)
            		board[2][2]=2;  }
        	        	        		
        }
        public void win()
        {
        	for(b=0;b<3;b++){
        		for(a=0;a<3;a++)
        	if((board[a][b]==1 || board[a][b]==2) && board[a][b]==board[a+1][b]&& board[a][b]==board[a+2][b])
        		winner=2;
        		if(count==1)
        			winner=1;
          	if((board[a][b]==1 || board[a][b]==2) && board[a][b]==board[a][b+1]&& board[a][b] ==board[a][b+2])
          		winner=2;
          		if(count==1)
        			winner=1;
          	}
        		
        	if((board[a][b]==1 || board[a][b]==2) && board[a][b]==board[a+1][b+1]&& board[a][b]==board[a+2][b+2])
        		winner=2;
        		if(count==1)
        			winner=1;
        		
        	if((board[a][b]==1 || board[a][b]==2) && board[a][b]==board[a+1][b+1]&& board[a][b]==board[a+2][b+2])
        		winner=2;
        		if(count==1)
        			winner=1;
        		
        	if((board[a][b]==1 || board[a][b]==2) && board[a][b]==board[a-1][b-1]&& board[a][b]==board[a-2][b-2])
        		winner=2;
        		if(count==1)
        			winner=1;
        		
        		
        }
        public void paint(Graphics g) 
        {
        	if(board[0][0]==1||board[0][0]==2){
        		x=100;y=100;        	}
        	if(board[1][0]==1||board[1][0]==2){
        		x=200;y=100;        	}
        	if(board[2][0]==1||board[2][0]==2){
        		x=300;y=100;        	}
        	if(board[0][1]==1||board[0][1]==2){
        		x=100;y=200;        	}
        	if(board[1][1]==1||board[1][1]==2){
        		x=200;y=200;        	}
        	if(board[2][1]==1||board[2][1]==2){
        		x=300;y=200;        	}
        	if(board[0][2]==1||board[0][2]==2){
        		x=100;y=300;        	}
        	if(board[1][2]==1||board[1][2]==2){
        		x=100;y=300;        	}
        	if(board[2][2]==1||board[2][2]==2){
        		x=100;y=300;        	}
        	  		
        			
        		
        	
        	for(int r=0;r<3;r++){
        	for(int e=0;e<3;e++)
        	if(board[e][r]==1)
        		g.drawOval(x,y,100,100);
        	else if(board[e][r]==2)
        		g.fillOval(x,y,100,100);
        	}
           	g.drawLine(100,200,400,200); 
            g.drawLine(100,300,400,300);
            g.drawLine(200,100,200,400);
            g.drawLine(300,100,300,400);
        } 
} 

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