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,148 +10,161 @@ 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[] = [];
constructor() {
super();
this.initUi();
}
initUi() {
this.initBtns();
this.createMap();
}
/**
* @description: 初始化6个按钮
*/
initBtns() {
this.btns = [];
for (let i = 0; i < Config.GRID_COLORS.length; i++) {
let sx = 0;
let sy = 110 * i + 50;
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);
this.btns[i] = _rect;
}
}
private moves: number = 0;
private btns: FYGE.Graphics[] = [];
private gameContainer: FYGE.Container;
private stepNum: FYGE.TextField;
/**
* @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();
}
/**
* @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]);
constructor() {
super();
this.initUi();
}
// 上下左右搂一圈
if (x > 0) {
this.doMatch(oldColor, newColor, x - 1, y);
initUi() {
this.initBtns();
this.createMapByColor();
}
if (x < Config.GRID_COUNT - 1) {
this.doMatch(oldColor, newColor, x + 1, y);
/**
* @description: 初始化6个按钮
*/
initBtns() {
this.btns = [];
for (let i = 0; i < Config.GRID_COLORS.length; i++) {
let sx = 0;
let sy = 110 * i + 50;
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.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;
}
if (y > 0) {
this.doMatch(oldColor, newColor, x, y - 1);
get step(): number {
return this.moves;
}
if (y < Config.GRID_COUNT - 1) {
this.doMatch(oldColor, newColor, x, y + 1);
set step(v: number) {
this.moves = v;
this.stepNum.text = "步数" + String(v);
}
}
/**
* @description: 开始更新格子
* @param {*}
* @return {*}
*/
doChangeColor() {
console.log("doChangeColor");
/** 创建地图 */
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);
}
}
this.rendermap();
this.matched.map((item) => {
item
.clear()
.beginFill(+item.newColor, 1)
.drawRect(0, 0, Config.GRID_SIZE, Config.GRID_SIZE)
.endFill();
item.oldColor = item.newColor;
});
}
/** 渲染地图 */
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;
});
});
}
if (this.checkWin()) {
console.log("你赢了");
} else if (this.moves >= Config.MAX_MOVES) {
console.log("你输了");
}
}
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;
/** 匹配 */
matchColor(newColor: number) {
let lastMap = [];
let oldColor = this.map[0][0];
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;
});
}
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());
/** 选择颜色 */
onClickBtn(color: number) {
this.matchColor(color);
this.rendermap();
if (this.onCheck() == STATUS.win) {
alert("成功");
} else if (this.onCheck() == STATUS.lose) {
alert("失败");
}
this.step++;
}
_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;
}
/** 检查输赢 */
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;
}
}
}
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