import java.awt.*;import java.awt.event.*;import java.util.*;import java.io.*;
/*************************************************** 類功能簡介:游戲場景的實現(xiàn) 2004-03-05* @author gowithfox@163.com* @version 1.01**************************************************/
public class GamePlat extends Canvas //implements KeyListener{ public static final int BOARDNUM = 3; //可以活動的元素 public static final int WIDACTION = 201; //設(shè)置拼圖游戲活動場景圖片的寬度 public static final int HEIACTION = 201; //設(shè)置拼圖游戲活動場景圖片的高度 public static final int WID = 281; //設(shè)置整個GamePlat的寬度 public static final int HEI = 281; //設(shè)置整個GamePlat的高度
private int [][] boardArray; //游戲里面的可以變換的數(shù)組
private final int [][] WINARRAY = {{0,1,2}, //設(shè)定游戲成功的條件 {3,4,5}, {6,7,8}}; private int stepNum = 0; //記錄玩家走走過的步驟
private Random rand = null; private Toolkit tk = null; private Image spiriteImage = null; private int x8 = BOARDNUM-1,y8 = x8;
/************************************************** * 構(gòu)造方法:構(gòu)造整個游戲最重要的數(shù)據(jù) ***************************************************/
public GamePlat() { tk = getToolkit(); rand = new Random(); boardArray = new int[BOARDNUM][BOARDNUM]; try { spiriteImage = tk.createImage("photo.jpg"); } catch (Exception ex) { System.out.println(ex.getMessage()); } init(); }
/************************************************** * 方法介紹:初始化記錄步驟和并隨機(jī)生成每個數(shù)組里面的值 * 輸入?yún)?shù):無 * 返回類型:無 ****************************************************/
public void init() { stepNum = 0; generateBoardNum(); }
/************************************************** * 方法介紹:隨機(jī)生成每個數(shù)組里面的值 * 輸入?yún)?shù):無 * 返回類型:無 ****************************************************/
private void generateBoardNum() { int k=0; for (int i = 0 ; i < 3 ; i++) for (int j = 0 ; j < 3 ; j++) { boardArray[i][j] = k++; } for (int i = 0 ; i < 100 ; i++) { int x1,y2,x2,y1,temp; x1=Math.abs(rand.nextInt()%3); x2=Math.abs(rand.nextInt()%3); y1=Math.abs(rand.nextInt()%3); y2=Math.abs(rand.nextInt()%3); temp = boardArray[x1][y1]; boardArray[x1][y1]=boardArray[x2][y2]; boardArray[x2][y2]=temp; } int x=0,y=0,temp; for (int i = 0 ; i < 3 ; i++) for(int j = 0 ; j < 3 ; j++) &nbs