如何解决Dart - 如何以一定的难度开始我的井字游戏简单
我正在使用 flutter/dart 开发一款井字游戏。我设置了 3 个不同的难度,EASY、MEDIUM、HARD。当用户开始井字游戏时,我希望它立即以 EASY 难度设置开始,而不必选择简单。我该怎么做?下面的代码是我对不同难度和设置难度的简单对话框的逻辑。但是如何在默认模式下以 EASY 模式启动游戏?
displayMessage(computerPlayer + " MOVED,X's TURN");
if (_gameChoice == 1) {
getRandomMove();
}
if (_gameChoice == 2) {
if (winningCheck == 0) {
getWinningMove();
}
if (winningCheck == 0) {
getRandomMove();
}
}
if (_gameChoice == 3) {
if (winningCheck == 0) {
getWinningMove();
}
if (winningCheck == 0 && blockingCheck == 0) {
getBlockingMove();
}
if (winningCheck == 0 && blockingCheck == 0) {
getRandomMove();
}
}
}
// Get winning move
void getWinningMove() {
// First see if there's a move O can make to win
for (int i = 0; i < boardSize; i++) {
if (_mBoard[i] != humanPlayer && _mBoard[i] != computerPlayer) {
String curr = _mBoard[i];
_mBoard[i] = computerPlayer;
print(computerPlayer + " made a winning move to " + "$i");
if (checkWinner() == 3) {
winningCheck = 1;
return;
} else
_mBoard[i] = curr;
winningCheck = 0;
}
}
}
// Get Blocking move
void getBlockingMove() {
for (int i = 0; i < boardSize; i++) {
if (_mBoard[i] != humanPlayer && _mBoard[i] != computerPlayer) {
String curr = _mBoard[i]; // Save the current number
_mBoard[i] = humanPlayer;
if (checkWinner() == 2) {
_mBoard[i] = computerPlayer;
blockingCheck = 1;
print(computerPlayer + " made a blocking move to" + ' $i');
return;
} else
_mBoard[i] = curr;
blockingCheck = 0;
}
}
}
// Get random move
void getRandomMove() {
var move;
// Generate random move
var count = 0;
do {
count = count + 1;
move = rnd.nextInt(9);
print("Computer random move is " + '$move');
} while (
(_mBoard[move] == humanPlayer || _mBoard[move] == computerPlayer) &&
(count < 9));
if ((_mBoard[move] == humanPlayer) || (_mBoard[move] == computerPlayer)) {
return;
} else {
print("Computer is making a move to " + '$move');
_mBoard[move] = computerPlayer;
return;
}
}
Future _showSimpleDialogue() async {
switch (await showDialog(
context: context,/*it shows a popup with few options which you can select,for option we
created enums which we can use with switch statement,in this first switch
will wait for the user to select the option which it can use with switch cases*/
builder: (BuildContext context) {
return SimpleDialog(
title: Text('Select the Difficulty Level'),children: <Widget>[
SimpleDialogOption(
child: Text('Easy'),onPressed: () {
Navigator.pop(context,Choice.EASY);
computerTurn();
},),new SimpleDialogOption(
child: Text('Medium'),Choice.MEDIUM);
computerTurn();
},new SimpleDialogOption(
child: Text('Hard'),Choice.HARD);
computerTurn();
},],);
},)) {
case Choice.EASY:
_gameChoice = 1;
break;
case Choice.MEDIUM:
_gameChoice = 2;
break;
case Choice.HARD:
_gameChoice = 3;
break;
}
print('The selection was Choice = ' + '$_gameChoice');
}```
解决方法
一个解决方案是使用 StatefulWidget。它的状态对象会将 _gameChoice 初始化为 Choice.EASY,如果用户选择另一个难度级别,它的值可以稍后更新。
class TicTacToe extends StatefulWidget {
@override
_TicTacToeState createState() => _TicTacToeState();
}
class _TicTacToeState extends State<TicTacToe> {
var difficulty = Choice.EASY;
@override
Widget build(BuildContext context) {
return ...
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。