Java实现两人五子棋游戏的示例

  介绍

这篇文章主要介绍了Java实现两人五子棋游戏的示例,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获、下面让小编带着大家一起了解一下。

Java实现两人五子棋游戏,主要功能:

1)选择棋子

2)画棋子

3)判断胜负

4)交换行棋方

先实现画棋子部分

- - - - - - - - - - - - -画棋子代码示例如下- - - - - - - - - - - - - - - -

癑ava实现两人五子棋游戏的示例"

首先,定义一个棋子类,这个类有两个属性,棋子颜色(0 -表示黑色,1 -表示白色),是否落子(我计划用一个二维数组才存储棋子的落子信息)
棋子。java

package  xchen.test.simpleGobang;,   ,   {public  class  Chessman    ,private  int 颜色;//一只白,0-black    ,private  boolean  placed =,假的,,   ,   棋子,public  (int 颜色,boolean 放置){,   ,this.color=颜色;   ,this.placed=放置;   }大敌;   ,   ,public  boolean  getPlaced (), {,   ,return 放置;   }大敌;   ,   ,public  void  setPlaced (boolean 放置),{,=,this.placed 放置,,   }大敌;   ,   ,public  int 色鬼(),{,   ,return 颜色;   }大敌;   ,   ,public  void 改变颜色(int 颜色),{,=,this.color 颜色,,   }大敌;   }

接着我们上一部分的画好棋盘的代码部分,新增画棋子的代码,我用两个棋子(一白一黑,分别位于棋盘的【8】,【7】7日)来检验画棋子的代码
DrawChessBoard。java

package  xchen.test.simpleGobang;,   ,   import  java.awt.Graphics,   java . awt . graphics2d import ,,   import  java.awt.RadialGradientPaint,   import  java.awt.Image,   import  java.awt.Toolkit,   import  java.awt.Color,   import  javax.swing.JPanel,   ,   public  class  DrawChessBoard  extends  JPanel {,   ,final  static  int 黑=0,,   ,final  static  int 白色=1,,   ,public  int  chessColor =,黑色,,   ,   ,public  Image  boardImg;   ,final  private  int  ROWS =, 19岁,,   ,棋子[][],chessStatus=new 棋子(行)(行),,   ,   ,public  DrawChessBoard (), {,=,,boardImg  Toolkit.getDefaultToolkit () .getImage (“res/可拉的/chessboard2.png"),,   ,如果(boardImg ==, null),   ,System.err.println (“png  do  not  exist"),,   ,   ,//test  draw  chessman  part  simple    棋子,Chessman =new 棋子(0,,真的),,   ,chessStatus[7][7]=棋子;,   ,Chessman  chessman2 =, new 棋子(1,,真的),,   ,chessStatus [8] [8]=chessman2;,   ,//test  draw  chessman  part  simple    }大敌;   ,@Override    ,protected  void  paintComponent (Graphics  g), {,   ,//TODO  Auto-generated  method  stub    ,super.paintComponent (g),,   ,   ,int  imgWidth =, boardImg.getHeight(这个),,   ,int  imgHeight =, boardImg.getWidth(这个),,   ,int  FWidth =, getWidth (),,   ,int  FHeight=,获得(),,   ,   ,int  x=(FWidth-imgWidth)/2,,   ,int  y=(FHeight-imgHeight)/2,,   ,g.drawImage (boardImg, x,, y,, null),,   ,   ,int  margin =, x,,   ,int  span_x=imgWidth/行,   ,int  span_y=imgHeight/行,   ,//画横线,   ,(int  i=0; i<行;我+ +),   {大敌;   ,g.drawLine (x, y + i * span_y, FWidth-x, y + i * span_y),,   }大敌;   ,//画竖线,   ,(int  i=0; i<行;我+ +),   {大敌;   ,g.drawLine (x + i * span_x,, y,, x + i * span_x FHeight-y),,   }大敌;   ,   ,//画棋子,   ,(int  i=0; i<行;我+ +),   {大敌;   ,(int  j=0; j<行;j + +),   {大敌;   ,如果(chessStatus[我][j] !=null&, chessStatus[我][j] .getPlaced ()==true),   {大敌;   ,System.out.println (“draw  chessman “+我+“,“+ j),,   ,int  pos_x=x +我* span_x,,   ,int  pos_y=y + j * span_y;,   ,int  chessman_width=20;   ,float  radius_b=20;   ,float  radius_w=50;   ,浮子[]fractions =, new 浮动[]{0 f, f 1},,   ,java.awt.Color [], colors_b =, new  java.awt.Color [] {Color.BLACK, Color.WHITE},,   ,颜色[]colors_w =, new 颜色[]{Color.WHITE, Color.BLACK},,   ,RadialGradientPaint 颜料;   ,如果(chessStatus[我][j] .getColor ()==1),   {大敌;   ,System.out.println (“draw  white  chess"),,=,,paint  new  RadialGradientPaint (pos_x-chessman_width f/2,, pos_y-chessman_width f/2,, radius_w * 2,,分数,,colors_w),,   其他}{大敌;   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Java实现两人五子棋游戏的示例