From d729fbb5d3d067232b5fb7b1041da20f61beb72a Mon Sep 17 00:00:00 2001 From: SparksOfFire Date: Mon, 23 Aug 2021 11:46:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E5=88=86=E5=B8=83?= =?UTF-8?q?=E5=BC=8F=E4=BA=95=E5=AD=97=E8=BF=87=E4=B8=89=E5=85=B3sample?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: SparksOfFire --- CompleteApps/TicTacToeGame/README_zh.md | 62 +++ CompleteApps/TicTacToeGame/build.gradle | 52 ++ CompleteApps/TicTacToeGame/entry/build.gradle | 28 + .../TicTacToeGame/entry/src/main/config.json | 72 +++ .../samples/tictactoegame/MainAbility.java | 45 ++ .../samples/tictactoegame/MyApplication.java | 28 + .../tictactoegame/RemoteServiceAbility.java | 91 +++ .../connections/MyRemoteProxy.java | 83 +++ .../tictactoegame/data/mode/DeviceData.java | 49 ++ .../tictactoegame/slice/MainAbilitySlice.java | 79 +++ .../slice/StartAbilitySlice.java | 517 ++++++++++++++++++ .../tictactoegame/ui/DeviceSelectDialog.java | 155 ++++++ .../ui/listcomponent/CommentViewHolder.java | 98 ++++ .../listcomponent/ListComponentAdapter.java | 84 +++ .../tictactoegame/utils/DeviceUtils.java | 54 ++ .../tictactoegame/utils/GameRulesUtils.java | 61 +++ .../tictactoegame/utils/GameStatusUtils.java | 92 ++++ .../samples/tictactoegame/utils/PkUtils.java | 38 ++ .../samples/tictactoegame/utils/UiUtils.java | 32 ++ .../main/resources/base/element/string.json | 52 ++ .../base/graphic/background_ability_main.xml | 20 + .../graphic/background_button_radius_.xml | 19 + .../base/graphic/background_game_rule.xml | 22 + .../base/graphic/background_rule_yes.xml | 22 + .../base/graphic/background_toast_element.xml | 22 + .../graphic/background_white_radius_10.xml | 19 + .../resources/base/layout/ability_main.xml | 85 +++ .../resources/base/layout/ability_start.xml | 183 +++++++ .../base/layout/dialog_device_item.xml | 28 + .../base/layout/dialog_game_rule.xml | 81 +++ .../base/layout/dialog_layout_device.xml | 78 +++ .../resources/base/layout/game_status.xml | 27 + .../resources/base/layout/layout_toast.xml | 27 + .../src/main/resources/base/media/PK.png | Bin 0 -> 61734 bytes .../main/resources/base/media/app_icon.png | Bin 0 -> 10954 bytes .../src/main/resources/base/media/battles.png | Bin 0 -> 55323 bytes .../src/main/resources/base/media/bg.png | Bin 0 -> 204371 bytes .../resources/base/media/bg_start_game.png | Bin 0 -> 196291 bytes .../resources/base/media/checked_point.png | Bin 0 -> 2679 bytes .../src/main/resources/base/media/circled.png | Bin 0 -> 8886 bytes .../src/main/resources/base/media/dv_pad.png | Bin 0 -> 590 bytes .../main/resources/base/media/dv_phone.png | Bin 0 -> 765 bytes .../main/resources/base/media/dv_watch.png | Bin 0 -> 18021 bytes .../src/main/resources/base/media/fork.png | Bin 0 -> 5433 bytes .../main/resources/base/media/game_fail.png | Bin 0 -> 71308 bytes .../main/resources/base/media/game_tie.png | Bin 0 -> 45470 bytes .../main/resources/base/media/game_win.png | Bin 0 -> 120816 bytes .../src/main/resources/base/media/icon.png | Bin 0 -> 10954 bytes .../main/resources/base/media/icon_back.png | Bin 0 -> 24543 bytes .../base/media/icon_back_dont_set.png | Bin 0 -> 24563 bytes .../main/resources/base/media/icon_close.png | Bin 0 -> 2097 bytes .../resources/base/media/icon_refresh.png | Bin 0 -> 28246 bytes .../base/media/icon_refresh_dont_set.png | Bin 0 -> 27819 bytes .../main/resources/base/media/littleBK.png | Bin 0 -> 2268 bytes .../src/main/resources/base/media/tie.png | Bin 0 -> 45470 bytes .../resources/base/media/uncheck_point.png | Bin 0 -> 2650 bytes .../resources/base/media/user_circled.png | Bin 0 -> 5557 bytes .../base/media/user_circled_huawei.png | Bin 0 -> 11859 bytes .../resources/base/media/user_circled_me.png | Bin 0 -> 8064 bytes .../main/resources/base/media/user_fork.png | Bin 0 -> 4427 bytes .../resources/base/media/user_fork_huawei.png | Bin 0 -> 10808 bytes .../resources/base/media/user_fork_me.png | Bin 0 -> 7052 bytes .../src/main/resources/en/element/string.json | 52 ++ .../src/main/resources/zh/element/string.json | 52 ++ .../tictactoegame/ExampleOhosTest.java | 14 + .../samples/tictactoegame/ExampleTest.java | 9 + .../screenshort/TicTacToeGame.png | Bin 0 -> 2762484 bytes CompleteApps/TicTacToeGame/settings.gradle | 15 + 68 files changed, 2547 insertions(+) create mode 100644 CompleteApps/TicTacToeGame/README_zh.md create mode 100644 CompleteApps/TicTacToeGame/build.gradle create mode 100644 CompleteApps/TicTacToeGame/entry/build.gradle create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/config.json create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/MainAbility.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/MyApplication.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/RemoteServiceAbility.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/connections/MyRemoteProxy.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/data/mode/DeviceData.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/slice/MainAbilitySlice.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/slice/StartAbilitySlice.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/ui/DeviceSelectDialog.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/ui/listcomponent/CommentViewHolder.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/ui/listcomponent/ListComponentAdapter.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/DeviceUtils.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/GameRulesUtils.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/GameStatusUtils.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/PkUtils.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/UiUtils.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/element/string.json create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_ability_main.xml create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_button_radius_.xml create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_game_rule.xml create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_rule_yes.xml create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_toast_element.xml create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_white_radius_10.xml create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/layout/ability_main.xml create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/layout/ability_start.xml create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/layout/dialog_device_item.xml create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/layout/dialog_game_rule.xml create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/layout/dialog_layout_device.xml create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/layout/game_status.xml create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/layout/layout_toast.xml create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/PK.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/app_icon.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/battles.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/bg.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/bg_start_game.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/checked_point.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/circled.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/dv_pad.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/dv_phone.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/dv_watch.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/fork.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/game_fail.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/game_tie.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/game_win.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/icon.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/icon_back.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/icon_back_dont_set.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/icon_close.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/icon_refresh.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/icon_refresh_dont_set.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/littleBK.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/tie.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/uncheck_point.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/user_circled.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/user_circled_huawei.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/user_circled_me.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/user_fork.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/user_fork_huawei.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/base/media/user_fork_me.png create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/en/element/string.json create mode 100644 CompleteApps/TicTacToeGame/entry/src/main/resources/zh/element/string.json create mode 100644 CompleteApps/TicTacToeGame/entry/src/ohosTest/java/ohos/samples/tictactoegame/ExampleOhosTest.java create mode 100644 CompleteApps/TicTacToeGame/entry/src/test/java/ohos/samples/tictactoegame/ExampleTest.java create mode 100644 CompleteApps/TicTacToeGame/screenshort/TicTacToeGame.png create mode 100644 CompleteApps/TicTacToeGame/settings.gradle diff --git a/CompleteApps/TicTacToeGame/README_zh.md b/CompleteApps/TicTacToeGame/README_zh.md new file mode 100644 index 0000000000..b61611ff79 --- /dev/null +++ b/CompleteApps/TicTacToeGame/README_zh.md @@ -0,0 +1,62 @@ +# 井字过三关 + +##### 简介 + +​ 本sample由Java开发,可拉起身边小伙伴手机游戏加入到对局,过三关是非常经典的游戏,可通过时间来判断胜负。 + +##### 使用说明 + +1. ###### 拉起应用 + + ​ 通过点击首页开始游戏按钮可以查看同一组网内的设备,然后选择需要对战的设备,点击确定按钮就可以拉起对应设备中的FA进行游戏。 + + + +2. ###### 游戏玩法 + + ​ 点击九宫格中的任意一格未被填充的格子,会自动填充对应的○或者×,表示已经此格已下。 + + ​ 游戏左上角显示当前自己对应的是○还是×。 + + ​ 当轮到自己下的时候,左上角的图标会自动抖动且下方时间计时开始,否则的话非己方行动的时候,无法点击。 + + + +3. ###### 游戏结果显示 + + ​ 当一方先连三个格子(横竖以及对角线连三个都算),则判定为胜利,或者行动方在规定时间内未动作,则判定行动方失败,对方获胜。胜利一方会弹出你已经赢了的对话框,而失败方则弹出你失败了的对话框。若九个格子都下完而没有一方获胜的话,则为平局,双方弹出平局的对话框。 + + + +4. 重新开始 + + ​ 游戏结束后,点击右下角复位按钮则重新开始议论游戏。 + + + +5. 返回 + + ​ 游戏结束后,点击左下角返回按钮,可以回到选择对战设备的首页面,可以重新选择拉起的设备。 + + + +##### 约束与限制 + +1. 编译约束 + 已实名认证的开发者联盟账号 ,具体[参考](https://developer.huawei.com/consumer/cn/ ) + 开发工具:DevEcoStudio 下载地址 [HUAWEI DevEco Studio - HarmonyOS应用开发官网](https://developer.harmonyos.com/cn/develop/deveco-studio#download) + + •安装DevEco Studio + + 设置DevEco Studio开发环境,DevEco Studio开发环境需要连接到网络,以确保该正常使用。 + 可以根据以下两种情况配置开发环境: + 1).如果您可以直接访问Internet,则只需下载HarmonyOS SDK + 2).如果网络无法直接访问Internet,则可以通过代理服务器进行访问 • 生成密钥并申请证书 + + 具体环境搭建请[参考:](https://developer.harmonyos.com/cn/docs/documentation/doc-guides/installation_process-0000001071425528) + 更多资料请登录鸿蒙应用开发[官网:](https://developer.harmonyos.com/cn/) + +2. 使用限制 + •该应用必须要有两部同一组网内的手机,且只支持同时两部手机进行游戏 + + diff --git a/CompleteApps/TicTacToeGame/build.gradle b/CompleteApps/TicTacToeGame/build.gradle new file mode 100644 index 0000000000..56c9d92e34 --- /dev/null +++ b/CompleteApps/TicTacToeGame/build.gradle @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +//For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#ZH-CN_TOPIC_0000001154985555__section1112183053510 +ohos { + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 4 + } +} + +buildscript { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + jcenter() + } + dependencies { + classpath 'com.huawei.ohos:hap:2.4.4.2' + classpath 'com.huawei.ohos:decctest:1.2.4.0' + } +} + +allprojects { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + jcenter() + } +} diff --git a/CompleteApps/TicTacToeGame/entry/build.gradle b/CompleteApps/TicTacToeGame/entry/build.gradle new file mode 100644 index 0000000000..74c283ca63 --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/build.gradle @@ -0,0 +1,28 @@ +apply plugin: 'com.huawei.ohos.hap' +apply plugin: 'com.huawei.ohos.decctest' +//For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#ZH-CN_TOPIC_0000001154985555__section1112183053510 +ohos { + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 4 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } + +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) + implementation 'io.openharmony.tpc.thirdlib:ohosViewAnimations:1.0.2' + testImplementation 'junit:junit:4.13' + ohosTestImplementation 'com.huawei.ohos.testkit:runner:1.0.0.100' +} +decc { + supportType = ['html','xml'] +} diff --git a/CompleteApps/TicTacToeGame/entry/src/main/config.json b/CompleteApps/TicTacToeGame/entry/src/main/config.json new file mode 100644 index 0000000000..b03c5e06f8 --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/config.json @@ -0,0 +1,72 @@ +{ + "app": { + "bundleName": "ohos.samples.tictactoegame", + "vendor": "sample", + "version": { + "code": 1000000, + "name": "1.0.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.tictactoegame", + "name": ".MyApplication", + "mainAbility": "ohos.samples.tictactoegame.MainAbility", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree": false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "unspecified", + "name": "ohos.samples.tictactoegame.MainAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "$string:entry_MainAbility", + "type": "page", + "launchType": "standard" + }, + { + "name": "ohos.samples.tictactoegame.RemoteServiceAbility", + "icon": "$media:icon", + "description": "$string:remoteserviceability_description", + "type": "service" + } + ], + "reqPermissions": [ + { + "name": "ohos.permission.DISTRIBUTED_DATASYNC" + }, + { + "name": "ohos.permission.servicebus.ACCESS_SERVICE" + }, + { + "name": "com.huawei.hwddmp.servicebus.BIND_SERVICE" + }, + { + "name": "ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE" + }, + { + "name": "ohos.permission.GET_DISTRIBUTED_DEVICE_INFO" + }, + { + "name": "ohos.permission.GET_BUNDLE_INFO" + } + ] + } +} \ No newline at end of file diff --git a/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/MainAbility.java b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/MainAbility.java new file mode 100644 index 0000000000..27150b3cbb --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/MainAbility.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd.All rights reserved. + * Licensed under the Apache License,Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.tictactoegame; + +import ohos.samples.tictactoegame.slice.MainAbilitySlice; +import ohos.aafwk.ability.Ability; +import ohos.aafwk.content.Intent; +import ohos.samples.tictactoegame.slice.StartAbilitySlice; + +/** + * MainAbility + */ +public class MainAbility extends Ability { + private static final int GAME_DEFAULT_VALUE = 0; + private static final int GAME_COM_IS_SLAVE = 301; + + @Override + public void onStart(Intent intent) { + super.onStart(intent); + int recParam = intent.getIntParam("data", GAME_DEFAULT_VALUE); + if (recParam == GAME_COM_IS_SLAVE) { + super.setMainRoute(StartAbilitySlice.class.getName()); + } else { + super.setMainRoute(MainAbilitySlice.class.getName()); + super.addActionRoute("StartAbilitySlice", StartAbilitySlice.class.getName()); + } + requestPermissionsFromUser(new String[]{ + "ohos.permission.DISTRIBUTED_DATASYNC", + "ohos.permission.servicebus.ACCESS_SERVICE", + "com.huawei.hwddmp.servicebus.BIND_SERVICE"}, 0); + } +} diff --git a/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/MyApplication.java b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/MyApplication.java new file mode 100644 index 0000000000..ebc0139c44 --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/MyApplication.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd.All rights reserved. + * Licensed under the Apache License,Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.tictactoegame; + +import ohos.aafwk.ability.AbilityPackage; + +/** + * MyApplication + */ +public class MyApplication extends AbilityPackage { + @Override + public void onInitialize() { + super.onInitialize(); + } +} diff --git a/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/RemoteServiceAbility.java b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/RemoteServiceAbility.java new file mode 100644 index 0000000000..b7ae2f492c --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/RemoteServiceAbility.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd.All rights reserved. + * Licensed under the Apache License,Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.tictactoegame; + +import ohos.aafwk.ability.Ability; +import ohos.aafwk.content.Intent; +import ohos.rpc.IRemoteBroker; +import ohos.rpc.IRemoteObject; +import ohos.rpc.MessageOption; +import ohos.rpc.MessageParcel; +import ohos.rpc.RemoteObject; +import ohos.samples.tictactoegame.slice.StartAbilitySlice; +import ohos.samples.tictactoegame.utils.UiUtils; + +/** + * RemoteService Ability + */ +public class RemoteServiceAbility extends Ability { + @Override + public void onStart(Intent intent) { + super.onStart(intent); + } + + @Override + public void onBackground() { + super.onBackground(); + } + + @Override + public void onStop() { + super.onStop(); + } + + @Override + public void onCommand(Intent intent, boolean isRestart, int startId) { + UiUtils.showTip(this, "onCommand" + isRestart + "," + startId); + } + + @Override + public IRemoteObject onConnect(Intent intent) { + StartAbilitySlice.getHandler().sendEvent(1); + return new GameRemountObject("GameRemountObject").asObject(); + } + + @Override + public void onDisconnect(Intent intent) { + } + + /** + * GameRemount Object class + */ + private class GameRemountObject extends RemoteObject implements IRemoteBroker { + private static final int COMMAND_PLUS = IRemoteObject.MIN_TRANSACTION_ID; + private static final int ERR_OK = 0; + private static final int ERROR = -1; + + GameRemountObject(String descriptor) { + super(descriptor); + } + + public IRemoteObject asObject() { + return this; + } + + public boolean onRemoteRequest(int code, MessageParcel data, + MessageParcel reply, MessageOption option) { + if (code != COMMAND_PLUS) { + reply.writeInt(ERROR); + return false; + } + + int receiveCmd = data.readInt(); + StartAbilitySlice.getHandler().sendEvent(receiveCmd); + reply.writeInt(ERR_OK); + return true; + } + } +} \ No newline at end of file diff --git a/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/connections/MyRemoteProxy.java b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/connections/MyRemoteProxy.java new file mode 100644 index 0000000000..ae365428eb --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/connections/MyRemoteProxy.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd.All rights reserved. + * Licensed under the Apache License,Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.tictactoegame.connections; + +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.rpc.IRemoteBroker; +import ohos.rpc.IRemoteObject; +import ohos.rpc.MessageOption; +import ohos.rpc.MessageParcel; +import ohos.rpc.RemoteException; + +/** + * MyRemoteProxy class + */ +public class MyRemoteProxy implements IRemoteBroker { + private static final int COMMAND_PLUS = IRemoteObject.MIN_TRANSACTION_ID; + private static final int ERR_OK = 0; + + private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, ""); + private static final String LOG_FORMAT = "%{public}s: %{public}s"; + + private final IRemoteObject remote; + + /** + * MyRemoteProxy + * + * @param remote the remote + */ + public MyRemoteProxy(IRemoteObject remote) { + this.remote = remote; + } + + /** + * asObject: get the IRemote Object + * + * @return IRemote Object + */ + public IRemoteObject asObject() { + return remote; + } + + /** + * sendCmd + * + * @param cmd the cmd to send + * @return send result. + */ + public int sendCmd(int cmd) { + int result = 0; + MessageParcel data = MessageParcel.obtain(); + data.writeInt(cmd); + + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(MessageOption.TF_SYNC); + try { + remote.sendRequest(COMMAND_PLUS, data, reply, option); + int ec = reply.readInt(); + if (ec != ERR_OK) { + throw new RemoteException(); + } + result = reply.readInt(); + } catch (RemoteException e) { + HiLog.error(LABEL_LOG, LOG_FORMAT, "MyRemoteProxy", "" + e); + } + + return result; + } +} + diff --git a/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/data/mode/DeviceData.java b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/data/mode/DeviceData.java new file mode 100644 index 0000000000..7b490a5501 --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/data/mode/DeviceData.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd.All rights reserved. + * Licensed under the Apache License,Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.tictactoegame.data.mode; + +import ohos.distributedschedule.interwork.DeviceInfo; + +/** + * DeviceData class + */ +public class DeviceData { + private boolean isChecked; + private final DeviceInfo deviceInfo; + + /** + * DeviceData + * + * @param isChecked the device is checked + * @param deviceInfo device info + */ + public DeviceData(boolean isChecked, DeviceInfo deviceInfo) { + this.isChecked = isChecked; + this.deviceInfo = deviceInfo; + } + + public DeviceInfo getDeviceInfo() { + return deviceInfo; + } + + public boolean isChecked() { + return isChecked; + } + + public void setChecked(boolean isCheck) { + isChecked = isCheck; + } +} \ No newline at end of file diff --git a/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/slice/MainAbilitySlice.java b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/slice/MainAbilitySlice.java new file mode 100644 index 0000000000..20306e4f7c --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/slice/MainAbilitySlice.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd.All rights reserved. + * Licensed under the Apache License,Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.tictactoegame.slice; + +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; +import ohos.aafwk.content.Operation; +import ohos.agp.components.*; +import ohos.samples.tictactoegame.ResourceTable; +import ohos.samples.tictactoegame.ui.DeviceSelectDialog; +import ohos.samples.tictactoegame.utils.GameRulesUtils; + +/** + * MainAbilitySlice + */ +public class MainAbilitySlice extends AbilitySlice { + private static final int GAME_COM_IS_SLAVE = 301; + + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setUIContent(ResourceTable.Layout_ability_main); + + Button button = (Button) findComponentById(ResourceTable.Id_mainButton); + button.setClickedListener(component -> { + DeviceSelectDialog dialog = new DeviceSelectDialog(getContext()); + dialog.setListener(deviceInfo -> { + Intent myIntent = new Intent(); + Operation op = new Intent.OperationBuilder() + .withDeviceId(deviceInfo.getDeviceId()) + .withBundleName(getBundleName()) + .withAbilityName("ohos.samples.tictactoegame.MainAbility") + .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE) + .build(); + myIntent.setOperation(op); + myIntent.setParam("data", GAME_COM_IS_SLAVE); + startAbility(myIntent); + + Intent intent1 = new Intent(); + intent1.setParam("user", "main"); + dialog.hide(); + present(new StartAbilitySlice(), intent1); + }); + dialog.show(); + }); + + Text imgGameRule = (Text) findComponentById(ResourceTable.Id_game_rule); + imgGameRule.setClickedListener(component -> { + GameRulesUtils gameRuleDialog = new GameRulesUtils(getContext()); + gameRuleDialog.show(); + }); + } + + @Override + public void onActive() { + super.onActive(); + } + + @Override + public void onForeground(Intent intent) { + super.onForeground(intent); + } +} + + + diff --git a/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/slice/StartAbilitySlice.java b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/slice/StartAbilitySlice.java new file mode 100644 index 0000000000..7da6b15f46 --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/slice/StartAbilitySlice.java @@ -0,0 +1,517 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd.All rights reserved. + * Licensed under the Apache License,Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.tictactoegame.slice; + +import com.daimajia.ohosanimations.library.Techniques; +import com.daimajia.ohosanimations.library.YoYo; + +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.ability.IAbilityConnection; +import ohos.aafwk.content.Intent; +import ohos.aafwk.content.Operation; +import ohos.agp.components.Image; +import ohos.agp.components.RoundProgressBar; +import ohos.app.dispatcher.TaskDispatcher; +import ohos.bundle.ElementName; +import ohos.eventhandler.EventHandler; +import ohos.eventhandler.EventRunner; +import ohos.eventhandler.InnerEvent; +import ohos.rpc.IRemoteObject; +import ohos.samples.tictactoegame.ResourceTable; +import ohos.samples.tictactoegame.connections.MyRemoteProxy; +import ohos.samples.tictactoegame.utils.DeviceUtils; +import ohos.samples.tictactoegame.utils.GameStatusUtils; +import ohos.samples.tictactoegame.utils.PkUtils; +import ohos.samples.tictactoegame.utils.UiUtils; + +import java.util.Timer; +import java.util.TimerTask; + +/** + * StartAbilitySlice + */ +public class StartAbilitySlice extends AbilitySlice { + private static final int NOT_USE = 0; + private static final int USE = 1; + private static final int STATSUS_OFF = 0; + private static final int STATSUS_ON = 1; + private static final int IS_HOST = 1; + private static final int IS_ADV = 2; + private static final int IS_STOP = 3; + private static final int COL_NUM = 3; + private static final int ROW_NUM = 3; + private static final int GAME_LOSE = 0; + private static final int GAME_WIN = 1; + private static final int GAME_DOGFALL = 2; + + private static final int GAME_COM_CLICK1 = 101; + private static final int GAME_COM_CLICK9 = 109; + private static final int GAME_COM_GAME_START = 110; + + private static final int GAME_DEFAULT_VALUE = 0; + private static final int GAME_COM_IS_SLAVE = 301; + private static final int GAME_COM_LOST = 401; + private static final int GAME_COM_WIN = 402; + private static final int GAME_COM_DOGFALL = 403; + private static final int GAME_COM_EXIT = 404; + private static final int GAME_COM_RESTART = 501; + + private static final int GAME_COUNT_TIME = 10; + private static final int GAME_DELAY_LONG = 1000; + private static final int GAME_DELAY_SHORT = 500; + + private static final int HALF_GRID = 5; + + private static final int BUTTON_IDX_0 = 0; + private static final int BUTTON_IDX_1 = BUTTON_IDX_0 + 1; + private static final int BUTTON_IDX_2 = BUTTON_IDX_1 + 1; + private static final int BUTTON_IDX_3 = BUTTON_IDX_2 + 1; + private static final int BUTTON_IDX_4 = BUTTON_IDX_3 + 1; + private static final int BUTTON_IDX_5 = BUTTON_IDX_4 + 1; + private static final int BUTTON_IDX_6 = BUTTON_IDX_5 + 1; + private static final int BUTTON_IDX_7 = BUTTON_IDX_6 + 1; + private static final int BUTTON_IDX_8 = BUTTON_IDX_7 + 1; + + private final static int[][] HOST = {{NOT_USE, NOT_USE, NOT_USE}, + {NOT_USE, NOT_USE, NOT_USE}, {NOT_USE, NOT_USE, NOT_USE}}; + private final static int[][] ADVERSARY = {{NOT_USE, NOT_USE, NOT_USE}, + {NOT_USE, NOT_USE, NOT_USE}, {NOT_USE, NOT_USE, NOT_USE}}; + + private static boolean isButtonRefresh; + private static boolean isButtonBack; + private int pageStatus = STATSUS_OFF; + private static int gameStatus = STATSUS_OFF; + private static int playGame; + + private static Image image1; + private static Image image2; + private static Image image3; + private static Image image4; + private static Image image5; + private static Image image6; + private static Image image7; + private static Image image8; + private static Image image9; + + private static Image imageBack; + private static Image imageRefresh; + private static Image imageMe; + + private static int myImg; + private static int advImg; + private static int resetImg; + private static MyRemoteProxy myProxy = null; + + private static boolean countDownSwitch; + private static Timer timer = null; + private static int midTime; + private static TaskDispatcher myTaskDis; + private static RoundProgressBar roPrbar; + + private final IAbilityConnection conn = new IAbilityConnection() { + @Override + public void onAbilityConnectDone(ElementName elementName, IRemoteObject remoteObject, int i) { + myProxy = new MyRemoteProxy(remoteObject); + } + + @Override + public void onAbilityDisconnectDone(ElementName elementName, int i) { + } + }; + + private void buttonClick(Image imageButton, int idx) { + int row = idx / ROW_NUM; + int column = idx % COL_NUM; + + if (myProxy == null) { + return; + } + + if ((HOST[row][column] != NOT_USE) || (ADVERSARY[row][column] != NOT_USE)) { + return; + } + + if ((gameStatus == STATSUS_ON) && (playGame == IS_HOST)) { + HOST[row][column] = USE; + myProxy.sendCmd(GAME_COM_CLICK1 + idx); + imgUiGenOpe(); + imageButton.setPixelMap(myImg); + } + } + + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setUIContent(ResourceTable.Layout_ability_start); + + myTaskDis = getUITaskDispatcher(); + resetImg = ResourceTable.Media_littleBK; + PkUtils.showTip(StartAbilitySlice.this); + + preInit(); + imageMe = (Image) findComponentById(ResourceTable.Id_img_me); + Image imageDv = (Image) findComponentById(ResourceTable.Id_img_dv); + + int recParam = intent.getIntParam("data",GAME_DEFAULT_VALUE); + if (recParam == GAME_COM_IS_SLAVE) { + imageMe.setPixelMap(ResourceTable.Media_user_fork_me); + imageDv.setPixelMap(ResourceTable.Media_user_circled_huawei); + if (pageStatus == STATSUS_OFF) { + pageStatus = STATSUS_ON; + gameStatus = STATSUS_ON; + + myImg = ResourceTable.Media_fork; + advImg = ResourceTable.Media_circled; + playGame = IS_ADV; + } + } else { + imageMe.setPixelMap(ResourceTable.Media_user_circled_me); + imageDv.setPixelMap(ResourceTable.Media_user_fork_huawei); + String recParamUser = intent.getStringParam("user"); + if (recParamUser != null) { + pageStatus = STATSUS_ON; + gameStatus = STATSUS_ON; + myImg = ResourceTable.Media_circled; + advImg = ResourceTable.Media_fork; + playGame = IS_HOST; + + countDownSwitch = true; + midTime = GAME_COUNT_TIME; + gameTimer(); + } + } + initView(); + initListener(); + resetImg(); + } + + private void initView() { + String deviceId = DeviceUtils.getDeviceId(); + if (deviceId != null) { + Intent connectPaIntent = new Intent(); + Operation op1 = new Intent.OperationBuilder() + .withDeviceId(deviceId) + .withBundleName(getBundleName()) + .withAbilityName("ohos.samples.tictactoegame.RemoteServiceAbility") + .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE) + .build(); + connectPaIntent.setOperation(op1); + connectAbility(connectPaIntent, conn); + } + + imageBack = (Image) findComponentById(ResourceTable.Id_img_back); + imageBack.setPixelMap(ResourceTable.Media_icon_back_dont_set); + imageBack.setClickedListener(componet -> { + if (isButtonBack) { + isButtonRefresh = false; + countDownSwitch = false; + Intent intent1 = new Intent(); + present(new MainAbilitySlice(),intent1); + } + }); + imageRefresh = (Image) findComponentById(ResourceTable.Id_img_refresh); + imageRefresh.setPixelMap(ResourceTable.Media_icon_refresh_dont_set); + imageRefresh.setClickedListener(component -> { + if (isButtonRefresh) { + reSetStart(); + myProxy.sendCmd(GAME_COM_RESTART); + PkUtils.showTip(StartAbilitySlice.this); + playGame = IS_HOST; + isButtonRefresh = false; + countDownSwitch = true; + midTime = GAME_COUNT_TIME; + gameTimer(); + } + }); + } + + private void initListener() { + image1 = (Image) findComponentById(ResourceTable.Id_img1); + image1.setClickedListener(componet ->buttonClick(image1, BUTTON_IDX_0)); + + image2 = (Image) findComponentById(ResourceTable.Id_img2); + image2.setClickedListener(componet ->buttonClick(image2, BUTTON_IDX_1)); + + image3 = (Image) findComponentById(ResourceTable.Id_img3); + image3.setClickedListener(componet ->buttonClick(image3, BUTTON_IDX_2)); + + image4 = (Image) findComponentById(ResourceTable.Id_img4); + image4.setClickedListener(componet ->buttonClick(image4, BUTTON_IDX_3)); + + image5 = (Image) findComponentById(ResourceTable.Id_img5); + image5.setClickedListener(componet ->buttonClick(image5, BUTTON_IDX_4)); + + image6 = (Image) findComponentById(ResourceTable.Id_img6); + image6.setClickedListener(componet ->buttonClick(image6, BUTTON_IDX_5)); + + image7 = (Image) findComponentById(ResourceTable.Id_img7); + image7.setClickedListener(componet ->buttonClick(image7, BUTTON_IDX_6)); + + image8 = (Image) findComponentById(ResourceTable.Id_img8); + image8.setClickedListener(componet ->buttonClick(image8, BUTTON_IDX_7)); + + image9 = (Image) findComponentById(ResourceTable.Id_img9); + image9.setClickedListener(componet ->buttonClick(image9, BUTTON_IDX_8)); + } + + private static void runImg(Image useImage) { + YoYo.with(Techniques.Swing) + .duration(GAME_DELAY_LONG) + .delay(GAME_DELAY_SHORT) + .playOn(useImage); + } + + private void preInit() { + isButtonBack = false; + isButtonRefresh = false; + countDownSwitch = false; + timer = null; + + pageStatus = STATSUS_OFF; + gameStatus = STATSUS_OFF; + for (int row = 0; row < ROW_NUM; row++) { + for (int col = 0; col < COL_NUM; col++) { + HOST[row][col] = NOT_USE; + ADVERSARY[row][col] = NOT_USE; + } + } + cleanCheckerboard(HOST); + cleanCheckerboard(ADVERSARY); + roPrbar = (RoundProgressBar)findComponentById(ResourceTable.Id_round_progress_bar); + roPrbar.setProgressValue(0); + roPrbar.setProgressHintText("0"); + } + + private static void gameOver() { + playGame = IS_STOP; + countDownSwitch = false; + if (timer != null) { + timer.cancel(); + } + isButtonBack = true; + } + + private void imgUiGenOpe() { + playGame = IS_ADV; + countDownSwitch = false; + timer.cancel(); + + isButtonBack = false; + int ret = drudge(); + if (ret == GAME_WIN) { + myProxy.sendCmd(GAME_COM_LOST); + GameStatusUtils.showWin(this); + isButtonBack = true; + imageBack.setPixelMap(ResourceTable.Media_icon_back); + playGame = IS_STOP; + } else if (ret == GAME_DOGFALL) { + myProxy.sendCmd(GAME_COM_DOGFALL); + GameStatusUtils.showTie(this); + isButtonBack = true; + imageBack.setPixelMap(ResourceTable.Media_icon_back); + } + myProxy.sendCmd(GAME_COM_GAME_START); + } + + private static void backEventGenOpe() { + isButtonRefresh = false; + isButtonBack = false; + countDownSwitch = true; + playGame = IS_HOST; + gameStatus = STATSUS_OFF; + midTime = GAME_COUNT_TIME; + gameTimer(); + } + + private static final EventRunner EVENT_RUNNER = EventRunner.current(); + private static final EventHandler EVENT_HANDLER = new EventHandler(EVENT_RUNNER) { + private void dealButtonClick(int idx) { + int row = idx / ROW_NUM; + int column = idx % COL_NUM; + Image[] imageButton = {image1, image2, image3, image4, image5, image6, image7, image8, image9}; + + imageButton[idx].setPixelMap(advImg); + ADVERSARY[row][column] = USE; + backEventGenOpe(); + } + + @Override + protected void processEvent(InnerEvent event) { + super.processEvent(event); + + if (event.eventId >= GAME_COM_CLICK1 && event.eventId <= GAME_COM_CLICK9) { + dealButtonClick(event.eventId - GAME_COM_CLICK1); + return; + } + + switch (event.eventId) { + case GAME_COM_GAME_START: + gameStatus = STATSUS_ON; + break; + case GAME_COM_RESTART: + if (isButtonBack) { + reSetStart(); + PkUtils.showTip(StartAbilitySlice.image1.getContext()); + } + imageBack.setPixelMap(ResourceTable.Media_icon_back_dont_set); + playGame = IS_ADV; + break; + case GAME_COM_LOST: + GameStatusUtils.showFaild(StartAbilitySlice.image1.getContext()); + isButtonRefresh = true; + imageRefresh.setPixelMap(ResourceTable.Media_icon_refresh); + isButtonBack = true; + imageBack.setPixelMap(ResourceTable.Media_icon_back); + gameOver(); + break; + case GAME_COM_WIN: + GameStatusUtils.showWin(StartAbilitySlice.image1.getContext()); + isButtonBack = true; + imageBack.setPixelMap(ResourceTable.Media_icon_back); + gameOver(); + break; + case GAME_COM_EXIT: + countDownSwitch = true; + break; + case GAME_COM_DOGFALL: + isButtonRefresh = true; + imageRefresh.setPixelMap(ResourceTable.Media_icon_refresh); + isButtonBack = true; + imageBack.setPixelMap(ResourceTable.Media_icon_back); + GameStatusUtils.showTie(StartAbilitySlice.image1.getContext()); + gameOver(); + break; + default: + } + } + }; + + @Override + public void onActive() { + super.onActive(); + } + + @Override + public void onForeground(Intent intent) { + super.onForeground(intent); + } + + public static EventHandler getHandler() { + return EVENT_HANDLER; + } + + private static void resetImg() { + image1.setPixelMap(resetImg); + image2.setPixelMap(resetImg); + image3.setPixelMap(resetImg); + image4.setPixelMap(resetImg); + image5.setPixelMap(resetImg); + image6.setPixelMap(resetImg); + image7.setPixelMap(resetImg); + image8.setPixelMap(resetImg); + image9.setPixelMap(resetImg); + } + + private static void reSetStart() { + isButtonBack = false; + resetImg(); + cleanCheckerboard(HOST); + cleanCheckerboard(ADVERSARY); + imageRefresh.setPixelMap(ResourceTable.Media_icon_refresh_dont_set); + imageBack.setPixelMap(ResourceTable.Media_icon_back_dont_set); + gameStatus = STATSUS_ON; + } + + private int drudge() { + for (int i = 0; i < ROW_NUM; i++) { + if (HOST[i][BUTTON_IDX_0] == USE && HOST[i][BUTTON_IDX_1] == USE && + HOST[i][BUTTON_IDX_2] == USE) { + return GAME_WIN; + } + } + for (int i = 0; i < COL_NUM; i++) { + if (HOST[BUTTON_IDX_0][i] == USE && HOST[BUTTON_IDX_1][i] == USE && + HOST[BUTTON_IDX_2][i] == USE) { + return GAME_WIN; + } + } + if (HOST[BUTTON_IDX_0][BUTTON_IDX_0] == USE && HOST[BUTTON_IDX_1][BUTTON_IDX_1] == USE && + HOST[BUTTON_IDX_2][BUTTON_IDX_2] == USE) { + return GAME_WIN; + } + if (HOST[BUTTON_IDX_0][BUTTON_IDX_2] == USE && HOST[BUTTON_IDX_1][BUTTON_IDX_1] == USE && + HOST[BUTTON_IDX_2][BUTTON_IDX_0] == USE) { + return GAME_WIN; + } + + int useCount = 0; + for (int horl = 0; horl < ROW_NUM; horl++) { + for (int verl = 0; verl < COL_NUM; verl++) { + if (HOST[horl][verl] == USE) { + useCount++; + } + } + } + if (useCount < HALF_GRID) { + return GAME_LOSE; + } else { + return GAME_DOGFALL; + } + } + + private static void cleanCheckerboard(int[][] array) { + for (int horl = 0; horl < ROW_NUM; horl++) { + for (int verl = 0; verl < COL_NUM; verl++) { + array[horl][verl] = NOT_USE; + } + } + } + + private static void gameTimer() { + timer = new Timer(); + timer.schedule(new TimerTask() { + public void run() { + midTime--; + if (!countDownSwitch) { + myTaskDis.asyncDispatch(()-> roPrbar.setProgressValue(0)); + timer.cancel(); + return; + } + if (midTime >= 0) { + runImg(imageMe); + myTaskDis.asyncDispatch(()-> { + roPrbar.setProgressValue(midTime); + String sTime = String.valueOf(midTime); + roPrbar.setProgressHintText(sTime); + }); + } else { + timer.cancel(); + myTaskDis.asyncDispatch(() -> { + GameStatusUtils.showFaild(StartAbilitySlice.image1.getContext()); + isButtonRefresh = true; + imageRefresh.setPixelMap(ResourceTable.Media_icon_refresh); + isButtonBack = true; + imageBack.setPixelMap(ResourceTable.Media_icon_back); + gameOver(); + if (myProxy != null) { + myProxy.sendCmd(GAME_COM_WIN); + } + }); + } + } + }, 0, GAME_DELAY_LONG); + } +} diff --git a/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/ui/DeviceSelectDialog.java b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/ui/DeviceSelectDialog.java new file mode 100644 index 0000000000..67ac403927 --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/ui/DeviceSelectDialog.java @@ -0,0 +1,155 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd.All rights reserved. + * Licensed under the Apache License,Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.tictactoegame.ui; + +import ohos.agp.components.Component; +import ohos.agp.components.LayoutScatter; +import ohos.agp.components.ListContainer; +import ohos.agp.components.Text; +import ohos.agp.utils.LayoutAlignment; +import ohos.agp.window.dialog.CommonDialog; +import ohos.app.Context; +import ohos.distributedschedule.interwork.DeviceInfo; +import ohos.samples.tictactoegame.ResourceTable; +import ohos.samples.tictactoegame.data.mode.DeviceData; +import ohos.samples.tictactoegame.ui.listcomponent.CommentViewHolder; +import ohos.samples.tictactoegame.ui.listcomponent.ListComponentAdapter; +import ohos.samples.tictactoegame.utils.DeviceUtils; + +import java.util.ArrayList; +import java.util.List; + +import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT; +import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT; + +/** + * DeviceSelectDialog + */ +public class DeviceSelectDialog extends CommonDialog { + private static final int RADIO_SIZE = 10; + + private DeviceInfo mCheckedDevice; + private final List mDeviceList = new ArrayList<>(); + private ListComponentAdapter listComponentAdapter; + + /** + * OnclickListener + */ + public interface OnclickListener { + void onYesClick(DeviceInfo deviceInfo); + } + + private final Context mContext; + private OnclickListener mOnclickListener; + + /** + * DeviceSelectDialog + * + * @param context the context + */ + public DeviceSelectDialog(Context context) { + super(context); + this.mContext = context; + } + + /** + * setListener + * + * @param listener the listener + */ + public void setListener(OnclickListener listener) { + mOnclickListener = listener; + } + + private void initDeviceList() { + List deviceInfoList = DeviceUtils.getRemoteDevice(); + mDeviceList.clear(); + for (DeviceInfo deviceInfo : deviceInfoList) { + mDeviceList.add(new DeviceData(false, deviceInfo)); + } + if (deviceInfoList.size() > 0) { + mCheckedDevice = deviceInfoList.get(0); + } + } + + private void initUi(Component rootView) { + listComponentAdapter = new ListComponentAdapter(mContext, + mDeviceList, ResourceTable.Layout_dialog_device_item) { + @Override + public void onBindViewHolder(CommentViewHolder commonViewHolder, DeviceData item, int position) { + commonViewHolder.getTextView(ResourceTable.Id_item_desc).setText(item.getDeviceInfo().getDeviceName()); + switch (item.getDeviceInfo().getDeviceType()) { + case SMART_PHONE: + commonViewHolder.getImageView(ResourceTable.Id_item_type).setPixelMap( + ResourceTable.Media_dv_phone); + break; + case SMART_PAD: + commonViewHolder.getImageView(ResourceTable.Id_item_type).setPixelMap( + ResourceTable.Media_dv_pad); + break; + case SMART_WATCH: + commonViewHolder.getImageView(ResourceTable.Id_item_type).setPixelMap( + ResourceTable.Media_dv_watch); + break; + } + commonViewHolder.getImageView(ResourceTable.Id_item_check).setPixelMap( + item.isChecked() ? ResourceTable.Media_checked_point : ResourceTable.Media_uncheck_point); + } + + @Override + public void onItemClick(Component component, DeviceData item, int position) { + super.onItemClick(component, item, position); + for (DeviceData deviceData : mDeviceList) { + deviceData.setChecked(false); + } + mDeviceList.get(position).setChecked(true); + listComponentAdapter.notifyDataChanged(); + mCheckedDevice = item.getDeviceInfo(); + } + }; + + ListContainer mContainer = (ListContainer) rootView.findComponentById( + ResourceTable.Id_list_container_device); + mContainer.setItemProvider(listComponentAdapter); + + Text operateYes = (Text) rootView.findComponentById(ResourceTable.Id_operate_yes); + operateYes.setClickedListener(component -> { + if (mOnclickListener != null && mCheckedDevice != null) { + mOnclickListener.onYesClick(mCheckedDevice); + } + }); + Text operateNo = (Text) rootView.findComponentById(ResourceTable.Id_operate_no); + operateNo.setClickedListener(component -> hide()); + } + + @Override + protected void onCreate() { + super.onCreate(); + + Component rootView = LayoutScatter.getInstance(mContext).parse( + ResourceTable.Layout_dialog_layout_device, null, false); + + initDeviceList(); + initUi(rootView); + + setSize(MATCH_PARENT, MATCH_CONTENT); + setAlignment(LayoutAlignment.BOTTOM); + setCornerRadius(RADIO_SIZE); + setAutoClosable(true); + setContentCustomComponent(rootView); + setTransparent(true); + } +} diff --git a/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/ui/listcomponent/CommentViewHolder.java b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/ui/listcomponent/CommentViewHolder.java new file mode 100644 index 0000000000..1c600865fb --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/ui/listcomponent/CommentViewHolder.java @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd.All rights reserved. + * Licensed under the Apache License,Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.tictactoegame.ui.listcomponent; + +import ohos.agp.components.Component; +import ohos.agp.components.Image; +import ohos.agp.components.LayoutScatter; +import ohos.agp.components.Text; +import ohos.app.Context; + +import java.util.HashMap; +import java.util.Map; + +/** + * CommentViewHolder + */ +public class CommentViewHolder { + private final Component convertView; + private final Map mViews = new HashMap<>(); + + /** + * CommentViewHolder + * + * @param convertView the component view + */ + public CommentViewHolder(Component convertView) { + this.convertView = convertView; + convertView.setTag(this); + } + + /** + * CommentViewHolder + * + * @param context the context + * @param convertView the component view + * @param resource the resource id + * @return CommentViewHolder + */ + public static CommentViewHolder getCommentViewHolder(Context context, Component convertView, int resource) { + if (convertView == null) { + convertView = LayoutScatter.getInstance(context).parse(resource, null, false); + return new CommentViewHolder(convertView); + } else { + return (CommentViewHolder) convertView.getTag(); + } + } + + private T getView(int resId) { + Component view = mViews.get(resId); + if (view == null) { + view = convertView.findComponentById(resId); + mViews.put(resId, view); + } + return (T) view; + } + + /** + * getTextView + * + * @param resId the resource id + * @return Text widget + */ + public Text getTextView(int resId) { + return getView(resId); + } + + /** + * getImageView + * + * @param resId the resource id + * @return Image widget + */ + public Image getImageView(int resId) { + return getView(resId); + } + + /** + * getConvertView + * + * @return Component + */ + public Component getConvertView() { + return convertView; + } +} diff --git a/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/ui/listcomponent/ListComponentAdapter.java b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/ui/listcomponent/ListComponentAdapter.java new file mode 100644 index 0000000000..ca0c2c3864 --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/ui/listcomponent/ListComponentAdapter.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd.All rights reserved. + * Licensed under the Apache License,Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.tictactoegame.ui.listcomponent; + +import ohos.agp.components.Component; +import ohos.agp.components.ComponentContainer; +import ohos.agp.components.RecycleItemProvider; +import ohos.app.Context; + +import java.util.List; + +/** + * ListComponentAdapter class + * + * @param params type + */ +public abstract class ListComponentAdapter extends RecycleItemProvider { + private final Context mContext; + private final List mListBean; + private final int mXmlId; + + /** + * ListComponentAdapter + * + * @param context the context + * @param list the list + * @param xmlId the xml file id + */ + public ListComponentAdapter(Context context, List list, int xmlId) { + this.mContext = context; + this.mListBean = list; + this.mXmlId = xmlId; + } + + /** + * onBindViewHolder + * + * @param commonViewHolder the adapter holder + * @param item the item in the holder + * @param position the item position + */ + public abstract void onBindViewHolder(CommentViewHolder commonViewHolder, T item, int position); + + @Override + public int getCount() { + return mListBean.size(); + } + + @Override + public T getItem(int idx) { + return mListBean.get(idx); + } + + @Override + public long getItemId(int idx) { + return idx; + } + + @Override + public Component getComponent(int i, Component component, ComponentContainer componentContainer) { + CommentViewHolder commentViewHolder = CommentViewHolder.getCommentViewHolder(mContext, component, mXmlId); + T item = mListBean.get(i); + + onBindViewHolder(commentViewHolder, item, i); + commentViewHolder.getConvertView().setClickedListener(component1 -> onItemClick(component, item, i)); + return commentViewHolder.getConvertView(); + } + + public void onItemClick(Component component, T item, int position) { + } +} \ No newline at end of file diff --git a/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/DeviceUtils.java b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/DeviceUtils.java new file mode 100644 index 0000000000..c6d974b0d7 --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/DeviceUtils.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd.All rights reserved. + * Licensed under the Apache License,Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.tictactoegame.utils; + +import ohos.distributedschedule.interwork.DeviceInfo; +import ohos.distributedschedule.interwork.DeviceManager; + +import java.util.List; + +/** + * DeviceUtils + */ +public class DeviceUtils { + private DeviceUtils() {} + + /** + * Get remote device info + * + * @return Remote device info list. + */ + public static List getRemoteDevice() { + return DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE); + } + + /** + * getDeviceId + * + * @return java.lang.String + */ + public static String getDeviceId() { + String deviceId = ""; + List list = getRemoteDevice(); + if (list == null || list.size() == 0) { + return deviceId; + } + + deviceId = list.get(0).getDeviceId(); + + return deviceId; + } +} diff --git a/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/GameRulesUtils.java b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/GameRulesUtils.java new file mode 100644 index 0000000000..7eeb7fe136 --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/GameRulesUtils.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd.All rights reserved. + * Licensed under the Apache License,Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.tictactoegame.utils; + +import ohos.agp.components.Component; +import ohos.agp.components.LayoutScatter; +import ohos.agp.components.Text; +import ohos.agp.utils.LayoutAlignment; +import ohos.agp.window.dialog.CommonDialog; +import ohos.app.Context; +import ohos.samples.tictactoegame.ResourceTable; + +import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT; +import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT; + +/** + * GameRulesUtils + */ +public class GameRulesUtils extends CommonDialog { + private static final int RADIO_SIZE = 10; + + private final Context mContext; + + /** + * create GameRulesUtils + * + * @param context the context + */ + public GameRulesUtils(Context context) { + super(context); + this.mContext = context; + } + + @Override + protected void onCreate() { + super.onCreate(); + Component rootView = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_dialog_game_rule, + null, false); + Text operateYes = (Text) rootView.findComponentById(ResourceTable.Id_rule_yes); + operateYes.setClickedListener(component -> hide()); + setSize(MATCH_PARENT, MATCH_CONTENT); + setAlignment(LayoutAlignment.BOTTOM); + setCornerRadius(RADIO_SIZE); + setAutoClosable(true); + setContentCustomComponent(rootView); + setTransparent(true); + } +} diff --git a/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/GameStatusUtils.java b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/GameStatusUtils.java new file mode 100644 index 0000000000..cdaa598379 --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/GameStatusUtils.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd.All rights reserved. + * Licensed under the Apache License,Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.tictactoegame.utils; + +import ohos.agp.components.DirectionalLayout; +import ohos.agp.components.Image; +import ohos.agp.components.LayoutScatter; +import ohos.agp.utils.LayoutAlignment; +import ohos.agp.window.dialog.ToastDialog; +import ohos.app.Context; +import ohos.samples.tictactoegame.ResourceTable; + +/** + * Game Status Utils + */ +public class GameStatusUtils { + private static final int DIS_FAILED = 0; + private static final int DIS_WIN = 1; + private static final int DIS_TIE = 3; + + private static final int SHOW_TIMES = 1000; + + /** + * showTip + * + * @param context the context + * @param durationTime show times + * @param flag image type + */ + public static void showTip(Context context, int durationTime, int flag) { + DirectionalLayout toastLayout = (DirectionalLayout) LayoutScatter.getInstance(context) + .parse(ResourceTable.Layout_game_status, null, false); + + Image img = (Image) toastLayout.findComponentById(ResourceTable.Id_img_gameStatus); + if (flag == DIS_FAILED) { + img.setPixelMap(ResourceTable.Media_game_fail); + } + if (flag == DIS_WIN) { + img.setPixelMap(ResourceTable.Media_game_win); + } + if (flag == DIS_TIE) { + img.setPixelMap(ResourceTable.Media_game_tie); + } + new ToastDialog(context) + .setComponent(toastLayout) + .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT) + .setAlignment(LayoutAlignment.CENTER) + .setDuration(durationTime) + .show(); + } + + /** + * showWin + * + * @param context the context + */ + public static void showWin(Context context) { + showTip(context, SHOW_TIMES, DIS_WIN); + } + + /** + * showFaild + * + * @param context the context + */ + public static void showFaild(Context context) { + showTip(context, SHOW_TIMES, DIS_FAILED); + } + + /** + * showTie + * + * @param context the context + */ + public static void showTie(Context context) { + showTip(context, SHOW_TIMES, DIS_TIE); + } +} + diff --git a/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/PkUtils.java b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/PkUtils.java new file mode 100644 index 0000000000..6502bba42a --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/PkUtils.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd.All rights reserved. + * Licensed under the Apache License,Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.tictactoegame.utils; + +import ohos.agp.components.DirectionalLayout; +import ohos.agp.components.LayoutScatter; +import ohos.agp.utils.LayoutAlignment; +import ohos.agp.window.dialog.ToastDialog; +import ohos.app.Context; +import ohos.samples.tictactoegame.ResourceTable; + +/** + * PkUtils + */ +public class PkUtils { + public static void showTip(Context context) { + DirectionalLayout toastLayout = (DirectionalLayout) LayoutScatter.getInstance(context) + .parse(ResourceTable.Layout_layout_toast, null, false); + new ToastDialog(context) + .setComponent(toastLayout) + .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT) + .setAlignment(LayoutAlignment.CENTER) + .show(); + } +} diff --git a/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/UiUtils.java b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/UiUtils.java new file mode 100644 index 0000000000..63ac9b7a34 --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/java/ohos/samples/tictactoegame/utils/UiUtils.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd.All rights reserved. + * Licensed under the Apache License,Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.tictactoegame.utils; + +import ohos.agp.utils.LayoutAlignment; +import ohos.agp.window.dialog.ToastDialog; +import ohos.app.Context; + +/** + * UiUtils + */ +public class UiUtils { + public static void showTip(Context context, String msg) { + ToastDialog toastDialog = new ToastDialog(context); + toastDialog.setText(msg); + toastDialog.setAlignment(LayoutAlignment.CENTER); + toastDialog.show(); + } +} \ No newline at end of file diff --git a/CompleteApps/TicTacToeGame/entry/src/main/resources/base/element/string.json b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000..ebd78545d6 --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/element/string.json @@ -0,0 +1,52 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "TicTacToeGame" + }, + { + "name": "mainability_description", + "value": "Java_Empty Ability" + }, + { + "name": "mainability_HelloWorld", + "value": "Hello World" + }, + { + "name": "remoteserviceability_description", + "value": "hap sample empty service" + }, + { + "name": "title_label", + "value": "Tic tac toe battle" + }, + { + "name": "game_rules", + "value": "Rules" + }, + { + "name": "game_start_label", + "value": "Start" + }, + { + "name": "rules_info", + "value": "Two players, one with a circle (o) and one with a cross (x), take turns to play their own symbols on the grid of 3 by 3. If they are first connected into a line horizontally, vertically and obliquely, they will win. If both sides play correctly, they will be drawn. This game is actually controlled by the first player. The first player is attacking and the second player is defending." + }, + { + "name": "rules_info1", + "value": "If the first player is the first player in the corner position, he will win the most. If the second player is in the side position, the first player can tie down the second player with two wires, and then create \"two snakes\". People who have played this game will find that if both players make the best choice, the game will be tied. Therefore, tic tac toe chess is most often used as a children's game." + }, + { + "name": "confirm_label", + "value": "Confirm" + }, + { + "name": "cancel_label", + "value": "Cancel" + }, + { + "name": "add_device_label", + "value": "Add game device" + } + ] +} \ No newline at end of file diff --git a/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_ability_main.xml b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_ability_main.xml new file mode 100644 index 0000000000..44fb138779 --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_ability_main.xml @@ -0,0 +1,20 @@ + + + + + \ No newline at end of file diff --git a/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_button_radius_.xml b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_button_radius_.xml new file mode 100644 index 0000000000..fb21cf38be --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_button_radius_.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_game_rule.xml b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_game_rule.xml new file mode 100644 index 0000000000..11372f74fe --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_game_rule.xml @@ -0,0 +1,22 @@ + + + + + + \ No newline at end of file diff --git a/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_rule_yes.xml b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_rule_yes.xml new file mode 100644 index 0000000000..9d2236d1ef --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_rule_yes.xml @@ -0,0 +1,22 @@ + + + + + + \ No newline at end of file diff --git a/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_toast_element.xml b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_toast_element.xml new file mode 100644 index 0000000000..a9cf13cafc --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_toast_element.xml @@ -0,0 +1,22 @@ + + + + + + \ No newline at end of file diff --git a/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_white_radius_10.xml b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_white_radius_10.xml new file mode 100644 index 0000000000..9b066afd70 --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/graphic/background_white_radius_10.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/CompleteApps/TicTacToeGame/entry/src/main/resources/base/layout/ability_main.xml b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/layout/ability_main.xml new file mode 100644 index 0000000000..f1d2135e5c --- /dev/null +++ b/CompleteApps/TicTacToeGame/entry/src/main/resources/base/layout/ability_main.xml @@ -0,0 +1,85 @@ + + + + + + + +