import { promptAction } from '@kit.ArkUI'; @Entry @Component struct HjcbPage { @State message: string = '老虎,鸡 虫子 棒子 '; @State userCz: string = '' //用户出招 @State comCz: string = '' //电脑 @State res: string = '' //结果 @State uY: number = 0 //用户赢多少次 @State cY: number = 0 //电脑赢多少次 @State pY: number = 0 //平局多少次 @State isj: boolean = false //选中状态 @State iss: boolean = false //选中状态 @State isb: boolean = false //选中状态 @State isz: boolean = false //选中状态 build() { Column() { Text(this.message).fontSize(30).fontColor(Color.Red) Row() { Radio({ value: '老虎', group: 'gm' }) .onChange((isChecked: boolean) => { promptAction.showToast({ message: `选中状态:${isChecked}` }) this.userCz = '老虎' this.isj = isChecked }) .checked(this.isj) Text('老虎').fontSize(30) Radio({ value: '鸡', group: 'gm' }) .onChange((isChecked: boolean) => { promptAction.showToast({ message: `选中状态:${isChecked}` }) this.userCz = '鸡' this.iss = isChecked }) .checked(this.iss) Text('鸡').fontSize(30) Radio({ value: '虫子', group: 'gm' }) .onChange((isChecked: boolean) => { promptAction.showToast({ message: `选中状态:${isChecked}` }) this.userCz = '虫子' this.isb = isChecked }) .checked(this.isb) Text('虫子').fontSize(30) Radio({ value: '棒子', group: 'gm' }) .onChange((isChecked: boolean) => { promptAction.showToast({ message: `选中状态:${isChecked}` }) this.userCz = '棒子' this.isz = isChecked }) .checked(this.isz) Text('棒子').fontSize(30) } .justifyContent(FlexAlign.SpaceAround) .height(90) Text(`电脑:${this.comCz}`).width('100%').backgroundColor(Color.Blue).fontSize(30) Text(`胜利者${this.res}`).fontSize(30).fontColor('red') Row() { Button('重新开始') .onClick(() => { //清零 this.userCz = '' this.comCz = '' this.res = '' this.uY = 0 this.cY = 0 this.pY = 0 //初始化选项 this.isj = false this.iss = false this.isb = false this.isz= false }) Button('电脑出招') .onClick(() => { if (this.userCz !== '') { let num: number = parseInt(`${Math.random() * 4}`) switch (num) { case 0: this.comCz = '老虎' break case 1: this.comCz = '鸡' break case 3: this.comCz = '虫子' break case 4: this.comCz = '棒子' break } //1,电脑赢 if ( (this.comCz == '老虎' && this.userCz == '鸡') || (this.comCz == '鸡' && this.userCz == '虫子') || (this.comCz == '虫子' && this.userCz == '棒子')|| (this.comCz == '棒子' && this.userCz == '老虎') ) { this.res = '电脑' this.cY++ } else if ( (this.comCz == '棒子' && this.userCz == '虫子') || (this.comCz == '鸡' && this.userCz == '老虎') || (this.comCz == '虫子' && this.userCz == '鸡')|| (this.comCz == '老虎' && this.userCz == '棒子') ) { //2,用户赢 this.res = '用户' this.uY++ } else { //3 平 this.res = '不吃' this.pY++ } }else { promptAction.showToast({ message: '请用户先出招' }) } }) } .width('80%').justifyContent(FlexAlign.SpaceAround) Text(`赢:${this.uY}`).fontSize(30).backgroundColor('red') Text(`败:${this.cY}`).fontSize(30).backgroundColor('red') Text(`没人吃:${this.pY}`).fontSize(30).backgroundColor('red') Text(`总:${this.uY + this.cY + this.pY}`).fontSize(30).backgroundColor('red') Text(`胜率:${this.uY}/${this.uY + this.cY + this.pY}`).fontSize(30).backgroundColor('red') } .height('100%') .width('100%') } }
‘老虎,鸡 虫子 棒子 ‘
最新推荐文章于 2024-09-02 20:28:23 发布