package go;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Gomoku implements Runnable {
public static void main(String args[]){
ChessFrame cf = new ChessFrame();
cf.show();
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
/*
*类ChessFrame
**/
class ChessFrame extends JFrame implements ActionListener {
public static boolean iscomputer=true,checkcomputer=true;
private int width,height;
private ChessModel cm;
private MainPanel mp;
static boolean startflag=false;
static boolean machinefirst=true;
//
public ChessFrame() {
this.setTitle("Gomoku");
cm=new ChessModel(1);
//cm.setisOdd(false);
mp=new MainPanel(cm);
Container con=this.getContentPane();
con.add(mp,"Center");
this.setResizable(false);
this.addWindowListener(new ChessWindowEvent());
MapSize(cm.getWidth(),cm.getHeight());
SwingUtilities.updateComponentTreeUI(this);
JMenuBar mbar = new JMenuBar();
this.setJMenuBar(mbar);
JMenu startMenu = new JMenu("Start");
mbar.add(makeMenu(startMenu, new Object[] {
"Start"
}, this));
JMenu ruleMenu = new JMenu("Rule");
mbar.add(makeMenu(ruleMenu, new Object[] {
"Rule"
}, this));
JMenu quitMenu = new JMenu("Quit");
mbar.add(makeMenu(quitMenu, new Object[] {
"Quit"
}, this));
}
//menu
public JMenu makeMenu(Object parent, Object items[], Object target){
JMenu m = null;
if(parent instanceof JMenu)
m = (JMenu)parent;
else if(parent instanceof String)
m = new JMenu((String)parent);
else
return null;
for(int i = 0; i < items.length; i++)
if(items[i] == null)
m.addSeparator();
else
m.add(makeMenuItem(items[i], target));
return m;
}
//menu
public JMenuItem makeMenuItem(Object item, Object target){
JMenuItem r = null;
if(item instanceof String)
r = new JMenuItem((String)item);
else if(item instanceof JMenuItem)
r = (JMenuItem)item;
else
return null;
if(target instanceof ActionListener)
r.addActionListener((ActionListener)target);
return r;
}
public void MapSize(int w,int h){
setSize(w * 20+50 , h * 20+100 );
if(this.checkcomputer)
this.iscomputer=true;
else
this.iscomputer=false;
mp.setModel(cm);
mp.repaint();
}
public boolean getiscomputer(){
return this.iscomputer;
}
void initial()
{
mp.MachinePlay();
};
public void restart(){
int modeChess = cm.getModeChess();
if(modeChess <= 3 && modeChess >= 1){
cm = new ChessModel(modeChess);
MapSize(cm.getWidth(),cm.getHeight());
}else{
System.out.println("\u81EA\u5B9A\u4E49");
}
mp.TimeInit();
}
public void actionPerformed(ActionEvent e){
String arg=e.getActionCommand();
if(arg.equals("Start")){
if(System.currentTimeMillis()%2==1)
{
System.out.println("奇数");
machinefirst=true;
JOptionPane.showMessageDialog(this, "you are white","black or white", 0);
}
else
{
System.out.println("偶数");
JOptionPane.showMessageDialog(this,"you are black","black or white", 0);
machinefirst=false;
}
startflag=true;
restart();
}
if(arg.equals("Rule"))
{
//以下为读文件操作
InputStream in = null ;
try
{ File f_ = new File("E:\\rule.txt") ;
in = new FileInputStream(f_) ;
}
catch (FileNotFoundException e3)
{
e3.printStackTrace();
}
// 开辟一个空间用于接收文件读进来的数据
byte b1[] = new byte[1024] ;
int i = 0 ;
try
{
// 将b1的引用传递到read()方法之中,同时此方法返回读入数据的个数
i = in.read(b1) ;
}
catch (IOException e4)
{
e4.printStackTrace();
}
//将byte数组转换为字符串输出
JOptionPane.showMessageDialog(this, new String(b1,0,i), "Rule", 0);
}
if(arg.equals("Quit"))
System.exit(0);
restart();
}
}
/*
*类ChessModel the main part
*/
class ChessModel {
static int BoardDimension=15;//the n
static int ChainM=5;//the m
//ChessBoard width and height
private int width,height,modeChess;
//ChessBoard coordinate
private int x=0,y=0;
//the information of pieces in the board
//arrMapShowwith value:1,2,3,-5,
//1,Black
//2,White
//3,Unoccupied
//-5,cannot
private int[][] arrMapShow;
//balck or white palyer's turn ??
private boolean isOdd,isExist;
public ChessModel() {}
//Chess Board dimension
public ChessModel(int modeChess){
this.isOdd=true;
PanelInit(BoardDimension, BoardDimension, modeChess);
}
private void PanelInit(int width, int height, int modeChess){
this.width = width;
this.height = height;
this.modeChess = modeChess;
arrMapShow = new int[width+1][height+1];
for(int i = 0; i <= width; i++){
for(int j = 0; j <= height; j++){
arrMapShow[i][j] = -5;
}
}
}
//when other class need isOdd
public boolean getisOdd(){
return this.isOdd;
}
//change the state
public void setisOdd(boolean isodd){
if(isodd)
this.isOdd=true;
else
this.isOdd=false;
}
//unoccupied?
public boolean getisExist(){
return this.isExist;
}
//board width
public int getWidth(){
return this.width;
}
//board height
public int getHeight(){
return this.height;
}
//the mode of board
public int getModeChess(){
return this.modeChess;
}
//get information of pieces in the board
public int[][] getarrMapShow(){
return arrMapShow;
}
//OUTside the board??
private boolean badxy(int x, int y){
if(x >= width+20 || x < 0)
return true;
return y >= height+20 || y < 0;
}
public boolean chessExist(int i,int j){
if(this.arrMapShow[i][j]==1 || this.arrMapShow[i][j]==2)
return true;
return false;
}
//Unoccupied??
public void readyplay(int x,int y){
if(badxy(x,y))
return;
if (chessExist(x,y))
return;
this.arrMapShow[x][y]=3;
}
//place the piece
public void play(int x,int y){
if(badxy(x,y))
return;
if(chessExist(x,y)){
this.isExist=true;
return;
}else
this.isExist=false;
if(getisOdd()){
setisOdd(false);
this.arrMapShow[x][y]=1;
}else{
setisOdd(true);
this.arrMapShow[x][y]=2;
}
}
//machine think
/*
*count the number of pieces with same color around ,select the maximum
*
**/
public void computerDo(int width,int height){
int[] pos=new int[2];
System.out.println("机器在思考");
int max_black,max_white,max_temp,max=0;
setisOdd(true);
// System.out.println("machine place...");
pos=checkNull();
this.x=pos[0];
this.y=pos[1];
setX(this.x);
setY(this.y);
this.arrMapShow[this.x][this.y]=2;
}
//machine place x coordinate
public void setX(int x){
this.x=x;
}
//machine place y coordinate
public void setY(int y){
this.y=y;
}
//get,machine place x coordinate
public int getX(){
return this.x;
}
//get,machine place y coordinate
public int getY(){
return this.y;
}
//count the number of pieces in different direction ,and select the maximum
//
public int checkMax(int x, int y,int black_or_white){
int num=0,max_num,max_temp=0;
int x_temp=x,y_temp=y;
int x_temp1=x_temp,y_temp1=y_temp;
//judge right
for(int i=1;i<ChainM;i++)