import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Spider extends JFrame{
//整型变量,表示难度等级为:简单
public static final int EASY = 1;
//整型变量,表示难度等级为:普通
public static final int NATURAL = 2;
//整型变量,表示难度等级为:难
public static final int HARD = 3;
//设定初始难度等级为简单
private int grade = Spider.EASY;
private Container pane = null;
//生成纸牌数组
private PKCard cards[] = new PKCard[104];
private JLabel clickLabel = null;
private int c = 0;
private int n = 0;
private int a = 0;
private int finish = 0;
Hashtable table = null;
private JLabel groundLabel[] = null;
public static void main(String[] args){
Spider spider = new Spider();
spider.setVisible(true);
}
/**
**构造函数
*/
public Spider(){
setTitle("蜘蛛牌");
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
//设置框架的大小
setSize(1024, 742);
//生成SpiderMenuBar对象,并放置在框架之上
setJMenuBar(new SpiderMenuBar(this));
pane = this.getContentPane();
//设置背景颜色
pane.setBackground(new Color(0, 112, 26));
//将布局管理器设置成为null
pane.setLayout(null);
clickLabel = new JLabel();
clickLabel.setBounds(883, 606, 121, 96);
pane.add(clickLabel);
clickLabel.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent me){
if (c < 60){
Spider.this.deal();
}
}
});
this.initCards();
this.randomCards();
this.setCardsLocation();
groundLabel = new JLabel[10];
int x = 20;
for (int i = 0; i < 10; i++)
{
groundLabel[i] = new JLabel();
groundLabel[i]
.setBorder(javax.swing.BorderFactory
.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
groundLabel[i].setBounds(x, 25, 71, 96);
x += 101;
this.pane.add(groundLabel[i]);
}
this.setVisible(true);
this.deal();
this.addKeyListener(new KeyAdapter(){
class Show extends Thread{
public void run(){
Spider.this.showEnableOperator();
}
}
public void keyPressed(KeyEvent e){
if (finish != 8) if (e.getKeyCode() == KeyEvent.VK_D && c < 60){
Spider.this.deal();
}
else if (e.getKeyCode() == KeyEvent.VK_M){
new Show().start();
}
}
});
}
/**
**开始新游戏
*/
public void newGame(){
this.randomCards();
this.setCardsLocation();
this.setGroundLabelZOrder();
this.deal();
}
/**
**返回值:int
**返回牌的数量
*/
public int getC(){
return c;
}
/**
**设置等级
*/
public void setGrade(int grade){
this.grade = grade;
}
/**
**纸牌初始化
*/
public void initCards(){
//如果纸牌已被赋值,即将其从框架的面板中移去
if (cards[0] != null){
for (int i = 0; i < 104; i++){
pane.remove(cards[i]);
}
}
int n = 0;
//通过难度等级,为n赋值
if (this.grade == Spider.EASY){
n = 1;
}
else if (this.grade == Spider.NATURAL){
n = 2;
}
else{
n = 4;
}
//为card赋值
for (int i = 1; i <= 8; i++){
for (int j = 1; j <= 13; j++){
cards[(i - 1) * 13 + j - 1] = new PKCard((i % n + 1) + "-" + j,
this);
}
}
//随机纸牌初始化
this.randomCards();
}
/**
**纸牌随机分配
*/
public void randomCards(){
PKCard temp = null;
//随机生成牌号
for (int i = 0; i < 52; i++){
int a = (int) (Math.random() * 104);
int b = (int) (Math.random() * 104);
temp = cards[a];
cards[a] = cards[b];
cards[b] = temp;
}
}
/**
**设置还原
*/
public void setNA(){
a = 0;
n = 0;
}
/**
**设置纸牌的位置
*/
public void setCardsLocation(){
table = new Hashtable();
c = 0;
finish = 0;
n = 0;
a = 0;
int x = 883;
int y = 580;
//初始化待展开的纸牌
for (int i = 0; i < 6; i++){
for (int j = 0; j < 10; j++){
int n = i * 10 + j;
pane.add(cards[n]);
//将card转向背面
cards[n].turnRear();
//将card放在固定的位置上
cards[n].moveto(new Point(x, y));
//将card的位置及相关信息存入
table.put(new Point(x, y), cards[n]);
}
x += 10;
}
x = 20;
y = 45;
//初始化表面显示的纸牌
for (int i = 10; i > 5; i--){
for (int j = 0; j < 10; j++){
int n = i * 10 + j;
if (n >= 104) continue;
pane.add(cards[n]);
cards[n].turnRear();
cards[n].moveto(new Point(x, y));
table.put(new Point(x, y), cards[n]);
x += 101;
}
x = 20;
y -= 5;
}
}
/**
**返回值:void
**方法:显示可移动的操作
*/
public void showEnableOperator(){
int x = 0;
out: while (true){
Point point = null;
PKCard card = null;
do{
if (point != null){
n++;
}
point = this.getLastCardLocation(n);
while (point == null){
point = this.getLastCardLocation(++n);
if (n == 10) n = 0;
x++;
if (x == 10) break out;
}
card = (PKCard) this.table.get(point);
}
while (!card.isCardCanMove());
while (this.getPreviousCard(card) != null
&& this.getPreviousCard(card).isCardCanMove()){
card = this.getPreviousCard(card);
}
if (a == 10){
a = 0;
}
for (; a < 10; a++){
if (a != n){
Point p = null;
PKCard c = null;
do{
if (p != null){
a++;
}
p = this.getLastCardLocation(a);
int z = 0;
while (p == null){
p = this.getLastCardLocation(++a);
if (a == 10) a = 0;
if (a == n) a++;
z++;
if (z == 10) break out;
}
c = (PKCard) this.table.get(p);
}
while (!c.isCardCanMove());
if (c.getCardValue() == card.getCardValue() + 1){
card.flashCard(card);
try{
Thread.sleep(800);
}
catch (InterruptedException e){
e.printStackTrace();
}
c.flashCard(c);
a++;
if (a == 10){
n++;
}
break out;
}
}
}
n++;
if (n == 10){
n = 0;
}
x++;
if (x == 10){
tanghuluxue
- 粉丝: 0
- 资源: 10
最新资源
- 基于java+ssm+mysql+微信小程序的中国剪纸微信小程序 源码+数据库+论文(高分毕业设计).zip
- 基于java+springboot+mysql+微信小程序的社区超市管理系统 源码+数据库+论文(高分毕业设计).zip
- 基于java+ssm+mysql+微信小程序的智慧消防小程序 源码+数据库+论文(高分毕业设计).zip
- 基于java+ssm+mysql+微信小程序的走失人员报备平台 源码+数据库+论文(高分毕业设计).zip
- 基于java+ssm+mysql+微信小程序的自驾游拼团小程序 源码+数据库+论文(高分毕业设计).zip
- Cartridge:一个用Python编写的基于 Mezzanine 构建的购物车应用
- mellisearch windows
- 多图片转视频,支持多文件夹、多文件夹分别排序
- 四川省妇幼管理系统接口文档
- VSCODE安装包20241227
- 基于springboot的论坛管理系统源码(java毕业设计完整源码+LW).zip
- 收到防护服快快快啊啊啊啊啊
- 二级减速器含三维模型二维CAD图课程sw2014可编辑全套技术资料100%好用.zip
- Thinkphp小额贷款网贷系统源码 可封装APP,内附简单安装说明
- alipay:非官方的 Python 支付宝 API
- 学习Matlab的经验和技巧
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈