?? game.java
字號:
/*
* Created on 2005-6-2
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package bomb;
import java.util.Observable;
import java.util.List;
import java.util.LinkedList;;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Game extends Observable {
public Game(){
bricks = new Brick[MAX_ROWS][MAX_COLS];
for (int i=0;i<MAX_ROWS;i++){
for (int j=0;j<MAX_COLS;j++){
bricks[i][j] = new Brick(i,j);
}
}
queue = new LinkedList();
this.reset(Game.PRIMARY);
}
public void begin(){
this.state = PLAYING;
}
public void reset(int level){
this.preLevel = this.level;
this.level = level;
queue.clear();
if (level == Game.PRIMARY){
rows = 9;
cols = 8;
totalBombs = 10;
}else if (level == Game.MIDDLE){
rows = 16;
cols = 16;
totalBombs = 40;
}else if (level == Game.ADVANCE){
rows = MAX_ROWS;
cols = MAX_COLS;
totalBombs = 99;
}
taggedBombs = 0;
diggedBricks = 0;
for (int i=0;i<rows;i++){
for (int j=0;j<cols;j++){
bricks[i][j].reset() ;
}
}
//set the trap
for (int k=0;k<totalBombs;k++)
{
while(true){
int x = (int)(Math.random() * rows);
int y = (int)(Math.random() * cols);
if (!bricks[x][y].getTrap()){
bricks[x][y].setTrap(true);
break;
}
}
}
//set bombs atround
for (int i=0;i<rows;i++){
for (int j=0;j<cols;j++){
if (!bricks[i][j].getTrap()){
int bombsAround = 0;
if (i-1 >= 0){
if (j-1 >=0){
if (bricks[i-1][j-1].getTrap() ){
bombsAround++;
}
}
if (bricks[i-1][j].getTrap() ){
bombsAround++;
}
if (j+1 < cols){
if (bricks[i-1][j+1].getTrap() ){
bombsAround++;
}
}
}
if (j-1 >=0){
if (bricks[i][j-1].getTrap() ){
bombsAround++;
}
}
if (j+1 < cols){
if (bricks[i][j+1].getTrap() ){
bombsAround++;
}
}
if (i+1 < rows){
if (j-1 >=0){
if (bricks[i+1][j-1].getTrap() ){
bombsAround++;
}
}
if (bricks[i+1][j].getTrap() ){
bombsAround++;
}
if (j+1 < cols){
if (bricks[i+1][j+1].getTrap() ){
bombsAround++;
}
}
}
bricks[i][j].setBombs( bombsAround);
}
}
}
state = READY;
sendChanges();
}
public void digIt(int x,int y){
bricks[x][y].dig();
}
public void dig(int x,int y){
testBegin();
if (bricks[x][y].getState() .equals(Brick.NORMAL) &&
state.equals(PLAYING))
{
bricks[x][y].dig();
if (bricks[x][y].getTrap() ){
state = LOST;
for (int i = 0;i<this.rows;i++){
for (int k=0;k<this.cols;k++){
//System.out.println("1");
if (bricks[i][k].getTrap()){
//System.out.println()
bricks[i][k].display();
}
}
}
sendChanges();
}else {
digAround(x,y);
if (isSuccess()){
state = WIN;
sendChanges();
}
}
}
}
private void digAround(int xx,int yy){
queue.add(bricks[xx][yy]);
while(!queue.isEmpty()){
Brick brick = (Brick)queue.remove(0);
brick.dig();
diggedBricks ++;
if (brick.getBombs() == 0){
int x = brick.getX();
int y = brick.getY();
if (canbeDigged(x-1,y-1))queue.add(bricks[x-1][y-1]);
if (canbeDigged(x-1,y))queue.add(bricks[x-1][y]);
if (canbeDigged(x-1,y+1))queue.add(bricks[x-1][y+1]);
if (canbeDigged(x,y+1))queue.add(bricks[x][y+1]);
if (canbeDigged(x,y-1))queue.add(bricks[x][y-1]);
if (canbeDigged(x+1,y-1))queue.add(bricks[x+1][y-1]);
if (canbeDigged(x+1,y))queue.add(bricks[x+1][y]);
if (canbeDigged(x+1,y+1))queue.add(bricks[x+1][y+1]);
}
}
}
private boolean isSuccess(){
return (taggedBombs == totalBombs
&& diggedBricks == (rows * cols - totalBombs));
}
private boolean canbeDigged(int x,int y){
if (x>=0 && x<rows && y >= 0 && y < cols
&& bricks[x][y].getState().equals(Brick.NORMAL) ){
return true;
}else{
return false;
}
}
private void testBegin(){
if (state.equals(READY)){
state = PLAYING;
sendChanges();
}
}
public void tag(int x,int y){
testBegin();
if (bricks[x][y].getState().equals(Brick.NORMAL)
&& state.equals(PLAYING) ){
bricks[x][y].tagged();
taggedBombs++;
if (isSuccess()){
state = WIN;
}
sendChanges();
}
}
public void untag(int x,int y){
testBegin();
if (bricks[x][y].getState().equals(Brick.TAGGED)
&& state.equals(PLAYING) ){
taggedBombs--;
bricks[x][y].untagged();
if (isSuccess()){
state = WIN;
}
sendChanges();
}
}
private void sendChanges(){
this.setChanged();
this.notifyObservers();
}
public int getRows(){
return this.rows;
}
public int getCols(){
return this.cols;
}
public void addObserver(int x,int y,BrickButton button){
bricks[x][y].addObserver(button);
}
public int getLevle(){
return this.level;
}
public int getLeftBombs(){
return this.totalBombs - this.taggedBombs;
}
public String getState(){
return this.state;
}
public int getTotalBombs(){
return this.totalBombs;
}
public void clearListener(int x,int y){
bricks[x][y].deleteObservers();
}
public void clearAllListener(){
for (int i=0;i<rows;i++){
for (int k=0;k<cols;k++){
clearListener(i,k);
}
}
}
private final int MAX_ROWS = 16;
private final int MAX_COLS = 30;
public final static String PLAYING = "PLAYING";
public final static String WIN = "WIN";
public final static String LOST = "LOST";
public final static String PAUSE = "PAUSE";
public final static String READY = "READY";
public final static int PRIMARY = 1;
public final static int ADVANCE = 3;
public final static int MIDDLE = 2;
private int cols = 8;
private int rows = 10;
private int totalBombs = 15;
private int taggedBombs = 0;
private int diggedBricks = 0;
private String state = READY;
private List queue = null;
private int level = PRIMARY;
private int preLevel = PRIMARY;
private Brick bricks[][] = null;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -