No older revisions available
No older revisions available
~cpp
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class FirstJava extends JFrame {
int count = 0;
int board[][] = new int[3][3];
public FirstJava() {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
board[i][j] = 0;
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("x 좌표 : " + x);
System.out.println("y 좌표 : " + y);
if (x > 100 && y > 100 && x < 200 && y < 200
&& board[0][0] == 0) {
if (count % 2 == 0)
board[0][0] = 1;
else
board[0][0] = 2;
count++;
} else if (x > 200 && y > 100 && x < 300 && y < 200
&& board[0][1] == 0) {
if (count % 2 == 0)
board[0][1] = 1;
else
board[0][1] = 2;
count++;
} else if (x > 300 && y > 100 && x < 400 && y < 200
&& board[0][2] == 0) {
if (count % 2 == 0)
board[0][2] = 1;
else
board[0][2] = 2;
count++;
} else if (x > 100 && y > 200 && x < 200 && y < 300
&& board[1][0] == 0) {
if (count % 2 == 0)
board[1][0] = 1;
else
board[1][0] = 2;
count++;
} else if (x > 200 && y > 200 && x < 300 && y < 300
&& board[1][1] == 0) {
if (count % 2 == 0)
board[1][1] = 1;
else
board[1][1] = 2;
count++;
} else if (x > 300 && y > 200 && x < 400 && y < 300
&& board[1][2] == 0) {
if (count % 2 == 0)
board[1][2] = 1;
else
board[1][2] = 2;
count++;
} else if (x > 100 && y > 300 && x < 200 && y < 400
&& board[2][0] == 0) {
if (count % 2 == 0)
board[2][0] = 1;
else
board[2][0] = 2;
count++;
} else if (x > 200 && y > 300 && x < 300 && y < 400
&& board[2][1] == 0) {
if (count % 2 == 0)
board[2][1] = 1;
else
board[2][1] = 2;
count++;
} else if (x > 300 && y > 300 && x < 400 && y < 400
&& board[2][2] == 0) {
if (count % 2 == 0)
board[2][2] = 1;
else
board[2][2] = 2;
count++;
}
repaint();
for (int i = 0; i < 3; i++) // 가로 승리 조건
{
if (board[i][0] == 1 && board[i][1] == 1
&& board[i][2] == 1)
{
JOptionPane.showMessageDialog(FirstJava.this, "흑 승리 ");
for (int k = 0; k < 3; k++) {
for (int j = 0; j < 3; j++) {
board[k][j] = 0;
}
}
} else if (board[i][0] == 2 && board[i][1] == 2
&& board[i][2] == 2)
{
JOptionPane.showMessageDialog(FirstJava.this, "백 승리 ");
for (int k = 0; k < 3; k++)
for (int j = 0; j < 3; j++)
board[k][j] = 0;
}
}
for (int i = 0; i < 3; i++) // 세로 승리 조건
{
if (board[0][i] == 1 && board[1][i] == 1
&& board[2][i] == 1)
{
JOptionPane.showMessageDialog(FirstJava.this, "흑 승리 ");
for (int k = 0; k < 3; k++)
for (int j = 0; j < 3; j++)
board[k][j] = 0;
} else if (board[0][i] == 2 && board[1][i] == 2
&& board[2][i] == 2)
{
JOptionPane.showMessageDialog(FirstJava.this, "백 승리 ");
for (int k = 0; k < 3; k++)
for (int j = 0; j < 3; j++)
board[k][j] = 0;
}
}
if (board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1)
{
JOptionPane.showMessageDialog(FirstJava.this, "흑 승리 ");
for (int k = 0; k < 3; k++)
for (int j = 0; j < 3; j++)
board[k][j] = 0;
} else if (board[0][0] == 2 && board[1][1] == 2
&& board[2][2] == 2)
{
JOptionPane.showMessageDialog(FirstJava.this, "백 승리 ");
for (int k = 0; k < 3; k++)
for (int j = 0; j < 3; j++)
board[k][j] = 0;
}
if (board[0][2] == 1 && board[1][1] == 1 && board[2][0] == 1)
{
JOptionPane.showMessageDialog(FirstJava.this, "흑 승리 ");
for (int k = 0; k < 3; k++)
for (int j = 0; j < 3; j++)
board[k][j] = 0;
} else if (board[0][2] == 2 && board[1][1] == 2
&& board[2][0] == 2)
{
JOptionPane.showMessageDialog(FirstJava.this, "백 승리 ");
for (int k = 0; k < 3; k++)
for (int j = 0; j < 3; j++)
board[k][j] = 0;
}
repaint();
}
});
}
public static void main(String args[]) {
FirstJava helloWorld = new FirstJava();
helloWorld.setBounds(200, 200, 500, 500);
helloWorld.show();
}
public void paint(Graphics g) {
Dimension size = getSize();
Color oldColor = g.getColor();
g.setColor(Color.WHITE);
g.fillRect(0,0,size.width, size.height);
g.setColor(oldColor);
for (int i = 1; i < 4; i++)
for (int j = 1; j < 4; j++)
g.drawRect(100 * j, 100 * i, 100, 100);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == 1)
g.fillOval(120 + 100 * j, 120 + 100 * i, 60, 60);
else if (board[i][j] == 2)
g.drawOval(120 + 100 * j, 120 + 100 * i, 60, 60);
}
System.out.println("painted.." + board[0][0]);
}
}
}