电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

java五子棋小游戲實現代碼

瀏覽:7日期:2022-08-08 17:26:41
前言

之前學完java基礎課程,試著簡單做了一下java的一個五子棋小游戲,記錄下來。

界面

由于直接用的java庫中的一些基本控件寫的一個GUI,并沒有做過多優化,感覺比較丑下面是界面展示:

java五子棋小游戲實現代碼

黑子先行,但是我這邊簡化規則,并沒有考慮黑子先行的一些禁手。

下面直接貼代碼

接口類

我把五子棋界面的一些常量都定義在了這個接口類中,包括棋盤的起始坐標,棋盤線的間距和棋子半徑

public interface constant { int[][] chessLocation = new int[15][15]; static final int x = 50; //左上角位置 static final int y = 50; static final int LN = 15; //棋盤一些常量 static final int R = 45;}實現類

接口

這個類中繼承了 constant、MouseListener、ActionListener三個接口

其中:

constant為自己定義 MouseListener為鼠標監聽 ActionListener為事件監聽

函數

show()繪制窗口基本框架paint()繪制棋盤網格線和棋子IsWin()判斷輸贏的基本邏輯mouseClicked()獲取鼠標位置,判斷棋子落點等actionPerformed()判斷鼠標點擊哪個按鈕(開始游戲or認輸or悔棋)執行相應操作

import java.awt.Color;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;public class game_logic extends JPanel implements constant, MouseListener, ActionListener { int chess_x = 0, chess_y = 0; int X = 0, Y = 0; boolean IsBlack = true; //判斷黑白 boolean flag = false; //是否已經開始游戲 //生成三個響應按鈕 JFrame frame = new JFrame(); JButton start = new JButton('開始游戲'); JButton regret = new JButton('悔棋'); JButton Lost = new JButton('認輸'); public void ShowUI() {frame.setSize(740, 800);frame.setTitle('五子棋');frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//點擊關閉結束程序frame.setLocationRelativeTo(null);//窗口居中frame.setVisible(true);//窗體可視化frame.setResizable(false);//窗體大小不可調整frame.add(this);this.setBackground(Color.LIGHT_GRAY);//設置背景顏色this.addMouseListener(this);//窗體中添加鼠標監聽器start.setSize(50, 80);//設置按鈕大小start.addActionListener(this);//按鈕添加事件監聽器Lost.setSize(50, 80);Lost.addActionListener(this);regret.setSize(50, 80);regret.addActionListener(this);this.add(start);//添加按鈕到棋盤上this.add(Lost);this.add(regret); } /** * 繪制方法 * 繪制五子棋棋盤 * @param g */ @Override public void paint(Graphics g) {super.paint(g);for (int i = 0; i < LN; i++) { //畫棋盤 g.drawLine(x, y + i * R, x + (LN - 1) * R, y + i * R);//行*15 g.drawLine(x + i * R, y, x + i * R, y + (LN - 1) * R);//列*15}for (int i = 0; i < LN; i++) { //畫棋子 for (int j = 0; j < LN; j++) {if (chessLocation[i][j] == 1) { g.setColor(Color.BLACK);//黑棋先行 g.fillOval(50 + i * R - 23, 50 + j * R - 23, R, R);}if (chessLocation[i][j] == 2) { g.setColor(Color.WHITE); g.fillOval(50 + i * R - 23, 50 + j * R - 23, R, R);}repaint(); }} } /** *判斷輸贏 * */ public int IsWin() {int k = 0;for (int f = 2; f < 12; f++) { for (int g = 2; g < 12; g++) {if (chessLocation[f][g] == 1) { if (chessLocation[f][g] == chessLocation[f - 1][g] && chessLocation[f - 1][g] == chessLocation[f - 2][g] && chessLocation[f - 2][g] == chessLocation[f + 1][g] && chessLocation[f + 1][g] == chessLocation[f + 2][g]) {k = 1;break; } if (chessLocation[f][g] == chessLocation[f][g - 1] && chessLocation[f][g - 1] == chessLocation[f][g - 2] && chessLocation[f][g - 2] == chessLocation[f][g + 1] && chessLocation[f][g + 1] == chessLocation[f][g + 2]) {k = 1;break; } if (chessLocation[f][g] == chessLocation[f - 1][g - 1] && chessLocation[f - 1][g - 1] == chessLocation[f - 2][g - 2] && chessLocation[f - 2][g - 2] == chessLocation[f + 1][g + 1] && chessLocation[f + 1][g + 1] == chessLocation[f + 2][g + 2]) {k = 1;break; } if (chessLocation[f][g] == chessLocation[f - 1][g + 1] && chessLocation[f - 1][g + 1] == chessLocation[f - 2][g + 2] && chessLocation[f - 2][g + 2] == chessLocation[f + 1][g - 1] && chessLocation[f + 1][g - 1] == chessLocation[f + 2][g - 2]) {k = 1;break; }}if (chessLocation[f][g] == 2) { if (chessLocation[f][g] == chessLocation[f - 1][g] && chessLocation[f - 1][g] == chessLocation[f - 2][g] && chessLocation[f - 2][g] == chessLocation[f + 1][g] && chessLocation[f + 1][g] == chessLocation[f + 2][g]) {k = 2;break; } if (chessLocation[f][g] == chessLocation[f][g - 1] && chessLocation[f][g - 1] == chessLocation[f][g - 2] && chessLocation[f][g - 2] == chessLocation[f][g + 1] && chessLocation[f][g + 1] == chessLocation[f][g + 2]) {k = 2;break; } if (chessLocation[f][g] == chessLocation[f - 1][g - 1] && chessLocation[f - 1][g - 1] == chessLocation[f - 2][g - 2] && chessLocation[f - 2][g - 2] == chessLocation[f + 1][g + 1] && chessLocation[f + 1][g + 1] == chessLocation[f + 2][g + 2]) {k = 2;break; } if (chessLocation[f][g] == chessLocation[f - 1][g + 1] && chessLocation[f - 1][g + 1] == chessLocation[f - 2][g + 2] && chessLocation[f - 2][g + 2] == chessLocation[f + 1][g - 1] && chessLocation[f + 1][g - 1] == chessLocation[f + 2][g - 2]) {k = 2;break; }} }}return k; } @Override public void mouseClicked(MouseEvent e) {X = e.getX();Y = e.getY(); //獲取鼠標位置if (flag == true) { if (X >= 25 && X <= 705 && Y >= 25 && Y <= 705) { //比棋盤稍微大一點的落子判定范圍,即棋盤邊緣位置//應該安放的棋子的位置chess_x = (X - 20) / R;chess_y = (Y - 20) / R;if (chessLocation[chess_x][chess_y] == 0) { //存儲棋子狀態,轉換棋子顏色 if (IsBlack == true) {chessLocation[chess_x][chess_y] = 1;IsBlack = false; } else {chessLocation[chess_x][chess_y] = 2;IsBlack = true; } if (IsWin() == 1) {JOptionPane.showMessageDialog(this, '黑棋獲勝');flag = false; } if (IsWin() == 2) {JOptionPane.showMessageDialog(this, '白棋獲勝');flag = false; } repaint();} }} } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void actionPerformed(ActionEvent e) {String buttonName = e.getActionCommand();if (buttonName.equals('開始游戲') && flag == false) {//開始游戲,棋盤清空 flag = true; for (int i = 0; i < LN; i++) {for (int j = 0; j < LN; j++) { chessLocation[i][j] = 0;} } IsBlack = true; repaint();}if (buttonName.equals('認輸') && flag == true) { flag = false; if (IsBlack) {JOptionPane.showMessageDialog(this, ',白棋認輸,黑棋獲勝'); } else {JOptionPane.showMessageDialog(this, ',黑棋認輸,白棋獲勝'); }}if (buttonName.equals('悔棋') && flag == true) { if (chessLocation[chess_x][chess_y] == 1) {JOptionPane.showMessageDialog(this, '黑方悔棋'); } if (chessLocation[chess_x][chess_y] == 2) {JOptionPane.showMessageDialog(this, '白方悔棋'); } chessLocation[chess_x][chess_y] = 0; IsBlack = !IsBlack; repaint();} }}

其中比較有趣的是五子棋判贏方式,假設棋盤大小15*15,則我只需要判斷正中間的13*13d的格子,向兩邊擴展,判斷是否五子連珠。

具體說明代碼里都有注釋,不多贅述。

主函數類

public class Main_game { public static void main(String[] args) {game_logic start=new game_logic();start.ShowUI(); }}總結

實現了五子棋小游戲的基本功能,但是略感粗糙,細節不足。對于基本控件調用一學就會,做一個小的游戲demo這是對流程控制和操作邏輯的訓練很有效的一種方式。之前看了別人的代碼覺得簡單,但是自己寫的時候往往邏輯流程難以連續,思維混亂,有些過程只有自己寫了才知道其中的坑。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 南京泽朗生物科技有限公司-液体饮料代加工_果汁饮料代加工_固体饮料代加工 | 湖南自考_湖南自学考试网| 光谱仪_积分球_分布光度计_灯具检测生产厂家_杭州松朗光电【官网】 | 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 湖南成人高考报名-湖南成考网| 自动气象站_气象站监测设备_全自动气象站设备_雨量监测站-山东风途物联网 | 起好名字_取个好名字_好名网免费取好名在线打分 | 焊接烟尘净化器__焊烟除尘设备_打磨工作台_喷漆废气治理设备 -催化燃烧设备 _天津路博蓝天环保科技有限公司 | 废气处理_废气处理设备_工业废气处理_江苏龙泰环保设备制造有限公司 | 定制异形重型钢格栅板/钢格板_定做踏步板/排水沟盖板_钢格栅板批发厂家-河北圣墨金属制品有限公司 | 玻璃钢格栅盖板|玻璃钢盖板|玻璃钢格栅板|树篦子-长沙川皖玻璃钢制品有限公司 | 不锈钢反应釜,不锈钢反应釜厂家-价格-威海鑫泰化工机械有限公司 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 餐饮加盟网_特色餐饮加盟店_餐饮连锁店加盟 | 铝机箱_铝外壳加工_铝外壳厂家_CNC散热器加工-惠州市铂源五金制品有限公司 | 浙江富广阀门有限公司 | 电动卫生级调节阀,电动防爆球阀,电动软密封蝶阀,气动高压球阀,气动对夹蝶阀,气动V型调节球阀-上海川沪阀门有限公司 | 网站建设-高端品牌网站设计制作一站式定制_杭州APP/微信小程序开发运营-鼎易科技 | 首页-恒温恒湿试验箱_恒温恒湿箱_高低温试验箱_高低温交变湿热试验箱_苏州正合 | 皮带机-带式输送机价格-固定式胶带机生产厂家-河南坤威机械 | 气弹簧定制-气动杆-可控气弹簧-不锈钢阻尼器-工业气弹簧-可调节气弹簧厂家-常州巨腾气弹簧供应商 | 控显科技 - 工控一体机、工业显示器、工业平板电脑源头厂家 | 至顶网| 光纤测温-荧光光纤测温系统-福州华光天锐光电科技有限公司 | 纯水电导率测定仪-万用气体检测仪-低钠测定仪-米沃奇科技(北京)有限公司www.milwaukeeinst.cn 锂辉石检测仪器,水泥成分快速分析仪-湘潭宇科分析仪器有限公司 手术室净化装修-手术室净化工程公司-华锐手术室净化厂家 | 巨野电机维修-水泵维修-巨野县飞宇机电维修有限公司 | 优宝-汽车润滑脂-轴承润滑脂-高温齿轮润滑油脂厂家 | 缓蚀除垢剂_循环水阻垢剂_反渗透锅炉阻垢剂_有机硫化物-郑州威大水处理材料有限公司 | 叉车电池-叉车电瓶-叉车蓄电池-铅酸蓄电池-电动叉车蓄电池生产厂家 | PC构件-PC预制构件-构件设计-建筑预制构件-PC构件厂-锦萧新材料科技(浙江)股份有限公司 | 四探针电阻率测试仪-振实密度仪-粉末流动性测定仪-宁波瑞柯微智能 | 济南货架定做_仓储货架生产厂_重型货架厂_仓库货架批发_济南启力仓储设备有限公司 | 国产液相色谱仪-超高效液相色谱仪厂家-上海伍丰科学仪器有限公司 | 机制砂选粉机_砂石选粉机厂家-盐城市助成粉磨科技有限公司 | 威实软件_软件定制开发_OA_OA办公系统_OA系统_办公自动化软件 | 锂电叉车,电动叉车_厂家-山东博峻智能科技有限公司 | 不锈钢螺丝 - 六角螺丝厂家 - 不锈钢紧固件 - 万千紧固件--紧固件一站式采购 | 【北京写字楼出租_写字楼租赁_办公室出租网/出售】-远行地产官网 | 硅胶制品-硅橡胶制品-东莞硅胶制品厂家-广东帝博科技有限公司 | 油液红外光谱仪-油液监测系统-燃油嗅探仪-上海冉超光电科技有限公司 | 乳化沥青设备_改性沥青设备_沥青加温罐_德州市昊通路桥工程有限公司 | 废气处理设备-工业除尘器-RTO-RCO-蓄热式焚烧炉厂家-江苏天达环保设备有限公司 |