?? othellopanel.java
字號:
// Othello 主類
import java.awt.*;
import java.applet.*;
import MySocket;
import ChatFrm;
import MessageBox;
import ImgButton;
import java.awt.event.*;
class OthelloPanel extends Panel
{
/* ***********************
My Parameter
**************************/
Applet applet;
int m_value[][];//=new int [9][9];
Image m_chessGif,m_backgdGif,m_swordGif,m_faceGif;
Image with_net[],with_loc[],with_com[];
int m_TempX,m_TempY; //棕色棋子(標志可以放子)位置
int m_side; //當前方
int m_step; //當前步數
int m_CurrentX,m_CurrentY; //當前位置
int m_chess[][]; //棋盤狀況
int m_TrunkMin ,m_BranchMin;
boolean m_TempPut; //是否有棕色子存在
boolean m_exit; //
int m_BackupX[]; //存儲每一步棋
int m_BackupY[];
int m_WhichSide;
boolean m_MePut; //是否該自己走棋
MySocket m_socket;
ChatFrm m_ChatFrm;
int m_State; //當前狀態
String m_Rival; //網絡對戰時的對手的名字
int m_RivalSide; //網絡對戰時的對手黑白
final static int FREE = 0; //
final static int WITH_NET = 1; //網絡對戰中
final static int WITH_COM = 2; //人機對戰中
final static int WITH_LOC = 3; //本地兩人對戰
// 關于棋盤畫面的一些數值
static int SEARCH_DEEPNESS;
final static int WHITE = 1;
final static int BLACK = -1 ;
final static int TEMP = 2;
final static int NONE = 0;
static int SWORD_B_OFF_X,SWORD_B_OFF_Y,SWORD_W_OFF_X,SWORD_W_OFF_Y;
static int SWORD_WIDTH,SWORD_HEIGHT;
static int CHESS_OFF_X,CHESS_GRILLE_X,CHESS_OFF_Y,CHESS_GRILLE_Y;
static int CHESS_WIDTH,CHESS_HEIGHT;
static int BACK_OFF_X,BACK_OFF_Y;
static int BACK_WIDTH,BACK_HEIGHT;
// 自定義的按鈕
ImgButton m_bNew,m_bPass,m_bBack,m_bNet,m_bLoc;
int ComputePut;
int I_Put;
int PASS ;
public Dimension getPreferredSize()
{
return new Dimension(m_backgdGif.getWidth(this),m_backgdGif.getHeight(this));
}
/* ********************
初始化函數
***********************/
public OthelloPanel(Applet main)
{
applet = main;
setSize(524,440);
InitData();
InitGra();
m_socket = null ;
m_ChatFrm = new ChatFrm(this);
m_State = WITH_LOC;
// paint(getGraphics());
}
void InitGra()
{
Graphics g= this.getGraphics();
with_net = new Image[3];
with_loc = new Image[3];
with_com = new Image[3];
//調入位圖按鈕的圖片
m_chessGif = getImage ("image\\chess.gif ");
m_swordGif = getImage ("image\\sword.gif");
m_faceGif = getImage ("image\\face.gif");
m_backgdGif = getImage ("image\\back.jpg");
with_net[0] = getImage("image\\withnetc.gif");
with_net[1] = getImage("image\\withneth.gif");
with_net[2] = getImage("image\\withnetd.gif");
with_com[0] = getImage("image\\withcomc.gif");
with_com[1] = getImage("image\\withcomh.gif");
with_com[2] = getImage("image\\withcomd.gif");
with_loc[0] = getImage("image\\withlocc.gif");
with_loc[1] = getImage("image\\withloch.gif");
with_loc[2] = getImage("image\\withlocd.gif");
Image cold,hot,down;
cold = this.getImage("image\\newc.Gif");
hot = this.getImage ("image\\newh.gif");
down = this.getImage("image\\newd.gif");
m_bNew.setImage(cold,hot,down,cold);
cold = this.getImage("image\\backc.gif");
hot = this.getImage ("image\\backh.gif");
down = this.getImage("image\\backd.gif");
m_bBack.setImage(cold,hot,down,cold);
cold = this.getImage("image\\passc.gif");
hot = this.getImage ("image\\passh.gif");
down = this.getImage("image\\passd.gif");
m_bPass.setImage(cold,hot,down,cold);
cold = this.getImage("image\\netc.gif");
hot = this.getImage("image\\neth.gif");;
down = this.getImage("image\\netd.gif");
m_bNet.setImage(cold,hot,down,cold);
m_bLoc.setImage(with_loc[0],with_loc[1],with_loc[2],with_loc[0]);
}
public Image getImage(String filename)
{
return applet.getImage(applet.getDocumentBase(),filename);
}
void InitData()
{
//常量初始化
ComputePut = -1;
I_Put = 1;
SEARCH_DEEPNESS = 4 ;
PASS = -1;
BACK_OFF_X = 0;
BACK_OFF_Y = 0;
SWORD_B_OFF_X = 51 + BACK_OFF_X;
SWORD_B_OFF_Y = 111 + BACK_OFF_Y;
SWORD_W_OFF_X = 51 + BACK_OFF_X;
SWORD_W_OFF_Y = 250 + BACK_OFF_Y;
SWORD_WIDTH = 32;
SWORD_HEIGHT = 32;
CHESS_OFF_X = 168 + BACK_OFF_X;
CHESS_OFF_Y = 62 + BACK_OFF_Y;
CHESS_GRILLE_X = 35;
CHESS_GRILLE_Y = 35;
CHESS_WIDTH = 26;
CHESS_HEIGHT = 26;
BACK_WIDTH = 520;
BACK_HEIGHT = 420;
m_chess = new int[10][10];
m_BackupX = new int[82];
m_BackupY = new int[82];
this.m_value = new int[10][10];
int i;
int j;
for (i=1;i<=8;i++)
for (j=1;j<=8;j++)
m_value[i][j] = 1;
//棋盤各位置的權值
m_value[1][1] = m_value[8][8] = m_value[1][8] = m_value[8][1] = 10;
m_value[2][2] = m_value[7][7] = m_value [2][7] = m_value [7][2] = -6;
m_value[2][1] = m_value [7][1] = m_value[1][2] = m_value[1][7] = m_value[7][1] = m_value[7][7] = m_value[2][8] = m_value[7][8] = -6;
for(i=3;i<=6;i++)
{
m_value[1][i] = m_value[i][1] = m_value [8][i] =m_value[i][8] =4;
}
RefreshData();
this.setLayout(null);
//初始化5個按鈕
m_bNew = new ImgButton("New");
m_bPass = new ImgButton("Pass");
m_bBack = new ImgButton("Back");
m_bNet = new ImgButton("Net");
m_bLoc = new ImgButton("Loc");
m_bNew.setBounds(20,5,40,60);
m_bPass.setBounds(75,5,40,60);
m_bBack.setBounds(130,5,40,60);
m_bNet.setBounds( 355,5,40,60);
m_bLoc.setBounds( 410,10,55,58);
add(m_bNew);
add(m_bPass);
add(m_bBack);
add(m_bLoc);
add(m_bNet);
ButtonListener listener = new ButtonListener();
m_bNew.addActionListener(listener);
m_bPass.addActionListener(listener);
m_bBack.addActionListener(listener);
m_bNet.addActionListener(listener);
m_bLoc.addActionListener(listener);
this.addMouseListener(new MListener());
this.addMouseMotionListener(new MMListener());
}
void RefreshData()
{
//棋盤初始化
int x,y;
for (x=1;x<=8;x++)
for (y=1;y<=8;y++)
m_chess[x][y]=0;
m_chess[4][4]=WHITE;
m_chess[5][5]=WHITE;
m_chess[4][5]=BLACK;
m_chess[5][4]=BLACK;
for(x=0;x<=40;x++)
{
m_BackupX[x] = 0;
m_BackupY[x] = 0;
}
m_TempX = m_TempY = 0;
m_CurrentX = m_CurrentY=0;
m_TempPut=false;
m_side=BLACK;
m_step=1;
m_WhichSide=BLACK;
m_MePut = true;
}
void TRACE(String s)
{
System.out.println(s+"\n");
}
/* *******************************
AI 算法函數
********************************/
void Put (int chess[][],int x,int y,int side)
{
//在某位置上放置棋子
int i,j,ii,jj;
if (!((x<=8) && (x>=1) && (y<=8) && (y>=1) && (chess[x][y] == 0) ))
{
Integer _px,_py,_c;
_px = new Integer(x);
_py = new Integer (y);
_c = new Integer (chess[x][y]);
String s = "Put Error in : "+_px.toString () + '.' +_py.toString () + "chess[][] is " +_c.toString();
MessageBox.createMessageBox (s,"Error");
return;
}
chess[x][y]=side;
if (y<=6)
{
if (chess[x][y+1]==-side) // down
{
for (j=y+2;j<=8;j++)
if(chess[x][j]==side)
{
for(jj=y+1;jj<=j-1;jj++)
chess[x][jj]=side;
break;
}
else if(chess[x][j]==0) break;
}
if (chess[x+1][y+1]==-side) // right down
{
j = y+2;
for(i=x+2;(i<=8 && j<=8);i++)
{
if( chess[i][j]==side)
{
jj = y+1;
for(ii=x+1;(ii<=i-1 && jj<=j-1);ii++)
{
chess[ii][jj]=side;
jj++;
}
break;
}
else if(chess[i][j]==0) break;
j++;
}
}
if (chess[x-1][y+1]==-side)
{
j = y+2;
for(i=x-2;(i>=1 && j<=8);i--)
{
if( chess[i][j]==side)
{
jj = y+1;
for(ii=x-1;(ii>=i+1 && jj<=j-1);ii--)
{
chess[ii][jj]=side;
jj++;
}
break;
}
else if(chess[i][j]==0) break;
j++;
}
}
}
if (y>=2)
{
if (chess[x][y-1]==-side)
{
for(j=y-2;j>=1;j--)
if (chess[x][j]==side)
{
for(jj=y-1;jj>=j+1;jj--)
chess[x][jj]=side;
break;
}
else if(chess[x][j]==0) break;
}
if (chess[x+1][y-1]==-side)
{
j = y-2;
for(i=x+2;(i<=8 && j>=1);i++)
{
if( chess[i][j]==side)
{
jj = y-1;
for(ii=x+1;(ii<=i-1 && jj>=j+1);ii++)
{
chess[ii][jj]=side;
jj--;
}
break;
}
else if(chess[i][j]==0) break;
j--;
}
}
if (chess[x-1][y-1]==-side)
{
j = y-2;
for(i=x-2;(i>=1 && j>=1);i--)
{
if (chess[i][j]==side)
{
jj = y-1;
for(ii=x-1;(ii>=i+1 && jj>=j+1);ii--)
{
chess[ii][jj]=side;
jj--;
}
break;
}
else if(chess[i][j]==0) break;
j--;
}
}
}
if (x<=6)
{
if (chess[x+1][y]==-side)
{
for(i=x+2;i<=8;i++)
if (chess[i][y]==side)
{
for(ii=x+1;ii<=i-1;ii++)
chess[ii][y]=side;
break;
}
else if(chess[i][y]==0) break;
}
}
if (x>=2)
{
if (chess[x-1][y]==-side)
{
for(i=x-2;i>=1;i--)
if (chess[i][y]==side)
{
for(ii=x-1;ii>=i+1;ii--)
chess[ii][y]=side;
break;
}
else if(chess[i][y]==0) break;
}
}
}
boolean Judge( int chess[][],int x,int y,int side)
{
//判斷是否可以放置
int i,j;
if (!((x<=8) && (x>=1) && (y<=8) && (y>=1)))
{
Integer _px,_py;
_px = new Integer(x);
_py = new Integer (y);
String s = "Judge Error in : "+_px.toString () + '.' +_py.toString ();
MessageBox.createMessageBox (s);
return false;
}
if (chess[x][y]==0)
{
if (y<=6)
{
if(chess[x][y+1]==-side)
{
for (j=y+2;j<=8;j++)
if(chess[x][j]==side){return true;}
else if (chess[x][j]==0) break;
}
if(chess[x-1][y+1]==-side)
{
j = y+2;
for (i=x-2;(j<=8)&&(i>=1);i--)
{
if(chess[i][j]==side) {return true;}
else if (chess[i][j]==0) break;
j++;
}
}
if(chess[x+1][y+1]==-side)
{
j = y+2;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -