java五子棋小游戲?qū)崿F(xiàn)代碼
前言
之前學(xué)完java基礎(chǔ)課程,試著簡(jiǎn)單做了一下java的一個(gè)五子棋小游戲,記錄下來(lái)。
界面
由于直接用的java庫(kù)中的一些基本控件寫(xiě)的一個(gè)GUI,并沒(méi)有做過(guò)多優(yōu)化,感覺(jué)比較丑
下面是界面展示:

黑子先行,但是我這邊簡(jiǎn)化規(guī)則,并沒(méi)有考慮黑子先行的一些禁手。
下面直接貼代碼
接口類(lèi)
我把五子棋界面的一些常量都定義在了這個(gè)接口類(lèi)中,包括棋盤(pán)的起始坐標(biāo),棋盤(pán)線(xiàn)的間距和棋子半徑
public interface constant {
int[][] chessLocation = new int[15][15];
static final int x = 50; //左上角位置
static final int y = 50;
static final int LN = 15; //棋盤(pán)一些常量
static final int R = 45;
}
實(shí)現(xiàn)類(lèi)
接口
這個(gè)類(lèi)中繼承了 constant、MouseListener、ActionListener三個(gè)接口
其中:
- constant為自己定義
- MouseListener為鼠標(biāo)監(jiān)聽(tīng)
- ActionListener為事件監(jiān)聽(tīng)
函數(shù)
show()繪制窗口基本框架
paint()繪制棋盤(pán)網(wǎng)格線(xiàn)和棋子
IsWin()判斷輸贏的基本邏輯
mouseClicked()獲取鼠標(biāo)位置,判斷棋子落點(diǎn)等
actionPerformed()判斷鼠標(biāo)點(diǎn)擊哪個(gè)按鈕(開(kāi)始游戲or認(rèn)輸or悔棋)執(zhí)行相應(yīng)操作
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; //是否已經(jīng)開(kāi)始游戲
//生成三個(gè)響應(yīng)按鈕
JFrame frame = new JFrame();
JButton start = new JButton("開(kāi)始游戲");
JButton regret = new JButton("悔棋");
JButton Lost = new JButton("認(rèn)輸");
public void ShowUI() {
frame.setSize(740, 800);
frame.setTitle("五子棋");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//點(diǎn)擊關(guān)閉結(jié)束程序
frame.setLocationRelativeTo(null);//窗口居中
frame.setVisible(true);//窗體可視化
frame.setResizable(false);//窗體大小不可調(diào)整
frame.add(this);
this.setBackground(Color.LIGHT_GRAY);//設(shè)置背景顏色
this.addMouseListener(this);//窗體中添加鼠標(biāo)監(jiān)聽(tīng)器
start.setSize(50, 80);//設(shè)置按鈕大小
start.addActionListener(this);//按鈕添加事件監(jiān)聽(tīng)器
Lost.setSize(50, 80);
Lost.addActionListener(this);
regret.setSize(50, 80);
regret.addActionListener(this);
this.add(start);//添加按鈕到棋盤(pán)上
this.add(Lost);
this.add(regret);
}
/**
* 繪制方法
* 繪制五子棋棋盤(pán)
* @param g
*/
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < LN; i++) { //畫(huà)棋盤(pán)
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++) { //畫(huà)棋子
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(); //獲取鼠標(biāo)位置
if (flag == true) {
if (X >= 25 && X <= 705 && Y >= 25 && Y <= 705) { //比棋盤(pán)稍微大一點(diǎn)的落子判定范圍,即棋盤(pán)邊緣位置
//應(yīng)該安放的棋子的位置
chess_x = (X - 20) / R;
chess_y = (Y - 20) / R;
if (chessLocation[chess_x][chess_y] == 0) { //存儲(chǔ)棋子狀態(tài),轉(zhuǎn)換棋子顏色
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("開(kāi)始游戲") && flag == false) {//開(kāi)始游戲,棋盤(pán)清空
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("認(rèn)輸") && flag == true) {
flag = false;
if (IsBlack) {
JOptionPane.showMessageDialog(this, ",白棋認(rèn)輸,黑棋獲勝");
} else {
JOptionPane.showMessageDialog(this, ",黑棋認(rèn)輸,白棋獲勝");
}
}
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();
}
}
}
其中比較有趣的是五子棋判贏方式,假設(shè)棋盤(pán)大小15*15,則我只需要判斷正中間的13*13d的格子,向兩邊擴(kuò)展,判斷是否五子連珠。
具體說(shuō)明代碼里都有注釋?zhuān)欢噘樖觥?/p>
主函數(shù)類(lèi)
public class Main_game {
public static void main(String[] args) {
game_logic start=new game_logic();
start.ShowUI();
}
}
總結(jié)
實(shí)現(xiàn)了五子棋小游戲的基本功能,但是略感粗糙,細(xì)節(jié)不足。對(duì)于基本控件調(diào)用一學(xué)就會(huì),做一個(gè)小的游戲demo這是對(duì)流程控制和操作邏輯的訓(xùn)練很有效的一種方式。之前看了別人的代碼覺(jué)得簡(jiǎn)單,但是自己寫(xiě)的時(shí)候往往邏輯流程難以連續(xù),思維混亂,有些過(guò)程只有自己寫(xiě)了才知道其中的坑。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java數(shù)據(jù)結(jié)構(gòu)和算法學(xué)習(xí)之漢諾塔示例
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)和算法中的漢諾塔示例,需要的朋友可以參考下2014-02-02
Java程序順序結(jié)構(gòu)中邏輯控制語(yǔ)句詳解流程
在程序開(kāi)發(fā)的過(guò)程之中一共會(huì)存在有三種程序邏輯:順序結(jié)構(gòu)、分支結(jié)構(gòu)、循環(huán)結(jié)構(gòu),對(duì)于之前所編寫(xiě)的代碼大部分都是順序結(jié)構(gòu)的定義,即:所有的程序?qū)凑斩x的代碼順序依次執(zhí)行2021-10-10
Java從內(nèi)存角度帶你理解數(shù)組名實(shí)質(zhì)是個(gè)地址的論述
這篇文章主要介紹了Java如何從內(nèi)存解析的角度理解“數(shù)組名實(shí)質(zhì)是一個(gè)地址”,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09
SpringBoot?JavaMailSender發(fā)送郵件功能(實(shí)例詳解)
JavaMailSender是Spring提供的,非常好用的,實(shí)現(xiàn)郵件發(fā)送的接口 ,這篇文章主要介紹了SpringBoot?JavaMailSender發(fā)送郵件功能,需要的朋友可以參考下2024-03-03
Java實(shí)現(xiàn)大文件的切割與合并操作示例
這篇文章主要介紹了Java實(shí)現(xiàn)大文件的切割與合并操作,結(jié)合實(shí)例形式分析了java基于io及util操作大文件按指定個(gè)數(shù)分割與合并相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
創(chuàng)建Java線(xiàn)程安全類(lèi)的七種方法
線(xiàn)程安全是指某個(gè)方法或某段代碼,在多線(xiàn)程中能夠正確的執(zhí)行,不會(huì)出現(xiàn)數(shù)據(jù)不一致或數(shù)據(jù)污染的情況,我們把這樣的程序稱(chēng)之為線(xiàn)程安全的,反之則為非線(xiàn)程安全的,下面這篇文章主要給大家介紹了關(guān)于創(chuàng)建Java線(xiàn)程安全類(lèi)的七種方法,需要的朋友可以參考下2022-06-06
java中實(shí)現(xiàn)對(duì)象排序的兩種方法(Comparable,Comparator)
這篇文章主要給大家介紹了關(guān)于java中實(shí)現(xiàn)對(duì)象排序的兩種方法,一種是實(shí)現(xiàn)Comparable進(jìn)行排序,另一種是實(shí)現(xiàn)Comparator進(jìn)行排序,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
SpringBoot+Redis Bitmap實(shí)現(xiàn)活躍用戶(hù)統(tǒng)計(jì)
Redis的Bitmap數(shù)據(jù)結(jié)構(gòu)是一種緊湊的位圖,它可以用于實(shí)現(xiàn)各種場(chǎng)景,其中統(tǒng)計(jì)活躍用戶(hù)是一種經(jīng)典的業(yè)務(wù)場(chǎng)景,下面我們就來(lái)學(xué)習(xí)一下SpringBoot如何利用Redis中的Bitmap實(shí)現(xiàn)活躍用戶(hù)統(tǒng)計(jì)吧2023-11-11

