assets.zip
// 참고한 html 소스 파일 및 이미지 파일도 첨부합니다.
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class FrnOmok extends JFrame {
private JPanel contentPane;
ImageIcon ii0 = new ImageIcon("C:\\Users\\\uCEF4\uD4E8\uD130\\Desktop\\eclipse\\0.jpg");
ImageIcon ii1 = new ImageIcon("C:\\Users\\\uCEF4\uD4E8\uD130\\Desktop\\eclipse\\1.jpg");
ImageIcon ii2 = new ImageIcon("C:\\Users\\\uCEF4\uD4E8\uD130\\Desktop\\eclipse\\2.jpg");
ArrayList<JLabel> kans = new ArrayList<JLabel>();
boolean flag_turn = true;
int[][] arr_kan =
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FrnOmok frame = new FrnOmok();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FrnOmok() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 450);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
for(int i=0; i<100; i++){
JLabel kan = new JLabel("Hello");
kan.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("hello Daejeon University");
JLabel temp = (JLabel) e.getComponent();
onclickkan(temp);
System.out.println(temp.getText());
}
});
int kunbang = i/10;
int jakunbang = i%10;
kan.setText(i+"");
kan.setIcon(ii0);
kan.setBounds(0+(jakunbang*40), 0+(kunbang*40), 40, 40);
contentPane.add(kan);
kans.add(kan);
}
fn_drawkans();
}
public void fn_drawkans() {
for (int i = 0; i < arr_kan.length; i++) {
for (int j = 0; j < arr_kan[i].length; j++) {
if(arr_kan[i][j] ==0) {
kans.get(i * 10 + j).setIcon(ii0);
}
if(arr_kan[i][j] ==1) {
kans.get(i * 10 + j).setIcon(ii1);
}
if(arr_kan[i][j] ==2) {
kans.get(i * 10 + j).setIcon(ii2);
}
}
}
}
public void onclickkan(JLabel obj) {
int decimal = Integer.parseInt(obj.getText());
int kunbang = decimal / 10;
int jakunbang = decimal % 10;
if (arr_kan[kunbang][jakunbang] > 0) {
return;
}
if (flag_turn) {
arr_kan[kunbang][jakunbang] = 1;
} else {
arr_kan[kunbang][jakunbang] = 2;
}
fn_drawkans();
int cntUp = getUp(kunbang, jakunbang, flag_turn);
int cntDown = getDown(kunbang, jakunbang, flag_turn);
int cntIn = getIn(kunbang, jakunbang, flag_turn);
int cntOut = getOut(kunbang, jakunbang, flag_turn);
int cntInUp = getInUp(kunbang, jakunbang, flag_turn);
int cntOutUp = getOutUp(kunbang, jakunbang, flag_turn);
int cntOutDown = getOutDown(kunbang, jakunbang, flag_turn);
int cntInDown = getInDown(kunbang, jakunbang, flag_turn);
ArrayList<Integer> arr = new ArrayList<Integer>();
// var arr = new Array();
arr.add(cntUp + cntDown + 1);
arr.add(cntIn + cntOut + 1);
arr.add(cntInUp + cntOutDown + 1);
arr.add(cntOutUp + cntInDown + 1);
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i) == 5) {
if (flag_turn) {
System.out.println("백돌이 이겼습니다.(you win!)");
return;
}
}
}
System.out.println("cntUp:" + cntUp);
System.out.println("cntDown:" + cntDown);
System.out.println("cntIn:" + cntIn);
System.out.println("cntOut:" + cntOut);
System.out.println("cntInUp:" + cntInUp);
System.out.println("cntOutUp:" + cntOutUp);
System.out.println("cntOutDown:" + cntOutDown);
System.out.println("cntInDown:" + cntInDown);
System.out.println("");
com_ai();
}
public int getUp(int kunbang, int jakunbang, boolean flag_turn) {
int flag_count = 0;
if (flag_turn) {
flag_count = 1;
} else {
flag_count = 2;
}
int total_cnt = 0;
while (true) {
kunbang = kunbang - 1;
try {
if (flag_count == arr_kan[kunbang][jakunbang]) {
total_cnt++;
} else {
break;
}
} catch (Exception e) {
return total_cnt;
}
}
return total_cnt;
}
public int getDown(int kunbang, int jakunbang, boolean flag_turn) {
int flag_count = 0;
if (flag_turn) {
flag_count = 1;
} else {
flag_count = 2;
}
int total_cnt = 0;
while (true) {
kunbang = kunbang + 1;
try {
if (flag_count == arr_kan[kunbang][jakunbang]) {
total_cnt++;
} else {
break;
}
} catch (Exception e) {
return total_cnt;
}
}
return total_cnt;
}
public int getIn(int kunbang, int jakunbang, boolean flag_turn) {
int flag_count = 0;
if (flag_turn) {
flag_count = 1;
} else {
flag_count = 2;
}
int total_cnt = 0;
while (true) {
kunbang = kunbang - 1;
jakunbang = jakunbang - 1;
try {
if (flag_count == arr_kan[kunbang][jakunbang]) {
total_cnt++;
} else {
break;
}
} catch (Exception e) {
return total_cnt;
}
}
return total_cnt;
}
public int getOut(int kunbang, int jakunbang, boolean flag_turn) {
int flag_count = 0;
if (flag_turn) {
flag_count = 1;
} else {
flag_count = 2;
}
int total_cnt = 0;
while (true) {
jakunbang = jakunbang + 1;
try {
if (flag_count == arr_kan[kunbang][jakunbang]) {
total_cnt++;
} else {
break;
}
} catch (Exception e) {
return total_cnt;
}
}
return total_cnt;
}
public int getInUp(int kunbang, int jakunbang, boolean flag_turn) {
int flag_count = 0;
if (flag_turn) {
flag_count = 1;
} else {
flag_count = 2;
}
int total_cnt = 0;
while (true) {
kunbang = kunbang - 1;
jakunbang = jakunbang - 1;
try {
if (flag_count == arr_kan[kunbang][jakunbang]) {
total_cnt++;
} else {
break;
}
} catch (Exception e) {
return total_cnt;
}
}
return total_cnt;
}
public int getOutUp(int kunbang, int jakunbang, boolean flag_turn) {
int flag_count = 0;
if (flag_turn) {
flag_count = 1;
} else {
flag_count = 2;
}
int total_cnt = 0;
while (true) {
kunbang = kunbang - 1;
jakunbang = jakunbang + 1;
try {
if (flag_count == arr_kan[kunbang][jakunbang]) {
total_cnt++;
} else {
break;
}
} catch (Exception e) {
return total_cnt;
}
}
return total_cnt;
}
public int getOutDown(int kunbang, int jakunbang, boolean flag_turn) {
int flag_count = 0;
if (flag_turn) {
flag_count = 1;
} else {
flag_count = 2;
}
int total_cnt = 0;
while (true) {
kunbang = kunbang + 1;
jakunbang = jakunbang + 1;
try {
if (flag_count == arr_kan[kunbang][jakunbang]) {
total_cnt++;
} else {
break;
}
} catch (Exception e) {
return total_cnt;
}
}
return total_cnt;
}
public int getInDown(int kunbang, int jakunbang, boolean flag_turn) {
int flag_count = 0;
if (flag_turn) {
flag_count = 1;
} else {
flag_count = 2;
}
int total_cnt = 0;
while (true) {
kunbang = kunbang + 1;
jakunbang = jakunbang - 1;
try {
if (flag_count == arr_kan[kunbang][jakunbang]) {
total_cnt++;
} else {
break;
}
} catch (Exception e) {
return total_cnt;
}
}
return total_cnt;
}
public void com_ai() {
int[][] arr_weight =
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
for (int i = 0; i < arr_weight.length; i++) {
for (int j = 0; j < arr_weight[i].length; j++) {
int cntUp = getUp(i, j, true);
int cntDown = getDown(i, j, true);
int cntIn = getIn(i, j, true);
int cntOut = getOut(i, j, true);
int cntInUp = getInUp(i, j, true);
int cntOutUp = getOutUp(i, j, true);
int cntOutDown = getOutDown(i, j, true);
int cntInDown = getInDown(i, j, true);
if(arr_kan[i][j]>0){
arr_weight[i][j] = -1;
} else {
arr_weight[i][j] = cntUp + cntDown + cntIn + cntOut +
cntInUp + cntOutUp + cntOutDown + cntInDown;
}
System.out.println(i + "." + j + "."+ arr_weight[i][j]);
}
}
int max=0;
for (int i = 0; i < arr_weight.length; i++) {
for (int j = 0; j < arr_weight[i].length; j++) {
if(max < arr_weight[i][j]){
max = arr_weight[i][j];
}
}
}
System.out.println("max:"+max);
int ii = 0;
int jj = 0;
for (int i = 0; i < arr_weight.length; i++) {
for (int j = 0; j < arr_weight[i].length; j++) {
if(max == arr_weight[i][j]){
ii = i;
jj = j;
}
}
}
System.out.println(ii+"."+jj);
arr_kan[ii][jj] = 2;
fn_drawkans();
int cntUp = getUp(ii, jj, false);
int cntDown = getDown(ii, jj, false);
int cntIn = getIn(ii, jj, false);
int cntOut = getOut(ii, jj, false);
int cntInUp = getInUp(ii, jj, false);
int cntOutUp = getOutUp(ii, jj, false);
int cntOutDown = getOutDown(ii, jj, false);
int cntInDown = getInDown(ii, jj, false);
ArrayList<Integer> arr = new ArrayList<Integer>();
// var arr = new Array();
arr.add(cntUp + cntDown + 1);
arr.add(cntIn + cntOut + 1);
arr.add(cntInUp + cntOutDown + 1);
arr.add(cntOutUp + cntInDown + 1);
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i) == 5) {
System.out.println("흑돌이 이겼습니다.(com win!)");
return;
}
}
}
}