⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 j2ee.txt

📁 j2ee 平台的五子棋游戏
💻 TXT
字号:
<applet code="mychess.class" height=400 width=400>
</Applet> 

 

java五子棋游戏程序:

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class mychess extends Applet implements MouseListener,MouseMotionListener 

{

int pstate[][]; //记录棋盘上的棋子信息1(黑),2(白),0(未下子)

int whoplay;//1-黑棋下,2-白棋下

int currc; //光标当前位置(x)

int currr; //光标当拔恢茫▂)appletviewer mychess.html

int prex; //光标前一位置(x)

int prey; //光标前一位置(y)

boolean cursormove; //是否鼠标移动

boolean end; //是否对奕结束

boolean Movevalid(int m,int n) { //判断鼠标位置是否合法

if ((m >=0) && (m <19)&&(n>=0)&& (n<19) && (pstate[m][n]==0))

{

return true;

}

return false;

}

public void init(){

pstate=new int[19][19];

setBackground(new Color(239,219,170));

end=false; 

whoplay=1;

cursormove=false;

for (int i=0;i<19;i++)

for (int j=0;j<19;j++)

pstate[j]=0;

this.addMouseListener(this);

this.addMouseMotionListener(this);

}

public void update(Graphics g)

{ 

if (cursormove)

{ 

cursorchange(g);

} 

else { 

paint(g);

}

}

// 红色光标移动的重画

public void cursorchange(Graphics g)

{

Dimension dd = getSize();

int d=dd.height;

int xoff=d/20;

int step=d/20;

int x,y;

int xmiddle,ymiddle,xstart,ystart,xend,yend;

if (Movevalid(prex,prey))

{ //清除原来位置的光标

g.setColor(getBackground()); 

g.fillRect(prex*step+xoff-step/5, prey*step+xoff-step/5, step/3,step/3);

//以下重画相应位置的棋盘线

if ((prex>=0) &&(prex<19) &&(prey>=0)&&(prey<19))

{x=prex;

y=prey; 

g.setColor(Color.black);

xmiddle=xoff+x*step;

ymiddle=xoff+y*step;

//以下进行边界处理

if (x==0) 

{xstart=xoff+x*step;

}

else

{xstart=xoff+x*step-step/3;

}

if (x==18)

{xend=xoff+x*step;

}

else

{xend=xoff+x*step+step/3;

}

if (y==0)

{ystart=xoff+y*step;

}

else

{ystart=xoff+y*step-step/3;

}

if (y==18)

{yend=xoff+y*step;

}

else

{yend=xoff+y*step+step/3;

}

g.drawLine(xstart, ymiddle, xend, ymiddle);

g.drawLine(xmiddle, ystart,xmiddle,yend);

}

}

if (Movevalid(currc,currr))

//条件的作用是避免光标画入已有子位置和边界外,

{

g.setColor(Color.green); //绘新光标

g.fillRect(currc*step+xoff-step/5, currr*step+xoff-step/5, step/3,step/3); 

prex=currc;

prey=currr;

}

}

//绘棋盘及下子

public void paint(Graphics g) {

Dimension dd = getSize();

int d=dd.height;

//int d=this.getHeight();

int xoff=d/20;

int yoff=d/20;

int step=d/20;

int step1=step/2;

g.setColor(Color.black);

//画线

for (int i=0;i<19;i++)

g.drawLine(xoff+i*step, yoff, xoff+i*step, xoff+18*step);

for (int i=0;i<19;i++)

g.drawLine(xoff, yoff+i*step, yoff+18*step, yoff+i*step);

//画棋子

for (int r =0 ; r <19 ; r++) {

for (int c = 0 ; c < 19 ; c++) {

if (pstate[c][r]==1) {

g.setColor(Color.black);

g.fillArc(step1+c*step+step/8,step1+r*step+step/8,step-3,step-3,0,360);

 

}

else if (pstate[c][r]==2) {

g.setColor(Color.white);

g.fillArc(step1+c*step+step/8,step1+r*step+step/8,step-3,step-3,0,360);

}

}

}

//画绿色光标

g.setColor(Color.blue);

g.fillRect(currc*step+xoff-step/5,currr*step+xoff-step/5, step/3,step/3);

 

if (end){

  if(pstate[currc][currc]==1)

g.drawString("游戏结束,黑方胜!",390,100);

  else if (pstate[currc][currc]==2)

g.drawString("游戏结束,白方胜!",390,100);

}

  }

   private boolean isend() //判断四个方向是否有一个五子同线

{

if (five(currc,currr,0,1)|| five(currc,currr,1,0)|| five(currc,currr,1,1)|| five(currc,currr,-1,-1))

return true;

else

return false;

}

private boolean five(int cx,int cy,int xinc,int yinc) 

{ //判断某个方向是否有紧连的同方五个子,cx,cy刚下子位置,xinc,yinc方向的增减

int m=pstate[cx][cy];

int x=cx;

int y=cy;

for (int k=1;k<=5;k++) //先找到该方向的左(下)的本方棋子

{

if (pstate[x][y]==m)

{ x=x-xinc;

y=y-yinc;

}

else {

break;

} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -