博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java编写的2048程序
阅读量:5088 次
发布时间:2019-06-13

本文共 6231 字,大约阅读时间需要 20 分钟。

import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.*;import javax.swing.border.*;public class Test extends JApplet{	private static final int UP=0,DOWN=1,LEFT=2,RIGHT=3;	private static final int WID=150,SP=10;	private int scores=0;	private static JLabel scoreLabel;	private boolean change,CheckMode=false,gameOverFlag=false,successFlag=false;	private int[] label={2,4,8,16,32,64,128};	private Color[] clo={			new Color(200,200,200),new Color(228,228,160),new Color(214,163,92),			new Color(234,124,72),new Color(240,84,77),new Color(255,68,53),new Color(200,200,64)	};	private Map cmap=new HashMap();	public static RectObject[][] rset=new RectObject[4][4];	public RectObject[][] list=new RectObject[4][4];	private My2048Panel myp;	private LinkedList saveList=new LinkedList();	private JButton goBackButton;		KeyListener kl=new KeyListener(){		public void keyPressed(KeyEvent e){			saveTheStep();			goBackButton.setVisible(true);			if(gameOverFlag==true){				return;			}			if(!aDirAble()){				gameOver();			}			int key=e.getKeyCode();			switch(key){			case KeyEvent.VK_UP:				change=false;				moveUp(true);				if(change==true){					getARandomRect();					//saveTheStep();				}				break;			case KeyEvent.VK_DOWN:				change=false;				moveDown(true);				if(change==true){					getARandomRect();					//saveTheStep();				}				break;			case KeyEvent.VK_LEFT:				change=false;				moveLeft(true);				if(change==true){					getARandomRect();					//saveTheStep();				}				break;			case KeyEvent.VK_RIGHT:				change=false;				moveRight(true);				if(change==true){					getARandomRect();					//saveTheStep();				}				break;			}			//saveTheStep();		}		public void keyTyped(KeyEvent e){}		public void keyReleased(KeyEvent e){}	};		class RectObject{		private int value;		public RectObject(){			value=0;		}		public RectObject(RectObject obj){			value=obj.value;		}		public boolean equals(Object inobj){			RectObject obj=(RectObject)inobj;			if(obj.value==value){				return true;			}			return false;		}	} 	class Point{		int x;		int y;		public Point(int i,int j){			x=i;			y=j;		}	}		class My2048Panel extends JPanel{		private int[] xindex={SP,2*SP+WID,3*SP+2*WID,4*SP+3*WID};		private int[] yindex={SP,2*SP+WID,3*SP+2*WID,4*SP+3*WID};				public void paintComponent(Graphics g){			//background			super.paintComponent(g);			for(int i=0;i
-1;j--){ if(rset[i][j]!=null){ list[i][k--]=new RectObject(rset[i][j]); } } } for(int i=0;i<4 && flag;i++){ for(int j=3;j>0;j--){ if(list[i][j]!=null && list[i][j-1]!=null && list[i][j].value==list[i][j-1].value){ list[i][j].value*=2; if(CheckMode==false){ int sum=Integer.parseInt(scoreLabel.getText()); sum+=list[i][j].value; scoreLabel.setText(""+sum); } list[i][j-1]=null; j--; } } } if(isChange()){ if(CheckMode==false){ copySet(rset,list); repaint(); moveRight(false); } change=true; }else{ repaint(); } } //moveup public void moveUp(boolean flag){ clearList(list); for(int j=0;j<4;j++){ int k=0; for(int i=0;i<4;i++){ if(rset[i][j]!=null){ list[k++][j]=new RectObject(rset[i][j]); } } } for(int j=0;j<4 && flag;j++){ for(int i=0;i<3;i++){ if(list[i][j]!=null && list[i+1][j]!=null && list[i][j].value==list[i+1][j].value){ list[i][j].value*=2; if(CheckMode==false){ int sum=Integer.parseInt(scoreLabel.getText()); sum+=list[i][j].value; scoreLabel.setText(""+sum); } list[i+1][j]=null; i++; } } } if(isChange()){ if(CheckMode==false){ copySet(rset,list); repaint(); moveUp(false); } change=true; }else{ repaint(); } } //movedown public void moveDown(boolean flag){ clearList(list); for(int j=0;j<4;j++){ int k=3; for(int i=3;i>-1;i--){ if(rset[i][j]!=null){ list[k--][j]=new RectObject(rset[i][j]); } } } for(int j=0;j<4 && flag;j++){ for(int i=3;i>0;i--){ if(list[i][j]!=null && list[i-1][j]!=null && list[i][j].value==list[i-1][j].value){ list[i][j].value*=2; if(CheckMode==false){ int sum=Integer.parseInt(scoreLabel.getText()); sum+=list[i][j].value; scoreLabel.setText(""+sum); } list[i-1][j]=null; i--; } } } if(isChange()){ if(CheckMode==false){ copySet(rset,list); repaint(); moveDown(false); } change=true; }else{ repaint(); } } //other functions private void copySet(RectObject[][] dst, RectObject[][] src){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ dst[i][j]=src[i][j]; } } } //detect whether rset is different from list or not private boolean isChange(){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(rset[i][j]!=null && list[i][j]!=null && !rset[i][j].equals(list[i][j])){ return true; } if(rset[i][j]!=null && list[i][j]==null){ return true; } if(rset[i][j]==null && list[i][j]!=null){ return true; } } } return false; } private void clearList(RectObject[][] s){ for(int i=0;i
best){ file.writeInt(cur); } file.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); }catch(IOException e2){ e2.printStackTrace(); } } public static void saveRecord(){ try{ RandomAccessFile file=new RandomAccessFile(new File("LASTRECORD"),"rw"); int score=Integer.parseInt(scoreLabel.getText()); file.writeInt(score); for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(rset[i][j]!=null){ file.writeInt(rset[i][j].value); }else{ file.writeInt(0); } } } }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } public static void run(JApplet applet,int width,int height){ JFrame frame=new JFrame(title(applet)); frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ refreshBest(); saveRecord(); //System.out.println("The score is "+scoreLabel.getText()); } }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(applet); frame.setSize(width,height); applet.init(); applet.start(); frame.setVisible(true); } public static void main(String[] args){ run(new Test(), 900, 800); }}

转载于:https://www.cnblogs.com/lxjshuju/p/6729357.html

你可能感兴趣的文章
js中的事件代理(委托)
查看>>
Django组件——分页器和中间件
查看>>
scala 14 trait
查看>>
You need to run build with JDK or have tools.jar问题解决
查看>>
BZOJ 1030: [JSOI2007]文本生成器 [AC自动机 DP]
查看>>
HDU 3949 XOR [高斯消元XOR 线性基]
查看>>
LeetCode Best Time to Buy and Sell Stock III
查看>>
PHP变量引用赋值后unset 输出原始变量,值存在.
查看>>
简单的新浪微博爬虫-Python版-(下载部分)---(上)
查看>>
for-each用法误区(不能改变数组元素值)
查看>>
f.select
查看>>
SSH2各部分作用
查看>>
不设置默认网关,导致traceroute无法获取途经路由信息原因
查看>>
MySql优化—删除操作
查看>>
三天打渔两天晒网
查看>>
python编码的那些事
查看>>
5.MVC框架开发(强类型开发,控制器向界面传递数据的几种方法)
查看>>
编程语言分类
查看>>
[转]GIT PUSH Error 403的解决方法
查看>>
Unity 移动主角的时候,鼠标被固定在屏幕中心而且被隐藏
查看>>