通过 上下左右 控制棋盘走动 空格落子 (深度优先搜索)
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();
}
// 键的一个值被输出
@Override
public void keyTyped(KeyEvent e) {
}
// 键被按下
@Override
public 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 stub
this.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;
}
}
// 键被释放
@Override
public void keyReleased(KeyEvent e) {
}
}