编号规则NumberSeq::newGetNumFromCode

本文提供了一个生成唯一图片ID的代码示例,使用NumberSeq方法获取序列号并应用于图片标识符,确保每个图片拥有独一无二的ID。

this.PicId = NumberSeq::newGetNumFromCode('PicId',true).num();

转载于:https://www.cnblogs.com/perock/archive/2011/08/10/2133758.html

import { promptAction } from "@kit.ArkUI"; import { LogDomain, LogHelper } from "../../../common/utils/LogHelper"; import { themeManager } from "../manager/ThemeManager"; import { LabyrinthModel } from "../model/LabyrinthModel"; import { LabyrinthUiMode } from "../model/LabyrinthUiMode"; const TAG = 'StarlightLabyrinth1'; const log: LogHelper = LogHelper.getLogHelper(LogDomain.HOME, TAG); //每块样式 @Component export struct HuaRongDao3x3 { // 1~8 + 0(空白) @State tiles: number[] = [1,2,3,4,5,6,7,8,0]; // 目标顺序 readonly goal:number[] = [1,2,3,4,5,6,7,8,0]; // @State LabyrinthUiMode: LabyrinthUiMode = new LabyrinthUiMode(); // LabyrinthModel: LabyrinthModel = new LabyrinthModel(); onAboutToAppear(){ // themeManager.registerOnClickListener(this); } // 页面加载时随机打乱(保证可解) aboutToAppear() { this.shuffle(); themeManager.registerOnClickListener(this); } onPageClick(){ console.log('进来了22224') // this.reset(); this.onTileClick(1); console.log('进来了22223') } // Fisher-Yates 洗牌并校验可解 shuffle() { log.info('进来了1111') console.log('进来了2222') for (let i = this.tiles.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); const temp = this.tiles[i]; this.tiles[i] = this.tiles[j]; this.tiles[j] = temp; } } swap(numberSeq: number[], indexLeft: number, indexRight: number) { const temp = numberSeq[indexLeft]; numberSeq[indexLeft] = numberSeq[indexRight]; numberSeq[indexRight] = temp; } validShuffle() { //1 todo 打乱 this.shuffle(); console.log(`111112${this.shuffle()}`); //2 todo 合法 if (this.isSolvable() && !this.isWin()) { // 有解,并且不是成功状态 return; } if (!this.isSolvable()) { for (let i = this.tiles.length - 1; i > 1; i--) { if (this.tiles[i] && this.tiles[i-1]) { this.swap(this.tiles, i, i-1); break; } } console.log(`111113${this.isSolvable()}`); } if (this.isWin()) { console.log(`111114${this.isWin()}`); } } // 计算逆序数 + 空白行号,判断可解 isSolvable(): boolean { let inv = 0; let blankRow = 0; const arr = this.tiles.filter(v => v !== 0); for (let i = 0; i < arr.length; i++) { for (let j = i + 1; j < arr.length; j++) { if (arr[i] > arr[j]) inv++; } } blankRow = Math.floor(this.tiles.indexOf(0) / 3); return (inv + blankRow) % 2 === 0; } getReverseNum(queList: number[]): number { return 1; } // 点击数字块 onTileClick(index: number) { console.log('进来了22225') const blank = this.tiles.indexOf(0); const row = Math.floor(index / 3); const col = index % 3; const blankRow = Math.floor(blank / 3); const blankCol = blank % 3; // 仅允许上下左右移动 const dx = Math.abs(row - blankRow); const dy = Math.abs(col - blankCol); if ((dx === 1 && dy === 0) || (dx === 0 && dy === 1)) { // [this.tiles[index], this.tiles[blank]] = [this.tiles[blank], this.tiles[index]]; const temp = this.tiles[index]; this.tiles[index] = this.tiles[blank]; this.tiles[blank] = temp; } } // 是否已胜利 isWin(): boolean { return this.tiles.every((v, i) => v === this.goal[i]); } // 重新开始 reset() { this.shuffle(); } build() { Column({ space: 12 }) { // 九宫格 Flex({ direction: FlexDirection.Row, wrap: FlexWrap.Wrap,justifyContent:FlexAlign.Center}) { ForEach(this.tiles, (item: number, index: number) => { Image(item === 0?$r('app.media.nova_puzzle_a00'): $r(`app.media.nova_puzzle_a0${item}`)) .width('28%').height(55) .backgroundColor(item === 0 ? Color.Black : Color.Blue) .margin(4) // .onClick(() => this.onTileClick(index)); }); } .width('100%') // 胜利提示 // if (this.isWin()) { // Text('恭喜完成!🎉') // .fontSize(22).fontColor(Color.Green) // .margin({ top: 20 }); // } // 重新开始按钮 // Button('重新开始') // .width('60%').height(45) // .fontSize(18) // .onClick(() => this.reset()); // // .onClick(() => this.onPageClick()); } .width('100%').height('100%') .alignItems(HorizontalAlign.Center); } }//todo:我该怎么把foreach里面的index参数传给onPageClick里的this.onTileClick()
最新发布
09-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值