简单的java小游戏–单机版五子棋

学了java有一段时间了,今天就来搞一个简单的单机版五子棋游戏。

实现功能:那必须能进行基础的输赢判断。还有重新开始的功能,悔棋的功能,先手设置的功能和退出的功能。在右上角能够显示目前轮到哪个棋种下棋。右下角还有一个记录信息框,记录行为,当信息量过多时,可以清除信息内容。

成果:

初始界面:

游戏(获胜)界面:

附上代码:

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("单机版五子棋游戏");

}

}

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时是白棋,为2时是黑棋,为0时是空

*whitenow为true时,到白棋下,为false时,到黑棋下

*empty为true时,该位置为空,为false,该位置不为空

*win为true,某方胜利,为false,无胜利

*information用来储存消息

*/

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 chessstack; //栈

class Chess{ //棋类,用于储存棋子的x,y坐标

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("先手设置");

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("现在轮到", 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("黑棋悔棋,坐标"+"("+chess1.x+","+chess1.y+")");

if(!whitenow)writeinformation("白棋悔棋,坐标"+"("+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{ //先手设置按钮

public void mouseClicked(MouseEvent e) {

if(chessstack.size()==0) {

Object[] possibleValues = { "白棋", "黑棋", "随机" };

Object a = JOptionPane.showInputDialog(null,

"选择先手的棋子", "提示框",

JOptionPane.INFORMATION_MESSAGE, null,

possibleValues, possibleValues[0]);

if(a=="白棋") whitenow=true;

if(a=="黑棋") whitenow=false;

if(a=="随机") {

Random random = new Random();

int b =random.nextInt(2);

if(b==0)whitenow=true;

if(b==1)whitenow=false;

}

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 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("重新开始战局!");

repaint();

}

private int getx(int x) { //x归位

x -=x_start;

if(x%size

return x/size;

}

else {

return x/size+1;

}

}

private int gety(int y) { //y归位

y -=y_start;

if(y%size

return y/size;

}

else {

return y/size+1;

}

}

}

End!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

单机java_java实现单机版五子棋小游戏相关推荐

  1. 五子棋聊天java_Java如何实现五子棋小游戏(1)

    1. 前言 该项目为经典版本的五子棋游戏和自创的毁灭玩法所结合,总体而言是一个休闲的小游戏.其中的规则不难,主要是为了丰富大家的文娱生活,让大家在忙碌的学习课后可以轻松一小下.这就是本程序的编写初衷. ...

  2. C语言---简单五子棋小游戏

    效果图如下: 设计思路: 棋盘设计为15×15格,初始状态光标在棋盘的中央,白棋先走,轮流落子,当一方连成五子或下满棋盘时,游戏结束(连成五子的一方获胜,下满棋盘为和棋).当游戏一方胜利后显示胜利信息 ...

  3. Python:五子棋小游戏

    临近期末考试,班主任把所有的不参与考试的副科课程全都停了,天天语数外历史物理,实在是无聊,同学们便在课间互相约战五子棋,棋盘便是平时写作业用的玛丽大号写字本,棋子就是xo,不到两个星期,我就成功地用完 ...

  4. Java实现五子棋小游戏(附思路讲解,全部代码,游戏截图)

    本文章是如何实现一个单机版双人五子棋小游戏,通过Swing技术进行可视操作. 个人简介:

  5. Java编写的五子棋小游戏

    看书的时候看到一个不完整的Java编写的一个五子棋小游戏,为了恢复一下编程能力刚刚把这个小程序完成了. 实现的功能很简单,两人对下五子棋,程序自动回判断输赢.在ubuntu下搞得,没有装什么高端的输入 ...

  6. 简单的5*5,五子棋小游戏

    使用C语言简单的实现棋盘为5*5的五子棋小游戏,以下为源代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...

  7. php 设计五子棋游戏,基于js+canvas实现五子棋小游戏

    本文实例为大家分享了js+canvas实现五子棋小游戏的具体代码,供大家参考,具体内容如下 效果展示: 源码展示: 五子棋 * { margin: 0; padding: 0; } body { ma ...

  8. 基于 Blazor 开发五子棋小游戏

    今天是农历五月初五,端午节.在此,祝大家端午安康! 端午节是中华民族古老的传统节日之一.端午也称端五,端阳.此外,端午节还有许多别称,如:午日节.重五节.五月节.浴兰节.女儿节.天中节.地腊.诗人节. ...

  9. java 五子棋项目_Java项目如何实现五子棋小游戏

    Java项目如何实现五子棋小游戏 发布时间:2020-07-21 14:53:06 来源:亿速云 阅读:77 作者:小猪 小编这次要给大家分享的是Java项目如何实现五子棋小游戏,文章内容丰富,感兴趣 ...

最新文章

  1. .Net Framework中的委托与事件
  2. 第一回写的用arraylist模拟栈操作
  3. OMG: daily scrum six
  4. 菜鸟级springmvc+spring+mybatis整合开发用户登录功能(下)
  5. NYOJ 137 取石子(三)
  6. java set去重复元素_java List去掉重复元素的几种方式
  7. Python实例讲解 -- wxpython 基本的控件 (按钮)
  8. Iocomp .net仿真仪表控件包
  9. 解决Ubuntu刚装好的时候su命令密码错误的问题
  10. 三年研发、数亿美元成本,Mate 20的“大杀器”麒麟980是怎样炼成的?
  11. 人手一份的Java面试精选题,你值得拥有!
  12. 力扣-451 根据字符出现频率排序
  13. ctex linux安装_Deepin Linux 安装和搭建LaTex环境
  14. 密码系列-Base32
  15. 计算机绘图作业西南交大,西南交大计算机绘图A 离线作业.doc
  16. 小米商城——HTML,CSS(附:源码)
  17. 1234变4321java_java:把1234成4321整数倒逆代码
  18. Android9怎样安装xposed,EdXposed 在android 9 上的安装和使用
  19. 尚硅谷python部分学习笔记
  20. 快乐二级域名分发程序-美化版源码

热门文章

  1. starccm+电池包热管理-新能源汽车电池包共轭传热仿真
  2. scala:查询圆周率pi
  3. 2016蓝桥杯报纸页数(C++C组)
  4. 春招面试经验系列(一)菜鸟网络
  5. 网络骗子的特征。大家一定要转载。
  6. 计算机老师新年贺卡祝福语,新年贺卡祝老师祝福语精选
  7. 新闻爬虫步骤python_抓取新闻网站:异步爬虫实现的流程和细节
  8. mongodb遍历万亿级数据,论索引的重要性
  9. 基于51单片机的温室大棚环境检测系统
  10. 仿生学导论学习笔记——第二章