U E D R , A S I H C RSS

Tic Tac Toe/박능규,황재선

import java.awt.Graphics;
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
 
import javax.swing.*;
 
public class TicTacToe extends JFrame{
				
		int isPainted[] = new int[9];
		
		int x;
		int y;
		int count = 1;
		public TicTacToe() 
		{
			for (int i=0 ; i<9 ; i++)
				isPainted[i] = 0;
			
			addMouseListener(new MouseAdapter() {  
			public void mouseClicked(MouseEvent e) { 
				x = e.getX(); 
				y = e.getY();
				repaint();
				
				System.out.println("x 좌표 : " + x); 
				System.out.println("y 좌표 : " + y); 
			} 
 			}); 
		} 
		public static void main(String args[]) { 
			TicTacToe pan = new TicTacToe(); 
			pan.setBounds(500,300,250,250); 
			pan.show();
                 
		}
		public void paint(Graphics g) 
		{
			g.drawRect(50,50,150,150);
			g.drawLine(100, 50, 100, 200);
			g.drawLine(150, 50, 150, 200);
			g.drawLine(50, 100, 200, 100);
			g.drawLine(50, 150, 200, 150);
			
			if(x>50 && x<100 && y>50 && y<100)
			{
				if (isPainted[0] == 0)
				{			
					if(count%2 == 1)
					{
					
						g.drawOval(55,55,40,40);
						isPainted[0] = 1;
					}						
					else
					{
						g.fillOval(55,55,40,40);
						isPainted[0] = 2;
					}
					count++;
					
				}									
			}
			else if(x>100 && x<150 && y>50 && y<100)
			{
				if (isPainted[1] == 0)
				{			
					if(count%2 == 1)
					{
						isPainted[1]= 1;
						g.drawOval(105,55,40,40);
					}
					else
					{
						g.fillOval(105,55,40,40);
						isPainted[1]= 2;
					}
					count++;
				}									
			}
			else if(x>150 && x<200 && y>50 && y<100)
			{
				if (isPainted[2] == 0)
				{			
					if(count%2 == 1)
					{
						isPainted[2]= 1;
						g.drawOval(155,55,40,40);
					}
					else
					{
						isPainted[2]= 2;
						g.fillOval(155,55,40,40);
					}
					count++;
					
				}									
			}
			else if(x>50 && x<100 && y>100 && y<150)
			{
				if (isPainted[3] == 0)
				{			
					if(count%2 == 1)
					{
						isPainted[3]= 1;
					
						g.drawOval(55,105,40,40);
					}
					else
					{
						isPainted[3]= 2;
						g.fillOval(55,105,40,40);
					}						
					count++;
					
				}									
			}
			else if(x>100 && x<150 && y>100 && y<150)
			{
				if (isPainted[4] == 0)
				{			
					if(count%2 == 1)
					{
						isPainted[4]= 1;
						g.drawOval(105,105,40,40);
					}						
					else
					{
						isPainted[4]= 2;					
						g.fillOval(105,105,40,40);
					}
					count++;
					
				}									
			}
			else if(x>150 && x<200 && y>100 && y<150)
			{
				if (isPainted[5] == 0)
				{			
					if(count%2 == 1)
					{
						isPainted[5]= 1;
						g.drawOval(155,105,40,40);						
					}						
					else
					{
						isPainted[5]= 2;
						g.fillOval(155,105,40,40);
					}					
					count++;
					
				}									
			}
			else if(x>50 && x<100 && y>150 && y<200)
			{
				if (isPainted[6] == 0)
				{			
					if(count%2 == 1)
					{
						isPainted[6]= 1;
						g.drawOval(55,155,40,40);
					}						
					else
					{
						isPainted[6]= 2;
						g.fillOval(55,155,40,40);
					}						
					count++;
					
				}									
			}
			else if(x>100 && x<150 && y>150 && y<200)
			{
				if (isPainted[7] == 0)
				{			
					if(count%2 == 1)
					{
						isPainted[7]= 1;
						g.drawOval(105,155,40,40);
					}						
					else
					{
						isPainted[7]= 2;
						g.fillOval(105,155,40,40);
					}						
					count++;					
				}									
			}
			else if(x>150 && x<200 && y>150 && y<200)
			{
				if (isPainted[8] == 0) 
				{			
					if(count%2 == 1)
					{
						isPainted[8]= 1;
						g.drawOval(155,155,40,40);
					}
						
					else
					{
						isPainted[8]= 2;
						g.fillOval(155,155,40,40);
					}						
					count++;					
				}									
			}
			gameOver();
			
		}
		void gameOver()
		{
			if(isPainted[0]==isPainted[1] && isPainted[1]==isPainted[2] &&
				isPainted[0]==isPainted[2])				
					whoWin(isPainted[0]);							
			else if(isPainted[3]==isPainted[4] && isPainted[4]==isPainted[5] &&
				isPainted[3]==isPainted[5])
					whoWin(isPainted[3]);										
			else if(isPainted[6]==isPainted[7] && isPainted[7]==isPainted[8] &&
				isPainted[6]==isPainted[8])
				{
					if (isPainted[6] == 1)
						JOptionPane.showMessageDialog(null, "백 승", "Game Over",
						JOptionPane.INFORMATION_MESSAGE);						
					else if (isPainted[6] == 2)
						JOptionPane.showMessageDialog(null, "흑 승", "Game Over",
						JOptionPane.INFORMATION_MESSAGE);	 
				}		
			if(isPainted[0]==isPainted[3] && isPainted[3]==isPainted[6] &&
				isPainted[0]==isPainted[6])
				{
					if (isPainted[0] == 1)
						JOptionPane.showMessageDialog(null, "백 승", "Game Over",
						JOptionPane.INFORMATION_MESSAGE);						
					else if (isPainted[0] == 2)
						JOptionPane.showMessageDialog(null, "흑 승", "Game Over",
						JOptionPane.INFORMATION_MESSAGE);	 
				}			
			if(isPainted[1]==isPainted[4] && isPainted[4]==isPainted[7] &&
				isPainted[1]==isPainted[7])
				{
					if (isPainted[1] == 1)
						JOptionPane.showMessageDialog(null, "백 승", "Game Over",
						JOptionPane.INFORMATION_MESSAGE);						
					else if (isPainted[1] == 2)
						JOptionPane.showMessageDialog(null, "흑 승", "Game Over",
						JOptionPane.INFORMATION_MESSAGE);	 
				}			
			if(isPainted[2]==isPainted[5] && isPainted[5]==isPainted[8] &&
				isPainted[2]==isPainted[8])
				{
					if (isPainted[2] == 1)
						JOptionPane.showMessageDialog(null, "백 승", "Game Over",
						JOptionPane.INFORMATION_MESSAGE);						
					else if (isPainted[2] == 2)
						JOptionPane.showMessageDialog(null, "흑 승", "Game Over",
						JOptionPane.INFORMATION_MESSAGE);	 
				}			
			if(isPainted[0]==isPainted[4] && isPainted[4]==isPainted[8] &&
				isPainted[0]==isPainted[8])
				{
					if (isPainted[0] == 1)
						JOptionPane.showMessageDialog(null, "백 승", "Game Over",
						JOptionPane.INFORMATION_MESSAGE);						
					else if (isPainted[0] == 2)
						JOptionPane.showMessageDialog(null, "흑 승", "Game Over",
						JOptionPane.INFORMATION_MESSAGE);	 
				}			
			if(isPainted[2]==isPainted[4] && isPainted[4]==isPainted[6] &&
				isPainted[2]==isPainted[6])
				{
					if (isPainted[2] == 1)
						JOptionPane.showMessageDialog(null, "백 승", "Game Over",
						JOptionPane.INFORMATION_MESSAGE);						
					else if (isPainted[2] == 2)
						JOptionPane.showMessageDialog(null, "흑 승", "Game Over",
						JOptionPane.INFORMATION_MESSAGE);	 
				}			

		}
		public void whoWin(int aIsPainted){
			if (aIsPainted == 1)
				JOptionPane.showMessageDialog(null, "백 승", "Game Over",
				JOptionPane.INFORMATION_MESSAGE);						
			else if (aIsPainted == 2)
				JOptionPane.showMessageDialog(null, "흑 승", "Game Over",
				JOptionPane.INFORMATION_MESSAGE); 

		}
		  
		

} 



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