import java.awt.*;import java.awt.event.*;import java.util.*;import java.io.*;
/*************************************************** 类功能简介:游戏场景的实现 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; //设置拼图游戏活动场景图片的宽度 public static final int HEIACTION = 201; //设置拼图游戏活动场景图片的高度 public static final int WID = 281; //设置整个GamePlat的宽度 public static final int HEI = 281; //设置整个GamePlat的高度
private int [][] boardArray; //游戏里面的可以变换的数组
private final int [][] WINARRAY = {{0,1,2}, //设定游戏成功的条件 {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;
/************************************************** * 构造方法:构造整个游戏最重要的数据 ***************************************************/
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(); }
/************************************************** * 方法介绍:初始化记录步骤和并随机生成每个数组里面的值 * 输入参数:无 * 返回类型:无 ****************************************************/
public void init() { stepNum = 0; generateBoardNum(); }
/************************************************** * 方法介绍:随机生成每个数组里面的值 * 输入参数:无 * 返回类型:无 ****************************************************/
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