zoukankan      html  css  js  c++  java
  • 2048小游戏(Java)(swing实现)(一)

    自己写的2048小游戏,仅支持鼠标操作

    主要是我不知道怎么添加键盘监听

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class JF2048 extends JFrame {
    
    /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
    private Ja2048 ja;
    
    public JButton b[] = {
    new JButton(),
    new JButton(),
    new JButton(),
    new JButton()
    };
    
    public JButton back = new JButton("back");
    
    private ActionListener b0 = new ActionListener(){
    public void actionPerformed(ActionEvent e){
    ja.cp0();
    }};
    
    private ActionListener b1 = new ActionListener(){
    public void actionPerformed(ActionEvent e){
    ja.cp1();
    }};
    
    private ActionListener b2 = new ActionListener(){
    public void actionPerformed(ActionEvent e){
    ja.cp2();
    }};
    
    private ActionListener b3 = new ActionListener(){
    public void actionPerformed(ActionEvent e){
    ja.cp3();
    }};
    
    private ActionListener back1 = new ActionListener(){
    public void actionPerformed(ActionEvent e){
    ja.back();
    }};
    
    public JLabel[][] la ={
    {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
    {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
    {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
    {new JLabel(),new JLabel(),new JLabel(),new JLabel()},
    };
    
    
    public JF2048(){
    
    super("2048");
    
    //this.addKeyListener(x);
    
    b[0].setBounds(3,20,16,156);
    b[1].setBounds(178,20,16,156);
    b[2].setBounds(20,3,156,16);
    b[3].setBounds(20,178,156,16);
    back.setBounds(3,3,16,16);
    
    b[0].addActionListener(b0);
    b[1].addActionListener(b1);
    b[2].addActionListener(b2);
    b[3].addActionListener(b3);
    back.addActionListener(back1); 
    
    
    for(int i =0;i<4;i++)
    for(int j =0;j<4;j++){
    la[i][j].setBounds(20+40*i,20+40*j,36,36);
    la[i][j].setOpaque(true);
    //la[i][j].setFont(new Font("幼圆",1,24));
    la[i][j].setHorizontalAlignment(SwingConstants.CENTER);
    }
    
    this.setSize(217,238);
    this.add(b[0]);
    this.add(b[1]);
    this.add(b[2]);
    this.add(b[3]);
    this.add(back);
    for(int i =0;i<4;i++)
    for(int j =0;j<4;j++)
    this.add(la[i][j]);
    JLabel p = new JLabel();
    p.setBackground(new Color(127,127,127));
    p.setOpaque(true);
    this.add(p);
    
    
    }
    
    public static void main(String[] args){
    JF2048 jf = new JF2048();
    jf.ja=new Ja2048(jf);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setVisible(true);
    }
    
    }
    界面层代码
    import java.awt.*;
    public class Ja2048{
    
    public static int[][] state=new int[4][4];
    public static int[][] bac=new int[4][4];
    
    private JF2048 linkF;
    
    
    public Ja2048(JF2048 a){
    this.linkF = a;
    setNull(state,getRandom());
    setNull(state,getRandom());
    setState();
    } 
    
    public void cp0(){
    boolean bool= false;
    for(int i = 1;i<4;i++)
    for(int j = 0;j<4;j++)
    if(state[i][j]!=0&&(state[i-1][j]==0||state[i-1][j]==state[i][j]))
    bool=true;
    if(!bool)return;
    
    for(int i =0;i<4;i++)
    for(int j =0;j<4;j++)
    bac[i][j]=state[i][j];
    
    int[][] b = new int[4][4];
    for(int j=0;j<4;j++){
    int[] a ={state[0][j],state[1][j],state[2][j],state[3][j]};
    b[j]=LierIntArr.drop(a);
    }
    setNull(b,getRandom());
    int[][] x=new int[4][4];
    for(int i=0;i<4;i++)
    for(int j=0;j<4;j++)
    x[i][j]=b[j][i];
    state=x;
    setState();
    }//向左
    public void cp1(){
    boolean bool=false;
    for(int i=0;i<3;i++)
    for(int j=0;j<4;j++)
    if(state[i][j]!=0&&(state[i+1][j]==0||state[i+1][j]==state[i][j]))
    bool=true;
    if(!bool)return;
    bac=state;
    int[][] b = new int[4][4];
    for(int j=0;j<4;j++){
    int[] a = {state[3][j],state[2][j],state[1][j],state[0][j]};
    b[j]=LierIntArr.drop(a);
    }
    setNull(b,getRandom());
    int[][] x=new int[4][4];
    for(int i=0;i<4;i++)
    for(int j=0;j<4;j++)
    x[i][j]=b[j][3-i];
    state=x;
    setState();
    }//向右
    public void cp2(){
    boolean bool=false;
    for(int i=0;i<4;i++)
    for(int j=1;j<4;j++)
    if(state[i][j]!=0&&(state[i][j-1]==0||state[i][j-1]==state[i][j]))
    bool=true;
    if(!bool)return;
    bac=state.clone();
    int[][] b = new int[4][4];
    for(int i=0;i<4;i++)
    b[i]=LierIntArr.drop(state[i]);
    setNull(b,getRandom());
    state=b.clone();
    setState();
    }//向上
    public void cp3(){
    boolean bool=false;
    for(int i=0;i<4;i++)
    for(int j=0;j<3;j++)
    if(state[i][j]!=0&&(state[i][j+1]==0||state[i][j+1]==state[i][j]))
    bool=true;
    if(!bool)return;
    bac=state.clone();
    int[][] b=new int[4][4];
    for(int i=0;i<4;i++){
    int[] a ={state[i][3],state[i][2],state[i][1],state[i][0]};
    b[i]=LierIntArr.drop(a);
    }
    setNull(b,getRandom());
    int[][] x=new int[4][4];
    for(int i=0;i<4;i++)
    for(int j=0;j<4;j++)
    x[i][j]=b[i][3-j];
    state=x;
    setState();
    }//向下
    public  void back(){
    state=bac.clone();
    setState();
    }
    private void setState(){
    for(int i=0;i<4;i++)
    for(int j=0;j<4;j++){
    if(state[i][j]==0){
    linkF.la[i][j].setText("");
    linkF.la[i][j].setBackground(new Color(227,227,227));
    linkF.la[i][j].setForeground(new Color(0,0,0));
    }
    else if(state[i][j]==2){
    linkF.la[i][j].setText("2");
    linkF.la[i][j].setFont(new Font("幼圆",1,20));
    linkF.la[i][j].setBackground(new Color(255,255,255));
    linkF.la[i][j].setForeground(new Color(0,0,0));
    }
    else if(state[i][j]==4){
    linkF.la[i][j].setText("4");
    linkF.la[i][j].setFont(new Font("幼圆",1,20));
    linkF.la[i][j].setBackground(new Color(127,227,127));
    linkF.la[i][j].setForeground(new Color(0,0,0));
    }
    else if(state[i][j]==8){
    linkF.la[i][j].setText("8");
    linkF.la[i][j].setFont(new Font("幼圆",1,20));
    linkF.la[i][j].setBackground(new Color(0,127,127));
    linkF.la[i][j].setForeground(new Color(255,255,255));
    }
    else if(state[i][j]==16){
    linkF.la[i][j].setText("16");
    linkF.la[i][j].setFont(new Font("幼圆",1,20));
    linkF.la[i][j].setBackground(new Color(0,255,0));
    linkF.la[i][j].setForeground(new Color(255,255,255));
    }
    else if(state[i][j]==32){
    linkF.la[i][j].setText("32");
    linkF.la[i][j].setFont(new Font("幼圆",1,20));
    linkF.la[i][j].setBackground(new Color(127,127,0));
    linkF.la[i][j].setForeground(new Color(255,255,255));
    }
    else if(state[i][j]==64){
    linkF.la[i][j].setText("64");
    linkF.la[i][j].setFont(new Font("幼圆",1,20));
    linkF.la[i][j].setBackground(new Color(255,0,0));
    linkF.la[i][j].setForeground(new Color(255,255,255));
    }
    else if(state[i][j]==128){
    linkF.la[i][j].setText("128");
    linkF.la[i][j].setFont(new Font("幼圆",1,20));
    linkF.la[i][j].setBackground(new Color(127,255,0));
    linkF.la[i][j].setForeground(new Color(255,255,255));
    }
    else if(state[i][j]==256){
    linkF.la[i][j].setText("256");
    linkF.la[i][j].setFont(new Font("幼圆",1,20));
    linkF.la[i][j].setBackground(new Color(255,255,0));
    linkF.la[i][j].setForeground(new Color(0,0,0));
    }
    else if(state[i][j]==512){
    linkF.la[i][j].setText("512");
    linkF.la[i][j].setFont(new Font("幼圆",1,20));
    linkF.la[i][j].setBackground(new Color(255,255,0));
    linkF.la[i][j].setForeground(new Color(0,0,0));
    }
    else if(state[i][j]==1024){
    linkF.la[i][j].setText("1024");
    linkF.la[i][j].setFont(new Font("幼圆",1,16));
    linkF.la[i][j].setBackground(new Color(63,63,63));
    linkF.la[i][j].setForeground(new Color(255,255,255));
    }
    }//for循环
    
    }//setState方法
    
    private static int getRandom(){
    int a = (int)(1000*Math.random());
    if(a%10<3)
    return 4;
    else
    return 2;
    }//随机生成一个2或4,可通过调整判断条件中的数字大小来调整2和4所占的比率
    /**
     * 用于在4x4二维数组中随机挑出一个值为0的元素,并将其赋值为给定整数。特殊地,若该二维数组已满,返回false。
     * @param x    该二维数组
     * @param y    给定整数
     * @return 
     */
    private static boolean setNull(int[][] x,int y){
    boolean bool=false;
    for(int i=0;i<4;i++)
    for(int j=0;j<4;j++)
    if(x[i][j]==0)bool=true;
    if(!bool)return false;
    
    int a = (int)(100*Math.random());
    int b = (int)(6+10*Math.random());
    int c = a%b;
    while(true){
    for(int i=0;i<4;i++)
    for(int j=0;j<4;j++){
    if(x[i][j]==0&&c<=0){
    x[i][j]=y;
    return true;
    }
    else if(x[i][j]==0&&c>0)
    c--;
    i=(i==4?0:i);
    j=(j==4?0:j);
    }
    }
    }//boolean setNull(int[][],int)方法用于在4x4二维数组中随机挑出一个值为0的元素,并将其赋值为给定整数。特殊地,若该二维数组已满,返回false。
    
    
    }
    算法层代码
     1 /**
     2  * 
     3  * @author qliujinming@qq.com 
     4  * 
     5  * @see http://www.cnblogs.com/liujinming/
     6  *
     7  */
     8 public class LierIntArr{
     9 /**
    10 * 该方法用于接受一个整数数组,对该数组进行drop操作后返回
    11 * 示例:接受 2 0 2 0 5 5,返回4 10 0 0 0 0
    12 * @param 需要进行drop操作的数组
    13 * @return drop操作之后的数组
    14 */
    15 public static int[] drop(int[] a){
    16 int b = a.length;
    17 if(b<=1)return a;
    18 int[] c = new int[b];
    19 int j=0;
    20 for(int i=0;i<b;i++){
    21 if(c[j]==0&&a[i]!=0)
    22 c[j]=a[i];
    23 else if(c[j]!=0&&a[i]==c[j]){
    24 c[j]=2*a[i];
    25 j++;
    26 }
    27 else if(a[i]!=0&&c[j]!=0&&a[i]!=c[j]){
    28 j++;
    29 c[j]=a[i];
    30 }
    31 }
    32 return c;
    33 }
    34 //该方法用于接受一个整数数组,对该数组进行drop操作后返回
    35 //示例:接受 2 0 2 0 5 5,返回4 10 0 0 0 0
    36 
    37 public static void main(String[] args){
    38 int[] a = {0,2,0,2,4,0,0,4,2,0,2,5,5,0,10};
    39 int[] b = drop(a);
    40 for(int i = 0;i<b.length;i++)
    41 System.out.print(b[i]+",");
    42 }
    43 //输出结果:4,8,4,10,10,0,0,0,0,0,0,0,0,0,0,
    44 
    45 }
    辅助工具

    这里的实现了键盘监听

  • 相关阅读:
    PL/SQL注册码
    分页sql
    js获取url值
    C语言中的bool类型 stdbool.h
    语音朗读小程序
    50. Pow(x, n)
    二维数组旋转
    用一位数组代替二维数组作为形参使用
    单链表排序——交换数据成员
    C++重载输入流、输出流运算符
  • 原文地址:https://www.cnblogs.com/liujinming/p/7611506.html
Copyright © 2011-2022 走看看