這篇文章主要介紹了Java實(shí)現(xiàn)兩人五子棋游戲的示例,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)總部坐落于成都市區(qū),致力網(wǎng)站建設(shè)服務(wù)有成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷策劃、網(wǎng)頁設(shè)計(jì)、網(wǎng)站維護(hù)、公眾號搭建、微信小程序、軟件開發(fā)等為企業(yè)提供一整套的信息化建設(shè)解決方案。創(chuàng)造真正意義上的網(wǎng)站建設(shè),為互聯(lián)網(wǎng)品牌在互動行銷領(lǐng)域創(chuàng)造價(jià)值而不懈努力!
Java實(shí)現(xiàn)兩人五子棋游戲,主要功能:
1)選擇棋子
2)畫棋子
3)判斷勝負(fù)
4)交換行棋方
先實(shí)現(xiàn)畫棋子PART
-------------畫棋子代碼示例如下--------------
首先,定義一個棋子類,這個類有兩個屬性,棋子顏色(0-表示黑色,1-表示白色),是否落子(我計(jì)劃用一個二維數(shù)組才存儲棋子的落子信息)
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】)來檢驗(yàn)畫棋子的代碼
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<ROWS;i++) { g.drawLine(x, y+i*span_y, FWidth-x,y+i*span_y); } //畫豎線 for(int i=0;i<ROWS;i++) { g.drawLine(x+i*span_x, y, x+i*span_x,FHeight-y); } //畫棋子 for(int i=0;i<ROWS;i++) { for(int j=0;j<ROWS;j++) { if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) { System.out.println("draw chessman "+i+" "+j); int pos_x=x+i*span_x; int pos_y=y+j*span_y; int chessman_width=20; float radius_b=20; float radius_w=50; float[] fractions = new float[]{0f,1f}; java.awt.Color[] colors_b = new java.awt.Color[]{Color.BLACK,Color.WHITE}; Color[] colors_w = new Color[]{Color.WHITE,Color.BLACK}; RadialGradientPaint paint; if(chessStatus[i][j].getColor()==1) { System.out.println("draw white chess"); paint = new RadialGradientPaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fractions, colors_w); }else{ System.out.println("draw black chess"); paint = new RadialGradientPaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, fractions, colors_b); } ((Graphics2D)g).setPaint(paint); ((Graphics2D)g).fillOval(pos_x-chessman_width/2,pos_y-chessman_width/2,chessman_width,chessman_width); } } } } }
主模塊代碼不變
Main.java
package 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標(biāo)題 setTitle("單機(jī)五子棋"); Container containerPane =getContentPane(); containerPane.add(drawChessBoard); } public static void main(String[] args) { Main m = new Main(); m.setSize(800, 800); m.setVisible(true); } }
運(yùn)行一下!
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Java實(shí)現(xiàn)兩人五子棋游戲的示例”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
分享題目:Java實(shí)現(xiàn)兩人五子棋游戲的示例
標(biāo)題鏈接:http://aaarwkj.com/article34/igocse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、建站公司、響應(yīng)式網(wǎng)站、App開發(fā)、品牌網(wǎng)站建設(shè)、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)