javaFX 實現粗糙版的挖地雷

首先..因為javafx剛開始接觸..不是很熟悉..既然他能調用java類..所以我們用java來構建
地雷對象

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package landmine;

/**
*
* @author Administrator
*/
public class Landmine {
public Landmine getBottom() {
return bottom;
}

public void setBottom(Landmine bottom) {
this.bottom = bottom;
}

public Landmine getLeft() {
return left;
}

public void setLeft(Landmine left) {
this.left = left;
}

public Landmine getRight() {
return right;
}

public void setRight(Landmine right) {
this.right = right;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public Landmine getTop() {
return top;
}

public void setTop(Landmine top) {
this.top = top;
}

public Landmine getBottomLeft() {
return bottomLeft;
}

public void setBottomLeft(Landmine bottomLeft) {
this.bottomLeft = bottomLeft;
}

public Landmine getBottomRight() {
return bottomRight;
}

public void setBottomRight(Landmine bottomRight) {
this.bottomRight = bottomRight;
}

public Landmine getTopLeft() {
return topLeft;
}

public void setTopLeft(Landmine topLeft) {
this.topLeft = topLeft;
}

public Landmine getTopRight() {
return topRight;
}

public void setTopRight(Landmine topRight) {
this.topRight = topRight;
}
public void updateStatus(){
if(this.top!=null&&this.top.status!=-1){
this.top.status++ ;
}
if(this.topLeft!=null&&this.topLeft.status!=-1){
this.topLeft.status++ ;
}
if(this.topRight!=null&&this.topRight.status!=-1){
this.topRight.status++ ;
}
if(this.left!=null&&this.left.status!=-1){
this.left.status++ ;
}
if(this.right!=null&&this.right.status!=-1){
this.right.status++ ;
}
if(this.bottom!=null&&this.bottom.status!=-1){
this.bottom.status++ ;
}
if(this.bottomLeft!=null&&this.bottomLeft.status!=-1){
this.bottomLeft.status++ ;
}
if(this.bottomRight!=null&&this.bottomRight.status!=-1){
this.bottomRight.status++ ;
}
}

private Landmine top ;
private Landmine topLeft ;
private Landmine topRight ;
private Landmine bottom ;
private Landmine bottomLeft ;
private Landmine bottomRight ;
private Landmine left ;
private Landmine right ;
private int status ;

}


和地雷地圖對象

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package landmine;

import java.util.Random;

/**
*
* @author Administrator
*/
public class LandmineMap {

public static void main(String args[]) {
System.out.println("aaaaaaaaaaaa");
LandmineMap lm = new LandmineMap(10, 10, 20);
lm.makeMap();
}
public static Landmine[] getMap(int x , int y , int lmNums){
LandmineMap lm = new LandmineMap(x, x, lmNums);
return lm.makeMap() ;
}
private int x;
private int y;
private int lmNums;
private Random random = null;

public LandmineMap(int x, int y, int lmNums) {
this.x = x;
this.y = y;
this.lmNums = lmNums;
}

public Landmine[] makeMap() {
random = new Random();
Landmine[] landmines = new Landmine[x * y];
int location = 0;
for (int i = 0; i < lmNums; i++) {
location = getRandomNum();
if (landmines[location] == null) {
landmines[location] = new Landmine();
landmines[location].setStatus(-1);
} else {
i--;
}
}
for (int i = 0; i < x * y; i++) {
if (landmines[i] == null) {
landmines[i] = new Landmine();
}
}
int[] ints = null;
for (int i = 0; i < x * y; i++) {
ints = this.getLocation(i);
if (ints[0] != -1) {
landmines[i].setTopLeft(landmines[ints[0]]);
}
if (ints[1] != -1) {
landmines[i].setTop(landmines[ints[1]]);
}
if (ints[2] != -1) {
landmines[i].setTopRight(landmines[ints[2]]);
}
if (ints[3] != -1) {
landmines[i].setLeft(landmines[ints[3]]);
}
if (ints[4] != -1) {
landmines[i].setRight(landmines[ints[4]]);
}
if (ints[5] != -1) {
landmines[i].setBottomLeft(landmines[ints[5]]);
}
if (ints[6] != -1) {
landmines[i].setBottom(landmines[ints[6]]);
}
if (ints[7] != -1) {
landmines[i].setBottomRight(landmines[ints[7]]);
}
}
for (int i = 0; i < x * y; i++) {
if (landmines[i].getStatus() == -1) {
landmines[i].updateStatus();
}
}
return landmines ;
}
private int tempX = 0;
private int tempY = 0;

public int[] getLocation(int location) {
int[] ints = new int[8];
tempX = location % x;
tempY = location / y;
if ((tempX - 1) >= 0 && (tempY - 1) >= 0) {
ints[0] = location - x - 1;
} else {
ints[0] = -1;
}
if ((tempY - 1) >= 0) {
ints[1] = location - x;
} else {
ints[1] = -1;
}
if ((tempX + 1) < x && (tempY - 1) >= 0) {
ints[2] = location - x + 1;
} else {
ints[2] = -1;
}
if ((tempX - 1) >= 0) {
ints[3] = location - 1;
} else {
ints[3] = -1;
}
if ((tempX + 1) < x) {
ints[4] = location + 1;
} else {
ints[4] = -1;
}
if ((tempX - 1) >= 0 && (tempY + 1) < y) {
ints[5] = location + x - 1;
} else {
ints[5] = -1;
}
if ((tempY + 1) < y) {
ints[6] = location + x;
} else {
ints[6] = -1;
}
if ((tempX + 1) < x && (tempY + 1) < y) {
ints[7] = location + x + 1;
} else {
ints[7] = -1;
}
return ints;
}

public int getRandomNum() {
return random.nextInt(x * y);
}
}



這個代碼就不解釋了沒什麼好解釋的..

下麵是fx的代碼

/*
* Main.fx
*
* Created on 2009-8-15, 17:42:50
*/

package landmine;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.MouseButton;

import javafx.scene.layout.VBox;

import javafx.scene.text.FontWeight;
import javafx.scene.text.TextOrigin;
import javafx.scene.control.TextBox;
import javafx.scene.control.Button;


/**
* @author Administrator
*/
var x = 10 ;
var y = 10 ;

var lmNums = 20 ;

var landmines :Landmine[] = LandmineMap.getMap(x, y, lmNums) ;

var lmFxs:LmFx[] = [] ;

static var lmClose = Color.WHITE;

var stage:Stage = null ;


class MyCustomNode extends CustomNode {

public override function create(): Node {
return Group {
content: [
for(j in [0 .. y-1]){
for(i in [0 .. x-1]){
lmFxs[j*x+i] = LmFx {
isLm : (landmines[j*x+i].getStatus()==-1);
img : Rectangle {
x: i*26+8, y: j*26+8
width: 24, height: 24
fill: lmClose ;
onMouseReleased: function( e: MouseEvent ):Void {
if(e.button==MouseButton.PRIMARY){
lmFxs[j*x+i].onLeftClick() ;
}
if(e.button==MouseButton.SECONDARY){
lmFxs[j*x+i].onRightClick() ;
}
}
}
status : Text {
x: i*26+8+8, y: j*26+8+16
font : Font {
size: 12
}
visible : false ;
content : "{landmines[j*x+i].getStatus()}" ;
fill: Color.RED
}
}
}
}
]
};
}
}


class LmFx extends CustomNode {
override function create():Node {
return Group {
content:[img,status]
};
}
var flag = false ;
var img:Rectangle = null ;
var status:Text = null ;
var isLm = false ;
function onLeftClick(){
if(not flag){
if(isLm){
setBoard.visible = true;

}else{
img.visible = false ;
status.visible = true ;
}
}
}
function onRightClick(){
if(flag){
img.fill = lmClose;
flag = false ;
}else{
img.fill = Color.RED;
flag = true ;
}
}

}

var width = bind x*24+(x-1)*2+16 ;
var height = bind y*24+(y-1)*2+16 ;

var t1:Text = null ;
var widthField : TextBox = null ;
var heightField : TextBox = null ;
var setBoard = Group {
visible : false ;
blocksMouse: true;
translateX: bind width / 6
translateY: bind height / 4
content: [
Rectangle {
stroke: Color.WHITE
strokeWidth: 3
width: bind width*2/3 ;
height: bind height/2 ;
fill : Color.BLUE;
opacity: 0.5
arcHeight: 10
arcWidth: 10
},
VBox {
translateX: bind width / 8
translateY: bind height / 12
content: [
Text {
content: "宽度:"
textOrigin: TextOrigin.TOP
fill: Color.YELLOW
font: Font.font ( null, FontWeight.BOLD, 15);
},
widthField = TextBox {
text : "10"
columns: 7
selectOnFocus: true
}

Text {

content: "高度:"
textOrigin: TextOrigin.TOP
fill: Color.YELLOW
font: Font.font ( null, FontWeight.BOLD, 15);
},
heightField = TextBox {
text: "10"
columns: 7
selectOnFocus: true
},
Button {
text: "reSet"
action: function() {

}
}



]

}
]
}



function run(){
stage = Stage {
title: "挖地雷"
scene: Scene {
fill : Color.BLACK ;
width: x*24+(x-1)*2+16 ;
height: y*24+(y-1)*2+16 ;
content: [
MyCustomNode{},
setBoard
]
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值