java實(shí)現(xiàn)單機(jī)版五子棋小游戲
簡(jiǎn)單的java小游戲–單機(jī)版五子棋
學(xué)了java有一段時(shí)間了,今天就來搞一個(gè)簡(jiǎn)單的單機(jī)版五子棋游戲。
實(shí)現(xiàn)功能:那必須能進(jìn)行基礎(chǔ)的輸贏判斷。還有重新開始的功能,悔棋的功能,先手設(shè)置的功能和退出的功能。在右上角能夠顯示目前輪到哪個(gè)棋種下棋。右下角還有一個(gè)記錄信息框,記錄行為,當(dāng)信息量過多時(shí),可以清除信息內(nèi)容。
成果:
初始界面:

游戲(獲勝)界面:

附上代碼:
Chessframe.java
package firstgobang;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class ChessFrame extends JFrame{
public ChessBoard chessboard;
public ChessFrame(String title){
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle(title);
setVisible(true);
setLocationRelativeTo(null);
chessboard = new ChessBoard();
add(chessboard);
pack();
}
public static void main(String[] args) {
ChessFrame chessframe = new ChessFrame("單機(jī)版五子棋游戲");
}
}
ChessBoard.java
package firstgobang;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import java.util.Stack;
import javax.swing.*;
public class ChessBoard extends JComponent{
/*
*board為1時(shí)是白棋,為2時(shí)是黑棋,為0時(shí)是空
*whitenow為true時(shí),到白棋下,為false時(shí),到黑棋下
*empty為true時(shí),該位置為空,為false,該位置不為空
*win為true,某方勝利,為false,無勝利
*information用來儲(chǔ)存消息
*/
public static int x_start = 30;
public static int y_start = 60;
public static int size = 30;
public static int radius = 10;
private int[][] board = new int[19][19];
private boolean whitenow = false;
private boolean empty = true;
private boolean win = false;
private JTextArea area;
private String information="";
private static Stack<Chess> chessstack; //棧
class Chess{ //棋類,用于儲(chǔ)存棋子的x,y坐標(biāo)
int x;
int y;
public Chess(int x,int y) {
this.x=x;
this.y=y;
}
}
public ChessBoard() {
chessstack = new Stack<>();
area = new JTextArea(5,5);
JButton button1 = new JButton("重新開始");
JButton button2 = new JButton("悔棋");
JButton button3 = new JButton("退出");
JButton button4 = new JButton("先手設(shè)置");
JButton button5 = new JButton("清空消息");
JPanel panel = new JPanel();
JScrollPane js = new JScrollPane();
button1.setBounds(620,60,100,30);
button2.setBounds(620,110,100,30);
button3.setBounds(620,160,100,30);
button4.setBounds(620,210,100,30);
button5.setBounds(620,260,100,30);
panel.setBounds(600,310,140,300);
js.setBounds(600,310,140,300);
panel.setLayout(new BorderLayout());
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
panel.add(area);
js.getViewport().add(panel);
js.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(js);
button1.addMouseListener(new b1Action());
button2.addMouseListener(new b2Action());
button3.addMouseListener(new b3Action());
button4.addMouseListener(new b4Action());
button5.addMouseListener(new b5Action());
addMouseListener(new theMouseListener());
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.orange);
g.fillRect(x_start-size/2,y_start-size/2, size*19, size*19);
g.setColor(Color.black);
// 橫
for (int i = 0; i < 19; i++) {
g.drawLine(x_start, y_start + i * size, x_start+18*size, y_start + i * size);
g.drawString(((Integer)i).toString(),x_start/2-radius,y_start + i * size);
}
// 豎
for (int i = 0; i < 19; i++) {
g.drawLine(x_start + i * size, y_start, x_start + i * size, y_start+18*size);
g.drawString(((Integer)i).toString(),x_start + i * size,y_start/2+radius);
}
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
if (board[i][j] == 1) {
g.setColor(Color.white);
g.fillOval(x_start-radius + i * size, y_start-radius + j * size, radius*2,radius*2);
g.setColor(Color.black);
g.drawOval(x_start-radius + i * size, y_start-radius + j * size, radius*2,radius*2);
}
if (board[i][j] == 2) {
g.setColor(Color.black);
g.fillOval(x_start-radius + i * size, y_start-radius + j * size, radius*2,radius*2);
}
}
}
g.setFont(new Font("微軟雅黑",Font.BOLD,15));
g.drawString("現(xiàn)在輪到", 600, 35);
if(whitenow==true) {
g.setColor(Color.white);
g.fillOval(680, 20, 20,20);
g.setColor(Color.black);
g.drawOval(680, 20, 20,20);
}
if(whitenow==false) {
g.setColor(Color.black);
g.fillOval(680, 20, 20,20);
}
}
public Dimension getPreferredSize() {
return new Dimension(750,650);
}
public class theMouseListener implements MouseListener{ //下棋
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
int x=getx(e.getX());
int y=gety(e.getY());
try {
if(board[x][y] !=0)empty = false;
} catch (Exception e1) {
}
if (e.getX() > x_start-size/2 && e.getX() < x_start+size/2+18*size && e.getY() > y_start-size/2 && e.getY() < y_start+size/2+18*size) {
if(empty == true) {
Chess chess = new Chess(x,y);
chessstack.push(chess);
if (whitenow == true) {
writeinformation("白棋下了"+"("+x+","+y+")"+"的位置");
board[x][y]=1;
repaint();
}
if (whitenow == false){
writeinformation("黑棋下了"+"("+x+","+y+")"+"的位置");
board[x][y]=2;
repaint();
}
iswin(whitenow,x,y);
if(win==true) {
if(whitenow==true) {
writeinformation("白棋獲勝!");
JOptionPane.showInternalMessageDialog(null, "白棋獲勝",
"提示框", JOptionPane.INFORMATION_MESSAGE);
}
else {
writeinformation("黑棋獲勝!");
JOptionPane.showInternalMessageDialog(null, "黑棋獲勝",
"提示框", JOptionPane.INFORMATION_MESSAGE);
}
}
if(chessstack.size()==361) {
writeinformation("和局");
JOptionPane.showInternalMessageDialog(null, "和局",
"提示框", JOptionPane.INFORMATION_MESSAGE);
}
whitenow=!whitenow;
}
else {
JOptionPane.showInternalMessageDialog(null, "該位置已有棋子!",
"提示框", JOptionPane.INFORMATION_MESSAGE);
empty=true;
}
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
class b1Action implements MouseListener{ //重新開始按鈕
public void mouseClicked(MouseEvent e) {
int a = JOptionPane.showConfirmDialog(null,
"你確定重新開始?", "提示框", JOptionPane.YES_NO_OPTION);
if(a==0) cleanstart();
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
class b2Action implements MouseListener{ //悔棋按鈕
public void mouseClicked(MouseEvent e) {
int a = JOptionPane.showConfirmDialog(null,
"你確定悔棋?", "提示框", JOptionPane.YES_NO_OPTION);
if(a==0) {
if(chessstack.size()>0) {
Chess chess1 = chessstack.pop();
if(whitenow)writeinformation("黑棋悔棋,坐標(biāo)"+"("+chess1.x+","+chess1.y+")");
if(!whitenow)writeinformation("白棋悔棋,坐標(biāo)"+"("+chess1.x+","+chess1.y+")");
board[chess1.x][chess1.y]=0;
whitenow=!whitenow;
repaint();
}
else {
JOptionPane.showInternalMessageDialog(null, "不能在悔棋了!",
"提示框", JOptionPane.INFORMATION_MESSAGE);
}
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
class b3Action implements MouseListener{ //退出按鈕
public void mouseClicked(MouseEvent e) {
int a = JOptionPane.showConfirmDialog(null,
"你確定退出游戲?", "提示框", JOptionPane.YES_NO_OPTION);
if(a==0) {
System.exit(0);
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
class b4Action implements MouseListener{ //先手設(shè)置按鈕
public void mouseClicked(MouseEvent e) {
if(chessstack.size()==0) {
Object[] possibleValues = { "白棋", "黑棋", "隨機(jī)" };
Object a = JOptionPane.showInputDialog(null,
"選擇先手的棋子", "提示框",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);
if(a=="白棋") whitenow=true;
if(a=="黑棋") whitenow=false;
if(a=="隨機(jī)") {
Random random = new Random();
int b =random.nextInt(2);
if(b==0)whitenow=true;
if(b==1)whitenow=false;
}
repaint();
}
else {
JOptionPane.showInternalMessageDialog(null, "戰(zhàn)局已經(jīng)開始,不能設(shè)置",
"提示框", JOptionPane.INFORMATION_MESSAGE);
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
class b5Action implements MouseListener{ //清空消息按鈕
public void mouseClicked(MouseEvent e) {
int a = JOptionPane.showConfirmDialog(null,
"你確定清空所有消息?", "提示框", JOptionPane.YES_NO_OPTION);
if(a==0) {
information="";
area.setText(information);
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
public void writeinformation(String infor){ //消息寫入
information +=infor+"\n";
area.setText(information);
}
public boolean iswin(boolean whitenow,int startx,int starty) { //勝利判斷
int color = whitenow?1:2;
int count = 1;
int x=1;
int y=1;
//橫
while((startx-x)>-1 && board[startx-x][starty]==color) {
count++;
x++;
}
x=y=1;
while((startx+x)<19 && board[startx+x][starty]==color) {
count++;
x++;
}
if(count>=5) {
return win = true;
}
count=x=y=1;
//豎
while((starty-y)>-1 && board[startx][starty-y]==color) {
count++;
y++;
}
x=y=1;
while((starty+y)<19 && board[startx][starty+y]==color) {
count++;
y++;
}
if(count>=5) {
return win = true;
}
count=x=y=1;
//45右斜
while((startx+x)<19 && (starty-y)>-1 && board[startx+x][starty-y]==color) {
count++;
x++;
y++;
}
x=y=1;
while((startx-x)>-1 && (starty+y)<19 && board[startx-x][starty+y]==color) {
count++;
x++;
y++;
}
if(count>=5) {
return win = true;
}
count=x=y=1;
//135左斜
while((startx-x)>0 && (starty-y)>0 && board[startx-x][starty-y]==color) {
count++;
x++;
y++;
}
x=y=1;
while((startx+x)<19 && (starty+y)<19 && board[startx+x][starty+y]==color) {
count++;
x++;
y++;
}
if(count>=5) {
return win = true;
}
return false;
}
private void cleanstart() { //清理棋盤
for(int i=0;i<19;i++) {
for(int j=0;j<19;j++) {
board[i][j]=0;
}
}
win=false;
chessstack.clear();
writeinformation("重新開始戰(zhàn)局!");
repaint();
}
private int getx(int x) { //x歸位
x -=x_start;
if(x%size<(size/2)) {
return x/size;
}
else {
return x/size+1;
}
}
private int gety(int y) { //y歸位
y -=y_start;
if(y%size<(size/2)) {
return y/size;
}
else {
return y/size+1;
}
}
}
End!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java簡(jiǎn)易小游戲制作代碼
- java實(shí)現(xiàn)2048小游戲(含注釋)
- java實(shí)現(xiàn)簡(jiǎn)單的猜數(shù)字小游戲
- Java實(shí)現(xiàn)2048小游戲(可直接運(yùn)行)
- java實(shí)現(xiàn)單詞小游戲
- java實(shí)現(xiàn)象棋小游戲
- 利用Java編寫24點(diǎn)小游戲的實(shí)例代碼
- java面向?qū)ο笾藱C(jī)猜拳小游戲
- Java實(shí)現(xiàn)天天酷跑小游戲完整代碼(附源碼)
- Java實(shí)現(xiàn)簡(jiǎn)單的五子棋小游戲
- 利用java開發(fā)簡(jiǎn)易版掃雷游戲
相關(guān)文章
RocketMQ線程池創(chuàng)建實(shí)現(xiàn)原理詳解
這篇文章主要為大家介紹了RocketMQ線程池創(chuàng)建實(shí)現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Java實(shí)現(xiàn)讀取文件夾下(包括子目錄)所有文件的文件名
這篇文章主要介紹了Java實(shí)現(xiàn)讀取文件夾下(包括子目錄)所有文件的文件名,本文把代碼組織成了一個(gè)模塊,可以很方便的使用,需要的朋友可以參考下2015-06-06
Java中的SynchronousQueue阻塞隊(duì)列使用代碼實(shí)例
這篇文章主要介紹了Java中的SynchronousQueue阻塞隊(duì)列使用代碼實(shí)例,SynchronousQueue是無緩沖區(qū)的阻塞隊(duì)列,即不能直接向隊(duì)列中添加數(shù)據(jù),會(huì)報(bào)隊(duì)列滿異常,需要的朋友可以參考下2023-12-12
Java實(shí)現(xiàn)生成pdf并解決表格分割的問題
這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)生成pdf,并解決表格分割的問題,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
Java安全框架——Shiro的使用詳解(附springboot整合Shiro的demo)
這篇文章主要介紹了Java安全框架——Shiro的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用Shiro,感興趣的朋友可以了解下2021-04-04
SpringBoot異步線程父子線程數(shù)據(jù)傳遞的5種方式
這篇文章主要介紹了SpringBoot異步線程父子線程數(shù)據(jù)傳遞的5種方式,文中通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-08-08

