Commit 97019046 authored by 熊东起's avatar 熊东起

flex:--1

parent bf370dff
......@@ -24,5 +24,5 @@ export namespace Config {
export const GRID_COUNT = 10;
/** 最大可玩次数 */
export const MAX_MOVES = 2;
export const MAX_MOVES = 200;
}
......@@ -10,20 +10,24 @@ import { Grid } from "./components/Grid";
import { Config } from "./GameCfg";
/** 颜色类型 */
export type Color = string;
enum STATUS {
win = 1,
lose = 2,
continue = 3
}
export class GameScene extends FYGE.Container {
private moves: number = 0;
private btns: FYGE.Graphics[] = [];
private grids: Grid[][] = [];
private gameContainer: FYGE.Container;
private matched: Grid[] = [];
private stepNum: FYGE.TextField;
constructor() {
super();
this.initUi();
}
initUi() {
this.initBtns();
this.createMap();
this.createMapByColor();
}
/**
* @description: 初始化6个按钮
......@@ -36,122 +40,131 @@ export class GameScene extends FYGE.Container {
let color = Config.GRID_COLORS[i];
let _rect = this.addChild(new FYGE.Graphics()).lineStyle(0.5, 0xffffff, 1).beginFill(color, 1).drawRect(sx, sy, 100, 100).endFill();
_rect.name = String(color);
_rect.addEventListener(FYGE.MouseEvent.CLICK, () => this.handleBtnClick(String(color)), this);
_rect.addEventListener(FYGE.MouseEvent.CLICK, () => this.onClickBtn(color), this);
this.btns[i] = _rect;
}
let text = this.addChild(new FYGE.TextField());
text.text = "步数0";
text.x = 375;
text.y = 20;
this.stepNum = text;
}
/**
* @description: 点击按钮
* @param {*} color
*/
handleBtnClick(btnColor: Color) {
// TODO: 第二次点击不变了
console.log("dd", btnColor);
let oldColor = this.grids[0][0].oldColor;
console.log(oldColor);
this.moves++;
this.matched = [];
this.doMatch(oldColor, btnColor, 0, 0);
if (this.matched.length > 0) this.doChangeColor();
get step(): number {
return this.moves;
}
/**
* @description: 匹配色块
* @param {Color} oldColor
* @param {Color} newColor
* @param {number} x
* @param {number} y
* @return {*}
*/
doMatch(oldColor: Color, newColor: Color, x: number, y: number) {
if (oldColor === newColor || this.grids[x][y].newColor !== oldColor) {
return;
}
this.grids[x][y].newColor = newColor;
if (this.matched.indexOf(this.grids[x][y]) === -1) {
this.matched.push(this.grids[x][y]);
set step(v: number) {
this.moves = v;
this.stepNum.text = "步数" + String(v);
}
// 上下左右搂一圈
if (x > 0) {
this.doMatch(oldColor, newColor, x - 1, y);
/** 创建地图 */
map: number[][];
createMapByColor() {
this.gameContainer = new FYGE.Container();
this.addChild(this.gameContainer);
this.map = [];
for (let i = 0; i < Config.GRID_COUNT; i++) {
this.map[i] = [];
for (let j = 0; j < Config.GRID_COUNT; j++) {
let _color = Config.GRID_COLORS[Math.floor(Math.random() * 6)];
this.map[i].push(_color);
}
if (x < Config.GRID_COUNT - 1) {
this.doMatch(oldColor, newColor, x + 1, y);
}
if (y > 0) {
this.doMatch(oldColor, newColor, x, y - 1);
this.rendermap();
}
if (y < Config.GRID_COUNT - 1) {
this.doMatch(oldColor, newColor, x, y + 1);
/** 渲染地图 */
rendermap() {
if (this.map && this.map.length > 0) {
let startX = (Config.PSD_SIZE[0] - Config.GRID_SIZE * Config.GRID_COUNT) / 2;
this.gameContainer.removeAllChildren();
this.map.forEach((colum, i) => {
colum.forEach((row, j) => {
//渲染
let sx = startX + j * Config.GRID_SIZE;
let sy = 66 + i * Config.GRID_SIZE;
let _rect = this.gameContainer.addChild(new FYGE.Graphics());
_rect.lineStyle(1, 0xffffff, 1).beginFill(row, 1).drawRect(0, 0, Config.GRID_SIZE, Config.GRID_SIZE).endFill();
_rect.position.x = sx;
_rect.position.y = sy;
});
});
}
}
/**
* @description: 开始更新格子
* @param {*}
* @return {*}
*/
doChangeColor() {
console.log("doChangeColor");
/** 匹配 */
matchColor(newColor: number) {
let lastMap = [];
let oldColor = this.map[0][0];
this.matched.map((item) => {
item
.clear()
.beginFill(+item.newColor, 1)
.drawRect(0, 0, Config.GRID_SIZE, Config.GRID_SIZE)
.endFill();
item.oldColor = item.newColor;
for (let i = 0; i < this.map.length; i++) {
for (let j = 0; j < this.map[i].length; j++) {
let leftBlock = j > 0 ? this.map[i][j - 1] : 0,
topBlock = i > 0 ? this.map[i - 1][j] : 0;
let str = `${i}-${j}`;
if (this.map[i][j] == oldColor) {
if (topBlock == oldColor) {
if (lastMap.indexOf(`${i - 1}-${j}`) !== -1) {
if (lastMap.indexOf(str) === -1) {
lastMap.push(str);
}
}
}
if (leftBlock == oldColor) {
if (lastMap.indexOf(`${i}-${j - 1}`) !== -1) {
if (lastMap.indexOf(str) === -1) {
lastMap.push(str);
}
}
}
if (i == 0 && j == 0) {
if (lastMap.indexOf(str) === -1) {
lastMap.push(str);
}
}
}
}
}
lastMap.forEach((item, index) => {
let i = item.split("-")[0], j = item.split("-")[1];
this.map[i][j] = newColor;
});
}
if (this.checkWin()) {
console.log("你赢了");
} else if (this.moves >= Config.MAX_MOVES) {
console.log("你输了");
/** 选择颜色 */
onClickBtn(color: number) {
this.matchColor(color);
this.rendermap();
if (this.onCheck() == STATUS.win) {
alert("成功");
} else if (this.onCheck() == STATUS.lose) {
alert("失败");
}
this.step++;
}
checkWin() {
let startColor = this.grids[0][0].newColor;
for (var x = 0; x < Config.GRID_COUNT; x++) {
for (var y = 0; y < Config.GRID_COUNT; y++) {
if (this.grids[x][y].newColor !== startColor) {
return false;
/** 检查输赢 */
onCheck() {
if (this.step > Config.MAX_MOVES) {
return STATUS.lose;
}
if (this.step <= Config.MAX_MOVES) {
let start = this.map[0][0], flag = 0;
for (let i = 0; i < this.map.length; i++) {
for (let j = 0; j < this.map[i].length; j++) {
if (this.map[i][j] != start) {
flag = 1;
}
}
return true;
}
/**
* @description: 创建格子地图
*/
createMap() {
this.grids = [];
this.gameContainer = new FYGE.Container();
this.addChild(this.gameContainer);
let gSize = Config.GRID_SIZE;
// 起始X
let startX = (Config.PSD_SIZE[0] - gSize * Config.GRID_COUNT) / 2;
for (var x = 0; x < Config.GRID_COUNT; x++) {
this.grids[x] = [];
for (var y = 0; y < Config.GRID_COUNT; y++) {
let sx = startX + x * gSize;
let sy = 66 + y * gSize;
// 随机颜色
let _index = Math.floor(Math.random() * 6);
let _rect = this.gameContainer.addChild(new Grid());
_rect.lineStyle(1, 0xffffff, 1).beginFill(Config.GRID_COLORS[_index], 1).drawRect(0, 0, gSize, gSize).endFill();
_rect.position.x = sx;
_rect.position.y = sy;
let color = String(Config.GRID_COLORS[_index]);
_rect.oldColor = color;
_rect.newColor = color;
this.grids[x][y] = _rect;
if (flag == 0) {
return STATUS.win;
}
}
return STATUS.continue;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment