package panel;
import java.awt.Color;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class Config {
private static int port;
/**
* ¾à×ó±ß¿ò¾àÀë
*/
public static final int STARTX = 50;
/**
* ¾ÝÉϱ߿ò¾àÀë
*/
public static final int STARTY = 90;
/**
* ¼ä¸ô
*/
public static int SIZE = 50;
/**
* ÐÐÊý
*/
public static int ROW = 10;
/**
* ÁÐÊý
*/
public static int CLOUNM = 10;
private static Color color = Color.BLACK;
private static byte[][] chess ;
private static int[] notify = { -1, -1 };
static {
try {
File f = new File("Config.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("CONFIG");
// SIZE=Integer.parseInt(doc.getElementsByTagName("SIZE").item(0).getFirstChild().getNodeValue()));
for (int i = 0; i < nl.getLength(); i++) {
SIZE = Integer.parseInt(doc.getElementsByTagName("SIZE")
.item(i).getFirstChild().getNodeValue());
ROW = Integer.parseInt(doc.getElementsByTagName("ROW").item(i)
.getFirstChild().getNodeValue());
CLOUNM = Integer.parseInt(doc.getElementsByTagName("CLOUNM")
.item(i).getFirstChild().getNodeValue());
// System.out.println(SIZE+" "+ROW+ " "+CLOUNM);
}
chess = new byte[ROW + 1][CLOUNM + 1];
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void setX(int x) {
notify[0] = x;
}
public static int getX() {
return notify[0];
}
public static void setY(int y) {
notify[1] = y;
}
public static int getY() {
return notify[1];
}
public static void clearNotify() {
notify[0] = notify[1] = -1;
}
/**
* ÉèÖÃÆå×ÓÑÕÉ«
*/
public static void setColor(Color cr) {
color = cr;
}
public static void exchangeColor(){
color=(color==Color.BLACK)?Color.WHITE:Color.BLACK;
}
/**
* »ñµÃÆå×ÓÑÕÉ«
*/
public static Color getColor() {
return color;
}
/**
* ÅжÏÊó±êËùµã»÷λÖÃÊÇ·ñ¿ÉÏÂÆå×Ó
*/
public static boolean isChessMan(int x, int y, Color color) {
int j = (x - STARTX) / SIZE;
int i = (y - STARTY) / SIZE;
if (chess[i][j] != 0)
return false;
else {
chess[i][j] = (color == Color.BLACK) ? (byte) 1 : -1;
return true;
}
}
/**
* ÅжÏÊó±êËùµã»÷×ø±êÊÇ·ñ¿ÉÏÂÆå×Ó
*/
public static boolean isChessMan(byte j, byte i, Color color) {
if (chess[i][j] != 0)
return false;
else {
chess[i][j] = (color == Color.BLACK) ? (byte) 1 : -1;
return true;
}
}
/**
* ÖØÖÃÆåÅÌ
*/
public static void resetChess(byte[][] buf) {
chess = buf;
}
/**
* ÅжÏÊÇ·ñʤÀû
*/
public static boolean isWin(Color color) {
boolean flag = false;
int n = 0;
int c = (color == Color.BLACK) ? 1 : -1;
// ÅжÏÁÐ
for (int i = 0; i <= ROW; i++) {
for (int j = 0; j <= CLOUNM; j++) {
if (chess[i][j] == c && flag == false) {
n = 1;
flag = true;
} else if (chess[i][j] == c && flag == true) {
n++;
} else if (chess[i][j] != c) {
n = 0;
flag = false;
}
if (5 == n) {
n = 0;
flag = false;
return true;
}
}
}
n = 0;
flag = false;
// ÅжÏÐÐ
for (int j = 0; j <= CLOUNM; j++) {
for (int i = 0; i <= ROW; i++) {
if (chess[i][j] == c && flag == false) {
n = 1;
flag = true;
} else if (chess[i][j] == c && flag == true) {
n++;
} else if (chess[i][j] != c) {
n = 0;
flag = false;
}
if (5 == n) {
n = 0;
flag = false;
return true;
}
}
}
n = 0;
flag = false;
// ÅжÏ×óб
for (int i = 0; i <= ROW; i++) {
for (int j = 0; j <= CLOUNM; j++) {
for (int ii = i, jj = j; ii <= ROW && jj >= 0; ii++, jj--) {
if (chess[ii][jj] == c && flag == false) {
n = 1;
flag = true;
} else if (chess[ii][jj] == c && flag == true) {
n++;
} else if (chess[ii][jj] != c) {
n = 0;
flag = false;
}
if (5 == n) {
n = 0;
flag = false;
return true;
}
}
}
}
n = 0;
flag = false;
// ÅжÏÓÒб
for (int i = 0; i <= ROW; i++) {
for (int j = 0; j <= CLOUNM; j++) {
for (int ii = i, jj = j; jj <= CLOUNM && ii <= ROW; ii++, jj++) {
if (chess[ii][jj] == c && flag == false) {
n = 1;
flag = true;
} else if (chess[ii][jj] == c && flag == true) {
n++;
} else if (chess[ii][jj] != c) {
n = 0;
flag = false;
}
if (5 == n) {
n = 0;
flag = false;
return true;
}
}
}
}
return false;
}
/**
* ÉèÖö˿ںÅ
*/
public static void setPort(int p) {
port = p;
// System.out.println(port);
}
/**
* »ñÈ¡¶Ë¿ÚºÅ
*/
public static int getPort() {
return port;
}
/**
* Çå¿ÕÆå×Ó
*/
public static void clearChess() {
chess = new byte[ROW + 1][CLOUNM + 1];
}
/**
* »ñµÃÆå×Ó
*/
public static byte[][] getChess() {
return chess;
}
}