通过 上下左右 控制棋盘走动  空格落子   (深度优先搜索)

package day_1;import java.awt.*;
import javax.swing.*;import java.awt.event.*;public class CircleRun extends JFrame {/*** */MyPanel mp = null;public static void main(String[] args) {CircleRun cr = new CircleRun();}public CircleRun() {mp = new MyPanel();this.add(mp);this.addKeyListener(mp);this.setTitle("双人五子棋正式版3.0");this.setSize(518, 538);// 设置窗体大小this.setLocation(340, 50);// 设置出现的位置this.setVisible(true);// 设置为可见this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JOptionPane.showMessageDialog(this, "双人五子棋游戏说明:通过←↑→↓控制旗子走向,空格下子先满五个子胜利", "游戏说明", JOptionPane.WARNING_MESSAGE);}// 游戏说明
}class Text2Frame {// 设置一个交互框JTextField jt1 = new JTextField(); // new一个文本框JTextArea jt2 = new JTextArea(); // new一个文本区JTextArea jt4 = new JTextArea(); // new一个文本区Text2Frame() {JScrollPane jsp = new JScrollPane(jt2); // new一个滚条String title = "正在思考。。。。";JFrame jf = new JFrame(title);Container container = jf.getContentPane();// container.setBackground(Color.yellow); //容器的背景色jf.setVisible(true); // 窗体可见jf.setLocation(150, 50);jf.setSize(300, 150); // 窗体大小jf.setLayout(new BorderLayout()); // 边界布局jf.add(BorderLayout.NORTH, jt1); // 文本框边界顶部放置jt1.setBackground(Color.yellow);jf.add(BorderLayout.CENTER, jt2); // 文本框边界中间放置jf.add(BorderLayout.EAST, jt4); // 文本框边界中间放置jt2.setBackground(Color.red);jt2.setFont(new Font("宋体", Font.PLAIN, 30));}void add(String a) {jt2.setText(a);}};class Text3Frame {JTextField jt1 = new JTextField(); // new一个文本框JTextArea jt2 = new JTextArea(); // new一个文本区JTextArea jt4 = new JTextArea(); // new一个文本区Text3Frame() {JScrollPane jsp = new JScrollPane(jt2); // new一个滚条String title = "当前坐标";JFrame jf = new JFrame(title);Container container = jf.getContentPane();// container.setBackground(Color.yellow); //容器的背景色jf.setVisible(true); // 窗体可见jf.setLocation(140, 180);jf.setSize(300, 200); // 窗体大小jf.setLayout(new BorderLayout()); // 边界布局jf.add(BorderLayout.NORTH, jt1); // 文本框边界顶部放置jt1.setBackground(Color.yellow);jf.add(BorderLayout.CENTER, jt2); // 文本框边界中间放置jf.add(BorderLayout.EAST, jt4); // 文本框边界中间放置jt2.setBackground(Color.green);jt2.setFont(new Font("宋体", Font.PLAIN, 30));}void add(String a) {jt2.setText(a);}String shu(int a, int b) {return "当前坐标(" + a + "," + b + ")";}};// 定义自己的面板
class MyPanel extends JPanel implements KeyListener {/*** */private static final long serialVersionUID = 4154597541232213984L;Text2Frame txw = new Text2Frame();Text3Frame txw3 = new Text3Frame();static JFrame sc = new JFrame();static int jishu = 1;static int summm = 1;static int summm2 = 1;static int arr[][] = new int[11][12];int x = 5;int y = 5;int sum = 0;int sum2 = 0;Node n1 = new Node(x, y);seqlist kai = new seqlist(n1);seqlist seq = new seqlist(null);seqlist seq2 = new seqlist(null);static void soushang(int a, int b) {if (a - 1 < 1) {return;}if (arr[a - 1][b] == 1 && a - 1 > 0) {summm++;if (summm == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "红色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}soushang(a - 1, b);}}static void souxia(int a, int b) {if (a + 1 > 10) {return;}if (arr[a + 1][b] == 1 && a + 1 <= 10) {summm++;if (summm == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "红色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}souxia(a + 1, b);}}static void souzuo(int a, int b) {if (b - 1 < 1) {return;}if (arr[a][b - 1] == 1 && b - 1 >= 1) {summm++;if (summm == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "红色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}souzuo(a, b - 1);}}static void souyou(int a, int b) {if (b + 1 > 10) {return;}if (arr[a][b + 1] == 1 && b + 1 <= 10) {summm++;if (summm == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "红色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}souyou(a, b + 1);}}static void soushangzuo(int a, int b) {if (a - 1 < 1 || b - 1 < 1) {return;}if (arr[a - 1][b - 1] == 1 && (a - 1 > 0) && (b - 1) > 0) {summm++;if (summm == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "红色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}soushangzuo(a - 1, b - 1);}}static void souxiazuo(int a, int b) {if (a + 1 > 10 || b - 1 < 1) {return;}if (arr[a + 1][b - 1] == 1 && (a + 1 <= 10) && (b - 1) >= 1) {summm++;if (summm == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "红色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}souxiazuo(a + 1, b - 1);}}static void souyoushang(int a, int b) {if (a - 1 < 1 && b + 1 > 10) {return;}if (arr[a - 1][b + 1] == 1 && a - 1 >= 1 && b + 1 <= 10) {summm++;if (summm == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "红色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}souyoushang(a - 1, b + 1);}}static void souyouxia(int a, int b) {if (b + 1 > 10 && a + 1 > 10) {return;}if (arr[a + 1][b + 1] == 1 && b + 1 <= 10 && a + 1 <= 10) {summm++;if (summm == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "红色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}souyouxia(a + 1, b + 1);}}static void soushang2(int a, int b) {if (a - 1 < 1) {return;}if (arr[a - 1][b] == 2 && a - 1 > 0) {summm2++;if (summm2 == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "绿色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}soushang2(a - 1, b);}}static void souxia2(int a, int b) {if (a + 1 > 10) {return;}if (arr[a + 1][b] == 2 && a + 1 <= 10) {summm2++;if (summm2 == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "绿色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}souxia2(a + 1, b);}}static void souzuo2(int a, int b) {if (b - 1 > 10) {return;}if (arr[a][b - 1] == 2 && b - 1 >= 1) {summm2++;if (summm2 == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "绿色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}souzuo2(a, b - 1);}}static void souyou2(int a, int b) {if (b + 1 > 10) {return;}if (arr[a][b + 1] == 2 && b + 1 <= 10) {summm2++;if (summm2 == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "绿色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}souyou2(a, b + 1);}}static void soushangzuo2(int a, int b) {if (a - 1 < 1 || b - 1 < 1) {return;}if (arr[a - 1][b - 1] == 2 && (a - 1 >= 1) && (b - 1) >= 1) {summm2++;if (summm2 == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "绿色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}soushangzuo2(a - 1, b - 1);}}static void souxiazuo2(int a, int b) {if (a + 1 > 10 || b - 1 < 1) {return;}if (arr[a + 1][b - 1] == 2 && (a + 1 <= 10) && (b - 1) >= 1) {summm2++;if (summm2 == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "绿色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}souxiazuo2(a + 1, b - 1);}}static void souyoushang2(int a, int b) {if (a - 1 < 1 && b + 1 > 10) {return;}if (arr[a - 1][b + 1] == 2 && a - 1 >= 1 && b + 1 <= 10) {summm2++;if (summm2 == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "绿色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}souyoushang2(a - 1, b + 1);}}static void souyouxia2(int a, int b) {if (b + 1 > 10 && a + 1 > 10) {return;}if (arr[a + 1][b + 1] == 2 && b + 1 <= 10 && a + 1 <= 10) {summm2++;if (summm2 == 5) {for (int i = 0; i < 11; i++) {for (int k = 0; k < 12; k++) {arr[i][k] = 0;}}System.out.println("胜利");JOptionPane.showMessageDialog(sc, "绿色方胜利,点击确定重新开始", "结果", JOptionPane.WARNING_MESSAGE);}souyouxia2(a + 1, b + 1);}}public void paint(Graphics g) {if (jishu % 2 == 1) {String aaa = "红方开始下棋";txw.add(aaa);} else {String aaa = "绿方开始下棋";txw.add(aaa);}txw3.add(txw3.shu(x, y));super.paint(g);for (int i = 1; i <= 10; i++) {for (int k = 1; k <= 10; k++) {g.fillOval((i - 1) * 50, (k - 1) * 50, 50, 50);}}Color c = g.getColor();g.fillOval(x * 50, y * 50, 50, 50);Node now = seq.head;Node tou = kai.head;Color u = g.getColor();if (jishu % 2 == 1) {g.setColor(Color.red);g.fillOval((tou.a - 1) * 50, (tou.b - 1) * 50, 50, 50);g.setColor(u);} else {g.setColor(Color.green);g.fillOval((tou.a - 1) * 50, (tou.b - 1) * 50, 50, 50);g.setColor(u);}while (now != null) {System.out.print("(" + now.a + " " + now.b + ")");Color r = g.getColor();g.setColor(Color.red);g.fillOval((now.a - 1) * 50, (now.b - 1) * 50, 50, 50);g.setColor(r);now = now.next;}Node now2 = seq2.head;while (now2 != null) {System.out.print("(" + now2.a + " " + now2.b + ")");Color r = g.getColor();g.setColor(Color.green);g.fillOval((now2.a - 1) * 50, (now2.b - 1) * 50, 50, 50);g.setColor(r);now2 = now2.next;}System.out.println();}// 键的一个值被输出@Overridepublic void keyTyped(KeyEvent e) {}// 键被按下@Overridepublic void keyPressed(KeyEvent e) {System.out.print("线性表为");System.out.println();// System.out.println("键被按下"+e.getKeyCode());if (e.getKeyCode() == KeyEvent.VK_DOWN) {// System.out.println("12");y = y + 1;if (y >= 11) {y = y % 11 + 1;}} else if (e.getKeyCode() == KeyEvent.VK_UP) {y = y - 1;if (y < 1) {y = y + 10;}} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {x = x - 1;if (x < 1) {x = x + 10;}} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {x = x + 1;if (x >= 11) {x = x % 11 + 1;}} else if (e.getKeyCode() == KeyEvent.VK_SPACE) {int luo1 = x;int luo2 = y;Node n2 = new Node(luo1, luo2);if (jishu % 2 != 0) {Node now3 = seq2.head;Node now2 = seq.head;int aaa = 1;if (arr[x][y] == 1 || arr[x][y] == 2) {JOptionPane.showMessageDialog(sc, "此处已有棋子,请下别处", "错误", JOptionPane.WARNING_MESSAGE);aaa = 2;}if (aaa == 1) {jishu++;seq.add(n2);arr[luo1][luo2] = 1;seq.tostring();System.out.println("摁下空格");soushang(x, y);System.out.println("sum1" + "  " + summm);if (summm == 5) {seq.head = null;seq2.head = null;}souxia(x, y);System.out.println("sum2" + "  " + summm);if (summm == 5) {seq.head = null;seq2.head = null;}summm = 1;souzuo(x, y);System.out.println("sum3" + "  " + summm);if (summm == 5) {seq.head = null;seq2.head = null;}souyou(x, y);System.out.println("sum4" + "  " + summm);if (summm == 5) {seq.head = null;seq2.head = null;}summm = 1;soushangzuo(x, y);System.out.println("sum5" + "  " + summm);if (summm == 5) {seq.head = null;seq2.head = null;}souyouxia(x, y);System.out.println("sum6" + "  " + summm);if (summm == 5) {seq.head = null;seq2.head = null;}summm = 1;souyoushang(x, y);System.out.println("sum7" + "  " + summm);if (summm == 5) {seq.head = null;seq2.head = null;}souxiazuo(x, y);if (summm == 5) {seq.head = null;seq2.head = null;}summm = 1;}} else {int aaa = 1;if (arr[x][y] == 1 || arr[x][y] == 2) {JOptionPane.showMessageDialog(sc, "此处已有棋子,请下别处", "错误", JOptionPane.WARNING_MESSAGE);aaa = 2;}if (aaa == 1) {jishu++;seq2.add(n2);arr[luo1][luo2] = 2;seq2.tostring();System.out.println("摁下空格");soushang2(x, y);System.out.println("sum1" + "  " + summm);if (summm2 == 5) {seq.head = null;seq2.head = null;}souxia2(x, y);System.out.println("sum2" + "  " + summm);if (summm2 == 5) {seq.head = null;seq2.head = null;}summm2 = 1;souzuo2(x, y);System.out.println("sum3" + "  " + summm);if (summm2 == 5) {seq.head = null;seq2.head = null;}souyou2(x, y);System.out.println("sum4" + "  " + summm);if (summm2 == 5) {seq.head = null;seq2.head = null;}summm2 = 1;soushangzuo2(x, y);System.out.println("sum5" + "  " + summm);if (summm2 == 5) {seq.head = null;seq2.head = null;}souyouxia2(x, y);System.out.println("sum6" + "  " + summm);if (summm2 == 5) {seq.head = null;seq2.head = null;}summm2 = 1;souyoushang2(x, y);System.out.println("sum7" + "  " + summm);if (summm2 == 5) {seq.head = null;seq2.head = null;}souxiazuo2(x, y);if (summm2 == 5) {seq.head = null;seq2.head = null;}summm2 = 1;}}for (int i = 1; i <= 10; i++) {for (int k = 1; k <= 10; k++) {System.out.print(arr[i][k] + " ");}System.out.println();}}kai.huan(x, y);// 调用repaint()函数,来重绘界面this.repaint();}class Node {// 设置 节点类int a;int b;Node next;Node(int a, int b) {this.a = a;this.b = b;this.next = null;}}class seqlist {// 设置链表类Node head;Node tail;int n = 0;public seqlist(Node head) {// TODO Auto-generated constructor stubthis.head = head;this.tail = head;n++;}void add(Node p) {Node now = head;p.next = now;head = p;n++;}void tostring() {Node now = head;System.out.print("线性表为");while (now != null) {System.out.print(now.a + ",");now = now.next;}System.out.println();}int length() {return n;}void insert(int a, Node b) {Node now1 = head;for (int i = 0; i < a - 1; i++) {now1 = now1.next;}b.next = now1.next;now1.next = b;n++;}void delete(int a) {Node now1 = head;for (int i = 0; i < a - 1; i++) {now1 = now1.next;}now1.next = now1.next.next;n--;}int geta(int n) {Node now1 = head;for (int i = 0; i < n - 1; i++) {now1 = now1.next;}return now1.a;}void huan(int a, int b) {head.a = a;head.b = b;}int getb(int n) {Node now1 = head;for (int i = 0; i < n - 1; i++) {now1 = now1.next;}return now1.b;}}// 键被释放@Overridepublic void keyReleased(KeyEvent e) {}
}

java 实现双人五子棋相关推荐

  1. 双人五子棋对战(需要EasyX图像库)

    实训要做项目呐.天天坐在电脑面前累死了.最近题刷的少.大多数都挺水.就不挨个编辑发上来了.发发白天写的项目吧.可能好几天更一下.实训结束恢复正常. 这个游戏需要EasyX的图像库.有兴趣的可以下一个图 ...

  2. linux控制台单人五子棋简书,Java控制台版五子棋的简单实现方法

    这篇文章主要给大家介绍了关于Java控制台版五子棋的简单实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 设计一个10*1 ...

  3. java课设 五子棋_Java课程设计 ————五子棋 (个人博客)

    JAVA课程设计 五子棋(博客个人版) •团队课程设计博客链接 •个人负责模块或任务说明 1.主框架类:设置棋盘窗体,颜色等 2.isWin方法:判断胜负 •自己的代码提交记录截图 •自己负责模块或任 ...

  4. C语言实践——双人五子棋(简易版)

    双人五子棋 前言 一.五子棋的元素 二.五子棋规则(简单) 三.主要功能的设计 1.头文件和宏定义 2.初始化棋盘 3.打印棋盘 4.判断输赢 5.辅助函数 6.主函数 四.程序结果展示 前言 五子棋 ...

  5. c++ 双人五子棋(可直接复制)

    今天,我给大家带来一个五子棋的代码,希望大家喜欢. 下面是代码: #include <iostream> #include <conio.h> using namespace ...

  6. java小游戏:五子棋人机大战

    一.java小游戏:五子棋人机大战 1.绘制窗口 package wuziqi;import javax.swing.*; import java.awt.event.MouseAdapter; im ...

  7. java实训五子棋_Java棋类游戏实践之单机版五子棋

    本文实例讲述了java实现的五子棋游戏代码,分享给大家供大家参考,具体代码如下 一.实践目标 1.掌握JavaGUI界面设计 2.掌握鼠标事件的监听(MouseListener,MouseMotion ...

  8. java图形化五子棋总结,Java从此丰富多彩-五子棋项目总结

    当前位置:我的异常网» 编程 » Java从此丰富多彩-五子棋项目总结 Java从此丰富多彩-五子棋项目总结 www.myexceptions.net  网友分享于:2013-11-08  浏览:7次 ...

  9. Java Applet实现五子棋游戏

    Java Applet实现五子棋游戏 从谷歌的AlphaGo到腾讯的绝艺,从人脸识别到无人驾驶,从谷歌眼镜到VR的兴起,人工智能领域在不断的向前迈进,也在不断深入的探索.但背后错综复杂的技术和利益成本 ...

  10. Java小游戏——五子棋

    Java小游戏--五子棋详细解析

最新文章

  1. 计算机基础知识:原码、反码、补码
  2. 数学趣题——魔幻方阵
  3. 我为什么用docker-compose来打包开发环境
  4. mysql 参照完整性规则_mysql参照完整性
  5. linux 文件系统 簇 浪费空间,Linux rm -rf删除文件不释放空间的解决办法
  6. Wireshark验证TCP三次握手四次挥手
  7. 2022年考研计算机组成原理_7 输入输出系统
  8. mobomarket android,MoboMarket
  9. Activeperl安装教程
  10. vue css 实现选中div 边框变色,右下角三角形
  11. 【串讲总结】涵盖ML/DL/NLP/推荐/风控/数学等知识点汇总列表
  12. 哪些学校有计算机博士点,哪些学校有计算机应用博士点
  13. Mybatis中的动态SQL,一对一,一对多以及标签
  14. 计算机连接电视显示超范围,HDMI连接后电脑操作界面的边框超出电视屏幕,怎么解决...
  15. 关于Vcc和Vdd的区别
  16. 解决mini_httpd_v1.30在使用http post请求出现 socket hang up的问题
  17. [数理知识]统计决策理论——贝叶斯决策与两类错误率
  18. Direx 自学总结一
  19. 程序员不得不知道的 API 接口常识
  20. 前端面试系列-输入url后全过程页面渲染机制DOM生成过程

热门文章

  1. tp5 上传路径反斜杠的问题 ,反斜杠json_decode函数输出出错的
  2. 3月23日—3月27日四年级课程表
  3. 一网打尽系列之史玉柱运营法则
  4. php启动后no input file specified.,php网站出现no input file specified 三种解决方法
  5. Class ‘xxx‘ is public, should be declared in a file named ‘xxx.java‘
  6. scala either_使用Either和Option处理功能错误
  7. [jzoj 4244] 【五校联考6day2】yi {贪心}
  8. python中摄氏度华氏度相互转换
  9. 如何使用命令提示符查找和打开文件
  10. 加来道雄 基因编辑 纳米机器人_潜力巨大:纳米材料结合CRISPR基因编辑,成功改善阿尔茨海默病症状...