这是我写的另一个小游戏,界面什么的不美观,先来看下效果神马的。

曾经看过一部动漫,叫做秦时明月,里面就有这样一种棋,里面的叫法是墨攻棋局,突然想到,我也就想写写看了,花了些小时写好了这货。。。

看下源码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;import javax.swing.*;public class Reversi extends JFrame implements ActionListener{JButton jbStart = new JButton("开始游戏");JButton jbStop = new JButton("结束游戏");MyPanel panel = new MyPanel();Container c = this.getContentPane();int width = Toolkit.getDefaultToolkit().getScreenSize().width;int height = Toolkit.getDefaultToolkit().getScreenSize().height;boolean canPlay = false;boolean isBlack = false;int count = 0;int f = 1;int cntBlack = 0;int cntWhite = 0;int reversi[][] = new int[9][9];public Reversi() {Init();addListener();}public void Init() {this.setTitle("Reversi");this.setSize(500, 500);this.setResizable(false);this.setLayout(null);this.setVisible(true);this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);this.setLocation((width - 500)/2, (height - 500)/2);jbStart.setBounds(135, 430, 100, 30);jbStop.setBounds(265, 430, 100, 30);panel.add(jbStart);panel.add(jbStop);panel.setLayout(null);panel.setSize(500, 500);c.add(panel);}public void addListener() {this.jbStart.addActionListener(this);this.jbStop.addActionListener(this);this.addMouseListener(new MouseListener() {public void mouseReleased(MouseEvent e) {// TODO Auto-generated method stub}public void mousePressed(MouseEvent e) {// TODO Auto-generated method stubif(canPlay) {if(e.getX() >= 50 && e.getX() <= 450 && e.getY() >= 50 && e.getY() <= 450) {//System.out.println(e.getX() + "---" + e.getY());int x = e.getX();int y = e.getY();x = (x - 50) / 50;y = (y - 50) / 50;
//                      System.out.println(x + " - " + y);if(reversi[x][y] == 0) {if(isBlack) {if(!isOK(x, y, 1))return;reversi[x][y] = 1;//System.out.println(x + "-" + y + reversi[x][y]);//System.out.println(reversi[4][3]);isEated(x, y);repaint();//System.out.println(reversi[4][3]);isBlack = false;if(Check(2)) {if(cntBlack > cntWhite)JOptionPane.showMessageDialog(null, "黑子多于白子 ,黑方赢");else if(cntBlack < cntWhite)JOptionPane.showMessageDialog(null, "白子多余黑子,白方赢");elseJOptionPane.showMessageDialog(null, "双方不分胜负!");}System.out.println(cntBlack + "---" + cntWhite);}else {if(!isOK(x, y, 2))return;reversi[x][y] = 2;isEated(x, y);repaint();isBlack = true;if(Check(1)) {if(cntBlack > cntWhite)JOptionPane.showMessageDialog(null, "黑子多于白子 ,黑方赢");else if(cntBlack < cntWhite)JOptionPane.showMessageDialog(null, "白子多余黑子,白方赢");elseJOptionPane.showMessageDialog(null, "双方不分胜负!");}System.out.println(cntBlack + "---" + cntWhite);}}repaint();}}}public void mouseExited(MouseEvent e) {// TODO Auto-generated method stub}public void mouseEntered(MouseEvent e) {// TODO Auto-generated method stub}public void mouseClicked(MouseEvent e) {// TODO Auto-generated method stub}});}public void actionPerformed(ActionEvent e) {if(e.getSource() == jbStart) {canPlay = true;jbStart.setEnabled(false);InitFrame();repaint();}if(e.getSource() == jbStop) {System.exit(0);}}public class MyPanel extends JPanel {public void paintComponent(Graphics scr){BufferedImage bi = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);Graphics g = bi.createGraphics();g.setColor(Color.BLACK);for(int i = 1; i <= 9; i++) {g.drawLine(i*50, 20, i*50, 420);}for(int i = 1; i <= 9; i++) {g.drawLine(50, i*50-30, 450, i*50-30);}for(int i = 0; i <= 7; i++) {for(int j = 0; j <= 7; j++) {int x;int y;x = i * 50 + 50;y = j * 50 + 20;if(reversi[i][j] == 1) {g.fillOval(x, y, 50, 50);}else if(reversi[i][j] == 2) {g.setColor(Color.WHITE);g.fillOval(x, y, 50, 50);g.setColor(Color.BLACK);g.drawOval(x, y, 50, 50);}}}scr.drawImage(bi, 0, 0, this);}}public void isEated(int x, int y) {int color;if(isBlack)color = 1;elsecolor = 2;int flag = 0;int tempx = x;int tempy = y;while(tempx > 0) { //左tempx--;if(reversi[tempx][tempy] == color) {for(int i = tempx + 1; i <= x; i++) {if(reversi[i][tempy] == 0)flag = 1;}if(flag == 1)break;for(int i = tempx + 1; i <= x; i++) {reversi[i][tempy] = color;}break;}}flag = 0;tempx = x;tempy = y;while(tempx < 7) { //右tempx++;if(reversi[tempx][tempy] == color) {for(int i = x + 1; i <= tempx; i++) {if(reversi[i][tempy] == 0)flag = 1;}if(flag == 1)break;for(int i = x + 1; i <= tempx; i++) {reversi[i][tempy] = color;}break;}}flag = 0;tempx = x;tempy = y;while(tempy > 0) { //上tempy--;if(reversi[tempx][tempy] == color) {for(int i = tempy + 1; i <= y; i++) {if(reversi[tempx][i] == 0) flag = 1;}if(flag == 1)break;for(int i = tempy + 1; i <= y; i++) {reversi[tempx][i] = color;}break;}}flag = 0;tempx = x;tempy = y;while(tempy < 7) { //下tempy++;if(reversi[tempx][tempy] == color) {for(int i = y + 1; i <= tempy; i++) {if(reversi[tempx][i] == 0)flag = 1;}if(flag == 1)break;for(int i = y + 1; i <= tempy; i++) {reversi[tempx][i] = color;}break;}}flag = 0;tempx = x;tempy = y;while(tempx > 0 && tempy > 0) { //左上tempx--;tempy--;if(reversi[tempx][tempy] == color) {for(int i = tempx + 1, j  = tempy + 1; i <= x && j <= y; i++, j++) {if(reversi[i][j] == 0)flag = 1;}if(flag == 1)break;for(int i = tempx + 1, j  = tempy + 1; i <= x && j <= y; i++, j++) {reversi[i][j] = color;}break;}}flag = 0;tempx = x;tempy = y;while(tempx > 0 && tempy < 7) { //左下tempx--;tempy++;if(reversi[tempx][tempy] == color) {for(int i = x - 1, j = y + 1; j < tempy; i--, j++) {if(reversi[i][j] == 0)flag = 1;}if(flag == 1)break;for(int i = x - 1, j = y + 1; j < tempy; i--, j++) {reversi[i][j] = color;}break;}}flag = 0;tempx = x;tempy = y;while(tempx < 7 && tempy > 0) { //右上tempx++;tempy--;if(reversi[tempx][tempy] == color) {//System.out.println("11");for(int i = x + 1, j  = y - 1; i < tempx; i++, j--) {//System.out.println(i + "--" + j + reversi[i][j]);if(reversi[i][j] == 0) {flag = 1;System.out.println(i + "----" + j + reversi[i][j]);}}if(flag == 1)break;for(int i = x + 1, j  = y - 1; i < tempx; i++, j--) {reversi[i][j] = color;}break;}}flag = 0;tempx = x;tempy = y;while(tempx < 7 && tempy < 7) { //右下tempx++;tempy++;if(reversi[tempx][tempy] == color) {for(int i = x + 1, j  = y + 1; i <= tempx && j <= tempy; i++, j++) {if(reversi[i][j] == 0)flag = 1;}if(flag == 1)break;for(int i = x + 1, j  = y + 1; i <= tempx && j <= tempy; i++, j++) {reversi[i][j] = color;}break;}}}public boolean isOK(int x, int y, int color) {f = 0;int flag = 0;int tempx = x;int tempy = y;while(tempx > 0) { //左tempx--;if(reversi[tempx][tempy] == color) {for(int i = tempx + 1; i < x; i++) {if(reversi[i][tempy] == 0)flag = 1;}if(flag == 1)break;for(int i = tempx + 1; i < x; i++) {//reversi[i][tempy] = color;f = 1;}break;}}flag = 0;tempx = x;tempy = y;while(tempx < 7) { //右tempx++;if(reversi[tempx][tempy] == color) {for(int i = x + 1; i < tempx; i++) {if(reversi[i][tempy] == 0)flag = 1;}if(flag == 1)break;for(int i = x + 1; i < tempx; i++) {//reversi[i][tempy] = color;f = 1;}break;}}flag = 0;tempx = x;tempy = y;while(tempy > 0) { //上tempy--;if(reversi[tempx][tempy] == color) {for(int i = tempy + 1; i < y; i++) {if(reversi[tempx][i] == 0) flag = 1;}if(flag == 1)break;for(int i = tempy + 1; i < y; i++) {//reversi[tempx][i] = color;f = 1;}break;}}flag = 0;tempx = x;tempy = y;while(tempy < 7) { //下tempy++;if(reversi[tempx][tempy] == color) {for(int i = y + 1; i < tempy; i++) {if(reversi[tempx][i] == 0)flag = 1;}if(flag == 1)break;for(int i = y + 1; i < tempy; i++) {//reversi[tempx][i] = color;f = 1;}break;}}flag = 0;tempx = x;tempy = y;while(tempx > 0 && tempy > 0) { //左上tempx--;tempy--;if(reversi[tempx][tempy] == color) {for(int i = tempx + 1, j  = tempy + 1; i < x && j < y; i++, j++) {if(reversi[i][j] == 0)flag = 1;}if(flag == 1)break;for(int i = tempx + 1, j  = tempy + 1; i < x && j < y; i++, j++) {//reversi[i][j] = color;f = 1;}break;}}flag = 0;tempx = x;tempy = y;while(tempx > 0 && tempy < 7) { //左下tempx--;tempy++;if(reversi[tempx][tempy] == color) {for(int i = x - 1, j = y + 1; j < tempy; i--, j++) {if(reversi[i][j] == 0)flag = 1;}if(flag == 1)break;for(int i = x - 1, j = y + 1; j < tempy; i--, j++) {//    reversi[i][j] = color;f = 1;}break;}}flag = 0;tempx = x;tempy = y;while(tempx < 7 && tempy > 0) { //右上tempx++;tempy--;if(reversi[tempx][tempy] == color) {//System.out.println("11");for(int i = x + 1, j  = y - 1; i < tempx; i++, j--) {//System.out.println(i + "--" + j + reversi[i][j]);if(reversi[i][j] == 0) {flag = 1;//System.out.println(i + "----" + j + reversi[i][j]);}}if(flag == 1)break;for(int i = x + 1, j  = y - 1; i < tempx; i++, j--) {//reversi[i][j] = color;f = 1;}break;}}flag = 0;tempx = x;tempy = y;while(tempx < 7 && tempy < 7) { //右下tempx++;tempy++;if(reversi[tempx][tempy] == color) {for(int i = x + 1, j  = y + 1; i < tempx && j < tempy; i++, j++) {if(reversi[i][j] == 0)flag = 1;}if(flag == 1)break;for(int i = x + 1, j  = y + 1; i < tempx && j < tempy; i++, j++) {//reversi[i][j] = color;f = 1;}break;}}if(f == 1)return true;elsereturn false;}public boolean Check(int color) {//        for(int i = 0; i <= 7; i++) {
//          for(int j = 0; j <= 7; j++) {
//              if(reversi[i][j] == color)
//                  return false;
//              else if(reversi[i][j] == color % 2 + 1)
//                  count++;
//          }
//      }
//      return true;cntBlack = 0;cntWhite = 0;int cnt = 0;for(int i = 0; i <= 7; i++) {for(int j = 0; j <= 7; j++) {if(reversi[i][j] == 1) {cntBlack++;}else if(reversi[i][j] == 2) {cntWhite++;}else {cnt++;}}}for(int i = 0; i <= 7; i++) {for(int j = 0; j <= 7; j++) {if(reversi[i][j] == 0 && isOK(i, j, color))return false;}}JOptionPane.showMessageDialog(null, color == 1?"黑子无处可下,换白子行动":"白子无处可下,换黑子行动");color = color % 2 + 1;for(int i = 0; i <= 7; i++) {for(int j = 0; j <= 7; j++) {if(reversi[i][j] == 0 && isOK(i, j, color)) {if(color == 1) {isBlack = true;}else {isBlack = false;}return false;}}}JOptionPane.showMessageDialog(null, "双方均无处可下,游戏结束");return true;}public void InitFrame() {reversi[3][3] = reversi[4][3] = 1;reversi[3][4] = reversi[4][4] = 2;}public static void main(String[] args) {// TODO Auto-generated method stubnew Reversi();}
}

游戏基本没什么BUG了,我改了好久T T,注释什么的也没写,哎呀,好失败-。-算了,就那样了~

有什么问题欢迎指出。。。

JAVA之翻转棋游戏相关推荐

  1. python 黑白棋_python实现翻转棋游戏(othello)

    利用上一篇的框架,再写了个翻转棋的程序,为了调试minimax算法,花了两天的时间. 几点改进说明: 拆分成四个文件:board.py,player.py,ai.py,othello.py.使得整个结 ...

  2. 翻转棋游戏c语言讲解,有没有人懂黑白棋(翻转棋)的核心算法

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 playchess(int i,int j) { int k,s,m,n,p=i,q=j; if(a[i][j]=2)/黑吃白 { s=i+1; whil ...

  3. java四连环游戏编程_Java实现四连环棋游戏

    本文实例为大家分享了Java实现四连环棋游戏的具体代码,供大家参考,具体内容如下 游戏规则: (1)双人游戏,有黑红两色棋子,双方各执一色棋子. (2)空棋局开盘,黑棋先发,从最上面一行开始下,棋子会 ...

  4. java 井字棋 人机_一个井字棋tictactoe游戏的java实现 | Soo Smart!

    这是一个井字棋游戏的java实现.摘录于stackoverflow. 游戏规则很简单,只要一方棋子在水平线,垂直线或者对角线任意一条线上排列成功即为获胜. 作者原先的代码存在着一些问题: 代码如下: ...

  5. Java版战棋(SLG)游戏AI及寻径处理入门

    代码下载地址:http://download.csdn.net/source/1047937 SLG或者说战棋游戏,在大多数英文站点是归类到Simulation Game的(包括模拟城市之类的纯SIM ...

  6. java——博弈算法实现井字棋游戏

    通过java语言开发了一个简单的井字棋游戏.主要有6个类,其中有一个是主类(Main.java),一个是抽象类(PiecesMove.java)组成. 下面对各个类简单介绍一下: TicTicToe. ...

  7. [Python] 黑白棋(翻转棋)小游戏

    [Python] 黑白棋(翻转棋)小游戏 游戏介绍 黑白棋(Reversi or Othello)在西方和日本很流行.游戏通过相互翻转对方的棋子,最后以棋盘上谁的棋子多来判断胜负. 规则 黑白棋的每颗 ...

  8. c语言翻转棋ai算法,黑白棋游戏(也叫翻转棋)(AI 版)

    黑白棋(也叫翻转棋)的棋盘是一个有8*8方格的棋盘.下棋时将棋下在空格中间,而不是像围棋一样下在交叉点上.开始时在棋盘正中有两白两黑四个棋子交叉放置,黑棋总是先下子. 下子的方法:把自己颜色的棋子放在 ...

  9. java象棋游戏用户特点_java-象棋游戏设计心得

    这是我的第一篇关于技术博客,额...不对,只能算是我的设计心得吧,因为我也不是一直从设计到完工负责过来的,我只是在别人的基础上进行理解.改正.调整.添加而已...这篇博客不预设会有人来看,但我还是会写 ...

最新文章

  1. 提高C#编程水平的50个要诀[转载]
  2. OpenGL Primitive Restart原始重启的实例
  3. 科普:手机里的陀螺仪到底是什么
  4. 职场提醒:面试失败n次以后
  5. TCPIP协议卷2之io中断
  6. html5录音支持pc和Android、ios部分浏览器,微信也是支持的,JavaScript getUserMedia
  7. BZOJ1018 堵塞的交通(线段树)
  8. 虚拟机中的linux系统无法获得ip(ifconfig命令无法查到ip)
  9. 初识计算机编程语言教案,完整版,初识VB教学设计
  10. SQL Server 2014 虚拟机的自动备份 (Resource Manager)
  11. 全球名校AI课程库(36)| 辛辛那提大学 · 离散数学课程『MATH1071 Discrete Math』
  12. 【读书笔记】-《软件测试的艺术》
  13. 记一次Full GC(Ergonomics)引发的思考
  14. SEM: 科研图片处理
  15. signature=c2533d7d1f3a9e27480c43aef903d102,2 AUTHORS, INCLUDING: Uwe Helmke
  16. 打破清晨喧嚣的 oom
  17. 网络安全工程师做什么?
  18. 企业三层架构、冗余、STP生成树协议总结
  19. html热区坐标,HTML之六:图像的热区连接
  20. 【转】微信小游戏开发源码_教程_工具_资源最新集合

热门文章

  1. Linux_操作系统-基础操作-教学
  2. 浏览器极速模式和兼容模式差异 1
  3. 行内元素、块元素、行内块元素的区别
  4. Phpstorm+phpstudy组合配置开发环境(Win端)
  5. For Developer-友盟+官网体验升级的打开方式
  6. Python-pytest、unittest
  7. python刷今日头条访问量_Python 自动刷博客浏览量实例代码
  8. vscode实用快捷键查找和替换
  9. 分布式事务解决方案和原理
  10. 网络基本概念之TCP, UDP, 单播(Unicast), 组播(Multicast)