import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
public class DrawQiPan extends JPanel implements MouseListener, ActionListener
{
private Image select_image = null;
private Image qiPanImage = null;
private String qiziString = "";
private int x = 0;
private int y = 0;
private int [] SelectposeX = new int[32];
private int [] SelectposeY = new int[32];
public int Before_x=0;
public int Before_y=1;
private int Temppos=0;
public int dispaySelect=1;
private boolean EatenJudge = true;
private boolean Rule = true;
public boolean HaveStart = false;
public int Hit_x = 0;
public int Hit_y = 0;
boolean JNM = true;
int pos[][] = {{ 1, 2, 3, 4, 5, 6, 7, 8, 9},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 10, 0, 0, 0, 0, 0, 11, 0},
{12, 0, 13, 0, 14, 0, 15, 0, 16},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0},
{28, 0, 29, 0, 30, 0, 31, 0, 32},
{ 0, 26, 0, 0, 0, 0, 0, 27, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0},
{17, 18, 19, 20, 21, 22, 23, 24, 25}};
private Image QiZi_image[] = new Image[32];
public DrawQiPan(){
try{ //划棋盘
qiPanImage = ImageIO.read(new File("./res/xqboard.gif"));
select_image = ImageIO.read(new File("./res/select.gif"));
for(int i=0; i<32; i++) //添加棋子
{
qiziString = "./res/"+(i+1)+".gif";
QiZi_image[i] = ImageIO.read(new File(qiziString));
}
}catch(Exception e){
e.printStackTrace();
}
this.addMouseListener(this); //监听器棋子
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(qiPanImage, 0, 0, this);
int k = 0;
int i = 0;
int j = 0;
for(i=0; i<10; i++)
{
for(j=0; j<9; j++)
{
if(pos[i][j] != 0)
{
if(HaveStart)
{
g.drawImage(QiZi_image[pos[i][j]-1], 10+j*48, 10+i*48, 42, 42, this);
}
SelectposeX[k] = 10+j*48;
SelectposeY[k] = 10+i*48;
if ( Hit_x == j && Hit_y == i && x != 0 && y != 0)
{
if(dispaySelect != 1)
{
g.drawImage(select_image, SelectposeX[k], SelectposeY[k] , 42, 42, this);
EatenJudge = true;
}
}
k++;
}
}
}
if(pos[Hit_y][Hit_x] != 0)
{
dispaySelect = 0;
}
if ( pos[Before_y][Before_x] != 0 )
{
if(pos[Hit_y][Hit_x] != 0 && EatenJudge && Rule)
{
Third.stack.push(Before_y);
Third.stack.push(Before_x);
Third.stack.push(Hit_y);
Third.stack.push(Hit_x);
if (pos[Hit_y][Hit_x] == 5)
{
try
{
JOptionPane.showMessageDialog(null, "好厉害,你胜利了!", "Game Over", JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e)
{
e.printStackTrace();
}
ResetPlayGame();
}
else if(pos[Hit_y][Hit_x] == 21)
{
try
{
JOptionPane.showMessageDialog(null,"很遗憾,你输了.", "Game Over", JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e)
{
e.printStackTrace();
}
ResetPlayGame();
}
else
{
pos[Hit_y][Hit_x] = pos[Before_y][Before_x];
pos[Before_y][Before_x] = 0;
dispaySelect = 1;
EatenJudge = false;
repaint();
}
}
}
if(pos[Hit_y][Hit_x] == 0)
{
if(pos[Before_y][Before_x]!=0 && EatenJudge && Rule)
{
Third.stack.push(Before_y);
Third.stack.push(Before_x);
Third.stack.push(Hit_y);
Third.stack.push(Hit_x);
if(Third.stack.empty() == false)
{
System.out.println("已进栈");
System.out.println("之前点击的位置为:" + "Y:" + Before_y + " " + "x:" + Before_x);
System.out.println("当前点击的位置为:" + "Y:" + Hit_y + " " + "x:" + Hit_x);
}
Temppos = pos[Before_y][Before_x];
pos[Before_y][Before_x] = pos[Hit_y][Hit_x];
pos[Hit_y][Hit_x] = Temppos;
dispaySelect = 1;
EatenJudge = false;
repaint();
}
}
}
public void actionPerformed(ActionEvent e){
}
public void MoveMethod(int Hy, int Hx, int By, int Bx, boolean Rule) //棋子的移动方法
{
System.out.println("调用方法成功" + "\n");
Hit_y = Hy;
Hit_x = Hx;
Before_y = By;
Before_x = Bx;
this.Rule = Rule;
dispaySelect = 0;
EatenJudge = true;
this.repaint();
}
public void ResetPlayGame() //重新开始游戏,初始化棋盘
{
int Num = 0;
for(int i = 0; i< 6; i++)
{
for(int j=0; j<9; j++)
{
if(i == 0)
{
Num++;
pos[i][j] = Num;
}
else if(i == 2 && (j == 1 || j == 7))
{
Num++;
pos[i][j] = Num;
}
else if( i == 3 && (j % 2 == 0))
{
Num++;
pos[i][j] = Num;
}
else
{
pos[i][j] = 0;
}
}
}
for(int i = 9 ; i > 5; i--)
{
for(int j=0; j<9; j++)
{
if(i == 9)
{
Num++;
pos[i][j] = Num;
}
else if( i == 7 && (j == 1 || j == 7))
{
Num++;
pos[i][j] = Num;
}
else if(i == 6 && (j % 2 == 0))
{
Num++;
pos[i][j] = Num;
}
else
{
pos[i][j] = 0;
}
}
}
Before_x = 0;
Before_y = 1;
Hit_x = 0;
Hit_y = 0;
EatenJudge = false;
Rule = false;
dispaySelect = 1;
repaint();
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e){
Before_x = Hit_x;
Before_y = Hit_y;
dispaySelect = 0;
x=e.getX();
y=e.getY();
System.out.println("之前点击的位置为:" + "Y:" + Before_y + " " + "x:" + Before_x);
System.out.println("当前点击的位置为:" + "Y:" + y/50 + " " + "x:" + x/50);
if (pos[y/50][x/50] != 0)
{
System.out.println("点击的棋子为:" + pos[y/50][x/50]);
}
Hit_x = x/50;
Hit_y = y/50;
if ((pos[Before_y][Before_x] == 17 || pos[Before_y][Before_x] == 25)) //以下是各个棋子的走法
{
Rule = true;
if(Before_x == Hit_x){
if(Before_y < Hit_y){
for(int y=Before_y+1; y<Hit_y; y++)
if(pos[y][Before_x] != 0)
Rule = false;
}else{
for(int y=Before_y-1; y>Hit_y; y--)
if(pos[y][Before_x] != 0)
Rule = false;
}
}else if(Before_y == Hit_y){
if(Before_x < Hit_x){
for(int x=Before_x+1; x<Hit_x; x++)
if(pos[Before_y][x] != 0)
Rule = false;
}else{
for(int x=Before_x-1; x>Hit_x; x--)
if(pos[Before_y][x] != 0)
Rule = false;
}
}else
{
Rule = false;
System.out.println("不能移动");
}
if(pos[Hit_y][Hit_x] >= 17 && pos[Hit_y][Hit_x] <= 32)
{
Rule = false;
}
}
if ((pos[Before_y][Before_x] == 26 || pos[Before_y][Before_x] == 27))
{
if(pos[Hit_y][Hit_x] == 0)
{
Rule = true;
if(Before_x == Hit_x){
if(Before_y < Hit_y){
for(int y=Before_y+1; y<Hit_y; y++)
if(pos[y][Before_x] != 0)
Rule = false;
}else{
for(int y=Before_y-1; y>Hit_y; y--)
if(pos[y][Before_x] != 0)
Rule = false;
}
}else if(Before_y == Hit_y){
if(Before_x < Hit_x){
for(int x=Before_x+1; x<Hit_x; x++)
if(pos[Before_y][x] != 0)
Rule = false;
}else{
for(int x=Before_x-1; x>Hit_x; x--)
if(pos[Before_y][x] != 0)
Rule = false;
}
}else
{
Rule = false;
System.out.println("不能移动");
}
if(pos[Hit_y][Hit_x] >= 17 && pos[Hit_y][Hit_x] <= 32)
{
Rule = false;
}
}
if (pos[Hit_y][Hit_x] >= 1 && pos[Hit_y][Hit_x] <= 16)
{
int JudgeNum = 0;
Rule = false;
System.out.println("进入炮轰处理");
if(Before_x == Hit_x){
if(Before_y < Hit_y){
for(int y=Before_y+1; y<Hit_y; y++)
{
if(pos[y][Before_x] != 0)
JudgeNum++;
}
if (JudgeNum == 1)
{
Rule = true;
}
}else{
for(int y=Before_y-1; y>Hit_y; y--)
{
if(pos[y][Before_x] != 0)
JudgeNum++;
}
if (JudgeNum == 1)
{
Rule = true;
}
}
}
else if(Before_y == Hit_y){
if(Before_x < Hit_x){
for(int x=Before_x+1; x<Hit_x; x++)
{
if(pos[Before_y][x] != 0)
JudgeNum++;
}
if (JudgeNum == 1)
{
Rule = true;
}
}else{
for(int x=Before_x-1; x>Hit_x; x--)
{
if(pos[Before_y][x] != 0)
JudgeNum++;
}
if (JudgeNum == 1)
{
Rule = true;
}
}
}
}
if(pos[Hit_y][Hit_x] >= 17 && pos[Hit_y][Hit_x] <= 32)
{
Rule = false;
}
}
if ((pos[Before_y][Before_x] == 18 || pos[Before_y][Before_x] == 24))
{
Rule = false;
if(Hit_y == Before_y + 2 && (Hit_x == Before_x + 1 || Hit_x == Before_x - 1))
{
if(pos[Before_y + 1][Before_x] != 0)
{
;
}
else if(pos[Hit_y][Hit_x] >= 17 && pos[Hit_y][Hit_x] <= 32)
{
Rule = false;
}
else
{
Rule = true;
}
}
if(Hit_y == Before_y - 2 && (Hit_x == Before_x + 1 || Hit_x == Before_x - 1))
{
if(pos[Before_y - 1][Before_x] != 0)
{
;
}
else if(pos[Hit_y][Hit_x] >= 17 && pos[Hit_y][Hit_x] <= 32)
{
Rule = false;
}
else
{
Rule = true;
}
}
if(Hit_x == Before_x + 2 && (Hit_y == Before_y + 1 || Hit_y == Before_y - 1))
{
if(pos[Before_y][Before_x + 1] != 0)
{
;
}
else if(pos[Hit_y][Hit_x] >= 17 && pos[Hit_y][Hit_x] <= 32)
{
Rule = false;
}
else
{
Rule = true;
}
}
if(Hit_x == Before_x - 2 && (Hit_y == Before_y + 1 || Hit_y == Before_y - 1))
{
if(pos[Before_y][Before_x - 1] != 0)
{
;
}
else if(pos[Hit_y][Hit_x] >= 17 && pos[Hit_y][Hit_x] <= 32)
{
Rule = false;
}
else
{
Rule = true;
}
}
}
if ((pos[Before_y][Before_x] == 19 || pos[Before_y][Before_x] == 23))
{
Rule = false;
if(Hit_y == Before_y - 2 && (Hit_x == Before_x + 2 || Hit_x == Before_x - 2))
{
if(Hit_x == Before_x + 2)
{
if(pos[Before_y - 1][Before_x + 1] != 0 )
{
;
}
else if(Hit_y >= 0 && Hit_y <= 4)
{
;
}
else if(pos[Hit_y][Hit_x] >= 17 && pos[Hit_y][Hit_x] <= 32)
{
Rule = false;
}
else
{
Rule = true;
}
}
if(Hit_x == Before_x - 2)
{
if(pos[Before_y - 1][Before_x - 1] != 0 )
{
;
}
else if(Hit_y >= 0 && Hit_y <= 4)
{
;
}
else if(pos[Hit_y][Hit_x] >= 17 && pos[Hit_y][Hit_x] <= 32)
{
Rule = false;
}
else
{
Rule = true;
}
}
}
if(Hit_y == Before_y + 2 && (Hit_x == Before_x + 2 || Hit_x == Before_x - 2))
{
if(Hit_x == Before_x + 2)
{
if(pos[Before_y + 1][Before_x + 1] != 0 )
{
;
}
else if(Hit_y >= 0 && Hit_y <= 4)
{
;
}
else if(pos[Hit_y][Hit_x] >= 17 && pos[Hit_y][Hit_x] <= 32)
{
Rule = false;
}
else
{
Rule = true;
}
}
if(Hit_x == Before_x - 2)
{
if(pos[Before_y + 1][Before_x - 1] != 0 )
{
;
}
else if(Hit_y >= 0 && Hit_y <= 4)
{
;
}
else if(pos[Hit_y][Hit_x] >= 17 && pos[Hit_y][Hit_x] <= 32)
{
Rule = false;
}
else
{
Rule = true;
}
}
}
}
if (pos[Before_y][Before_x] == 20 || pos[Before_y][Before_x] == 22)
{
Rule = false;
if((Hit_y >= 7 && Hit_y <= 9) && (Hit_x >= 3 && Hit_x <= 5))
{
if((Hit_y == Before_y + 1 || Hit_y == Before_y - 1) && (Hit_x == Before_x - 1 || Hit_x == Before_x + 1))
{
Rule = true;
}
if(pos[Hit_y][Hit_x] >= 17 && pos[Hit_y][Hit_x] <= 32)
{
Rule = false;
}
}
}
if (pos[Before_y][Before_x] == 21)
{
Rule = false;
if((Hit_y >= 7 && Hit_y <= 9) && (Hit_x >= 3 && Hit_x <= 5))
{
if((Hit_y == Before_y ) && (Hit_x == Before_x - 1 || Hit_x == Before_x + 1))
{
Rule = true;
}
else if((Hit_x == Before_x ) && (Hit_y == Before_y + 1 || Hit_y == Before_y - 1))
{
Rule = true;
}
if(pos[Hit_y][Hit_x] >= 17 && pos[Hit_y][Hit_x] <= 32)
{
Rule = false;
}
}
else if(pos[Hit_y][Hit_x] == 5)
{
System.out.println("进入对帅");
Rule = true;
for(int y=Before_y-1; y>Hit_y; y--)
if(pos[y][Before_x] != 0)
Rule = false;
}
}
if (pos[Before_y][Before_x] >= 28 && pos[Before_y][Before_x] <= 32)
{
Rule = false;
if(Before_y >= 5)
{
if(Hit_y == Before_y -1 && Hit_x == Before_x)
{
Rule = true;
}
if(pos[Hit_y][Hit_x] >= 17 && pos[Hit_y][Hit_x] <= 32)
{
Rule = false;
}
}
else
{
if(Hit_y == Before_y && (Hit_x == Before_x + 1 || Hit_x == Before_x - 1))
{
Rule = true;
}
else if(Hit_x == Before_x && Hit_y == Before_y - 1)
{
Rule = true;
}
if(pos[Hit_y][Hit_x] >= 17 && pos[Hit_y][Hit_x] <= 32)
{
Rule = false;
}
}
}
if(pos[Before_y][Before_x] >= 1 && pos[Before_y][Before_x] <= 16 )
{
Rule = false;
}
if
(pos[Hit_y][Hit_x] >= 1 && pos[Hit_y][Hit_x] <= 16 )
{
dispaySelect = 1;
}
repaint();
try
{
Second.dos.writeUTF("move");
Second.dos.writeUTF(Third.name2);
Second.dos.writeInt(Hit_y);
Second.dos.writeInt(Hit_x);
Second.dos.writeInt(Before_y);
Second.dos.writeInt(Before_x);
Second.dos.writeBoolean(Rule);
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
分享到:
相关推荐
【标题】:基于Java语言设计的网络五子棋游戏 在网络技术日新月异的今天,利用编程语言来开发网络游戏已经成为一种常见的实践。本项目是一个由Java新手开发的网络五子棋游戏,虽然在界面美观度上可能有所欠缺,但其...
Java Swing五子棋是一款基于Java GUI(图形用户界面)开发的桌面游戏,它允许两位玩家在二维棋盘上轮流放置黑白棋子,目标是形成连续的五个同色棋子,横向、纵向或对角线方向。这个项目是Java课程设计中的一个经典...
总的来说,这个基于Java Swing的“马踏棋盘”游戏项目涵盖了GUI设计、数据结构、算法实现、事件驱动编程、多线程以及版本控制等多个核心的IT知识点。对于学习Java编程和Swing库的初学者来说,这是一个很好的实践案例...
【Java棋类游戏--炮兵棋源代码】是一款基于Java编程语言实现的棋类游戏,主要涉及了面向对象设计、游戏逻辑控制、图形用户界面(GUI)开发等多个方面的知识点。以下将详细介绍这些关键领域的相关知识。 1. **Java...
总的来说,【基于Java的五子棋】项目涵盖了Java语言基础、面向对象设计、算法实现、GUI编程、文件操作和测试等多个方面,是学习和提升Java技能的一个综合实践案例。通过深入研究这个项目,开发者不仅可以提升编程...
Java五子棋游戏是一种基于Java编程语言开发的桌面游戏,它实现了两人对战的功能,让玩家可以在计算机上体验五子棋的乐趣。五子棋是一种古老的双人策略游戏,目标是在棋盘上连接五个相同的棋子(通常是黑色或白色),...
1. **Java语言基础**:Java连连看源码基于Java编程语言编写,展示了面向对象编程的基本概念,如类、对象、继承、封装和多态。游戏中的各种元素,如游戏面板、棋子、时间计时器等,都可以抽象为独立的类,通过对象...
总结来说,基于Java的单机版五子棋游戏开发涉及到Java基础、GUI编程、事件处理、游戏逻辑、AI设计等多个方面,是学习Java编程和游戏开发的优秀实践案例。通过分析和理解这段源码,开发者可以提升自己的编程技能,...
这里我们关注的是一个基于Java实现的五子棋项目。"java-五子棋-代码"这个标题表明这是一个关于使用Java语言编写的五子棋游戏的源代码。五子棋是一种双人对弈的策略游戏,目标是先在棋盘上连成五个子的玩家获胜。 ...
在本项目中,我们探讨的是基于Java编程语言的五子棋游戏开发与设计。五子棋,也称为连珠,是一种双人对弈的战略棋类游戏,目标是先在棋盘上形成连续的五个同色棋子,无论是横向、纵向还是对角线。通过这个项目,你...
本资源提供了一个基于Java实现的手机游戏大富翁的完整源代码,并附带了详尽的注释,这对于学习Java编程,尤其是游戏开发的初学者来说,是一份非常宝贵的学习资料。我们将详细探讨这份源代码中的关键知识点。 1. **...
Java五子棋是一款基于Java编程语言开发的桌面游戏,它实现了两人对弈的传统五子棋玩法。在设计这款五子棋游戏时,开发者通常会遵循以下几个关键知识点: 1. **图形用户界面(GUI)**:游戏首先从设计用户界面开始,这...
【Java独立钻石-单身贵族棋源代码】是一个基于Swing图形用户界面开发的小游戏,它展示了如何使用Java的Swing库来构建一个交互式的桌面应用。Swing是Java Standard Edition (Java SE) 部分的一部分,用于创建美观且...
在本项目中,我们探讨的是一个基于Java实现的学生学籍管理系统,它旨在帮助管理和维护学生的学籍信息。这个系统提供了对学生数据进行增删查改的功能,是学习Java编程基础以及理解软件工程实际应用的良好实践。 1. *...
### 基于Java的黑白棋游戏的设计与实现——关键技术及过程详解 #### 一、绪论 ##### 1.1 黑白棋简介 黑白棋,亦称苹果棋或奥赛罗棋,是一款历史悠久的经典策略游戏。其采用8×8的棋盘,玩家各执一方(通常是黑色和...
本设计基于Java语言,使用Java Swing库构建图形用户界面,运行环境为JDK 1.8及以上版本,支持Windows、Linux、Mac OS等多种操作系统。 3.1 系统体系结构设计 系统采用MVC(Model-View-Controller)设计模式,将业务...
在本教程中,我们将深入探讨基于Java的五子棋游戏的开发过程,这对于初学者来说是一次极好的学习机会,可以提升对Java编程语言的理解和实际应用能力。五子棋是一种简单而富有策略性的棋类游戏,通过用Java实现它,...
从提供的文件内容来看,这是一个关于Java五子棋游戏设计的报告,内容中涉及了游戏的多个关键技术和实现细节。由于文档内容不完整且存在一些扫描错误,以下知识点将基于文档中可辨识的信息进行整理。 1. MVC设计模式...
总的来说,基于Java的华容道小游戏是一个集编程技术、算法设计、用户交互于一体的项目,对学习和提升Java编程技能具有很高的价值。同时,它也是对经典游戏的现代化诠释,使得传统的智力游戏能够以全新的方式呈现在...