?????function?myTrim(str)?{
????????????//如果是null或者undefind直接返回空字符串
????????????if?(!str)?{
????????????????return?''
????????????}?else?{
????????????????return?str.trim()
????????????}
????????}
????????//?console.log(myTrim(null));
????????//?console.log(myTrim('??11111???'));
????????//?let?str?='???????z??z?z???'
????????//?console.log(str.trim());?//去除字符串前后空格
????????//?console.log(str);?//去除字符串前后空格
????????let?game?=?{
????????????//敏感詞數(shù)組
????????????sensitiveWord:?['CNM',?'QNM',?'NMSL'],
????????????//使用數(shù)組模擬用戶數(shù)據(jù)庫(kù)
????????????userList:?[],
????????????//寵物數(shù)組
????????????petlist:?[],
????????????currentuser:?null,
????????????//?petList:[
????????????//?????{
????????????//?????????name:'小黑',
????????????//?????????gender:'GG',
????????????//?????}
????????????//?],
????????????//登錄頁(yè)
????????????showLoginPage()?{
????????????????console.log('歡迎登錄');
????????????????let?userNameIsOk?=?true,
????????????????????passwordIsOk?=?true
????????????????let?password?=?'',
????????????????????userName?=?''
????????????????do?{
????????????????????userName?=?prompt('請(qǐng)輸入用戶名:')
????????????????????if?(!userName)?{
????????????????????????userNameIsOk?=?false
????????????????????????console.log('用戶名不能為空');
????????????????????}?else?{
????????????????????????userNameIsOk?=?true
????????????????????}
????????????????}?while?(!userNameIsOk);
????????????????//?this.petlist=userName
????????????????do?{
????????????????????password?=?prompt('請(qǐng)輸入密碼:')
????????????????????if?(!password)?{
????????????????????????passwordIsOk?=?false
????????????????????????console.log('密碼不能為空');
????????????????????}?else?if?(password.length?<?6)?{
????????????????????????passwordIsOk?=?false
????????????????????????console.log('密碼至少大于6位');
????????????????????}?else?{
????????????????????????passwordIsOk?=?true
????????????????????}
????????????????}?while?(!passwordIsOk);
????????????????this.currentuser=userName
????????????????//拿用戶輸入的用戶名和密碼,判斷是否存在
????????????????let?user?=?this.userList.find(function?(item)?{
????????????????????return?item.userName?===?userName
????????????????})
????????????????if?(!user)?{
????????????????????console.log('此用戶不存在!');
????????????????????arguments.callee.call(this)
????????????????}?else?{
????????????????????//用戶存在,則判斷密碼對(duì)不對(duì)
????????????????????if?(user.password?!==?password)?{
????????????????????????console.log('密碼錯(cuò)誤');
????????????????????????//有錯(cuò)則再次輸入
????????????????????????arguments.callee.call(this)
????????????????????}?else?{
????????????????????????//找到用戶,登錄成功
????????????????????????console.log('login?success');
????????????????????????this.showMainPage()
????????????????????}
????????????????}
????????????},
????????????//領(lǐng)養(yǎng)寵物
????????????showgetpetpage()?{
????????????????console.log('領(lǐng)養(yǎng)寵物');
????????????????let?nickname;
????????????????let?nicknameisok;
????????????????let?type;
????????????????let?typeisok;
????????????????let?gender;
????????????????let?genderisok
????????????????let?currentuser?=?this.currentuser
????????????????let?haspet?=?this.petlist.some(function?(item)?{
????????????????????return?item.master?===?currentuser
????????????????})
????????????????if?(haspet)?{
????????????????????console.log('你已有寵物,不能重復(fù)領(lǐng)取');
????????????????????return?this.showMainPage()
????????????????}
????????????????do?{
????????????????????type?=?myTrim(prompt('請(qǐng)選擇寵物類型:1.狗??2.貓'))
????????????????????if?(!['1',?'2'].includes(type))?{
????????????????????????typeisok?=?false
????????????????????????console.log('選擇的寵物類性不正確');
????????????????????}?else?{
????????????????????????typeisok?=?true
????????????????????}
????????????????}?while?(!type);
????????????????do?{
????????????????????nickname?=?myTrim(prompt('請(qǐng)輸入寵物昵稱'))
????????????????????if?(!nickname)?{
????????????????????????nicknameisok?=?false
????????????????????????console.log('寵物姓名不能為空');
????????????????????}?else?{
????????????????????????let?isSensitive?=?this.sensitiveWord.some(function?(item)?{
????????????????????????????return?nickname.toUpperCase().includes(item.toUpperCase())
????????????????????????})
????????????????????????if?(isSensitive)?{
????????????????????????????nicknameisok?=?false
????????????????????????????console.log('寵物名不合法');
????????????????????????}?else?{
????????????????????????????nicknameisok?=?true
????????????????????????}
????????????????????}
????????????????}?while?(!nicknameisok);
????????????????//?this.petlist.name=nickname
????????????????do?{
????????????????????gender?=?myTrim(prompt('請(qǐng)選擇寵物性別:1.GG?2.MM'))
????????????????????if?(!['1',?'2'].includes(gender))?{
????????????????????????genderisok?=?false
????????????????????}?else?{
????????????????????????genderisok?=?true
????????????????????}
????????????????}?while?(!genderisok);
????????????????console.log(type,?gender,?nickname);
????????????????this.petlist.push({
????????????????????name:?nickname,
????????????????????gender:?gender,
????????????????????type:?type,
????????????????????master:?this.currentuser,
????????????????????state:?80
????????????????})
????????????????//?console.log(petlist);
????????????????//?this.showgetpetpage()
????????????????this.showMainPage()
????????????},
????????????//查看寵物
????????????showpetinfopage()?{
????????????????console.log('查看寵物');
????????????????let?currentuser?=?this.currentuser
????????????????let?mypet?=?this.petlist.find(function?(item)?{
????????????????????return?item.master?===?currentuser
????????????????})
????????????????if?(mypet)?{
????????????????????console.log(`
????????????????????你好,
????????????????????我叫${mypet.name}
????????????????????我是個(gè)${mypet.type==='1'?'狗':'貓'}
????????????????????我的性別是${mypet.gender==='1'?'GG':'MM'}
????????????????????我的主人是${mypet.master}
????????????????????我的狀態(tài)是${mypet.state}
????????????????????`);
????????????????}?else?{
????????????????????console.log('你還沒(méi)有寵物');
????????????????????return?this.showMainPage()
????????????????}
????????????????let?type?=?prompt('請(qǐng)選擇互動(dòng)方式:1.喂食??2.逗TA玩??3.返回主菜單')
????????????????switch?(type)?{
????????????????????case?'1':
????????????????????console.log(mypet.name+'吃東西咯~');
????????????????????mypet.state+=10
????????????????????if(mypet.state>100)?mypet.state=100
????????????????????????break;
????????????????????case?'2':
????????????????????console.log(mypet.name+'做游戲咯~');
????????????????????mypet.state+=5
????????????????????if(mypet.state>100)mypet.state=100
????????????????????????break;
????????????????????case?'3':
???????????????????return?this.showMainPage()
????????????????????default:
????????????????????console.log('輸入有誤,請(qǐng)重新操作');
????????????????????????break;
????????????????}
????????????????arguments.callee.call(this)
????????????},
????????????showMainPage()?{
????????????????let?type?=?prompt('歡迎來(lái)到寵物世界,請(qǐng)選擇操作:?1.領(lǐng)養(yǎng)寵物??2.查看寵物???3.注銷登錄')
????????????????switch?(type)?{
????????????????????case?'1':
????????????????????????this.showgetpetpage()
????????????????????????break;
????????????????????case?'2':
????????????????????????this.showpetinfopage()
????????????????????????break;
????????????????????case?'3':
????????????????????????this.showWelComePage()
????????????????????????break;
????????????????????default:
????????????????????arguments.callee.call(this)
????????????????????????break;
????????????????}
????????????????console.log('這里是游戲主界面');
????????????},
????????????//注冊(cè)頁(yè)
????????????showRegisterPage()?{
????????????????console.log('歡迎注冊(cè)');
????????????????//定義狀態(tài),代表用戶名和密碼驗(yàn)證是否通過(guò)
????????????????let?userNameIsOk?=?true,
????????????????????passwordIsOk?=?true,
????????????????????cPasswordIsOk?=?true
????????????????//在外面定義userName和password,便于后續(xù)使用
????????????????let?password?=?'',
????????????????????userName?=?''
????????????????//因?yàn)楫?dāng)前場(chǎng)景需要先執(zhí)行后判斷,所以采用do-while
????????????????do?{
????????????????????userName?=?myTrim(prompt('請(qǐng)輸入用戶名:'))
????????????????????//如果去完空格是空字符串,則提示,設(shè)置驗(yàn)證為不通過(guò)
????????????????????if?(!userName)?{
????????????????????????userNameIsOk?=?false
????????????????????????console.log('用戶名不能為空');
????????????????????}?else?{
????????????????????????//通過(guò)some方法檢查用戶名是否包含敏感詞,
????????????????????????let?isSensitive?=?this.sensitiveWord.some(function?(item)?{
????????????????????????????//統(tǒng)一設(shè)置為大寫(xiě)再比對(duì),以此忽略大小寫(xiě)
????????????????????????????return?userName.toUpperCase().includes(item.toUpperCase())
????????????????????????})
????????????????????????if?(isSensitive)?{
????????????????????????????userNameIsOk?=?false
????????????????????????????console.log('用戶名不合法');
????????????????????????}?else?{
????????????????????????????//判斷用戶數(shù)組中是否已存在同樣的用戶名
????????????????????????????let?isRepeat?=?this.userList.some(function?(item)?{
????????????????????????????????return?item.userName?===?userName
????????????????????????????})
????????????????????????????if?(isRepeat)?{
????????????????????????????????userNameIsOk?=?false
????????????????????????????????console.log('用戶名已存在');
????????????????????????????}?else?{
????????????????????????????????userNameIsOk?=?true
????????????????????????????}
????????????????????????}
????????????????????}
????????????????}?while?(!userNameIsOk);?//不管是因?yàn)槭裁炊?yàn)證不通過(guò),就繼續(xù)循環(huán)要求重新輸入
????????????????do?{
????????????????????password?=?myTrim(prompt('請(qǐng)輸入密碼:'))
????????????????????if?(!password)?{
????????????????????????passwordIsOk?=?false
????????????????????????console.log('密碼不能為空');
????????????????????}?else?if?(password.length?<?6)?{
????????????????????????passwordIsOk?=?false
????????????????????????console.log('密碼至少大于6位');
????????????????????}?else?{
????????????????????????passwordIsOk?=?true
????????????????????}
????????????????}?while?(!passwordIsOk);
????????????????do?{
????????????????????let?passwordConfirm?=?myTrim(prompt('請(qǐng)輸入確認(rèn)密碼:'))?//將字符串左右去除空格
????????????????????if?(passwordConfirm?!==?password)?{
????????????????????????console.log('兩次密碼輸入不一致');
????????????????????????cPasswordIsOk?=?false
????????????????????}?else?{
????????????????????????cPasswordIsOk?=?true
????????????????????}
????????????????}?while?(!cPasswordIsOk);
????????????????//往用戶數(shù)組中添加一條用戶數(shù)據(jù)
????????????????let?newUser?=?{
????????????????????userName,
????????????????????password
????????????????}
????????????????this.userList.push(newUser)
????????????????console.log('當(dāng)前用戶:',?this.userList);
????????????????//注冊(cè)完成,回到歡迎頁(yè)
????????????????this.showWelComePage()
????????????},
????????????//歡迎頁(yè)
????????????showWelComePage()?{
????????????????let?type?=?prompt('歡迎來(lái)到寵物世界,請(qǐng)選擇操作:1.登錄??2.注冊(cè)??3.退出')
????????????????switch?(type)?{
????????????????????case?'1':
????????????????????????this.showLoginPage()
????????????????????????break;
????????????????????case?'2':
????????????????????????this.showRegisterPage()
????????????????????????break;
????????????????????case?'3':
????????????????????????console.log('退出成功,歡迎再來(lái)');
????????????????????????break;
????????????????????default:
????????????????????????console.log('輸入有誤,請(qǐng)重新操作');
????????????????????????arguments.callee.call(this)//指向本函數(shù),與函數(shù)名無(wú)關(guān)
????????????????????????//?this.showWelComePage()
????????????????????????break;
????????????????}
????????????}
????????}
????????//頁(yè)面初始顯示歡迎頁(yè)
????????game.showWelComePage()
效果如下圖所示:
