《游戏学习》Java实现蜘蛛纸牌小游戏源码(1),java面试题mysql |
您所在的位置:网站首页 › java纸牌游戏异常类的主要内容和意义 › 《游戏学习》Java实现蜘蛛纸牌小游戏源码(1),java面试题mysql |
先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7 深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前! 因此收集整理了一份《2024年最新Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。 既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Java开发知识点,真正体系化! 由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新 如果你需要这些资料,可以添加V获取:vip1024b (备注Java) public void mouseEntered(MouseEvent arg0){ } public void mouseExited(MouseEvent arg0){ } /** **用鼠标拖动纸牌 */ public void mouseDragged(MouseEvent arg0){ if (canMove){ int x = 0; int y = 0; Point p = arg0.getPoint(); x = p.x - point.x; y = p.y - point.y; this.moving(x, y); } } /** **返回值:void **方法:移动(x,y)个位置 */ public void moving(int x, int y){ PKCard card = main.getNextCard(this); Point p = this.getLocation(); //将组件移动到容器中指定的顺序索引。 pane.setComponentZOrder(this, 1); //在Hashtable中保存新的节点信息 main.table.remove§; p.x += x; p.y += y; this.setLocation§; main.table.put(p, this); if (card != null) card.moving(x, y); } public void mouseMoved(MouseEvent arg0){ } /** **构造函数 */ public PKCard(String name, Spider spider){ super(); this.type = new Integer(name.substring(0, 1)).intValue(); this.value = new Integer(name.substring(2)).intValue(); this.name = name; this.main = spider; this.pane = this.main.getContentPane(); this.addMouseListener(this); this.addMouseMotionListener(this); this.setIcon(new ImageIcon(“images/rear.gif”)); this.setSize(71, 96); this.setVisible(true); } /** **返回值:void **方法:令纸牌显示正面 */ public void turnFront(){ this.setIcon(new ImageIcon(“images/” + name + “.gif”)); this.isFront = true; } /** **返回值:void **方法:令纸牌显示背面 */ public void turnRear(){ this.setIcon(new ImageIcon(“images/rear.gif”)); this.isFront = false; this.canMove = false; } /** **返回值:void **方法:将纸牌移动到点point */ public void moveto(Point point){ this.setLocation(point); this.initPoint = point; } /** **返回值:void **方法:判断牌是否能移动 */ public void setCanMove(boolean can){ this.canMove = can; PKCard card = main.getPreviousCard(this); if (card != null && card.isCardFront()){ if (!can){ if (!card.isCardCanMove()){ return; } else{ card.setCanMove(can); } } else{ if (this.value + 1 == card.getCardValue() && this.type == card.getCardType()){ card.setCanMove(can); } else{ card.setCanMove(false); } } } } /** **返回值:boolean **方法:判断card是否是正面 */ public boolean isCardFront(){ return this.isFront; } /* **返回值:boolean **方法:返回是否能够移动 */ public boolean isCardCanMove(){ return this.canMove; } /** **返回值:int **方法:获得card的内容值 */ public int getCardValue(){ return value; } /** **返回值:int **方法:获得card的类型 */ public int getCardType(){ return type; } } 蜘蛛牌 菜单类 ======= import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; import javax.swing.ButtonGroup; public class SpiderMenuBar extends JMenuBar{ //生成spider框架对象 Spider main = null; //生成菜单组 JMenu jNewGame = new JMenu(“游戏”); JMenu jHelp = new JMenu(“帮助”); //生成菜单项 JMenuItem jItemAbout = new JMenuItem(“关于”); JMenuItem jItemOpen = new JMenuItem(“开局”); JMenuItem jItemPlayAgain = new JMenuItem(“重新发牌”); //生成单选框 JRadioButtonMenuItem jRMItemEasy = new JRadioButtonMenuItem(“简单:单一花色”); JRadioButtonMenuItem jRMItemNormal = new JRadioButtonMenuItem(“中级:双花色”); JRadioButtonMenuItem jRMItemHard = new JRadioButtonMenuItem(“高级:四花色”);; JMenuItem jItemExit = new JMenuItem(“退出”); JMenuItem jItemValid = new JMenuItem(“显示可行操作”); /** **构造函数,生成JMenuBar的图形界面 */ public SpiderMenuBar(Spider spider){ this.main = spider; /** **初始化“游戏”菜单栏 */ jNewGame.add(jItemOpen); jNewGame.add(jItemPlayAgain); jNewGame.add(jItemValid); jNewGame.addSeparator(); jNewGame.add(jRMItemEasy); jNewGame.add(jRMItemNormal); jNewGame.add(jRMItemHard); jNewGame.addSeparator(); jNewGame.add(jItemExit); ButtonGroup group = new ButtonGroup(); group.add(jRMItemEasy); group.add(jRMItemNormal); group.add(jRMItemHard); jHelp.add(jItemAbout); this.add(jNewGame); this.add(jHelp); //为组件添加事件监听并实现 //“开局” jItemOpen.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { main.newGame(); } }); //“重新发牌” jItemPlayAgain.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { if(main.getC() < 60){ main.deal(); } } }); //“显示可行操作” jItemValid.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { new Show().start(); } }); //“退出” jItemExit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { main.dispose(); System.exit(0); } }); //“简单级别”默认已选 jRMItemEasy.setSelected(true); //“简单级别” jRMItemEasy.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { main.setGrade(Spider.EASY); main.initCards(); main.newGame(); } }); //“中级” jRMItemNormal.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { main.setGrade(Spider.NATURAL); main.initCards(); main.newGame(); } }); //“高级” jRMItemHard.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { main.setGrade(Spider.HARD); main.initCards(); main.newGame(); } }); jNewGame.addMenuListener(new javax.swing.event.MenuListener() { public void menuSelected(javax.swing.event.MenuEvent e) { if(main.getC() < 60){ jItemPlayAgain.setEnabled(true); } else{ jItemPlayAgain.setEnabled(false); } } public void menuDeselected(javax.swing.event.MenuEvent e) {} public void menuCanceled(javax.swing.event.MenuEvent e) {} }); //“关于” jItemAbout.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { new AboutDialog(); } }); } /** **构造线程:显示可以执行的操作 */ class Show extends Thread{ public void run(){ main.showEnableOperator(); } } } 蜘蛛牌 主类、启动类 =========== (在IDE编译器中运行此类) ============== 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(){ //改变系统默认字体 Font font = new Font(“Dialog”, Font.PLAIN, 12); java.util.Enumeration keys = UIManager.getDefaults().keys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); Object value = UIManager.get(key); if (value instanceof javax.swing.plaf.FontUIResource) { UIManager.put(key, font); } } 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 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§; } while (!c.isCardCanMove()); if (c.getCardValue() == card.getCardValue() + 1){ card.flashCard(card); try{ Thread.sleep(800); } catch (InterruptedException e){ e.printStackTrace(); } c.flashCard©; a++; if (a == 10){ n++; } break out; } } } n++; if (n == 10){ n = 0; } x++; if (x == 10){ break out; } } } /* 总结蚂蚁面试比较重视基础,所以Java那些基本功一定要扎实。蚂蚁的工作环境还是挺赞的,因为我面的是稳定性保障部门,还有许多单独的小组,什么三年1班,很有青春的感觉。面试官基本水平都比较高,基本都P7以上,除了基础还问了不少架构设计方面的问题,收获还是挺大的。 经历这次面试我还通过一些渠道发现了需要大厂真实面试主要有:蚂蚁金服、拼多多、阿里云、百度、唯品会、携程、丰巢科技、乐信、软通动力、OPPO、银盛支付、中国平安等初,中级,高级Java面试题集合,附带超详细答案,希望能帮助到大家。 网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。 需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java) 一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长! ++){ 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§; } while (!c.isCardCanMove()); if (c.getCardValue() == card.getCardValue() + 1){ card.flashCard(card); try{ Thread.sleep(800); } catch (InterruptedException e){ e.printStackTrace(); } c.flashCard©; a++; if (a == 10){ n++; } break out; } } } n++; if (n == 10){ n = 0; } x++; if (x == 10){ break out; } } } /* 总结蚂蚁面试比较重视基础,所以Java那些基本功一定要扎实。蚂蚁的工作环境还是挺赞的,因为我面的是稳定性保障部门,还有许多单独的小组,什么三年1班,很有青春的感觉。面试官基本水平都比较高,基本都P7以上,除了基础还问了不少架构设计方面的问题,收获还是挺大的。 经历这次面试我还通过一些渠道发现了需要大厂真实面试主要有:蚂蚁金服、拼多多、阿里云、百度、唯品会、携程、丰巢科技、乐信、软通动力、OPPO、银盛支付、中国平安等初,中级,高级Java面试题集合,附带超详细答案,希望能帮助到大家。 [外链图片转存中…(img-hcB37NKU-1713429875335)] 网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。 需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java) [外链图片转存中…(img-B5xMFVo9-1713429875336)] 一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长! |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |