自己寫的1024

閒來無事自己寫了幾個applet小遊戲程序。

先來看下代碼構成

wKiom1kvz8KxOV7LAAAQNU6ZTuE137.png-wh_50


// 1024小遊戲源代碼

// 程序入口類My1024.java

package my1024;
import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.io.Serializable;
import java.util.Random;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import util.MyAWTEventListener;
public class My1024 extends JFrame implements Serializable {
 /**
  * 
  */
 private static final long serialVersionUID = 5977722980538750979L;
 static JTable table;
 static JLayeredPane desktop;
 static MyTableModule tbModule;
 static int rowCount = 5;
 static int colCount = 4;
 static RandomIndex randomIndex = new RandomIndex();
 public My1024() {
  super();
  desktop = this.getLayeredPane();
  setDefaultLookAndFeelDecorated(true);
  setSize(450, 600);
  setLocation(400, 200);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  buildFrame();
 }
 public static void main(String[] args) {
  My1024 myFrame = new My1024();
  myFrame.setVisible(true);
 }
 public static void buildFrame() {
  desktop.setLayout(new BoxLayout(desktop, BoxLayout.Y_AXIS));
  JPanel btnPanel = new JPanel();
  btnPanel.add(loadDescription());
  desktop.add(btnPanel);
  desktop.add(Box.createHorizontalStrut(50));
  JPanel tbPanel = new JPanel();
  tbPanel.add(loadTable());
  desktop.add(tbPanel);
 }
 public static long SCORE = 0L;
 static JLabel jtf = new JLabel();
 public static void setScore() {
  String SCORE_S = String.valueOf(SCORE);
  for (int i = 8; i > String.valueOf(SCORE).length(); i--) {
   SCORE_S = "0" + SCORE_S;
  }
  jtf.setText(SCORE_S);
 }
 public static JPanel loadDescription() {
  JPanel fr = new JPanel();
  JLabel j1, j2, j3;
  fr.setSize(60, 100);
  j1 = new JLabel("F1 - New Game");
  j1.setForeground(Color.BLUE);
  j1.setOpaque(true);
  j2 = new JLabel("F2 - Clear");
  j2.setForeground(Color.BLUE);
  j2.setOpaque(true);
  j3 = new JLabel("F3 - Cheat");
  j3.setForeground(Color.BLUE);
  j3.setOpaque(true);
  fr.add(j1);
  fr.add(j2);
  fr.add(j3);
  fr.setCursor(new Cursor(12));
  return fr;
 }
 public static void keyEvent(KeyEvent e) {
  // System.out.println(e.getKeyCode());
  switch (e.getKeyCode()) {
  case 112:// 112 F1
   begin();
   break;
  case 113:// 113 F2
   clear();
   break;
  case 114:// 114 F3
   cheat();
   break;
  case 37:// 37 left
   leftMethod();
   break;
  case 38:// 38 up
   upMethod();
   break;
  case 39:// 39 right
   rightMethod();
   break;
  case 40:// 40 down
   downMethod();
   break;
  default:
   break;
  }
 }
 public static JPanel loadTable() {
  JPanel fr4 = new JPanel();
  fr4.setBorder(new TitledBorder("My 1024"));
  fr4.setLayout(new BoxLayout(fr4, BoxLayout.Y_AXIS));
  tbModule = new MyTableModule(rowCount, colCount);
  table = new JTable(tbModule);
  table.setRowSelectionAllowed(false);
  table.setColumnSelectionAllowed(false);
  CellLabelRenderer tcr = new CellLabelRenderer();// 設置table內容居中
  tcr.setHorizontalAlignment(SwingConstants.CENTER);
  tcr.setVerticalAlignment(SwingConstants.CENTER);
  table.setDefaultRenderer(Object.class, tcr);
  for (int i = 0; i < table.getColumnCount(); i++) {
   table.getColumnModel().getColumn(i).setPreferredWidth(100);
  }
  table.setRowHeight(100);
  table.setRowHeight(0, 1);
  table.setFont(new Font("宋體", Font.BOLD, 20));
  Toolkit.getDefaultToolkit().addAWTEventListener(
    new MyAWTEventListener() {
     public void keyPressed(KeyEvent e) {
      keyEvent(e);
     }
    }, AWTEvent.KEY_EVENT_MASK);
  JPanel fr5 = new JPanel();
  JLabel jlb = new JLabel();
  jlb.setHorizontalAlignment(SwingConstants.CENTER);
  jlb.setVerticalAlignment(SwingConstants.CENTER);
  jlb.setText("分數:");
  fr5.add(jlb);
  jtf.setPreferredSize(new Dimension(140, 20));
  setScore();
  jtf.setFont(new Font("Goudy Stout", Font.BOLD, 18));
  jtf.setForeground(Color.RED);
  jtf.setOpaque(true);
  jtf.setBackground(Color.BLACK);
  jtf.setHorizontalAlignment(SwingConstants.CENTER);
  fr5.add(jtf);
  fr4.add(fr5);
  fr4.add(table);
  return fr4;
 }
 public static void cheat() {
 }
 public static void clear() {
  for (int i = 1; i < table.getModel().getRowCount(); i++) {
   for (int j = 0; j < table.getModel().getColumnCount(); j++) {
    Object value = table.getValueAt(i, j);
    if (!checkEmpty(value)) {
     if (Integer.valueOf(String.valueOf(value)) < 8) {
      table.setValueAt(null, i, j);
     }
    }
   }
  }
  desktop.repaint();
 }
 public static void begin() {
  for (int i = 1; i < table.getModel().getRowCount(); i++) {
   for (int j = 0; j < table.getModel().getColumnCount(); j++) {
    table.setValueAt(null, i, j);
   }
  }
  randomIndex = new RandomIndex();
  random();
  desktop.repaint();
 }
 static Random random = new Random();
 public static void random() {
  randomIndex.clear();
  for (int rowIndex = 1; rowIndex < rowCount; rowIndex++) {
   for (int colIndex = 0; colIndex < colCount; colIndex++) {
    if (!checkEmpty(tbModule.getValueAt(rowIndex, colIndex))) {
     randomIndex.addChoosneL(rowIndex * colCount + colIndex);
    } else {
     randomIndex.addIndexL(rowIndex * colCount + colIndex);
    }
   }
  }
  // 如果
  int index = randomIndex.randomIndex();
  int row = index / colCount;
  int col = index % colCount;
  int rnum = random.nextInt(50);
  int disNum = 2;
  if (rnum > 45) {
   // disNum = 4;
  }
  table.setValueAt(disNum, row, col);
  desktop.repaint();
 }
 public static boolean checkEmpty(Object cellV) {
  if (cellV == null || String.valueOf(cellV).length() == 0) {
   return true;
  }
  return false;
 }
 public static boolean compareData(Object val1, Object val2) {
  if ((val1 == null && val2 == null)
    || (String.valueOf(val1).equals(String.valueOf(val2)))) {
   return true;
  }
  return false;
 }
 public static boolean compareArr(Object[][] dataArr1, Object[][] dataArr2) {
  for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
   for (int colIndex = 0; colIndex < colCount; colIndex++) {
    if (!compareData(dataArr1[rowIndex][colIndex],
      dataArr2[rowIndex][colIndex])) {
     return false;
    }
   }
  }
  return true;
 }
 public static void upMethod() {
  // System.out.println("up");
  Object[][] dataArr = new Object[rowCount][colCount];
  int firstNum = -1;
  int secondNum = -1;
  // 按列判斷,行從低到高
  for (int colIndex = 0; colIndex < colCount; colIndex++) {
   for (int rowIndex = 1; rowIndex < rowCount; rowIndex++) {
    // 單元格非空
    if (!checkEmpty(table.getValueAt(rowIndex, colIndex))) {
     // System.out.println((table.getValueAt(rowIndex,
     // colIndex)));
     // 第一個值爲負賦給第一個,否則賦給第二個
     if (firstNum < 0) {
      firstNum = Integer.valueOf(String.valueOf(table
        .getValueAt(rowIndex, colIndex)));
     } else {
      secondNum = Integer.valueOf(String.valueOf(table
        .getValueAt(rowIndex, colIndex)));
     }
     // 如果兩個值一樣,那麼相加,清空第一個第二個
     if (firstNum >= 0 && secondNum >= 0
       && compareData(firstNum, secondNum)) {
      for (int i = 1; i < rowCount; i++) {
       if (checkEmpty(dataArr[i][colIndex])) {
        dataArr[i][colIndex] = firstNum + secondNum;
        SCORE += firstNum + secondNum;
        setScore();
        break;
       }
      }
      firstNum = -1;
      secondNum = -1;
     }
     // 如果兩個值不一樣,那麼賦值第一個,把第二個給第一個,清空能夠第二個
     else if (firstNum >= 0 && secondNum >= 0
       && !compareData(firstNum, secondNum)) {
      for (int i = 1; i < rowCount; i++) {
       if (checkEmpty(dataArr[i][colIndex])) {
        dataArr[i][colIndex] = firstNum;
        break;
       }
      }
      firstNum = secondNum;
      secondNum = -1;
     }
    }
    // 到達最大下標
    if (rowIndex == rowCount - 1) {
     for (int i = 1; i < rowCount; i++) {
      if (firstNum == -1) {
       break;
      }
      if (checkEmpty(dataArr[i][colIndex])) {
       dataArr[i][colIndex] = firstNum;
       firstNum = secondNum;
       secondNum = -1;
      }
     }
    }
   }
  }
  if (compareArr(dataArr, tbModule.dataArr)) {
   //System.out.println("Invalid Operation!");
  } else {
   tbModule.dataArr = dataArr;
   random();
   desktop.repaint();
  }
 }
 public static void downMethod() {
  // System.out.println("down");
  Object[][] dataArr = new Object[rowCount][colCount];
  int firstNum = -1;
  int secondNum = -1;
  // 按列判斷,行從高到低
  for (int colIndex = 0; colIndex < colCount; colIndex++) {
   for (int rowIndex = rowCount - 1; rowIndex >= 1; rowIndex--) {
    // 單元格非空
    if (!checkEmpty(table.getValueAt(rowIndex, colIndex))) {
     // System.out.println((table.getValueAt(rowIndex,
     // colIndex)));
     // 第一個值爲負賦給第一個,否則賦給第二個
     if (firstNum < 0) {
      firstNum = Integer.valueOf(String.valueOf(table
        .getValueAt(rowIndex, colIndex)));
     } else/* if (secondNum < 0) */{
      secondNum = Integer.valueOf(String.valueOf(table
        .getValueAt(rowIndex, colIndex)));
     }
     // 如果兩個值一樣,那麼相加,清空第一個第二個
     if (firstNum >= 0 && secondNum >= 0
       && compareData(firstNum, secondNum)) {
      for (int i = rowCount - 1; i >= 1; i--) {
       if (checkEmpty(dataArr[i][colIndex])) {
        dataArr[i][colIndex] = firstNum + secondNum;
        SCORE += firstNum + secondNum;
        setScore();
        break;
       }
      }
      firstNum = -1;
      secondNum = -1;
     }
     // 如果兩個值不一樣,那麼賦值第一個,把第二個給第一個,清空能夠第二個
     else if (firstNum >= 0 && secondNum >= 0
       && !compareData(firstNum, secondNum)) {
      for (int i = rowCount - 1; i >= 1; i--) {
       if (checkEmpty(dataArr[i][colIndex])) {
        dataArr[i][colIndex] = firstNum;
        break;
       }
      }
      firstNum = secondNum;
      secondNum = -1;
     }
    }
    // 到達最小下標
    if (rowIndex == 1) {
     for (int i = rowCount - 1; i >= 1; i--) {
      if (firstNum == -1) {
       break;
      }
      if (checkEmpty(dataArr[i][colIndex])) {
       dataArr[i][colIndex] = firstNum;
       firstNum = secondNum;
       secondNum = -1;
      }
     }
    }
   }
  }
  if (compareArr(dataArr, tbModule.dataArr)) {
//   System.out.println("Invalid Operation!");
  } else {
   tbModule.dataArr = dataArr;
   random();
   desktop.repaint();
  }
 }
 public static void leftMethod() {
  // System.out.println("left");
  Object[][] dataArr = new Object[rowCount][colCount];
  int firstNum = -1;
  int secondNum = -1;
  // 按行判斷,列從低到高
  for (int rowIndex = 1; rowIndex < rowCount; rowIndex++) {
   for (int colIndex = 0; colIndex < colCount; colIndex++) {
    // 單元格非空
    if (!checkEmpty(table.getValueAt(rowIndex, colIndex))) {
     // System.out.println((table.getValueAt(rowIndex,
     // colIndex)));
     // 第一個值爲負賦給第一個,否則賦給第二個
     if (firstNum < 0) {
      firstNum = Integer.valueOf(String.valueOf(table
        .getValueAt(rowIndex, colIndex)));
     } else/* if (secondNum < 0) */{
      secondNum = Integer.valueOf(String.valueOf(table
        .getValueAt(rowIndex, colIndex)));
     }
     // 如果兩個值一樣,那麼相加,清空第一個第二個
     if (firstNum >= 0 && secondNum >= 0
       && compareData(firstNum, secondNum)) {
      for (int i = 0; i < colCount; i++) {
       if (checkEmpty(dataArr[rowIndex][i])) {
        dataArr[rowIndex][i] = firstNum + secondNum;
        SCORE += firstNum + secondNum;
        setScore();
        break;
       }
      }
      firstNum = -1;
      secondNum = -1;
     }
     // 如果兩個值不一樣,那麼賦值第一個,把第二個給第一個,清空能夠第二個
     else if (firstNum >= 0 && secondNum >= 0
       && !compareData(firstNum, secondNum)) {
      for (int i = 0; i < colCount; i++) {
       if (checkEmpty(dataArr[rowIndex][i])) {
        dataArr[rowIndex][i] = firstNum;
        break;
       }
      }
      firstNum = secondNum;
      secondNum = -1;
     }
    }
    // 到達最大下標
    if (colIndex == colCount - 1) {
     for (int i = 0; i < colCount; i++) {
      if (firstNum == -1) {
       break;
      }
      if (checkEmpty(dataArr[rowIndex][i])) {
       dataArr[rowIndex][i] = firstNum;
       firstNum = secondNum;
       secondNum = -1;
      }
     }
    }
   }
  }
  if (compareArr(dataArr, tbModule.dataArr)) {
//   System.out.println("Invalid Operation!");
  } else {
   tbModule.dataArr = dataArr;
   random();
   desktop.repaint();
  }
 }
 public static void rightMethod() {
  // System.out.println("right");
  Object[][] dataArr = new Object[rowCount][colCount];
  int firstNum = -1;
  int secondNum = -1;
  // 按行判斷,列從高到低
  for (int rowIndex = 1; rowIndex < rowCount; rowIndex++) {
   for (int colIndex = colCount - 1; colIndex >= 0; colIndex--) {
    // 單元格非空
    if (!checkEmpty(table.getValueAt(rowIndex, colIndex))) {
     // System.out.println((table.getValueAt(rowIndex,
     // colIndex)));
     // 第一個值爲負賦給第一個,否則賦給第二個
     if (firstNum < 0) {
      firstNum = Integer.valueOf(String.valueOf(table
        .getValueAt(rowIndex, colIndex)));
     } else/* if (secondNum < 0) */{
      secondNum = Integer.valueOf(String.valueOf(table
        .getValueAt(rowIndex, colIndex)));
     }
     // 如果兩個值一樣,那麼相加,清空第一個第二個
     if (firstNum >= 0 && secondNum >= 0
       && compareData(firstNum, secondNum)) {
      for (int i = colCount - 1; i >= 0; i--) {
       if (checkEmpty(dataArr[rowIndex][i])) {
        dataArr[rowIndex][i] = firstNum + secondNum;
        SCORE += firstNum + secondNum;
        setScore();
        break;
       }
      }
      firstNum = -1;
      secondNum = -1;
     }
     // 如果兩個值不一樣,那麼賦值第一個,把第二個給第一個,清空能夠第二個
     else if (firstNum >= 0 && secondNum >= 0
       && !compareData(firstNum, secondNum)) {
      for (int i = colCount - 1; i >= 0; i--) {
       if (checkEmpty(dataArr[rowIndex][i])) {
        dataArr[rowIndex][i] = firstNum;
        break;
       }
      }
      firstNum = secondNum;
      secondNum = -1;
     }
    }
    // 到達最大下標
    if (colIndex == 0) {
     for (int i = colCount - 1; i >= 0; i--) {
      if (firstNum == -1) {
       break;
      }
      if (checkEmpty(dataArr[rowIndex][i])) {
       dataArr[rowIndex][i] = firstNum;
       firstNum = secondNum;
       secondNum = -1;
      }
     }
    }
   }
  }
  if (compareArr(dataArr, tbModule.dataArr)) {
//   System.out.println("Invalid Operation!");
  } else {
   tbModule.dataArr = dataArr;
   random();
   desktop.repaint();
  }
 }
}

//表格渲染器CellLabelRenderer.java

package my1024;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellRenderer;
import util.ObjectUtil;
public class CellLabelRenderer extends JLabel implements TableCellRenderer {
 private static final long serialVersionUID = 7083489923614792312L;
 public CellLabelRenderer() {
  setOpaque(true);
  setHorizontalAlignment(SwingConstants.CENTER);
  setVerticalAlignment(SwingConstants.CENTER);
 }
 static Color color_gray = new Color(232, 232, 232);
 public Component getTableCellRendererComponent(JTable table, Object value,
   boolean isSelected, boolean hasFocus, int row, int column) {
  String oValue = ObjectUtil.getNotNullValue(table
    .getValueAt(row, column));
  this.setFont(new Font("Arial", Font.BOLD, 28));
  this.setText(ObjectUtil.getNotNullValue(value));
  setVerticalAlignment(CENTER);
  setHorizontalAlignment(CENTER);
  if (row == 0) {
   this.setBackground(new Color(41, 1, 123));
  } else {
   if (My1024.compareData("2", oValue)) {
    this.setBackground(new Color(123, 123, 123));
   } else if (My1024.compareData("4", oValue)) {
    this.setBackground(new Color(123, 123, 223));
   } else if (My1024.compareData("8", oValue)) {
    this.setBackground(new Color(123, 223, 123));
   } else if (My1024.compareData("16", oValue)) {
    this.setBackground(new Color(223, 123, 123));
   } else if (My1024.compareData("32", oValue)) {
    this.setBackground(new Color(223, 223, 123));
   } else if (My1024.compareData("64", oValue)) {
    this.setBackground(new Color(223, 123, 223));
   } else if (My1024.compareData("128", oValue)) {
    this.setBackground(new Color(123, 223, 223));
   } else if (My1024.compareData("256", oValue)) {
    this.setBackground(new Color(223, 223, 223));//
   } else if (My1024.compareData("512", oValue)) {
    this.setBackground(new Color(76, 223, 223));
   } else if (My1024.compareData("1024", oValue)) {
    this.setBackground(new Color(223, 76, 223));
   } else if (My1024.compareData("2048", oValue)) {
    this.setBackground(new Color(223, 223, 76));
   } else if (My1024.compareData("4096", oValue)) {
    this.setBackground(new Color(76, 223, 76));
   } else if (My1024.compareData("8192", oValue)) {
    this.setBackground(new Color(76, 76, 223));
   } else if (My1024.compareData("16384", oValue)) {
    this.setBackground(new Color(76, 76, 76));
   } else {
    this.setBackground(Color.WHITE);
   }
  }
  return this;
 }
}

//鍵盤監聽類MyKeyListener.java

package my1024;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class MyKeyListener implements KeyListener {
 public void keyPressed(KeyEvent e) {
  System.out.println(e.getKeyCode());
  // 37 left
  // 38 up
  // 39 right
  // 40 down
 }
 public void keyReleased(KeyEvent e) {
 }
 public void keyTyped(KeyEvent e) {
 }
}

// 表格模型MyTableModule.java

package my1024;
import java.io.Serializable;
import javax.swing.table.AbstractTableModel;
public class MyTableModule extends AbstractTableModel implements Serializable {
 /**
  * 
  */
 private static final long serialVersionUID = 1978547795796513859L;
 private int rowCount = 1;
 private int columnCount = 1;
 public Object[][] dataArr = null;
 public MyTableModule(int rowCount, int columnCount) {
  if (columnCount > 1) {
   this.columnCount = columnCount;
  } else {
   this.columnCount = 1;
  }
  if (rowCount > 1) {
   this.rowCount = rowCount;
  } else {
   this.rowCount = 1;
  }
  dataArr = new Object[rowCount][columnCount];
 }
 public Class<?> getColumnClass(int columnIndex) {
  if (getValueAt(0, columnIndex) != null) {
   return getValueAt(0, columnIndex).getClass();
  } else {
   return String.class;
  }
 }
 public String getColumnName(int columnIndex) {
  return " 第" + columnIndex + "列";
 }
 public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
  if (rowIndex >= rowCount) {
   new Exception("Row Index Outbound!").printStackTrace();
  }
  if (columnIndex >= columnCount) {
   new Exception("Column Index Outbound!").printStackTrace();
  }
  if (dataArr == null) {
   dataArr = new Object[rowCount][columnCount];
  }
  dataArr[rowIndex][columnIndex] = aValue;
  fireTableCellUpdated(rowIndex, columnIndex);
 }
 public Object getValueAt(int rowIndex, int columnIndex) {
  if (dataArr == null) {
   dataArr = new Object[rowCount][columnCount];
  }
   return dataArr[rowIndex][columnIndex];
 }
 public boolean isCellEditable(int rowIndex, int columnIndex) {
  return false;
 }
 public int getRowCount() {
  return rowCount;
 }
 public int getColumnCount() {
  return columnCount;
 }
}

// 數據位置隨機類RandomIndex.java

package my1024;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class RandomIndex {
 List<Integer> indexL = null;
 List<Integer> choosenL = null;
 Random random = new Random();
 public RandomIndex() {
  init();
 }
 private void init() {
  indexL = new ArrayList<Integer>();
  choosenL = new ArrayList<Integer>();
  for (int i = 0; i < 16; i++) {
   indexL.add(i);
  }
 }
 public void addIndexL(Integer i) {
  if (indexL == null) {
   indexL = new ArrayList<Integer>();
  }
  indexL.add(i);
 }
 public void addChoosneL(Integer i) {
  if (choosenL == null) {
   choosenL = new ArrayList<Integer>();
  }
  choosenL.add(i);
 }
 public void clear() {
  indexL = new ArrayList<Integer>();
  choosenL = new ArrayList<Integer>();
 }
 public int randomIndex() {
  if (indexL.isEmpty()) {
   System.out.println("No more data!");
   return 0;
  }
  int index = random.nextInt(indexL.size());
  int number = indexL.get(index);
  choosenL.add(number);
  indexL.remove(index);
  return number;
 }
 public static void main(String args[]) {
  RandomIndex a = new RandomIndex();
  a.init();
  while (a.indexL.size() > 0) {
   System.out.println(a.randomIndex());
  }
  System.out.println(a.indexL);
 }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章