这篇文章主要介绍了Java实现两人五子棋游戏的示例,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
成都创新互联总部坐落于成都市区,致力网站建设服务有成都网站设计、成都网站建设、外贸网站建设、网络营销策划、网页设计、网站维护、公众号搭建、微信小程序、软件开发等为企业提供一整套的信息化建设解决方案。创造真正意义上的网站建设,为互联网品牌在互动行销领域创造价值而不懈努力!
Java实现两人五子棋游戏,主要功能:
1)选择棋子
2)画棋子
3)判断胜负
4)交换行棋方
先实现画棋子PART
-------------画棋子代码示例如下--------------
首先,定义一个棋子类,这个类有两个属性,棋子颜色(0-表示黑色,1-表示白色),是否落子(我计划用一个二维数组才存储棋子的落子信息)
Chessman.java
package xchen.test.simpleGobang; public class Chessman { private int color;//1-white,0-black private boolean placed = false; public Chessman(int color,boolean placed){ this.color=color; this.placed=placed; } public boolean getPlaced() { return placed; } public void setPlaced(boolean placed) { this.placed = placed; } public int getColor() { return color; } public void setColor(int color) { this.color = color; } }
接着我们上一部分的画好棋盘的代码部分,新增画棋子的代码,我用两个棋子(一白一黑,分别位于棋盘的【8,8】,【7,7】)来检验画棋子的代码
DrawChessBoard.java
package xchen.test.simpleGobang; import java.awt.Graphics; import java.awt.Graphics2D; 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 BLACK=0; final static int WHITE=1; public int chessColor = BLACK; public Image boardImg; final private int ROWS = 19; Chessman[][] chessStatus=new Chessman[ROWS][ROWS]; public DrawChessBoard() { boardImg = Toolkit.getDefaultToolkit().getImage("res/drawable/chessboard2.png"); if(boardImg == null) System.err.println("png do not exist"); //test draw chessman part simple Chessman chessman=new Chessman(0, true); chessStatus[7][7]=chessman; Chessman chessman2 = new Chessman(1, true); 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(this); int imgHeight = boardImg.getWidth(this); int FWidth = getWidth(); int FHeight= getHeight(); int x=(FWidth-imgWidth)/2; int y=(FHeight-imgHeight)/2; g.drawImage(boardImg, x, y, null); int margin = x; int span_x=imgWidth/ROWS; int span_y=imgHeight/ROWS; //画横线 for(int i=0;i主模块代码不变
Main.javapackage xchen.test.simpleGobang; import java.awt.Container; import javax.swing.JFrame; import xchen.test.simpleGobang.DrawChessBoard; public class Main extends JFrame{ private DrawChessBoard drawChessBoard; public Main() { drawChessBoard = new DrawChessBoard(); //Frame标题 setTitle("单机五子棋"); Container containerPane =getContentPane(); containerPane.add(drawChessBoard); } public static void main(String[] args) { Main m = new Main(); m.setSize(800, 800); m.setVisible(true); } }运行一下!
感谢你能够认真阅读完这篇文章,希望小编分享的“Java实现两人五子棋游戏的示例”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!
本文标题:Java实现两人五子棋游戏的示例
文章源于:http://cdxtjz.cn/article/igocse.html