
正文
java纸牌记忆代码 扑克牌游戏java代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
请高手帮我写一个java的记忆翻转游戏(400分)
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
class jButton extends JButton {
}
class mine implements MouseListener, ActionListener, ItemListener, Runnable {
HashMap imgmap; String lastsrc="";int rightcount=1,wincount=3;
jButton b_mine[][];
jButton b_ls;
JPanel p1
,
p2;
JLabel l2;
JFrame f;
Thread thread1;
MenuBar mb;
Menu m_games;
CheckboxMenuItem item2
,
item3
,
item4;
Container con;
Icon icon[]
,
mine_1
,
mine_2
,
mine_3
,
mine_if
,
mine_is
,
mine_scan
,
b_click1
,
b_click2
,
b_click3
,
b_click4;
int x
,
y
,
n;
Font font1;
boolean isBreak = false;
int count = 0;
int icon_size = 40
,
Difficulty = 1
,
Width = 5
,
Height = 5
,
Xpos = 300
,
Ypos = 100
,
Time1 = 999
,
Time2 = 999
,
Time3 = 999;
dialog1 a;
public mine() {
f = new JFrame("记忆力");
con = f.getContentPane();
con.setLayout(null);
menuCreate(); //生成菜单
p1 = new JPanel();
p1.setLayout(null);
p2 = new JPanel();
l2 = new JLabel("", Label.RIGHT);
font1 = new Font("Helvetica", Font.BOLD, 20);
l2.setFont(font1);
Border border2 = new EtchedBorder(EtchedBorder.RAISED, new Color(144, 80, 44), new Color(144, 80, 44));
l2.setBorder(BorderFactory.createLoweredBevelBorder());
l2.setBackground(new Color(100, 50, 100));
l2.setForeground(Color.red);
l2.setOpaque(true);
p2.setBorder(border2);
p1.add(l2);
con.add(p1);
con.add(p2);
f.setLocation(Xpos, Ypos);
layout_init(Width, Height, icon_size);
button_init(Width, Height, 2);
f.show();
f.setResizable(false);
f.addWindowListener(new WindowAdapter() //适配器:关闭窗口
{
public void windowClosing(WindowEvent e) {
f.setVisible(false);
System.exit(0);
}
});
try {
Thread.sleep(3000);
}
catch (Exception e) {
}
}
//****************窗口添加控件和控件初始化**************************//\
public void layout_init(int width, int height, int icon_size) //生成最初java纸牌记忆代码的版面
{
this.icon_size = icon_size;
l2.setBounds(width * icon_size - 42, 3, 40, 25);
p1.setBounds(0, 0, width * icon_size + 4, 30);
p2.setBounds(0, 32, width * icon_size + 4, height * icon_size + 4);
p2.setLayout(new GridLayout(height, width));
f.setSize(width * icon_size + 10, height * icon_size + 100);
}
public void button_init(int width, int height, int typecount) //添加按钮
{
l2.setText("000");
imgmap=new HashMap();
wincount= width-2;
this.Width = width;
this.Height = height;
b_mine = new jButton[height][width];
for (int i = 0; i height; i++)
for (int j = 0; j width; j++) {
b_mine[i][j] = new jButton();
p2.add(b_mine[i][j]);
b_mine[i][j].addMouseListener(this);
imgmap.put(i+"_"+j,(int)(Math.random()*typecount)+".gif");
}
}
public void button_remove(int width, int height) //清除按扭
{
for (int i = 0; i height; i++)
for (int j = 0; j width; j++) {
b_mine[i][j].removeMouseListener(this);
p2.remove(b_mine[i][j]);
}
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == item2) {
if (thread1 != null) {
thread1.yield();
thread1 = null;
}
item2.setState(true);
item3.setState(false);
item4.setState(false);
Difficulty = 1;
button_remove(Width, Height);
button_init(5, 5, 2);
layout_init(5, 5, icon_size);
f.show();
}
if (e.getSource() == item3) {
if (thread1 != null) {
thread1.yield();
thread1 = null;
}
item2.setState(false);
item3.setState(true);
item4.setState(false);
Difficulty = 2;
button_remove(Width, Height);
button_init(6,6, 3);
layout_init(6, 6, icon_size);
f.show();
}
if (e.getSource() == item4) {
if (thread1 != null) {
thread1.yield();
thread1 = null;
}
item2.setState(false);
item3.setState(false);
item4.setState(true);
Difficulty = 3;
button_remove(Width, Height);
button_init(7, 7, 4);
layout_init(7, 7, icon_size);
f.show();
}
layout_init(Width, Height, 40);
icon_size = 40;
f.show();
}
public void mousePressed(MouseEvent e) {
if (thread1 == null) {
thread1 = new Thread(this);
thread1.start();
}
//***************找出所点击的按钮的两个下标值*****************//
b_ls = (jButton) e.getSource();
for (int i = 0; i Height; i++)
for (int j = 0; j Width; j++) {
if (b_mine[i][j] == e.getSource()) {
x = j;
y = i;
break;
}
}
}
public void mouseReleased(MouseEvent e) {
if (e.getModifiers() == 16) //左键功能完成java纸牌记忆代码:
{
String thissrc = (String)imgmap.get(y+"_"+x);
b_mine[y][x].setIcon(new ImageIcon("D:\\java\\source\\baidujava\\classes\\"+thissrc));
if (!thissrc .equals(lastsrc) ) {
lastsrc = thissrc;
for (int ti = 0; ti Width; ti++) {
for (int tj = 0; tj Height; tj++) {
if (ti == y tj == x) continue;
b_mine[ti][tj].setIcon(null);
}
}
rightcount = 1;
}else{
rightcount++;
}
if(rightcount==wincount){
JLabel t=new JLabel();
t.setText("ok,You win.用java纸牌记忆代码了"+l2.getText()+"秒");
thread1=null;
button_remove(Width, Height);
button_init(Width, Height, Width-2);
layout_init(Width, Width, icon_size);
JOptionPane.showConfirmDialog(f,t,"你赢",JOptionPane.OK_CANCEL_OPTION ,JOptionPane.QUESTION_MESSAGE ) ;
}
}
}
public void moveMouseListener() {
for (int i = 0; i Height; i++)
for (int j = 0; j Width; j++)
b_mine[i][j].removeMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
//*****************菜单定义部分********************//
public void menuCreate() {
mb = new MenuBar();
m_games = new Menu("游戏");
item2 = new CheckboxMenuItem("初级");
item3 = new CheckboxMenuItem("中级");
item4 = new CheckboxMenuItem("高级");
m_games.add(item2);
m_games.add(item3);
m_games.add(item4);
mb.add(m_games);
f.setMenuBar(mb);
if (Difficulty == 1)
item2.setState(true);
else if (Difficulty == 2)
item3.setState(true);
else if (Difficulty == 3)
item4.setState(true);
item2.addItemListener(this);
item3.addItemListener(this);
item4.addItemListener(this);
}
public void run() {
n = 0;
String s;
while (thread1 != null) {
if (n 10)
s = "00";
else if (n 100)
s = "0";
else
s = "";
l2.setText(s + String.valueOf(++n));
try {
thread1.sleep(1000);
}
catch (Exception e) {
}
}
}
public void actionPerformed(ActionEvent e) {
//To change body of implemented methods use File | Settings | File Templates.
}
}
public class clearmine {
public static void main(String args[]) {
new mine();
}
}
class dialog1 extends Dialog implements ActionListener //自定义对话框
{
int width
,
height
,
mine;
TextField t1
,
t2
,
t3;
Label l1
,
l2
,
l3;
Button b1
,
b2;
public dialog1(JFrame f, int width, int height, int mine) {
super(f, "你赢java纸牌记忆代码了", true);
this.width = width;
this.height = height;
this.mine = mine;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
try {
width = Integer.parseInt(t1.getText());
}
catch (Exception ew) {
width = 6;
}
try {
height = Integer.parseInt(t2.getText());
}
catch (Exception eh) {
height = 6;
}
try {
mine = Integer.parseInt(t3.getText());
}
catch (Exception em) {
mine = 6;
}
if (width 6) width = 6;
else if (width 35) width = 34;
if (height 6) height = 6;
else if (height 25) height = 24;
if (mine width * height * 2 / 3) mine = width * height * 2 / 3;
setVisible(false);
} else if (e.getSource() == b2) {
setVisible(false);
}
}
}
class logo extends Dialog {
Icon logo1;
JLabel jl;
public logo(JFrame f) {
super(f, "记忆力V1.0", false);
logo1 = new ImageIcon("./images/finenesssoft.gif");
jl = new JLabel(logo1);
add(jl);
setSize(406, 308);
setResizable(false);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent aa) {
setVisible(false);
}
});
}
public void str() {
setVisible(true);
}
public void exit() {
System.exit(0);
}
}
写好了.4个图片要放在D:\java\source\baidujava\classes下,名字分别是0.gif一直到3.gif.如果要改路径,去看238行.
相关问答
Q1: java设计一个记忆测试系统的小游戏,该如何入手
说个简单点的吧:
用数组或者集合,装一副纸牌对象
定时5秒,然后错开其中一个位置,最后让找出错位的那个纸牌即可!
难度可以根据数量进行变换!控制台可以用数字做,gui也可以更加逼真一些!
Q2: java题目 小明左右手分别拿两张纸牌 黑桃10和红心8, 现在交换手中的牌 用程序模拟这一过程
交换牌的话只需java纸牌记忆代码:1.设一个中间变量赋值为左手中的纸牌10 2.把右手中的纸牌8赋值给左手中的纸牌10 3.把中间变量中的10赋值给右手中的纸牌8 就可以了
int j=10java纸牌记忆代码;左手中的纸牌
int k=8;右手中的纸牌
开始交换:
int i=j;
j=k;
k=i;
Q3: 高分求java纸牌游戏源代码
Araneid 蜘蛛纸牌游戏
详细参考工程主页:
源代码下载(或附件):
二进制下载:
运行时请先解压缩,然后执行 classes 目录下的 start.bat
Q4: java 怎么写compareto方法实现扑克牌排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
/*
* 思路:
* A:创建一个HashMap集合
* B:创建一个ArrayList集合
* C:创建花色数组和点数数组
* D:从0开始往HashMap里面存储编号,并存储对应的牌
* 同时往ArrayList里面存储编号即可。
* E:洗牌(洗的是编号)
* F:发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
* G:看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
*/
public class PokerDemo {
public static void main(String[] args) {
// 创建一个HashMap集合
HashMapInteger, String hm = new HashMapInteger, String();
// 创建一个ArrayList集合
ArrayListInteger array = new ArrayListInteger();
// 创建花色数组和点数数组
// 定义一个花色数组
String[] colors = { "♠", "♥", "♣", "♦" };
// 定义一个点数数组
String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
"K", "A", "2", };
// 从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号即可。
int index = 0;
for (String number : numbers) {
for (String color : colors) {
String poker = color.concat(number);
hm.put(index, poker);
array.add(index);
index++;
}
}
hm.put(index, "小王");
array.add(index);
index++;
hm.put(index, "大王");
array.add(index);
// 洗牌(洗的是编号)
Collections.shuffle(array);
// 发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
TreeSetInteger fengQingYang = new TreeSetInteger();
TreeSetInteger linQingXia = new TreeSetInteger();
TreeSetInteger liuYi = new TreeSetInteger();
TreeSetInteger diPai = new TreeSetInteger();
for (int x = 0; x array.size(); x++) {
if (x = array.size() - 3) {
diPai.add(array.get(x));
} else if (x % 3 == 0) {
fengQingYang.add(array.get(x));
} else if (x % 3 == 1) {
linQingXia.add(array.get(x));
} else if (x % 3 == 2) {
liuYi.add(array.get(x));
}
}
// 看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
lookPoker("风清扬", fengQingYang, hm);
lookPoker("林青霞", linQingXia, hm);
lookPoker("刘意", liuYi, hm);
lookPoker("底牌", diPai, hm);
}
// 写看牌的功能
public static void lookPoker(String name, TreeSetInteger ts,
HashMapInteger, String hm) {
System.out.print(name + "的牌是:");
for (Integer key : ts) {
String value = hm.get(key);
System.out.print(value + " ");
}
System.out.println();
}
}
关于java纸牌记忆代码和扑克牌游戏java代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。




