add gbanging

This commit is contained in:
LeeJu 2026-05-22 16:39:09 +08:00
parent 520c2fe6d7
commit 75daaa3bd3
70 changed files with 2954 additions and 0 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 151 KiB

After

Width:  |  Height:  |  Size: 206 KiB

21
gbanging/README.md Normal file
View File

@ -0,0 +1,21 @@
# pointgaming
### 1、项目说明
总体思路TSP求解仿真仿真项目开发的总体思路如图1.1示意旅行商问题求解仿真项目拟实现一个仿真系统。具体功能描述1能够让读者产生一个问题规模为n的TSP可以手工添加节点、边及权值来产生也可以由程序自动产生并在屏幕上显示2能够以遍历算法和贪心算法自动求解该TSP并将问题的解以可视化的方式显示3能够支持用户手动标记一条路径然后系统判断该路径是否是贪心算法路径4能够获取地图中的城市相关数据构造TSP并进行求解。
图1.1 项目开发的总体思路图
<img src="figs/overall.png" width=80% /></center>
### 2、开发环境说明
1系统版本OpenHarmony 4.0 Release
2SDK版本API 10
3Model类型Stage
4DevEco Studio版本Deveco Studio 4.0 release
5硬件环境Unionpi Whale、RK3566开发板
6预览器参数(1)DeviceType:default;(2)Resolution:560-1280;(3)DPI:240;其余参数自定。
### 3、项目主要函数调用关系图与项目输出界面演进图
图3.1 项目主要函数调用关系图
<img src="figs/relation.png" width=80% /></center>
图3.2 项目输出界面演进图
<img src="figs/evolution.png" width=80% /></center>

BIN
gbanging/figs/evolution.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 KiB

BIN
gbanging/figs/overall.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 360 KiB

BIN
gbanging/figs/relation.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

12
gbanging/gbang/.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
/node_modules
/oh_modules
/local.properties
/.idea
**/build
/.hvigor
.cxx
/.clangd
/.clang-format
/.clang-tidy
**/.test
/.appanalyzer

View File

@ -0,0 +1,10 @@
{
"app": {
"bundleName": "ets.text.book.relational.GBang",
"vendor": "example",
"versionCode": 1000000,
"versionName": "1.0.0",
"icon": "$media:layered_image",
"label": "$string:app_name"
}
}

View File

@ -0,0 +1,8 @@
{
"string": [
{
"name": "app_name",
"value": "GBang"
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

@ -0,0 +1,7 @@
{
"layered-image":
{
"background" : "$media:background",
"foreground" : "$media:foreground"
}
}

View File

@ -0,0 +1,49 @@
{
"app": {
"signingConfigs": [
{
"name": "default",
"material": {
"certpath": "C:/Users/Lee/.ohos/config/openharmony/default_GBang12_ywcc4wKsHX3KtIUfRy1nhL3K1JasWo-jsUoNCbLix08=.cer",
"keyAlias": "debugKey",
"keyPassword": "0000001A289116FFCB07EEBD1C5D4E5B0A6272248114C05A617BCBF5823BAD5B75CE453C4A386555ADB7",
"profile": "C:/Users/Lee/.ohos/config/openharmony/default_GBang12_ywcc4wKsHX3KtIUfRy1nhL3K1JasWo-jsUoNCbLix08=.p7b",
"signAlg": "SHA256withECDSA",
"storeFile": "C:/Users/Lee/.ohos/config/openharmony/default_GBang12_ywcc4wKsHX3KtIUfRy1nhL3K1JasWo-jsUoNCbLix08=.p12",
"storePassword": "0000001A66C2EAC9556A0F4ADB4EA098E038D8AA4C5468BB2584E4EC05D8FF72E1BF8BB2C137A6B84B74"
}
}
],
"products": [
{
"name": "default",
"signingConfig": "default",
"compileSdkVersion": 12,
"compatibleSdkVersion": 12,
"runtimeOS": "OpenHarmony"
}
],
"buildModeSet": [
{
"name": "debug",
},
{
"name": "release"
}
]
},
"modules": [
{
"name": "entry",
"srcPath": "./entry",
"targets": [
{
"name": "default",
"applyToProducts": [
"default"
]
}
]
}
]
}

View File

@ -0,0 +1,32 @@
{
"files": [
"**/*.ets"
],
"ignore": [
"**/src/ohosTest/**/*",
"**/src/test/**/*",
"**/src/mock/**/*",
"**/node_modules/**/*",
"**/oh_modules/**/*",
"**/build/**/*",
"**/.preview/**/*"
],
"ruleSet": [
"plugin:@performance/recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"@security/no-unsafe-aes": "error",
"@security/no-unsafe-hash": "error",
"@security/no-unsafe-mac": "warn",
"@security/no-unsafe-dh": "error",
"@security/no-unsafe-dsa": "error",
"@security/no-unsafe-ecdsa": "error",
"@security/no-unsafe-rsa-encrypt": "error",
"@security/no-unsafe-rsa-sign": "error",
"@security/no-unsafe-rsa-key": "error",
"@security/no-unsafe-dsa-key": "error",
"@security/no-unsafe-dh-key": "error",
"@security/no-unsafe-3des": "error"
}
}

6
gbanging/gbang/entry/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
/node_modules
/oh_modules
/.preview
/build
/.cxx
/.test

View File

@ -0,0 +1,28 @@
{
"apiType": "stageMode",
"buildOption": {
},
"buildOptionSet": [
{
"name": "release",
"arkOptions": {
"obfuscation": {
"ruleOptions": {
"enable": false,
"files": [
"./obfuscation-rules.txt"
]
}
}
}
},
],
"targets": [
{
"name": "default"
},
{
"name": "ohosTest",
}
]
}

View File

@ -0,0 +1,6 @@
import { hapTasks } from '@ohos/hvigor-ohos-plugin';
export default {
system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins: [] /* Custom plugin to extend the functionality of Hvigor. */
}

View File

@ -0,0 +1,23 @@
# Define project specific obfuscation rules here.
# You can include the obfuscation configuration files in the current module's build-profile.json5.
#
# For more details, see
# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5
# Obfuscation options:
# -disable-obfuscation: disable all obfuscations
# -enable-property-obfuscation: obfuscate the property names
# -enable-toplevel-obfuscation: obfuscate the names in the global scope
# -compact: remove unnecessary blank spaces and all line feeds
# -remove-log: remove all console.* statements
# -print-namecache: print the name cache that contains the mapping from the old names to new names
# -apply-namecache: reuse the given cache file
# Keep options:
# -keep-property-name: specifies property names that you want to keep
# -keep-global-name: specifies names that you want to keep in the global scope
-enable-property-obfuscation
-enable-toplevel-obfuscation
-enable-filename-obfuscation
-enable-export-obfuscation

View File

@ -0,0 +1,10 @@
{
"name": "entry",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {}
}

View File

@ -0,0 +1,835 @@
interface ChessPiece {
Cstep: number;
Cstate: number;
}
interface Regret_xy {
x: number;
y: number;
}
const TAG='funcitons'
export class Chess {
length: number // 棋盘的长
breadth: number // 棋盘的宽
ChessData: ChessPiece[][]//存储棋子数据
Currently_step: number // 棋子当前进行的步数
Chessimg: number[][]
regretFlAG: Regret_xy[]//悔棋的标签
Relabel: number[][] // 复盘的标签
ifReview: number//是否复盘
Score: number[][] // 计分数组
Max_Score: number//最大分数
Counts: number
New_count: number
Ovalone: number // 代表棋子
WinFLAG: number // 判断是否取得胜利
Gameover: number // 判断游戏是否结束
Depth: number // 搜索的深度
player: number // 轮到下棋的标志1=下0=不下
player2: number // 轮到下棋的标志1=下0=不下
computer: number // 轮到下棋的标志1=下0=不下
Current_Player:boolean//表示当前落子方
myColor: number // 玩家选择的棋子颜色
player2color: number//玩家2的棋子颜色
computercolor: number // 电脑的棋子颜色
Chess_Mode: number // 对弈方式 (=0人-人;=1人【黑】-机【白】;=2人【白】-机【黑】)
ifRandomChess:boolean//表示是否为随机棋局
ifRefresh:boolean//判断是否重新开始
ifSuggest:boolean//判断是否使用了落子建议
SuggestChessData: ChessPiece[][]//建议落子位置存储(多次点击落子建议时)
message:string | undefined//提示信息
ifSaveChess:boolean//是否保存了棋局
ifResetChess:boolean;//是否还原棋局
writenLen:number;//写入棋局步数数据的长度
chessDataState: number[][] = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
] // 提前写好的棋盘
chessDataStep: number[][] = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 19, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 22, 5, 21, 17, 23, 10, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 12, 1, 11, 9, 13, 14, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 2, 8, 3, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 18, 4, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
] // 提前写好的棋盘的棋子对应的步数
constructor(len: number, bre: number) {
this.length = len
this.breadth = bre
this.Currently_step = 0
this.ifReview = 0
this.Max_Score = 0
this.Counts = 0
this.New_count = 0
this.Ovalone = 0
this.WinFLAG = 0
this.Gameover = 0
this.Depth = 0
this.player = 0
this.player2 = 0
this.computer = 0
this.Current_Player = false
this.myColor = 0
this.player2color = 0
this.computercolor = 0
this.Chess_Mode = 3
this.writenLen = 0
this.ifRandomChess=false
this.ifRefresh=false
this.ifSuggest=false
this.message=''
this.ifSaveChess=false
this.ifResetChess=false
// 初始化ChessData
this.ChessData = new Array(len)
this.Chessimg = new Array(len)
this.regretFlAG = new Array(this.length * this.breadth)
this.SuggestChessData=new Array(len)
for (let i = 0; i < this.length * this.breadth; i++) {
this.regretFlAG[i] = {x: 0, y: 0}
}
this.Relabel = new Array(len)
this.Score = new Array(len)
for (let i = 0; i < len; i++) {
this.ChessData[i] = new Array(bre)
this.Chessimg[i] = new Array(bre)
this.Relabel[i] = new Array(bre)
this.Score[i] = new Array(bre)
this.SuggestChessData[i]=new Array(bre)
for (let j = 0; j < bre; j++) {
this.ChessData[i][j] = { Cstate: 0, Cstep: 0 } // Cstate=0无棋子1有黑棋子2有白棋子Cstep为该棋子落子的步骤
this.Chessimg[i][j] = 0
this.Relabel[i][j] = 0
this.Score[i][j] = 0
this.SuggestChessData[i][j] = { Cstate: 0, Cstep: 0 }
}
}
}
//估值函數
count_Score(x: number, y: number, computerColor: number) {
let sum = 0
let value = [0, 0, 0, 0]
let upcount = [0, 0, 0, 0]// 左、上、左斜上、左斜下 棋子数
let downcount = [0, 0, 0, 0]// 右、下、右斜向下、右斜向上 棋子数
let upflag = [0, 0, 0, 0]// 左、上、左斜上、左斜下 空格数
let downflag = [0, 0, 0, 0]// 右、下、右斜向下、右斜向上 空格数
for (let color = 1; color < 3; color++) {
this.ChessData[x][y].Cstate = color
for (let i = x - 1; i > -1; i--) {// 计算左边棋子数量
if (this.ChessData[i][y].Cstate == color) {//左侧有自己的棋子
upcount[0] += 1
} else if (this.ChessData[i][y].Cstate != 0 && this.ChessData[i][y].Cstate != color) {//左侧有对方的棋子
upflag[0] = -1
break
} else if (this.ChessData[i][y].Cstate == 0) {//左侧没有棋子
upflag[0] = 1
if (i - 1 >= 0 && this.ChessData[i - 1][y].Cstate == 0) {
upflag[0] = 2 //表示有两个空格
} else {
break
}
if (i - 2 >= 0 && this.ChessData[i - 2][y].Cstate == 0) {
upflag[0] = 3 //表示有三个空格
} else {
break
}
break
}
}
for (let i = x + 1; i < this.breadth; i++) {// 计算右边棋子数量
if (this.ChessData[i][y].Cstate == color) {//右侧有自己棋子
downcount[0] += 1
} else if (this.ChessData[i][y].Cstate != 0 && this.ChessData[i][y].Cstate != color) {// 右侧有对方棋子
downflag[0] = -1
break
} else if (this.ChessData[i][y].Cstate == 0) {//右侧没有棋子
downflag[0] = 1
if (i + 1 < this.breadth && this.ChessData[i + 1][y].Cstate == 0) {
downflag[0] = 2//表示有两个空格
} else {
break
}
if (i + 2 < this.breadth && this.ChessData[i + 2][y].Cstate == 0) {
downflag[0] = 3//表示有三个空格
} else {
break
}
break
}
}
for (let i = y - 1; i > -1; i--) {//计算方向向上
if (this.ChessData[x][i].Cstate == color) {//上有自己棋子
upcount[1] += 1
} else if (this.ChessData[x][i].Cstate != 0 && this.ChessData[x][i].Cstate != color) {//上有对方棋子
upflag[1] = -1
break
} else if (this.ChessData[x][i].Cstate == 0) {//上没有棋子
upflag[1] = 1
if (i - 1 >= 0 && this.ChessData[x][i - 1].Cstate == 0) {
upflag[1] = 2//表示上有两个空格
} else {
break
}
if (i - 2 >= 0 && this.ChessData[x][i - 2].Cstate == 0) {
upflag[1] = 3//表示上有三个空格
} else {
break
}
break
}
}
for (let i = y + 1; i < this.length; i++) {//计算方向向下
if (this.ChessData[x][i].Cstate == color) {//下有自己棋子
downcount[1] += 1
} else if (this.ChessData[x][i].Cstate != 0 && this.ChessData[x][i].Cstate != color) {//下有对方棋子
downflag[1] = -1
break
} else if (this.ChessData[x][i].Cstate == 0) {//下没有棋子
downflag[1] = 1
if (i + 1 < this.length && this.ChessData[x][i + 1].Cstate == 0) {
downflag[1] = 2//表示下有两个空格
} else {
break
}
if (i + 2 < this.length && this.ChessData[x][i + 2].Cstate == 0) {
downflag[1] = 3//表示下有三个空格
} else {
break
}
break
}
}
let j = 1
for (let i = x - 1; i > -1; i--) {//计算左斜向上
if (y - j >= 0 && this.ChessData[i][y - j].Cstate == color) {//左斜向上有自己棋子
upcount[2] += 1
j += 1
} else if (y - j >= 0 && this.ChessData[i][y - j].Cstate != 0 && this.ChessData[i][y - j].Cstate != color) {//左斜向上有对方棋子
upflag[2] = -1
j += 1
break
} else if (y - j >= 0 && this.ChessData[i][y - j].Cstate == 0) {//左斜向上没有棋子
upflag[2] = 1
if (i - 1 >= 0 && y - j - 1 >= 0 && this.ChessData[i - 1][y - j - 1].Cstate == 0) {
upflag[2] = 2 //表示有两个空格
j += 1
} else {
j += 1
break
}
if (i - 2 >= 0 && y - j - 2 >= 0 && this.ChessData[i - 2][y - j - 2].Cstate == 0) {
upflag[2] = 3 //表示有三个空格
j += 1
} else {
j += 1
break
}
j += 1
break
}
}
j = 1
for (let i = x + 1; i < this.breadth; i++) {//计算右斜向下
if (y + j < this.length && this.ChessData[i][y + j].Cstate == color) {//右斜向下有自己棋子
downcount[2] += 1
j += 1
} else if (y + j < this.length && this.ChessData[i][y + j].Cstate != 0 && this.ChessData[i][y + j].Cstate != color) {//右斜向下有对方棋子
downflag[2] = -1
j += 1
break
} else if (y + j < this.length && this.ChessData[i][y + j].Cstate == 0) {//右斜向下没有棋子
downflag[2] = 1
if (i + 1 < this.breadth && y + j + 1 < this.length && this.ChessData[i + 1][y + j + 1].Cstate == 0) {
downflag[2] = 2//表示有两个空格
j += 1
} else {
j += 1
break
}
if (i + 2 < this.breadth && y + j + 2 < this.length && this.ChessData[i + 2][y + j + 2].Cstate == 0) {
downflag[2] = 3//表示有三个空格
j += 1
} else {
j += 1
break
}
j += 1
break
}
}
j = 1
for (let i = x + 1; i < this.breadth; i++) {//计算右斜向上
if (y - j >= 0 && this.ChessData[i][y - j].Cstate == color) {//右斜向上有自己棋子
downcount[3] += 1
j += 1
} else if (y - j >= 0 && this.ChessData[i][y - j].Cstate != 0 && this.ChessData[i][y - j].Cstate != color) {//右斜向上有对方棋子
downflag[3] = -1
j += 1
break
} else if (y - j >= 0 && this.ChessData[i][y - j].Cstate == 0) {//右斜向上没有棋子
downflag[3] = 1
if (i + 1 < this.breadth && y - j - 1 >= 0 && this.ChessData[i + 1][y - j - 1].Cstate == 0) {
downflag[3] = 2//表示有两个空格
j += 1
} else {
j += 1
break
}
if (i + 2 < this.breadth && y - j - 2 >= 0 && this.ChessData[i + 2][y - j - 2].Cstate == 0) {
downflag[3] = 3//表示有三个空格
j += 1
} else {
j += 1
break
}
j += 1
break
}
}
j = 1
for (let i = x - 1; i > -1; i--) {//计算左斜向下
if (y + j < this.length && this.ChessData[i][y + j].Cstate == color) {//左斜向下有自己棋子
upcount[3] += 1
j += 1
} else if (y + j < this.length && this.ChessData[i][y + j].Cstate != 0 && this.ChessData[i][y + j].Cstate != color) {//左斜向下有对方棋子
upflag[3] = -1
j += 1
break
} else if (y + j < this.length && this.ChessData[i][y + j].Cstate == 0) {//左斜向下没有棋子
upflag[3] = 1
if (i - 1 >= 0 && y + j + 1 < this.length && this.ChessData[i - 1][y + j + 1].Cstate == 0) {
upflag[3] = 2//表示有两个空格
j += 1
} else {
j += 1
break
}
if (i - 2 >= 0 && y + j + 2 < this.length && this.ChessData[i - 2][y + j + 2].Cstate == 0) {
upflag[3] = 3//表示有三个空格
j += 1
} else {
j += 1
break
}
j += 1
break
}
}
//评分计算,如果是电脑方的话分数要高一点
if (computerColor == this.ChessData[x][y].Cstate) {//人机对战
for (let i = 0; i < 4; i++) {
let count = upcount[i] + downcount[i] + 1
if (count == 5) {//成五
value[i] = 40000
} else if (count == 4) {
if (upflag[i] >= 1 && downflag[i] >= 1) {//活四
value[i] = 19000
}
if ((upflag[i] >= 1 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 1)) {//眠四
value[i] = 3000
}
if (upflag[i] == -1 && downflag[i] == -1) {//死四
value[i] = -50
}
} else if (count == 3) {//活三
if ((upflag[i] >= 2 && downflag[i] >= 1) || (upflag[i] >= 1 && downflag[i] >= 2)) {//活三
value[i] = 4000
}
if ((upflag[i] >= 2 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 2) || (upflag[i] == 1 && downflag[i] == 1)) {//眠三
value[i] = 800
}
if (upflag[i] == -1 && downflag[i] == -1) {//死三
value[i] = -50
}
} else if (count == 2) {
if ((upflag[i] >= 3 && downflag[i] >= 1) || (upflag[i] >= 1 && downflag[i] >= 3) || (upflag[i] >= 2 && downflag[i] >= 2)) {//活二
value[i] = 1050
}
if ((upflag[i] >= 3 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 3) || (upflag[i] == 2 && downflag[i] == 1) || (upflag[i] == 1 && downflag[i] == 2)) {//眠二
value[i] = 350
}
if ((upflag[i] == -1 && downflag[i] == -1) || (upflag[i] == 1 && downflag[i] == 1) || (upflag[i] == -1 && downflag[i] == 2) || (upflag[i] == 2 && downflag[i] == -1)) {//死二
value[i] = -50
}
} else {
if ((upflag[i] >= 3 && downflag[i] >= 2) || (upflag[i] >= 2 && downflag[i] >= 3)) {//活一
value[i] = 80
}
if ((upflag[i] == 2 && downflag[i] == 2) || (upflag[i] == 1 && downflag[i] == 3) || (upflag[i] == 3 && downflag[i] == 1) || (upflag[i] == 1 && downflag[i] == 2)) {//眠一
value[i] = 20
}
if ((upflag[i] <= 1 && downflag[i] <= 2) || (upflag[i] <= 2 && downflag[i] <= 1) || (upflag[i] <= 3 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] <= 3)) {//死一
value[i] = -50
}
}
}
} else {//双人对战
for (let i = 0; i < 4; i++) {
let count = upcount[i] + downcount[i] + 1
if (count == 5) {//成五
value[i] = 30000
} else if (count == 4) {
if (upflag[i] >= 1 && downflag[i] >= 1) {//活四
value[i] = 15000
}
if ((upflag[i] >= 1 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 1)) {//眠四
value[i] = 2500
}
if (upflag[i] == -1 && downflag[i] == -1) {//死四
value[i] = -50
}
} else if (count == 3) {
if ((upflag[i] >= 2 && downflag[i] >= 1) || (upflag[i] >= 1 && downflag[i] >= 2)) {//活三
value[i] = 3000
}
if ((upflag[i] >= 2 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 2) || (upflag[i] == 1 && downflag[i] == 1)) {//眠三
value[i] = 500
}
if (upflag[i] == -1 && downflag[i] == -1) {//死三
value[i] = -50
}
} else if (count == 2) {
if ((upflag[i] >= 3 && downflag[i] >= 1) || (upflag[i] >= 1 && downflag[i] >= 3) || (upflag[i] >= 2 && downflag[i] >= 2)) {//活二
value[i] = 650
}
if ((upflag[i] >= 3 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 3) || (upflag[i] == 2 && downflag[i] == 1) || (upflag[i] == 1 && downflag[i] == 2)) {//眠二
value[i] = 150
}
if ((upflag[i] == -1 && downflag[i] == -1) || (upflag[i] == 1 && downflag[i] == 1) || (upflag[i] == -1 && downflag[i] == 2) || (upflag[i] == 2 && downflag[i] == -1)) {//死二
value[i] = -50
}
} else {
if ((upflag[i] >= 3 && downflag[i] >= 2) || (upflag[i] >= 2 && downflag[i] >= 3)) {//活一
value[i] = 50
}
if ((upflag[i] == 2 && downflag[i] == 2) || (upflag[i] == 1 && downflag[i] == 3) || (upflag[i] == 3 && downflag[i] == 1) || (upflag[i] == 1 && downflag[i] == 2)) {//眠一
value[i] = 10
}
if ((upflag[i] <= 1 && downflag[i] <= 2) || (upflag[i] <= 2 && downflag[i] <= 1) || (upflag[i] <= 3 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] <= 3)) {//死一
value[i] = -50
}
}
}
}
for (let i = 0; i < 4; i++) {
sum += value[i]
value[i] = 0
upcount[i] = 0
downcount[i] = 0
upflag[i] = 0
downflag[i] = 0
}
}
this.ChessData[x][y].Cstate = 0
return sum
}
// 定义一个函数实现AI策略可以根据分数随机选择一个分数最大的位置返回
return_chess(Depth: number, computercolor: number) {
this.New_count = 0
this.Max_Score = 0
this.Counts = 0
let pos: number[][] = new Array(10)
for (let i = 0; i < 10; i++) {
pos[i] = new Array(2)
for (let j = 0; j < 2; j++) {
pos[i][j] = 0
}
}
let evaFLAG = 0
let position = [0, 0] //初始化位置坐标数组
for (let i = 8 - Depth; i < 11 + Depth; i++) {
for (let j = 8 - Depth; j < 11 + Depth; j++) {
if (0 <= i && i < this.breadth && 0 <= j && j < this.length) {
if (this.ChessData[i][j].Cstate == 0) {
this.Score[i][j] = this.count_Score(i, j, computercolor)
this.New_count += 1
if (this.Score[i][j] > 0) {
this.Counts += 1
}
if (this.Max_Score < this.Score[i][j]) {
this.Max_Score = this.Score[i][j]
}
}
}
}
}
for (let i = 8 - Depth; i < 11 + Depth; i++) {
for (let j = 8 - Depth; j < 11 + Depth; j++) {
if (0<= i && i < this.breadth && 0 <= j && j < this.length && this.Score[i][j] == this.Max_Score && this.ChessData[i][j].Cstate == 0) {
pos[evaFLAG][0] = i
pos[evaFLAG][1] = j
evaFLAG += 1
}
}
}
let m = Math.floor(Math.random()*evaFLAG) // 生成0-evaFLAG的随机整数
position[0] = pos[m][0]
position[1] = pos[m][1]
return position
}
// 定义一个落子函数,如果用户点击位置合理,则落子
make_move(pointi: number, pointj: number) {
if (this.ChessData[pointi][pointj].Cstate == 0 && this.myColor == 1 && this.Currently_step % 2 == 0) {//人黑机白
this.ChessData[pointi][pointj].Cstate = 1
this.regretFlAG[this.Currently_step].x = pointi
this.regretFlAG[this.Currently_step].y = pointj
this.player = 0
this.player2 = 1
this.computer = 1
this.Currently_step+=1
console.info(TAG,'make_move的Currently_step+=1执行了Currently_step= '+this.Currently_step)
this.ChessData[pointi][pointj].Cstep = this.Currently_step
let a = Math.max(Math.abs(pointi - Math.floor((this.breadth - 1) / 2)), Math.abs(pointj - Math.floor((this.length - 1) / 2)))
this.Depth = Math.max(this.Depth, a)
return true
} else if (this.ChessData[pointi][pointj].Cstate == 0 && this.myColor == 2 && this.Currently_step % 2 != 0) {//人白机黑
this.ChessData[pointi][pointj].Cstate = 2
this.regretFlAG[this.Currently_step].x = pointi
this.regretFlAG[this.Currently_step].y = pointj
this.player = 0
this.computer = 1
this.Currently_step += 1
this.ChessData[pointi][pointj].Cstep = this.Currently_step
let a = Math.max(Math.abs(pointi - Math.floor((this.breadth - 1) / 2)), Math.abs(pointj - Math.floor((this.length - 1) / 2)))
this.Depth = Math.max(this.Depth, a)
return true
} else if (this.ChessData[pointi][pointj].Cstate == 0 && this.player2color == 2 && this.Currently_step % 2 != 0) {//双人对战
this.ChessData[pointi][pointj].Cstate = 2
this.regretFlAG[this.Currently_step].x = pointi
this.regretFlAG[this.Currently_step].y = pointj
this.player = 1
this.player2 = 0
this.Currently_step += 1
this.ChessData[pointi][pointj].Cstep = this.Currently_step
let a = Math.max(Math.abs(pointi - Math.floor((this.breadth - 1) / 2)), Math.abs(pointj - Math.floor((this.length - 1) / 2)))
this.Depth = Math.max(this.Depth, a)
return true
} else {
return false
}
}
// 定义一个评估某一方向上的棋型函数,辅助进行下一步的落子决策
get_direction_Score(y:number,x:number,y_offset:number,x_offset:number){
let nowcount = 0 //落子处我方连续子数
let _nowcount = 0 //落子处对方连续子数
let space:boolean|null = null // 我方连续子中有无空格
let _space:boolean|null = null // 对方连续子中有无空格
let both = 0 // 我方连续子两端有无阻挡
let _both = 0 // 对方连续子两端有无阻挡
let tmp_x = x
let tmp_y = y
if(0<=(y+y_offset)&&(y+y_offset)<=18&&0<=(x+x_offset)&&(x+x_offset)<=18){
let Cflag=this.ChessData[y+y_offset][x+x_offset].Cstate
if(Cflag!=0){
for(let i=1;i<6;i++){
x+=x_offset
y+=y_offset
if(0<=y&&y<18&&0<=x&&x<18){
if(Cflag==2){
if(this.ChessData[y][x].Cstate==2&&this.myColor==2){
nowcount+=1
if (space==false){
space=true
}
}else if(this.ChessData[y][x].Cstate==1&&this.myColor==2){
_both+=1
break
}else {
if (space==null){
space=false
}else{
break//遇到第二个空格退出
}
}
}
else if(Cflag==1){
if(this.ChessData[y][x].Cstate==2&&this.myColor==1){
_both+=1
break
}else if(this.ChessData[y][x].Cstate==1&&this.myColor==1){
_nowcount+=1
if (_space==false){
_space=true
}
}else{
if (_space==null){
_space=false
}else{
break
}
}
}
}else{
//遇到边也就是阻挡
if(Cflag==2){
both+=1
}else if(Cflag==1){
_both+=1
}
}
}
}
if (space==false){
space=null
}
if (_space==false){
_space=null
}
}
x=tmp_x
y=tmp_y
if(0<=(y-y_offset)&&(y-y_offset)<=18&&0<=(x-x_offset)&&(x-x_offset)<=18){
let _flag=this.ChessData[y-y_offset][x-x_offset].Cstate
if (_flag!=0){
for(let i=1;i<6;i++){
x-=x_offset
y-=y_offset
if(0<=y&&y<18&&0<=x&&x<18){
if (_flag==2){
if(this.ChessData[y][x].Cstate==2&&this.myColor==2){
nowcount+=1
if (space==false){
space=true
}
}else if(this.ChessData[y][x].Cstate==1&&this.myColor==2){
_both+=1
break
}else {
if (space==null){
space=false
}else{
break//遇到第二个空格退出
}
}
}
else if(_flag==1){
if(this.ChessData[y][x].Cstate==2&&this.myColor==1){
_both+=1
break
}else if(this.ChessData[y][x].Cstate==1&&this.myColor==1){
_nowcount+=1
if (_space==false){
_space=true
}
}else{
if (_space==null){
_space=false
}else{
break
}
}
}
}
else {
//遇到边也就是阻挡
if(_flag==1){
both+=1
}else if(_flag==2){
_both+=1
}
}
}
}
}
let Score=0
if (nowcount==4){
Score=10000
}else if(_nowcount==4){
Score=9000
}else if(nowcount==3){
if (both==0){
Score=1000
}else if(both==1){
Score=100
}else {
Score=0
}
}else if(_nowcount==3){
if (_both==0){
Score=900
}else if(_both==1){
Score=90
}else {
Score=0
}
}else if(nowcount==2){
if(both==0){
Score=100
}else if(both==1){
Score=10
}else Score=0
}else if(_nowcount==2){
if (_both==0){
Score=90
}else if(_both==1){
Score=9
}else {
Score=0
}
}else if(nowcount==1){
Score=10
}else if(_nowcount==1){
Score=9
}else {
Score=0
}
if (space||_space){
Score/=2
}
return Score
}
// 定义一个函数获取当前点对各个方向上的评估结果
get_point_Score(y:number,x:number){
let Score=0
// let offset=[[1,0],[0,1],[1,1],[1,-1]]
Score+=this.get_direction_Score(y,x,1,0)
Score+=this.get_direction_Score(y,x,0,1)
Score+=this.get_direction_Score(y,x,1,1)
Score+=this.get_direction_Score(y,x,1,-1)
return Score
}
// 定义一个函数实现简单的自动落子策略,根据评估函数计算每个位置的得分,并选择得分最高的位置作为下一步的落子位置
autoactive(){
let _Score=0
let position=[-1,-1]
for(let i=0;i<18;i++){
for(let j=0;j<18;j++){
if (this.ChessData[i][j].Cstate==0){
let Score=this.get_point_Score(i,j)
if(Score>_Score){
position[0]=i
position[1]=j
}else if (Score==_Score){
let r=Math.floor(Math.random()*101)
if (r%2==0){
position[0]=i
position[1]=j
}
}
}
}
}
return position
}
//胜负判定函数
ChessCheck(): number {
//垂直方向判定
for (let x = 0; x < this.breadth - 4; x++) {
for (let y = 0; y < this.length; y++) {
if (this.ChessData[x][y].Cstate == 1 && this.ChessData[x + 1][y].Cstate == 1 && this.ChessData[x + 2][y].Cstate == 1 && this.ChessData[x + 3][y].Cstate == 1 && this.ChessData[x + 4][y].Cstate == 1) {
this.Gameover = 1
return 1
}
if (this.ChessData[x][y].Cstate == 2 && this.ChessData[x + 1][y].Cstate == 2 && this.ChessData[x + 2][y].Cstate == 2 && this.ChessData[x + 3][y].Cstate == 2 && this.ChessData[x + 4][y].Cstate == 2) {
this.Gameover = 1
return 2
}
}
}
//水平方向判定
for (let x = 0; x < this.breadth; x++) {
for (let y = 0; y < this.length - 4; y++) {
if (this.ChessData[x][y].Cstate == 1 && this.ChessData[x][y + 1].Cstate == 1 && this.ChessData[x][y + 2].Cstate == 1 && this.ChessData[x][y + 3].Cstate == 1 && this.ChessData[x][y + 4].Cstate == 1) {
this.Gameover = 1
return 1
}
if (this.ChessData[x][y].Cstate == 2 && this.ChessData[x][y + 1].Cstate == 2 && this.ChessData[x][y + 2].Cstate == 2 && this.ChessData[x][y + 3].Cstate == 2 && this.ChessData[x][y + 4].Cstate == 2) {
this.Gameover = 1
return 2
}
}
}
//斜向右判定
for (let x = 0; x < this.breadth - 4; x++) {
for (let y = 4; y < this.length; y++) {
if (this.ChessData[x][y].Cstate == 1 && this.ChessData[x + 1][y - 1].Cstate == 1 && this.ChessData[x + 2][y - 2].Cstate == 1 && this.ChessData[x + 3][y - 3].Cstate == 1 && this.ChessData[x + 4][y - 4].Cstate == 1) {
this.Gameover = 1
return 1
}
if (this.ChessData[x][y].Cstate == 2 && this.ChessData[x + 1][y - 1].Cstate == 2 && this.ChessData[x + 2][y - 2].Cstate == 2 && this.ChessData[x + 3][y - 3].Cstate == 2 && this.ChessData[x + 4][y - 4].Cstate == 2) {
this.Gameover = 1
return 2
}
}
}
//斜向左判定
for (let x = 0; x < this.breadth - 4; x++) {
for (let y = 0; y < this.length - 4; y++) {
if (this.ChessData[x][y].Cstate == 1 && this.ChessData[x + 1][y + 1].Cstate == 1 && this.ChessData[x + 2][y + 2].Cstate == 1 && this.ChessData[x + 3][y + 3].Cstate == 1 && this.ChessData[x + 4][y + 4].Cstate == 1) {
this.Gameover = 1
return 1
}
if (this.ChessData[x][y].Cstate == 2 && this.ChessData[x + 1][y + 1].Cstate == 2 && this.ChessData[x + 2][y + 2].Cstate == 2 && this.ChessData[x + 3][y + 3].Cstate == 2 && this.ChessData[x + 4][y + 4].Cstate == 2) {
this.Gameover= 1
return 2
}
}
}
let avanum = 0
for (let x = 0; x < this.breadth; x++) {
for (let y = 0; y < this.length; y++) {
if (this.ChessData[x][y].Cstate == 0) {
break
} else if (this.ChessData[x][y].Cstate != 0) {
avanum += 1
}
}
}
//平局
if (avanum == this.length * this.breadth) {
return 3
}
return 0
}
}

View File

@ -0,0 +1,26 @@
//公共常量
export const boardNum: number = 19; //行列数
export const gridSize: number = 17; //格子大小
//8个点
export const startPoints = [[4, 4], [4, 9], [4, 14], [9, 4], [9, 14], [14, 4], [14, 9], [14, 14]];
//中心点
export const centerPoint = [9, 9];
//绘制棋盘背景
export const settings: RenderingContextSettings = new RenderingContextSettings(true);
export const context: CanvasRenderingContext2D = new CanvasRenderingContext2D(settings);
//绘制棋子
export const settings2: RenderingContextSettings = new RenderingContextSettings(true);
export const context2: CanvasRenderingContext2D = new CanvasRenderingContext2D(settings2);
//绘制复盘棋子
export const settings3: RenderingContextSettings = new RenderingContextSettings(true);
export const context3: CanvasRenderingContext2D = new CanvasRenderingContext2D(settings3);
export const img1:ImageBitmap = new ImageBitmap("common/pictures/black.png");//黑棋
export const img2:ImageBitmap = new ImageBitmap("common/pictures/white.png");//白棋
export const img3:ImageBitmap = new ImageBitmap("common/pictures/sug_black.png");//建议黑棋
export const img4:ImageBitmap = new ImageBitmap("common/pictures/sug_white.png");//建议白棋

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

View File

@ -0,0 +1,116 @@
//棋盘
import {Chess} from '../classes/Chess';
import {drawChessboard,drawPoints,drawChess,drawReviewChess, deleteChess,playgame_white, playgame_black,doublePeopleGame} from '../utils/functions'
import * as CommonConstant from '../common/constants/CommonConstants'
@Component
export struct ChessBoard {
@Link myChess:Chess
Chessimgx: number = 0 //玩家落子的横坐标
Chessimgy: number = 0 //玩家落子的纵坐标
private playX: number=0; //下棋位置的横坐标
private playY: number=0; //下棋位置的纵坐标
build() {
Column() {
Stack() {
//绘制棋盘
Canvas(CommonConstant.context)
// .width('100%')
// .height('100%')
// .margin({ left: 10, top: 5 })
.width(340)
.height(340)
.onReady(() => {
//绘制五子棋棋盘
drawChessboard();
//绘制特殊点
drawPoints();
})
//实时绘制棋局
Canvas(CommonConstant.context2)
// .width('100%')
// .height('100%')
// .margin({ left: 10, top: 5 })
.width(340)
.height(340)
.onTouch((event)=> {
//判断对局是否结束
if (this.myChess.WinFLAG == 0) {
//计算落子的横纵坐标
this.playX = event.touches[0].x - CommonConstant.gridSize / 2
this.playY = event.touches[0].y - CommonConstant.gridSize / 2
//计算行列数 横纵坐标/格子大小
let row: number = Math.round(this.playX / CommonConstant.gridSize)
let col: number = Math.round(this.playY / CommonConstant.gridSize)
if (row >= 0 && row <= 19 && col >= 0 && col <= 19) {
//是否开启了落子建议
if (this.myChess.ifSuggest) {
for (let x = 0; x < this.myChess.breadth; x++) {
for (let y = 0; y < this.myChess.length; y++) {
//存储了建议落子位置数据的棋子删除
if (this.myChess.SuggestChessData[x][y].Cstate != 0) {
deleteChess(x, y)
//删除后需要清空数据以便后续建议
this.myChess.SuggestChessData[x][y].Cstate = 0
}
}
}
this.myChess.ifSuggest = false
}
//判断落子位置是否合法
if (this.myChess.ChessData[row][col].Cstate == 0) {
drawChess(row, col, this.myChess)
this.myChess.make_move(row, col)
if (this.myChess.Chess_Mode == 0) {
doublePeopleGame(this.myChess)
} else if (this.myChess.Chess_Mode == 1) {
playgame_black(this.myChess)
} else if (this.myChess.Chess_Mode == 2) {
playgame_white(this.myChess)
}
}
}
}
})
//判断是否复盘
if (this.myChess.ifReview == 1) {
//复盘
Canvas(CommonConstant.context3)
// .width('100%')
// .height('100%')
// .margin({ left: 10, top: 5 })
.width(340)
.height(340)
.onReady(()=>{
for (let x = 0; x < this.myChess.breadth; x++) {
for (let y = 0; y < this.myChess.length; y++) {
drawReviewChess(x,y,this.myChess)
}
}
})
}
}
// .width('100%')
// .height('100%')
.width(350)
.height(350)
.backgroundImage($r('app.media.blankChessBoard'))
.backgroundImageSize({height:350,width:350})//开发板
// .backgroundImageSize({height:1050,width:1000})//模拟器
//.backgroundImageSize({width:CommonConstant.gridSize*20,height:CommonConstant.gridSize*21})
// .backgroundImagePosition(Alignment.Center)
}
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
.height('100%')
.width('100%')
}
}

View File

@ -0,0 +1,107 @@
//游戏弹窗
import { Chess } from '../classes/Chess';
import { doublepeople, peocomblack, peocomwhite, resetLast, saveCurrent,playgame_black,playgame_white} from '../utils/functions';
@CustomDialog
export default struct GameDialog {
controller: CustomDialogController
@State ifSetMode:boolean=false
@Link myChess:Chess
build(){
Column(){
if(this.ifSetMode){
Column({space:10}){
Text('人机模式(机白)')
.fontColor(Color.White)
.fontWeight(500)
.fontSize(18)
.onClick(()=>{
this.controller.close()
setTimeout(()=>{
this.myChess.Current_Player=true
this.myChess.ifRandomChess=false
peocomblack(this.myChess)
this.ifSetMode=false
},200)
})
Text('人机模式(机黑)')
.fontColor(Color.White)
.fontWeight(500)
.fontSize(18)
.onClick(()=>{
this.controller.close()
setTimeout(()=>{
this.myChess.Current_Player=false
this.myChess.ifRandomChess=false
peocomwhite(this.myChess)
this.ifSetMode=false
},200)
})
Text('双人模式')
.fontColor(Color.White)
.fontWeight(500)
.fontSize(18)
.onClick(()=>{
this.controller.close()
setTimeout(()=>{
this.myChess.Current_Player=true
this.myChess.ifRandomChess=false
doublepeople(this.myChess)
this.ifSetMode=false
},200)
})
}
}else{
Column({space:10}){
Text('开始游戏')
.fontColor(Color.White)
.fontWeight(FontWeight.Normal)
.fontSize(18)
.onClick(()=>{
this.ifSetMode=true
})
Text('保存当前局面')
.fontColor(Color.White)
.fontWeight(FontWeight.Normal)
.fontSize(18)
.onClick(()=>{
saveCurrent(this.myChess)
this.controller.close()
})
Text('重装以前局面')
.fontColor(Color.White)
.fontWeight(FontWeight.Normal)
.fontSize(18)
.onClick(()=>{
resetLast(this.myChess)
this.controller.close()
})
// Text('随机棋局')
// .fontColor(Color.White)
// .fontWeight(FontWeight.Normal)
// .fontSize(18)
// .onClick(()=>{
// this.myChess.ifRandomChess=true
// this.controller.close()
// })
//
// Text('测试模式')
// .fontColor(Color.White)
// .fontWeight(FontWeight.Normal)
// .fontSize(18)
}
}
}
.backgroundColor(Color.Brown)
.justifyContent(FlexAlign.Center)
.width(500)
.height(150)
}
}

View File

@ -0,0 +1,57 @@
//菜单栏
import GameDialog from './GameDialog'
import { Chess } from '../classes/Chess';
@Component
export struct MenuBar {
@Link myChess:Chess
//弹窗控制器
dialogController: CustomDialogController = new CustomDialogController({
builder: GameDialog({
myChess:$myChess
})
})
build() {
//游戏和帮助按钮
Column({space: 50}) {
Text('游戏')
.width(75)
.height(35)
.borderRadius(20)
.textAlign(TextAlign.Center)
.backgroundColor(Color.Brown)
.fontColor(Color.White)
.fontWeight(600)
.onClick(() => {
this.dialogController.open()
})
Text('帮助')
.width(75)
.height(35)
.borderRadius(20)
.textAlign(TextAlign.Center)
.backgroundColor(Color.Brown)
.fontColor(Color.White)
.fontWeight(600)
.onClick((event: ClickEvent) => {
AlertDialog.show({
title: '游戏规则',
message: '1、对局双方各执一色棋子。\n2、空棋盘开局。\n3、黑棋虽先行但有禁手黑方不能在一步之内形成两个“活三”“活四”或一步之内形成“长连”指一步形成超过五子连珠。白方自由无禁手。\n4、棋子下在棋盘的空白点上棋子下定后不得向其它点移动不得从棋盘上拿掉或拿起另落别处。\n5、黑方的第一枚棋子可下在棋盘任意交叉点上。',
//取消
confirm: {
value: '确定',
action: () => {
}
},
cancel: () => {}
})
})
}
.justifyContent(FlexAlign.Center)
.width('100%')
.height('100%')
}
}

View File

@ -0,0 +1,335 @@
//功能按钮
import { Chess } from '../classes/Chess';
import {review,cancelreview , regret,start_game ,refresh,suggestion,curnow} from '../utils/functions'
import MessageDialog from './MessageDialog'
@Component
export struct Message {
@Link myChess:Chess
//弹窗控制器
MessageDialogController: CustomDialogController = new CustomDialogController({
builder: MessageDialog({
myChess:$myChess
})
})
build() {
Row() {
Column({space: 6}) {
//当前落子方
Text('当前落子方')
.fontSize(15)
.fontColor(Color.White)
.margin({left:-85})
//当前落子方框
Row() {
if(this.myChess.Chess_Mode==3){//未设置游戏模式
Blank()
.height(30)
.width(90)
.borderWidth(1)
.borderColor(Color.White)
.margin({left:-12, right:10 })
Blank()
.height(30)
.width(90)
.borderWidth(1)
.borderColor(Color.White)
}else if(this.myChess.Chess_Mode==1){//人黑机白
Row() {
Image($r('app.media.black'))
.width(20)
.margin({left:5, right:5})
Text('玩家')
.fontSize(15)
.fontColor(Color.White)
}
.height(30)
.width(90)
.borderWidth(2)
.borderColor(this.myChess.Current_Player?Color.Red:Color.White)
.margin({left:-12, right:10 })
Row() {
Image($r('app.media.white'))
.width(20)
.margin({left:5,right:5})
Text('电脑')
.fontSize(15)
.fontColor(Color.White)
}
.height(30)
.width(90)
.borderWidth(2)
.borderColor(this.myChess.Current_Player?Color.White:Color.Red)
}else if(this.myChess.Chess_Mode==2) {//人白机黑模式
Row() {
Image($r('app.media.white'))
.width(20)
.margin({left:5, right:5})
Text('玩家')
.fontSize(15)
.fontColor(Color.White)
}
.height(30)
.width(90)
.borderWidth(2)
.borderColor(this.myChess.Current_Player?Color.White:Color.Red)
.margin({left:-12, right:10 })
Row() {
Image($r('app.media.black'))
.width(20)
.margin({left:5,right:5})
Text('电脑')
.fontSize(15)
.fontColor(Color.White)
}
.height(30)
.width(90)
.borderWidth(2)
.borderColor(this.myChess.Current_Player?Color.Red:Color.White)
} else if(this.myChess.Chess_Mode==0) {//双人对战模式
Row() {
Image($r('app.media.black'))
.width(20)
.margin({left:5, right:5})
Text('玩家1')
.fontSize(15)
.fontColor(Color.White)
}
.height(30)
.width(90)
.borderWidth(2)
.borderColor(this.myChess.Current_Player?Color.Red:Color.White)
.margin({left:-12, right:10 })
Row() {
Image($r('app.media.white'))
.width(20)
.margin({left:5,right:5})
Text('玩家2')
.fontSize(15)
.fontColor(Color.White)
}
.height(30)
.width(90)
.borderWidth(2)
.borderColor(this.myChess.Current_Player?Color.White:Color.Red)
}
}
//胜负判定
Text('胜负判定')
.fontSize(15)
.margin({left:-85})
.fontColor(Color.White)
//胜负判定框
Row() {
if(this.myChess.Gameover==1){
if(this.myChess.WinFLAG==1){
Image($r('app.media.black'))
.width(20)
.margin({right:10})
if(this.myChess.Chess_Mode==0){
Text('玩家1 胜利')
.fontSize(15)
.fontColor('#b53c59')
}else if(this.myChess.Chess_Mode==1){
Text('玩家 胜利')
.fontSize(15)
.fontColor('#b53c59')
}else if(this.myChess.Chess_Mode==2){
Text('电脑 胜利')
.fontSize(15)
.fontColor('#b53c59')
}
}else if(this.myChess.WinFLAG==2) {
Image($r('app.media.white'))
.width(20)
.margin({ right: 10 })
if(this.myChess.Chess_Mode==0){
Text('玩家2 胜利')
.fontSize(15)
.fontColor('#b53c59')
}else if(this.myChess.Chess_Mode==2){
Text('玩家 胜利')
.fontSize(15)
.fontColor('#b53c59')
}else if(this.myChess.Chess_Mode==1){
Text('电脑 胜利')
.fontSize(15)
.fontColor('#b53c59')
}
}else if(this.myChess.WinFLAG==3){
Text('平局')
.fontSize(15)
.fontColor('#b53c59')
}
}else{//初始化
Text('')
.fontSize(14)
.fontColor('#b53c59')
}
}
.height(60)
.width(200)
.padding({left:40})
.backgroundImage($r("app.media.noticeboard"))
.backgroundImageSize(ImageSize.Cover)
.backgroundImagePosition({x: -10})
//复盘和悔棋
Row() {
Row() {
// 复盘
Image($r('app.media.review'))
.width(81)
.height(42)
}
.height(42)
.width(81)
.margin({ right: 10 })
.onClick(()=>{
review(this.myChess)
if (this.myChess.WinFLAG == 0) {
this.myChess.message='对局结束后才可进行复盘哦!'
this.MessageDialogController.open()
}
})
Row() {
// 悔棋
Image($r('app.media.regret'))
.width(81)
.height(42)
}
.height(42)
.width(81)
.margin({ right: 10 })
.onClick(()=>{
//对局进行并且未分出胜负时落子框才会跳动
if(this.myChess.WinFLAG==0&&this.myChess.Currently_step>0){
this.myChess.Current_Player=!this.myChess.Current_Player
}
//棋盘上有棋子才能悔棋
if(this.myChess.Currently_step>0){
regret(this.myChess)
}
if (this.myChess.WinFLAG == 1||this.myChess.WinFLAG == 2||this.myChess.WinFLAG==3) {
this.myChess.message='对局已经结束了!'
this.MessageDialogController.open()
}
})
}
//撤销复盘和落子建议
Row() {
Row() {
// 撤销复盘
Image($r('app.media.cancel'))
.width(81)
.height(42)
}
.height(42)
.width(81)
.margin({ right: 10 })
.onClick(()=>{
if (this.myChess.ifReview == 0) {
this.myChess.message="请在对局结束并点击复盘后使用!"
this.MessageDialogController.open()
}
cancelreview(this.myChess)
})
Row() {
// 落子建议
Image($r('app.media.suggestion'))
.width(81)
.height(42)
}
.height(42)
.width(81)
.margin({ right: 10 })
.onClick(()=>{
suggestion(this.myChess)
this.myChess.ifSuggest=true
if(this.myChess.WinFLAG!=0){
this.myChess.message="请在对局中使用"
this.MessageDialogController.open()
}
})
}
//局面评估和重新开始
Row() {
Row() {
// 局面评估
Image($r('app.media.prediction'))
.width(81)
.height(42)
}
.height(42)
.width(81)
.margin({ right: 10 })
.onClick(()=>{
this.myChess.message=curnow(this.myChess)
console.log('this.myChess.message='+this.myChess.message)
if(this.myChess.message!=undefined) {
this.MessageDialogController.open()
}
})
//重新开始
Row() {
Image($r('app.media.again'))
.width(81)
.height(42)
}
.height(42)
.width(81)
.margin({ right: 10 })
.onClick(()=>{
refresh(this.myChess)
this.myChess.ifRandomChess=false
})
}
// 开始游戏
Row() {
// 开始游戏
Image($r('app.media.start'))
.width(95)
.height(30)
}
.height(30)
.width(95)
.margin({ right: 10 })
.onClick(() => {
this.myChess.Current_Player=true
this.myChess.ifRandomChess=false
start_game(this.myChess)
})
}
.height('100%')
.width('100%')
.justifyContent(FlexAlign.Center)
}
}
}

View File

@ -0,0 +1,31 @@
//信息提示弹窗
import { Chess } from '../classes/Chess';
@CustomDialog
export default struct MessageDialog {
controller: CustomDialogController
@Link myChess:Chess
build(){
Column({space:5}){
Text(this.myChess.message)
.fontSize(20)
.height(90)
.textAlign(TextAlign.Center)
.fontWeight(FontWeight.Bold)
.fontColor(Color.White)
Text('确定')
.fontSize(20)
.textAlign(TextAlign.Center)
.fontWeight(FontWeight.Bold)
.fontColor(Color.White)
.onClick(()=>{
this.controller.close()
})
}
.backgroundColor(Color.Brown)
.justifyContent(FlexAlign.Center)
.width(500)
.height(150)
}
}

View File

@ -0,0 +1,60 @@
//棋局呈现
import *as CommonConstant from '../common/constants/CommonConstants'
import {drawChessboard,drawPoints} from '../utils/functions'
import {Chess} from '../classes/Chess'
@Entry
@Component
export struct ShowChessBoard {
myChess:Chess=new Chess(19, 19)
build() {
Stack() {
Canvas(CommonConstant.context) //画棋盘
// .width('100%')
// .height('100%')
// .margin({ left: 10, top: 5 })
.width(340)
.height(340)
.onReady(() => {
//五子棋棋盘
drawChessboard();
//特殊点
drawPoints();
})
Canvas(CommonConstant.context2) // 画棋子
// .width('100%')
// .height('100%')
// .margin({ left: 10, top: 5 })
.width(340)
.height(340)
.onReady(() => {
for (let i = 0; i < 19; i++) {
for (let j = 0; j < 19; j++) {
if (this.myChess.chessDataState[i][j] == 1) {
CommonConstant.context2.drawImage(CommonConstant.img1,j*17,i*17,8*2,8*2)
CommonConstant.context2.fillStyle = '#ffffff'
CommonConstant.context2.font = '25px sans-serif'
CommonConstant.context2.fillText(`${this.myChess.chessDataStep[i][j]}`, j*17 + CommonConstant.gridSize/5, i*17 + CommonConstant.gridSize/1.6)
}else if(this.myChess.chessDataState[i][j] == 2){
CommonConstant.context2.drawImage(CommonConstant.img2,j*17,i*17,8*2,8*2)
CommonConstant.context2.fillStyle = '#000000'
CommonConstant.context2.font = '25px sans-serif'
CommonConstant.context2.fillText(`${this.myChess.chessDataStep[i][j]}`, j*17 + CommonConstant.gridSize/5, i*17 + CommonConstant.gridSize/1.6)
}
}
}
})
}
// .height('100%')
// .width('100%')
.width(350)
.height(350)
.backgroundImageSize({height:350})
.backgroundImage($r('app.media.blankChessBoard'))
// .backgroundImageSize(ImageSize.Contain)
}
}

View File

@ -0,0 +1,44 @@
import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
const DOMAIN = 0x0000;
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate');
}
onDestroy(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
return;
}
hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
});
}
onWindowStageDestroy(): void {
// Main window is destroyed, release UI related resources
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
// Ability has brought to foreground
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
// Ability has back to background
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
}
}

View File

@ -0,0 +1,16 @@
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit';
const DOMAIN = 0x0000;
export default class EntryBackupAbility extends BackupExtensionAbility {
async onBackup() {
hilog.info(DOMAIN, 'testTag', 'onBackup ok');
await Promise.resolve();
}
async onRestore(bundleVersion: BundleVersion) {
hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion));
await Promise.resolve();
}
}

View File

@ -0,0 +1,62 @@
import { Chess } from '../classes/Chess';
import { ChessBoard } from '../components/ChessBoard';
import { MenuBar } from '../components/MenuBar';
import { Message } from '../components/Message';
import { ShowChessBoard} from '../components/ShowChessBoard';
import common from '@ohos.app.ability.common';
import Window from '@ohos.window'
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Index {
//设置开发板横屏显示
private changeOrientation() {
// 获取上下文使用common模块
let context = getContext(this) as common.UIAbilityContext;
let promise = Window.getLastWindow(context);
promise.then((data) => {
let windowClass = data;
//切换成横屏
let orientation = Window.Orientation.LANDSCAPE;
windowClass.setPreferredOrientation(orientation, (err) => {
});
console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
}).catch((err:BusinessError) => {
console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
});
}
aboutToAppear(){
this.changeOrientation()
}
@State myChess:Chess=new Chess(19, 19)
build() {
Row() {
//菜单栏
MenuBar({ myChess:$myChess })
.width('20%')
if(this.myChess.ifRandomChess){
// 展示一个棋局对弈结果
ShowChessBoard()
.margin({ bottom: 5})
.width('45%')
}else {
ChessBoard({ myChess: $myChess })
.margin({ bottom: 5 })
.width('45%')
}
// 相关功能按键
Message({ myChess:$myChess })
.width('35%')
}
.height('100%')
.width('100%')
.backgroundImage($r('app.media.background'))
.backgroundImageSize({width:'100%', height:'100%'})
}
}

View File

@ -0,0 +1,789 @@
//功能函数
import {Chess} from "../classes/Chess";
import * as CommonConstant from '../common/constants/CommonConstants'
import fs from '@ohos.file.fs';
import common from '@ohos.app.ability.common';
const TAG='functions'
//人白机黑模式
export function peocomwhite(chess:Chess) {
if (chess.Chess_Mode == 3) {
chess.Chess_Mode = 2
playing(chess)
} else {
chess.Chess_Mode = 2
refresh(chess)
}
}
//人黑机白模式
export function peocomblack(chess:Chess) {
if (chess.Chess_Mode == 3) {
chess.Chess_Mode = 1
playing(chess)
} else {
chess.Chess_Mode = 1
refresh(chess)
}
}
//双人对战模式
export function doublepeople(chess:Chess) {
if (chess.Chess_Mode == 3) {
chess.Chess_Mode = 0
playing(chess)
} else {
chess.Chess_Mode = 0
refresh(chess)
}
}
//复盘函数
export function review(chess:Chess) {
if (chess.WinFLAG == 1 || chess.WinFLAG == 2 || chess.WinFLAG==3) {
if(chess.ifReview == 0){
chess.ifReview = 1
}
}
}
//取消复盘
export function cancelreview(chess:Chess) {
if (chess.ifReview == 1) {
chess.ifReview = 0
}
}
//悔棋
export function regret(chess:Chess) {
if (chess.WinFLAG == 0) {
let regretnum = 1
if (chess.Currently_step == 1 && chess.Chess_Mode == 2) { //人白机黑 第一步 默认无法悔棋
// let re_x = chess.regretFlAG[chess.Currently_step - regretnum].x
// let re_y = chess.regretFlAG[chess.Currently_step - regretnum].y
// // 删除(re_x, re_y)位置上的棋子
// deleteChess(re_x, re_y)
// chess.ChessData[re_x][re_y].Cstate = 0
// chess.ChessData[re_x][re_y].Cstep = 0
// chess.Currently_step -= 1
// chess.Chess_Mode=2
// playing(chess)
chess.Current_Player = false
}
else if ((chess.Chess_Mode == 1 || chess.Chess_Mode == 2) && chess.Currently_step > 0) { //人机模式
for (let i = 0; i < 2; i++) {
let re_x = chess.regretFlAG[chess.Currently_step - regretnum].x
let re_y = chess.regretFlAG[chess.Currently_step - regretnum].y
deleteChess(re_x, re_y)
chess.ChessData[re_x][re_y].Cstate = 0
chess.ChessData[re_x][re_y].Cstep = 0
chess.Currently_step -= 1
console.info(TAG,"chess.Currently_step="+chess.Currently_step)
}
if (chess.Chess_Mode == 1) {
chess.Current_Player =true//保证人黑机白时,悔棋后落子框为玩家
} else {
chess.Current_Player = false//保证人白机黑时,悔棋后落子框为玩家
}
}
else if (chess.Chess_Mode == 0 && chess.Currently_step > 0) { //双人对战模式
let re_x = chess.regretFlAG[chess.Currently_step - regretnum].x
let re_y = chess.regretFlAG[chess.Currently_step - regretnum].y
deleteChess(re_x, re_y)
chess.ChessData[re_x][re_y].Cstate = 0
chess.ChessData[re_x][re_y].Cstep = 0
chess.Currently_step -= 1
if (chess.Currently_step % 2 == 0) { //玩家1 下棋
chess.player = 1
chess.player2 = 0
} else if (chess.Currently_step % 2 != 0) { //玩家2 下棋
chess.player = 0
chess.player2 = 1
}
}
}
//点击落子建议再悔棋需要清除
if(chess.ifSuggest){
for (let x = 0; x < chess.breadth; x++) {
for (let y = 0; y < chess.length; y++) {
//存储了建议落子位置数据的棋子删除
if (chess.SuggestChessData[x][y].Cstate != 0) {
deleteChess(x, y)
//删除后需要清空数据以便后续建议
chess.SuggestChessData[x][y].Cstate=0
}
}
}
chess.ifSuggest=false
}
}
//开始游戏函数 默认为人黑机白模式
export function start_game(chess:Chess) {
if (chess.Chess_Mode == 3) {
chess.Chess_Mode = 1
playing(chess)
} else {
chess.Chess_Mode = 1
refresh(chess)
}
}
//刷新函数
export function refresh(chess:Chess) {
// 清除画布上的棋子
for (let x = 0; x < chess.breadth; x++) {
for (let y = 0; y < chess.length; y++) {
if(chess.ChessData[x][y].Cstate !=0){
deleteChess(x,y)
}
if(chess.SuggestChessData[x][y].Cstate !=0){
deleteChess(x,y)
}
chess.ChessData[x][y].Cstate = 0
chess.ChessData[x][y].Cstep = 0
chess.Chessimg[x][y] = 0
chess.Relabel[x][y] = 0
}
}
chess.Currently_step = 0
chess.ifReview = 0
chess.Gameover = 0
chess.Depth = 0
chess.player = 0
chess.player2 = 0
chess.computer = 0
chess.myColor = 0
chess.player2color = 0
chess.computercolor = 0
chess.WinFLAG = 0
if (chess.Chess_Mode == 1) {//人黑机白
playing(chess)
chess.Current_Player=true
} else if (chess.Chess_Mode == 2) {//人白机黑
playing(chess)
chess.Current_Player=false
//如果还原上次棋局,需要再删除一次中心的黑棋
if(chess.ifResetChess){
deleteChess(9,9)
chess.ifResetChess=false
}
} else if (chess.Chess_Mode == 0) {//双人对战
playing(chess)
chess.Current_Player=true
}
}
//绘制棋盘
export function drawChessboard() {
// 绘制棋盘线条
CommonConstant.context.strokeStyle = '#c89265'; //线条颜色
CommonConstant.context.lineWidth = 1.5; //线条宽
for (let i = 0; i < CommonConstant.boardNum; i++) {
// 横线
CommonConstant.context.beginPath();
CommonConstant.context.moveTo(CommonConstant.gridSize / 2, i * CommonConstant.gridSize + CommonConstant.gridSize / 2);
CommonConstant.context.lineTo(CommonConstant.gridSize * (CommonConstant.boardNum - 1) + CommonConstant.gridSize / 2, i * CommonConstant.gridSize + CommonConstant.gridSize / 2);
CommonConstant.context.stroke();
// 竖线
CommonConstant.context.beginPath();
CommonConstant.context.moveTo(i * CommonConstant.gridSize + CommonConstant.gridSize / 2, CommonConstant.gridSize / 2);
CommonConstant.context.lineTo(i * CommonConstant.gridSize + CommonConstant.gridSize / 2, CommonConstant.gridSize * (CommonConstant.boardNum - 1) + CommonConstant.gridSize / 2);
CommonConstant.context.stroke();
}
}
//绘制特殊点
export function drawPoints() {
// 设置填充样式
CommonConstant.context.fillStyle = '#c89265';
// 找到这8个点的横纵坐标并且定义它的半径
for (const point of CommonConstant.startPoints) {
const centerX = point[0] * CommonConstant.gridSize + CommonConstant.gridSize / 2;
const centerY = point[1] * CommonConstant.gridSize + CommonConstant.gridSize / 2;
const radius = 2.5;
//画圆
CommonConstant.context.beginPath();
CommonConstant.context.arc(centerX, centerY, radius, 0, 2 * Math.PI);
CommonConstant.context.fill();
}
// 中心点
{
const centerX = CommonConstant.centerPoint[0] * CommonConstant.gridSize + CommonConstant.gridSize / 2;
const centerY = CommonConstant.centerPoint[0] * CommonConstant.gridSize + CommonConstant.gridSize / 2;
const radius = 3.5;
CommonConstant.context.beginPath();
CommonConstant.context.arc(centerX, centerY, radius, 0, 2 * Math.PI);
CommonConstant.context.fill();
}
}
//绘制棋子
export function drawChess(row:number, col:number,chess:Chess) {
// 坐标
const X = row * CommonConstant.gridSize;
const Y = col * CommonConstant.gridSize;
// 半径
const radius = 8;
if(chess.Chess_Mode==0){//双人对战
if (chess.ChessData[row][col].Cstate == 0 && chess.myColor == 1 && chess.Currently_step % 2 == 0) {
CommonConstant.context2.drawImage(CommonConstant.img1, X, Y, radius * 2, radius * 2);
} else if (chess.ChessData[row][col].Cstate == 0 && chess.player2color == 2 && chess.Currently_step % 2 != 0) {
CommonConstant.context2.drawImage(CommonConstant.img2, X, Y, radius * 2, radius * 2);
}
}else if(chess.Chess_Mode==1){//人黑机白
if (chess.ChessData[row][col].Cstate == 0 && chess.myColor == 1 && chess.Currently_step % 2 == 0) {
CommonConstant.context2.drawImage(CommonConstant.img1, X, Y, radius * 2, radius * 2);
}
}else if(chess.Chess_Mode==2){//人白机黑
if (chess.ChessData[row][col].Cstate == 0 && chess.myColor == 2 && chess.Currently_step % 2 != 0) {
CommonConstant.context2.drawImage(CommonConstant.img2, X, Y, radius * 2, radius * 2);
}
}
chess.Current_Player=!chess.Current_Player
}
//绘制复盘棋子
export function drawReviewChess(row:number, col:number,chess:Chess){
// 坐标
const X = row * CommonConstant.gridSize;
const Y = col * CommonConstant.gridSize;
if(chess.ChessData[row][col].Cstep>=0&&chess.ChessData[row][col].Cstep<10){
// CommonConstant.context3.font = '33fp sans-serif'//fp适配 跑开发板使用
CommonConstant.context3.font = '33px sans-serif'//预览器时使用
if (chess.ChessData[row][col].Cstate == 1) {
CommonConstant.context3.fillStyle = '#ffffff'
CommonConstant.context3.fillText(chess.ChessData[row][col].Cstep.toString(), X+5, Y+10)
} else if (chess.ChessData[row][col].Cstate == 2) {
CommonConstant.context3.fillStyle = '#000000'
CommonConstant.context3.fillText(chess.ChessData[row][col].Cstep.toString(), X+5, Y+10)
}
}else if(chess.ChessData[row][col].Cstep>=10&&chess.ChessData[row][col].Cstep<100){
// CommonConstant.context3.font = '30fp sans-serif'//fp适配 跑开发板使用
CommonConstant.context3.font = '30px sans-serif'//预览器时使用
if (chess.ChessData[row][col].Cstate == 1) {
CommonConstant.context3.fillStyle = '#ffffff'
CommonConstant.context3.fillText(chess.ChessData[row][col].Cstep.toString(), X+2, Y+10)
} else if (chess.ChessData[row][col].Cstate == 2) {
CommonConstant.context3.fillStyle = '#000000'
CommonConstant.context3.fillText(chess.ChessData[row][col].Cstep.toString(), X+2, Y+10)
}
}else{
// CommonConstant.context3.font = '27fp sans-serif'//fp适配 跑开发板使用
CommonConstant.context3.font = '27px sans-serif'//预览器时使用
if (chess.ChessData[row][col].Cstate == 1) {
CommonConstant.context3.fillStyle = '#ffffff'
CommonConstant.context3.fillText(chess.ChessData[row][col].Cstep.toString(), X, Y+10)
} else if (chess.ChessData[row][col].Cstate == 2) {
CommonConstant.context3.fillStyle = '#000000'
CommonConstant.context3.fillText(chess.ChessData[row][col].Cstep.toString(), X, Y+10)
}
}
}
//删除棋子
export function deleteChess(row:number, col:number) {
const X = row * CommonConstant.gridSize
const Y = col * CommonConstant.gridSize
const radius = 8
CommonConstant.context2.clearRect(X, Y, radius * 2, radius * 2)
}
// 落子建议
export function suggestion(chess:Chess){
let color:number=0
if(chess.WinFLAG==0){
if(chess.Depth>=9){
chess.Depth=8
}
if(chess.Chess_Mode==1){//人黑机白模式
color=2
}else if(chess.Chess_Mode==2){//人白机黑模式
color=1
}else if(chess.Chess_Mode==0){//双人模式
if(chess.player==1&&chess.player2==0){//玩家1下棋
color=2
}else if(chess.player==0&&chess.player2==1){//玩家2下棋
color=1
}
}
let pos=chess.return_chess(chess.Depth,color)//调用估值函数
if(chess.ChessData[pos[0]][pos[1]].Cstate==0){
drawSuggestionChess(pos[0],pos[1],chess)
}
}
}
// 绘制建议棋子
export function drawSuggestionChess(row:number, col:number,chess:Chess) {
// 坐标
const X = row * CommonConstant.gridSize;
const Y = col * CommonConstant.gridSize;
// 半径
const radius = 8;
if(chess.Chess_Mode==0){//双人对战
if (chess.ChessData[row][col].Cstate == 0 && chess.myColor == 1 && chess.Currently_step % 2 == 0) {
CommonConstant.context2.drawImage(CommonConstant.img3, X, Y, radius * 2, radius * 2);
} else if (chess.ChessData[row][col].Cstate == 0 && chess.player2color == 2 && chess.Currently_step % 2 != 0) {
CommonConstant.context2.drawImage(CommonConstant.img4, X, Y, radius * 2, radius * 2);
}
}else if(chess.Chess_Mode==1){//人黑机白
if (chess.ChessData[row][col].Cstate == 0 && chess.myColor == 1 && chess.Currently_step % 2 == 0) {
CommonConstant.context2.drawImage(CommonConstant.img3, X, Y, radius * 2, radius * 2);
}
}else if(chess.Chess_Mode==2){//人白机黑
if (chess.ChessData[row][col].Cstate == 0 && chess.myColor == 2 && chess.Currently_step % 2 != 0) {
CommonConstant.context2.drawImage(CommonConstant.img4, X, Y, radius * 2, radius * 2);
}
}
chess.SuggestChessData[row][col].Cstate=1
}
//局面评估(貌似本身有bug)
export function curnow(chess:Chess){
let color:number=0
if(chess.Depth>=9){
chess.Depth=8
}
if(chess.Chess_Mode==1){//人黑机白
color=2
}else if(chess.Chess_Mode==2){//人白机黑
chess.return_chess(chess.Depth,color)//调用估值函数
color=1
}else {
if(chess.player==1&&chess.player2==0){//玩家1下棋
color=2
}else if(chess.player==0&&chess.player2==1){//玩家2下棋
color=1
}
}
chess.return_chess(chess.Depth,color)//调用估值函数
let OwnCounter:number
if(chess.Counts/chess.New_count==1){
OwnCounter=getRandomNumber(0.8,0.85)
}else{
OwnCounter=chess.Counts/chess.New_count
}
if(color==1){
//1.判断x-轴是否连续四子
for(let x=0;x<15;x++){
for(let y=0;y<19;y++){
if(chess.ChessData[x][y].Cstate==2&&chess.ChessData[x+1][y].Cstate==2&&chess.ChessData[x+2][y].Cstate==2&&chess.ChessData[x+3][y].Cstate==2
&&chess.ChessData[x+4][y].Cstate==0&&chess.ChessData[x-1][y].Cstate==0){
OwnCounter -= 0.5
}
}
}
//2.判断y-轴是否连续四子
for(let x=0;x<19;x++){
for(let y=0;y<15;y++){
if(chess.ChessData[x][y].Cstate==2&&chess.ChessData[x][y+1].Cstate==2&&chess.ChessData[x][y+2].Cstate==2&&chess.ChessData[x][y+3].Cstate==2
&&chess.ChessData[x][y+4].Cstate==0&&chess.ChessData[x][y-1].Cstate==0){
OwnCounter -= 0.5
}
}
}
//3.判断右上-左下是否连续四子
for(let x=0;x<15;x++){
for(let y=4;y<19;y++){
if(chess.ChessData[x][y].Cstate==2&&chess.ChessData[x+1][y-1].Cstate==2&&chess.ChessData[x+2][y-2].Cstate==2&&chess.ChessData[x+3][y-3].Cstate==2
&&chess.ChessData[x+4][y-4].Cstate==0&&chess.ChessData[x-1][y+1].Cstate==0){
OwnCounter -= 0.5
}
}
}
//4.判断左上-右下是否连续四子
for(let x=0;x<15;x++){
for(let y=0;y<15;y++){
if(chess.ChessData[x][y].Cstate==2&&chess.ChessData[x+1][y+1].Cstate==2&&chess.ChessData[x+2][y+2].Cstate==2&&chess.ChessData[x+3][y+3].Cstate==2
&&chess.ChessData[x+4][y+4].Cstate==0&&chess.ChessData[x-1][y-1].Cstate==0){
OwnCounter -= 0.5
}
}
}
}else if(color==2){
//1.判断x-轴是否连续四子
for(let x=0;x<15;x++){
for(let y=4;y<19;y++){
if(chess.ChessData[x][y].Cstate==1&&chess.ChessData[x+1][y].Cstate==1&&chess.ChessData[x+2][y].Cstate==1&&chess.ChessData[x+3][y].Cstate==1
&&chess.ChessData[x+4][y].Cstate==0&&chess.ChessData[x-1][y].Cstate==0){
OwnCounter -= 0.5
}
}
}
//2.判断y-轴是否连续四子
for(let x=0;x<19;x++){
for(let y=0;y<15;y++){
if(chess.ChessData[x][y].Cstate==1&&chess.ChessData[x][y+1].Cstate==1&&chess.ChessData[x][y+2].Cstate==1&&chess.ChessData[x][y+3].Cstate==1
&&chess.ChessData[x][y+4].Cstate==0&&chess.ChessData[x][y-1].Cstate==0){
OwnCounter -= 0.5
}
}
}
//3.判断右上-左下是否连续四子
for(let x=0;x<15;x++){
for(let y=4;y<19;y++){
if(chess.ChessData[x][y].Cstate==1&&chess.ChessData[x+1][y-1].Cstate==1&&chess.ChessData[x+2][y-2].Cstate==1&&chess.ChessData[x+3][y-3].Cstate==1
&&chess.ChessData[x+4][y-4].Cstate==0&&chess.ChessData[x-1][y+1].Cstate==0){
OwnCounter -= 0.5
}
}
}
//4.判断左上-右下是否连续四子
for(let x=0;x<15;x++){
for(let y=0;y<15;y++){
if(chess.ChessData[x][y].Cstate==1&&chess.ChessData[x+1][y+1].Cstate==1&&chess.ChessData[x+2][y+2].Cstate==1&&chess.ChessData[x+3][y+3].Cstate==1
&&chess.ChessData[x+4][y+4].Cstate==0&&chess.ChessData[x-1][y-1].Cstate==0){
OwnCounter -= 0.5
}
}
}
}
let OtherCouter=1-OwnCounter
//(Math.round(OwnCounter*10000)/10000) 四舍五入到第四位小数
let msg = '当前落子方的胜率为'+(Math.round(OwnCounter*10000)/10000)+'%!\n当前对方的胜率为'+ (Math.round(OtherCouter*10000)/10000) + '%!'
return msg
}
//获取[m,n]区间内的随机数
export function getRandomNumber(n:number,m:number) {
let result:number = Math.random()*(m+1-n)+n;
while(result>m) {
result = Math.random()*(m+1-n)+n;
}
return result;
}
// 定义函数实现ai落子
export function ai_location(chess:Chess){
if (chess.Depth >= 9){
chess.Depth = 8
}
let position=chess.return_chess(chess.Depth,chess.computercolor)
// let position=chess.autoactive()
let X=position[0]*CommonConstant.gridSize
let Y=position[1]*CommonConstant.gridSize
let radius=8
if(chess.ChessData[position[0]][position[1]].Cstate==0){
if (chess.computercolor==2 && chess.Currently_step%2!=0){//人黑机白模式
//绘制棋子
CommonConstant.context2.drawImage(CommonConstant.img2,X,Y,radius*2,radius*2)
chess.ChessData[position[0]][position[1]].Cstate=2
chess.ChessData[position[0]][position[1]].Cstep= chess.Currently_step
chess.Current_Player=!chess.Current_Player
}else if(chess.computercolor==1 && chess.Currently_step%2==0){//人白机黑模式
//绘制棋子
CommonConstant.context2.drawImage(CommonConstant.img1,X,Y,radius*2,radius*2)
chess.ChessData[position[0]][position[1]].Cstate=1
chess.ChessData[position[0]][position[1]].Cstep= chess.Currently_step
chess.Current_Player=!chess.Current_Player
}
chess.regretFlAG[chess.Currently_step].x = position[0]
chess.regretFlAG[chess.Currently_step].y = position[1]
chess.player = 1 // 玩家下棋标志置0
chess.computer = 0 // 电脑下棋标志置1
chess.Currently_step += 1
console.info(TAG,'ai_location的Currently_step+=1执行了Currently_step= '+chess.Currently_step)
chess.ChessData[position[0]][position[1]].Cstep= chess.Currently_step
let a = Math.max(Math.abs(position[0] - 9), Math.abs(position[1] - 9)) //计算该点到中心的最大的距离
chess.Depth = Math.max(chess.Depth, a) //不断更新Depth的值
}else {
console.log('该位置已有棋子')
}
}
//人黑机白
export function playgame_black(chess:Chess){
if(chess.WinFLAG==0&&chess.player==1&&chess.computer==0){
chess.WinFLAG=chess.ChessCheck()
}else if (chess.WinFLAG==0&&chess.player==0&&chess.computer==1){
ai_location(chess)
chess.WinFLAG=chess.ChessCheck()
}else if(chess.WinFLAG==1){
console.log('胜负已分')
}
}
//人白机黑
export function playgame_white(chess:Chess){
// console.log('chess.player='+chess.player+'chess.computer='+chess.computer)
if(chess.WinFLAG==0&&chess.player==1&&chess.computer==0){
chess.WinFLAG=chess.ChessCheck()
}else if (chess.WinFLAG==0&&chess.player==0&&chess.computer==1){//电脑下棋
ai_location(chess)
chess.WinFLAG=chess.ChessCheck()
}else if(chess.WinFLAG==1){
console.log('胜负已分')
}
}
//双人对战
export function doublePeopleGame(chess:Chess) {
if (chess.player == 1 && chess.player2 == 0 && chess.Currently_step % 2 == 0) {
chess.WinFLAG = chess.ChessCheck()
} else if (chess.player2 == 1 && chess.player == 0 && chess.Currently_step % 2 != 0) {
chess.WinFLAG = chess.ChessCheck()
}
}
//设置对弈模式
export function playing(chess: Chess) {
if (chess.Chess_Mode == 0) {//双人对战
chess.myColor = 1//玩家1棋子颜色
chess.player2color = 2//玩家2棋子颜色
chess.player = 1//玩家1先手
chess.player2 = 0//玩家2后手
chess.WinFLAG = 0
// 调用函数更新当前画布
}else if (chess.Chess_Mode == 1) {//人黑机白
chess.myColor = 1//玩家棋子颜色
chess.computercolor = 2//电脑棋子颜色
chess.player = 1//玩家先手
chess.computer = 0//电脑后手
chess.WinFLAG = 0
// 调用函数更新当前画布
} else if (chess.Chess_Mode == 2) {//人白机黑
chess.myColor = 2//玩家棋子颜色
chess.computercolor = 1//电脑棋子颜色
chess.ChessData[Math.floor((chess.breadth - 1) / 2)][Math.floor((chess.length - 1) / 2)].Cstate = 1
//绘制棋子
let X = 9 * CommonConstant.gridSize
let Y = 9 * CommonConstant.gridSize
let radius = 8
CommonConstant.context2.drawImage(CommonConstant.img1, X, Y, radius * 2, radius * 2);
chess.regretFlAG[chess.Currently_step].x = Math.floor((chess.breadth - 1) / 2)
chess.regretFlAG[chess.Currently_step].y = Math.floor((chess.length - 1) / 2)
chess.player = 1
chess.computer = 0
chess.Currently_step += 1
chess.ChessData[Math.floor((chess.breadth - 1) / 2)][Math
.floor((chess.length - 1) / 2)]
.Cstep = chess.Currently_step
chess.WinFLAG = 0
let a = Math.max(Math.abs(Math.floor((chess.breadth - 1) / 2) - Math.floor((chess.breadth - 1) / 2)), Math.abs(Math.floor((chess.length - 1) / 2) - Math.floor((chess.length - 1) / 2)))
chess.Depth = Math.max(chess.Depth, a)
}
}
//保存当前棋局
export function saveCurrent(chess:Chess){
let context = getContext() as common.UIAbilityContext;
let fileDir = context.filesDir;
let filePath =fileDir+"/data.txt";
let res = fs.access(filePath)
if (!res) {
console.info(TAG,"文件不存在,开始创建")
}
//读写模式打开文件,若不存在则创建文件
let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
let b=''
for(let i=0;i<19;i++){
for(let j=0;j<19;j++){
chess.chessDataState[i][j]=chess.ChessData[j][i].Cstate
b+=chess.chessDataState[i][j].toString()+'\t'
}
b+='\t'
b+='\n'
}
//写入文件
let writeLen = fs.writeSync(file.fd, b);
console.info(TAG,'chessDataState的大小为 '+b.length)
b=''
for(let i=0;i<19;i++){
for(let j=0;j<19;j++){
chess.chessDataStep[i][j]=chess.ChessData[j][i].Cstep
b+=chess.chessDataStep[i][j].toString()+'\t'
}
b+='\t'
b+='\n'
}
//写入文件
writeLen = fs.writeSync(file.fd, b);
console.info(TAG,'chessDataStep的大小为 '+b.length)
chess.writenLen=b.length+760
let saveChess_Mode=chess.Chess_Mode.toString()+'\t'
writeLen = fs.writeSync(file.fd, saveChess_Mode);
let saveWinFLAG=chess.WinFLAG.toString()+'\t'
writeLen = fs.writeSync(file.fd, saveWinFLAG);
let saveGameOver=chess.Gameover.toString()+'\t'
writeLen = fs.writeSync(file.fd, saveGameOver);
let saveCurrently_step=chess.Currently_step.toString()+'\t'
writeLen = fs.writeSync(file.fd, saveCurrently_step);
fs.closeSync(file.fd)
alertDialog('棋局已保存')
}
//还原上次局面
export function resetLast(chess:Chess){
chess.ifResetChess=true
refresh(chess)
chess.ifResetChess=false
let context = getContext() as common.UIAbilityContext;
let fileDir = context.filesDir;
let filePath =fileDir+"/data.txt";
let res = fs.access(filePath)
if (!res) {
console.info(TAG,"棋局不存在")
}
let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE);//读写模式打开文件
class Option{
public offset:number=0
public length:number=0
public encoding:string='utf-8'
}
let option = new Option();
option.length=40//一行数据的长度
let MaxStep=0
let stateData=''
let stepData:string[]=[]
let stepDataItem:string[]=[]
let m=0
for(let i=0;i<19;i++){
option.offset=40*i
stateData= fs.readTextSync(filePath, option)//一次只读取一行数据
for(let j=0;j<19;j++) {
chess.ChessData[j][i].Cstate = Number(stateData[m])//还原上次棋局状态
m+=2
}
m=0
}
for(let i=0;i<19;i++){
option.length=chess.writenLen
option.offset=760
stepData = fs.readTextSync(filePath, option).split('\n')//按换行符切割字符串
stepDataItem=stepData[i].split('\t')//按空格切割字符串
for(let j=0;j<19;j++) {
chess.ChessData[j][i].Cstep = Number(stepDataItem[j])//还原上次棋局状态
if(chess.ChessData[j][i].Cstep>MaxStep) {
MaxStep=chess.ChessData[j][i].Cstep
}
}
}
option.length=1
option.offset=chess.writenLen
chess.Chess_Mode=Number(fs.readTextSync(filePath,option))
option.offset=chess.writenLen+2
chess.WinFLAG=Number(fs.readTextSync(filePath,option))
option.offset=chess.writenLen+4
chess.Gameover=Number(fs.readTextSync(filePath,option))
if(MaxStep<10){//0<=Currently_step<10
option.length=1
}else if(MaxStep>=10){//10<=Currently_step<100
option.length=2
}else if(MaxStep>=100){//100<=Currently_step
option.length=3
}
option.offset=chess.writenLen+6
chess.Currently_step=Number(fs.readTextSync(filePath,option))
fs.closeSync(file.fd)
console.info(TAG,"chess.Chess_Mode="+chess.Chess_Mode+" chess.WinFLAG="+chess.WinFLAG)
console.info(TAG,"chess.Gameover="+chess.Gameover+" chess.Currently_step="+chess.Currently_step)
//还原棋局
for(let i=0;i<19;i++){
for(let j=0;j<19;j++){
if(chess.ChessData[i][j].Cstate==1){
CommonConstant.context2.drawImage(CommonConstant.img1, CommonConstant.gridSize*i, CommonConstant.gridSize*j, 8 * 2, 8 * 2);
} else if(chess.ChessData[i][j].Cstate==2){
CommonConstant.context2.drawImage(CommonConstant.img2, CommonConstant.gridSize*i, CommonConstant.gridSize*j, 8 * 2, 8 * 2);
}
}
}
if(chess.WinFLAG!=0){
//弹窗信息提示 msg='对局已经结束了'
chess.message='对局已经结束了'
alertDialog(chess.message)
}else if(chess.Chess_Mode==1&&chess.WinFLAG==0){//人黑机白模式
chess.myColor=1
chess.computercolor=2
if (chess.Currently_step%2==0){//轮到玩家下棋
chess.player=1
chess.computer=0
chess.Current_Player=true
//弹窗信息提示 msg='轮到玩家下棋了'
alertDialog('轮到玩家下棋了')
}else if(chess.Currently_step%2!=0){//轮到电脑下棋
chess.player=0
chess.computer=1
chess.Current_Player=false
playgame_black(chess)
}
}else if(chess.Chess_Mode==2&&chess.WinFLAG==0){//人白机黑模式
chess.myColor=2
chess.computercolor=1
if (chess.Currently_step%2==0){//轮到电脑下棋
chess.player=0
chess.computer=1
chess.Current_Player=true
playgame_white(chess)
}else if(chess.Currently_step%2!=0){//轮到玩家下棋
chess.player=1
chess.computer=0
chess.Current_Player=false
//弹窗信息提示 msg='轮到玩家下棋了'
alertDialog('轮到玩家下棋了')
}
} else if(chess.Chess_Mode==0&&chess.WinFLAG==0) { //双人对战
chess.myColor=1
chess.player2color=2
if(chess.Currently_step%2==0){//玩家1下棋
chess.player=1
chess.player2=0
chess.Current_Player=true
//弹窗信息提示 msg='轮到玩家1下棋了'
alertDialog('轮到玩家1下棋了')
}else if(chess.Currently_step!=0){//玩家2下棋
chess.player=0
chess.player2=1
chess.Current_Player=false
//弹窗信息提示 msg='轮到玩家2下棋了'
alertDialog('轮到玩家2下棋了')
}
}
}
//定义弹窗函数
export function alertDialog(msg:string){
AlertDialog.show({
title: '信息提示!',
message: msg,
gridCount: 3,
//取消
confirm: {
value: 'OK',
action: () => {
}
},
cancel: () => {}
})
}

View File

@ -0,0 +1,51 @@
{
"module": {
"name": "entry",
"type": "entry",
"description": "$string:module_desc",
"mainElement": "EntryAbility",
"deviceTypes": [
"default",
"tablet"
],
"deliveryWithInstall": true,
"installationFree": false,
"pages": "$profile:main_pages",
"abilities": [
{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ets",
"description": "$string:EntryAbility_desc",
"icon": "$media:icon",
"label": "$string:EntryAbility_label",
"startWindowIcon": "$media:startIcon",
"startWindowBackground": "$color:start_window_background",
"exported": true,
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home"
]
}
]
}
],
"extensionAbilities": [
{
"name": "EntryBackupAbility",
"srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets",
"type": "backup",
"exported": false,
"metadata": [
{
"name": "ohos.extension.backup",
"resource": "$profile:backup_config"
}
]
}
]
}
}

View File

@ -0,0 +1,8 @@
{
"color": [
{
"name": "start_window_background",
"value": "#FFFFFF"
}
]
}

View File

@ -0,0 +1,8 @@
{
"float": [
{
"name": "page_text_font_size",
"value": "50fp"
}
]
}

View File

@ -0,0 +1,16 @@
{
"string": [
{
"name": "module_desc",
"value": "module description"
},
{
"name": "EntryAbility_desc",
"value": "description"
},
{
"name": "EntryAbility_label",
"value": "label"
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 129 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@ -0,0 +1,7 @@
{
"layered-image":
{
"background" : "$media:background",
"foreground" : "$media:foreground"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

View File

@ -0,0 +1,3 @@
{
"allowToBackupRestore": true
}

View File

@ -0,0 +1,5 @@
{
"src": [
"pages/Index"
]
}

View File

@ -0,0 +1,8 @@
{
"color": [
{
"name": "start_window_background",
"value": "#000000"
}
]
}

View File

@ -0,0 +1,2 @@
{
}

View File

@ -0,0 +1,35 @@
import { hilog } from '@kit.PerformanceAnalysisKit';
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
export default function abilityTest() {
describe('ActsAbilityTest', () => {
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
beforeAll(() => {
// Presets an action, which is performed only once before all test cases of the test suite start.
// This API supports only one parameter: preset action function.
})
beforeEach(() => {
// Presets an action, which is performed before each unit test case starts.
// The number of execution times is the same as the number of test cases defined by **it**.
// This API supports only one parameter: preset action function.
})
afterEach(() => {
// Presets a clear action, which is performed after each unit test case ends.
// The number of execution times is the same as the number of test cases defined by **it**.
// This API supports only one parameter: clear action function.
})
afterAll(() => {
// Presets a clear action, which is performed after all test cases of the test suite end.
// This API supports only one parameter: clear action function.
})
it('assertContain', 0, () => {
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
hilog.info(0x0000, 'testTag', '%{public}s', 'it begin');
let a = 'abc';
let b = 'b';
// Defines a variety of assertion methods, which are used to declare expected boolean conditions.
expect(a).assertContain(b);
expect(a).assertEqual(a);
})
})
}

View File

@ -0,0 +1,5 @@
import abilityTest from './Ability.test';
export default function testsuite() {
abilityTest();
}

View File

@ -0,0 +1,12 @@
{
"module": {
"name": "entry_test",
"type": "feature",
"deviceTypes": [
"default",
"tablet"
],
"deliveryWithInstall": true,
"installationFree": false
}
}

View File

@ -0,0 +1,5 @@
import localUnitTest from './LocalUnit.test';
export default function testsuite() {
localUnitTest();
}

View File

@ -0,0 +1,33 @@
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
export default function localUnitTest() {
describe('localUnitTest', () => {
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
beforeAll(() => {
// Presets an action, which is performed only once before all test cases of the test suite start.
// This API supports only one parameter: preset action function.
});
beforeEach(() => {
// Presets an action, which is performed before each unit test case starts.
// The number of execution times is the same as the number of test cases defined by **it**.
// This API supports only one parameter: preset action function.
});
afterEach(() => {
// Presets a clear action, which is performed after each unit test case ends.
// The number of execution times is the same as the number of test cases defined by **it**.
// This API supports only one parameter: clear action function.
});
afterAll(() => {
// Presets a clear action, which is performed after all test cases of the test suite end.
// This API supports only one parameter: clear action function.
});
it('assertContain', 0, () => {
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
let a = 'abc';
let b = 'b';
// Defines a variety of assertion methods, which are used to declare expected boolean conditions.
expect(a).assertContain(b);
expect(a).assertEqual(a);
});
});
}

View File

@ -0,0 +1,22 @@
{
"modelVersion": "5.0.5",
"dependencies": {
},
"execution": {
// "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */
// "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */
// "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */
// "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */
// "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */
},
"logging": {
// "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */
},
"debugging": {
// "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */
},
"nodeOptions": {
// "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/
// "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/
}
}

View File

@ -0,0 +1,6 @@
import { appTasks } from '@ohos/hvigor-ohos-plugin';
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins: [] /* Custom plugin to extend the functionality of Hvigor. */
}

View File

@ -0,0 +1,28 @@
{
"meta": {
"stableOrder": true,
"enableUnifiedLockfile": false
},
"lockfileVersion": 3,
"ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.",
"specifiers": {
"@ohos/hamock@1.0.0": "@ohos/hamock@1.0.0",
"@ohos/hypium@1.0.21": "@ohos/hypium@1.0.21"
},
"packages": {
"@ohos/hamock@1.0.0": {
"name": "@ohos/hamock",
"version": "1.0.0",
"integrity": "sha512-K6lDPYc6VkKe6ZBNQa9aoG+ZZMiwqfcR/7yAVFSUGIuOAhPvCJAo9+t1fZnpe0dBRBPxj2bxPPbKh69VuyAtDg==",
"resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hamock/-/hamock-1.0.0.har",
"registryType": "ohpm"
},
"@ohos/hypium@1.0.21": {
"name": "@ohos/hypium",
"version": "1.0.21",
"integrity": "sha512-iyKGMXxE+9PpCkqEwu0VykN/7hNpb+QOeIuHwkmZnxOpI+dFZt6yhPB7k89EgV1MiSK/ieV/hMjr5Z2mWwRfMQ==",
"resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hypium/-/hypium-1.0.21.har",
"registryType": "ohpm"
}
}
}

View File

@ -0,0 +1,10 @@
{
"modelVersion": "5.0.5",
"description": "Please describe the basic information.",
"dependencies": {
},
"devDependencies": {
"@ohos/hypium": "1.0.21",
"@ohos/hamock": "1.0.0"
}
}