【发布时间】:2021-05-28 07:38:15
【问题描述】:
我正在使用 Dart 制作一个井字游戏,需要一些帮助来为我的游戏实现一个按钮。我不太确定如何开始为井字游戏屏幕上的按钮制作方法。我将如何从第一个按钮方法开始?我打算使用void _button0() {},但由于我是 Dart 新手,所以不知道如何处理第一个。
我还需要为井字游戏的每个按钮制作 9 个单独的方法吗?
import 'package:flutter/material.dart';
import 'dart:math';
class HomePage extends StatefulWidget {
@override
HomePageState createState() => HomePageState();
}
class HomePageState extends State<HomePage> {
// Constant Characters for each player
static const String humanPlayer = '1';
static const String computerPlayer = '2';
// Initial Text for Info Label
String text = "X's Turn";
// Constant for Board Size
static const boardSize = 9;
// Game Variables
var gameOver = false;
var win = 0;
var turn = 0;
var _mBoard = ["", "", "", "", "", "", "", "", ""];
var rnd = new Random(boardSize);
// Button Text Variables?
// Tic Tac Toe Game Code
void displayBoard() {
print("");
print(_mBoard[0] + " | " + _mBoard[1] + " | " + _mBoard[2]);
print("-----------");
print(_mBoard[3] + " | " + _mBoard[4] + " | " + _mBoard[5]);
print("-----------");
print(_mBoard[6] + " | " + _mBoard[7] + " | " + _mBoard[8]);
print("");
}
void checkGameOver(int win) {
print("");
if (win == 1) {
gameOver = true;
displayMessage("It's a tie.");
} else if (win == 2) {
gameOver = true;
displayMessage(humanPlayer + " wins!");
} else if (win == 3) {
gameOver = true;
displayMessage(computerPlayer + " wins!");
} else
displayMessage("There is a logic Problem!");
}
void displayMessage(String text) {
text = text;
print(text);
}
int checkWinner() {
// Check horizontal wins
for (int i = 0; i <= 6; i += 3) {
if (_mBoard[i] == (humanPlayer) &&
_mBoard[i + 1] == (humanPlayer) &&
_mBoard[i + 2] == (humanPlayer)) return 2;
if (_mBoard[i] == (computerPlayer) &&
_mBoard[i + 1] == (computerPlayer) &&
_mBoard[i + 2] == (computerPlayer)) return 3;
}
// Check vertical wins
for (int i = 0; i <= 2; i++) {
if (_mBoard[i] == (humanPlayer) &&
_mBoard[i + 3] == (humanPlayer) &&
_mBoard[i + 6] == (humanPlayer)) return 2;
if (_mBoard[i] == (computerPlayer) &&
_mBoard[i + 3] == (computerPlayer) &&
_mBoard[i + 6] == (computerPlayer)) return 3;
}
// Check for diagonal wins
if ((_mBoard[0] == (humanPlayer) &&
_mBoard[4] == (humanPlayer) &&
_mBoard[8] == (humanPlayer)) ||
(_mBoard[2] == (humanPlayer) &&
_mBoard[4] == (humanPlayer) &&
_mBoard[6] == (humanPlayer))) return 2;
if ((_mBoard[0] == (computerPlayer) &&
_mBoard[4] == (computerPlayer) &&
_mBoard[8] == (computerPlayer)) ||
(_mBoard[2] == (computerPlayer) &&
_mBoard[4] == (computerPlayer) &&
_mBoard[6] == (computerPlayer))) return 3;
for (int i = 0; i < boardSize; i++) {
// If we find a number, then no one has won yet
if (!(_mBoard[i] == (humanPlayer)) && !(_mBoard[i] == (computerPlayer)))
return 0;
}
// If we make it through the previous loop, all places are taken, so it's a tie*/
return 1;
}
void getComputerMove() {
int move;
// 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;
if (checkWinner() == 3) {
print('Computer is moving to ${i + 1}');
return;
} else
_mBoard[i] = curr;
}
}
// See if there's a move O can make to block X from winning
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;
print('Computer is moving to ${i + 1}');
return;
} else
_mBoard[i] = curr;
}
}
// Generate random move
do {
move = rnd.nextInt(boardSize);
} while (_mBoard[move] == humanPlayer || _mBoard[move] == computerPlayer);
print('Computer is moving to ${move + 1}');
_mBoard[move] = computerPlayer;
}
void _button0() {
}
//=====è
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Tic Tac Toe'),
leading: IconButton(
icon: Icon(
Icons.border_all,
semanticLabel: 'menu',
),
onPressed: () {},
),
actions: <Widget>[
new IconButton(
icon: new Icon(Icons.new_releases),
onPressed: () {},
tooltip: 'New Game',
),
IconButton(
icon: new Icon(Icons.refresh),
onPressed: () {},
tooltip: 'Quit Game',
),
PopupMenuButton(itemBuilder: (BuildContext context) {
return [
PopupMenuItem(
child: new GestureDetector(
onTap: () {},
child: new Text("About",
style: TextStyle(
fontWeight: FontWeight.bold,
)),
),
),
PopupMenuItem(
child: new GestureDetector(
onTap: () {
// Some Method
},
child: new Text(
"Settings",
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
),
];
})
]),
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///
///
///
body: Center(
child:
Column(mainAxisAlignment: MainAxisAlignment.start, children: [
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Container(
height: 100,
width: 100,
margin: const EdgeInsets.all(10.0),
child: RaisedButton(
padding: const EdgeInsets.all(10.0),
onPressed: () {
},
child: Text(
"",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 80.0,
fontWeight: FontWeight.bold,
fontFamily: 'Roboto',
),
),
)),
Container(
height: 100,
width: 100,
margin: EdgeInsets.all(10.0),
child: RaisedButton(
padding: const EdgeInsets.all(10.0),
onPressed: () {
},
child: Text(
"",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 80,
fontWeight: FontWeight.bold,
fontFamily: "Roboto"),
),
)),
Container(
height: 100,
width: 100,
margin: EdgeInsets.all(10.0),
child: RaisedButton(
padding: const EdgeInsets.all(10.0),
onPressed: () {},
child: Text(
"",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 80,
fontWeight: FontWeight.bold,
fontFamily: "Roboto"),
),
)),
]),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Container(
height: 100,
width: 100,
margin: const EdgeInsets.all(10.0),
child: RaisedButton(
padding: const EdgeInsets.all(10.0),
onPressed: () {},
child: Text(
"",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 80.0,
fontWeight: FontWeight.bold,
fontFamily: 'Roboto',
),
),
)),
Container(
height: 100,
width: 100,
margin: EdgeInsets.all(10.0),
child: RaisedButton(
padding: const EdgeInsets.all(10.0),
onPressed: () {},
child: Text(
"",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 80,
fontWeight: FontWeight.bold,
fontFamily: "Roboto"),
),
)),
Container(
height: 100,
width: 100,
margin: EdgeInsets.all(10.0),
child: RaisedButton(
padding: const EdgeInsets.all(10.0),
onPressed: () {},
child: Text(
"",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 80,
fontWeight: FontWeight.bold,
fontFamily: "Roboto"),
),
)),
]),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Container(
height: 100,
width: 100,
margin: const EdgeInsets.all(10.0),
child: RaisedButton(
padding: const EdgeInsets.all(10.0),
onPressed: () {},
child: Text(
"",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 80.0,
fontWeight: FontWeight.bold,
fontFamily: 'Roboto',
),
),
)),
Container(
height: 100,
width: 100,
margin: EdgeInsets.all(10.0),
child: RaisedButton(
padding: const EdgeInsets.all(10.0),
onPressed: () {},
child: Text(
"",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 80,
fontWeight: FontWeight.bold,
fontFamily: "Roboto"),
),
)),
Container(
height: 100,
width: 100,
margin: EdgeInsets.all(10.0),
child: RaisedButton(
padding: const EdgeInsets.all(10.0),
onPressed: () {},
child: Text(
"",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 80,
fontWeight: FontWeight.bold,
fontFamily: "Roboto"),
),
)),
]),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 100,
width: 300,
margin: const EdgeInsets.all(10.0),
child: Text(
"X's Turn",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
fontWeight: FontWeight.bold,
fontFamily: 'Roboto',
),
),
)
],
),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Container(
height: 40,
width: 200,
margin: const EdgeInsets.all(10.0),
child: RaisedButton(
onPressed: () {},
child: Text(
"Reset App",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 15.0,
fontWeight: FontWeight.bold,
fontFamily: 'Roboto',
),
),
))
]),
])));
}
}
【问题讨论】:
标签: flutter dart methods tic-tac-toe