Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.19 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'dart:math';
  3.  
  4. void main() {
  5. runApp(TTTApp());
  6. }
  7.  
  8. class TTTApp extends StatelessWidget {
  9. @override
  10. Widget build(BuildContext context) {
  11. return new MaterialApp(
  12. debugShowCheckedModeBanner: false,
  13. title: 'Tic Tac Toe',
  14. theme: new ThemeData(
  15. primarySwatch: Colors.blue,
  16. ),
  17. home: new MyHomePage(title: 'Tic Tac Toe'),
  18. );
  19. }
  20. }
  21.  
  22.  
  23. class MyHomePage extends StatefulWidget {
  24. MyHomePage({Key key, this.title}) : super(key: key);
  25.  
  26. final String title;
  27.  
  28. @override
  29. _MyHomePageState createState() => _MyHomePageState();
  30. }
  31.  
  32. class _MyHomePageState extends State<MyHomePage> {
  33.  
  34. String _mText = "";
  35.  
  36. // Initial Text for infoLabel
  37. String _text = "X's Turn";
  38.  
  39. // constant characters for each player
  40.  
  41. static const String HUMAN_PLAYER = "X";
  42. static const String COMPUTER_PLAYER = "O";
  43.  
  44. // constant for board size
  45. static const BOARD_SIZE = 9;
  46.  
  47. // Game variables
  48. var gameOver = false;
  49. var win = 0;
  50. var turn = 0;
  51. var _mBoard = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
  52. var rnd = new Random(BOARD_SIZE);
  53.  
  54. // Print board to console
  55.  
  56. void displayBoard() {
  57. print(_mBoard[0] + " | " + _mBoard[1] + " | " + _mBoard[2]);
  58. print("-----------");
  59. print(_mBoard[3] + " | " + _mBoard[4] + " | " + _mBoard[5]);
  60. print("-----------");
  61. print(_mBoard[6] + " | " + _mBoard[7] + " | " + _mBoard[8]);
  62. }
  63.  
  64. // Check board for winner
  65. int checkWinner() {
  66. // Check horizontal wins
  67. for (int i = 0; i <= 6; i += 3) {
  68. if (_mBoard[i] == (HUMAN_PLAYER) &&
  69. _mBoard[i + 1] == (HUMAN_PLAYER) &&
  70. _mBoard[i + 2] == (HUMAN_PLAYER)) return 2;
  71.  
  72. if (_mBoard[i] == (COMPUTER_PLAYER) &&
  73. _mBoard[i + 1] == (COMPUTER_PLAYER) &&
  74. _mBoard[i + 2] == (COMPUTER_PLAYER)) return 3;
  75. }
  76.  
  77. // Check vertical wins
  78. for (int i = 0; i <= 2; i++) {
  79. if (_mBoard[i] == (HUMAN_PLAYER) &&
  80. _mBoard[i + 3] == (HUMAN_PLAYER) &&
  81. _mBoard[i + 6] == (HUMAN_PLAYER)) return 2;
  82.  
  83. if (_mBoard[i] == (COMPUTER_PLAYER) &&
  84. _mBoard[i + 3] == (COMPUTER_PLAYER) &&
  85. _mBoard[i + 6] == (COMPUTER_PLAYER)) return 3;
  86. }
  87.  
  88. // Check for diagonal wins
  89. if ((_mBoard[0] == (HUMAN_PLAYER) &&
  90. _mBoard[4] == (HUMAN_PLAYER) &&
  91. _mBoard[8] == (HUMAN_PLAYER)) ||
  92. (_mBoard[2] == (HUMAN_PLAYER) &&
  93. _mBoard[4] == (HUMAN_PLAYER) &&
  94. _mBoard[6] == (HUMAN_PLAYER))) return 2;
  95.  
  96. if ((_mBoard[0] == (COMPUTER_PLAYER) &&
  97. _mBoard[4] == (COMPUTER_PLAYER) &&
  98. _mBoard[8] == (COMPUTER_PLAYER)) ||
  99. (_mBoard[2] == (COMPUTER_PLAYER) &&
  100. _mBoard[4] == (COMPUTER_PLAYER) &&
  101. _mBoard[6] == (COMPUTER_PLAYER))) return 3;
  102.  
  103. for (int i = 0; i < BOARD_SIZE; i++) {
  104. // If we find a number, then no one has won yet
  105. if (!(_mBoard[i] == (HUMAN_PLAYER)) && !(_mBoard[i] == (COMPUTER_PLAYER)))
  106. return 0;
  107. }
  108.  
  109. // If we make it through the previous loop, all places are taken, so it's a tie*/
  110. return 1;
  111. }
  112.  
  113. void computerTurn() {
  114. var move;
  115. displayMessage(COMPUTER_PLAYER + " MOVED, X's TURN");
  116.  
  117. // First see if there's a move O can make to win
  118. for (int i = 0; i < BOARD_SIZE; i++) {
  119. if (!(_mBoard[i] == (HUMAN_PLAYER)) &&
  120. !(_mBoard[i] == (COMPUTER_PLAYER))) {
  121. String curr = _mBoard[i];
  122. _mBoard[i] = COMPUTER_PLAYER;
  123. _mText = "O";
  124. if (checkWinner() == 3) {
  125. return;
  126. } else
  127. _mBoard[i] = curr;
  128. }
  129. }
  130.  
  131. // See if there's a move O can make to block X from winning
  132. for (int i = 0; i < BOARD_SIZE; i++) {
  133. if (!(_mBoard[i] == (HUMAN_PLAYER)) &&
  134. !(_mBoard[i] == (COMPUTER_PLAYER))) {
  135. String curr = _mBoard[i]; // Save the current number
  136. _mBoard[i] = HUMAN_PLAYER;
  137. if (checkWinner() == 2) {
  138. _mBoard[i] = COMPUTER_PLAYER;
  139. return;
  140. } else
  141. _mBoard[i] = curr;
  142. }
  143. }
  144.  
  145. // Generate random move
  146. var count = 0;
  147. do {
  148. count = count + 1;
  149. move = rnd.nextInt(9);
  150. print("Computer random move is " + '$move');
  151. } while (
  152. (_mBoard[move] == HUMAN_PLAYER || _mBoard[move] == COMPUTER_PLAYER) &&
  153. (count < 9));
  154.  
  155. if ((_mBoard[move] == HUMAN_PLAYER) || (_mBoard[move] == COMPUTER_PLAYER)) {
  156. return;
  157. } else {
  158. print("Computer is making a random moving to " + '$move');
  159.  
  160. _mBoard[move] = COMPUTER_PLAYER;
  161. }
  162. }
  163.  
  164. void displayMessage(String text) {
  165. setState(() {
  166. _text = text;
  167. print(_text);
  168. });
  169. }
  170.  
  171.  
  172. void checkGameOver(int win) {
  173. if (win == 1) {
  174. gameOver = true;
  175. displayMessage("It's a tie.");
  176. } else if (win == 2) {
  177. gameOver = true;
  178. displayMessage(HUMAN_PLAYER + " wins!");
  179. } else if (win == 3) {
  180. gameOver = true;
  181. displayMessage(COMPUTER_PLAYER + " wins!");
  182. }
  183. }
  184.  
  185. void _newGame() {
  186. setState(() {
  187. for (int i = 0; i < BOARD_SIZE; i++) _mBoard[i] = "";
  188. win = 0;
  189. gameOver = false;
  190. turn = 0;
  191. _text = "X's Turn";
  192. });
  193. }
  194.  
  195. void _button0() {
  196. setState(() {
  197. if (_mBoard[0] == HUMAN_PLAYER || _mBoard[0] == COMPUTER_PLAYER) {
  198. // _choice = "X";
  199. print("That space is occupied. Please choose another space.");
  200.  
  201. return;
  202. } else if (win == 0) {
  203. if (turn == 0) {
  204. _mBoard[0] = HUMAN_PLAYER;
  205. _text = HUMAN_PLAYER + " MOVED, O's TURN";
  206.  
  207. print("onTap called. card #0 was pressed");
  208. turn = 1;
  209. _mText = "X";
  210. win = checkWinner();
  211. checkGameOver(win);
  212. displayBoard();
  213. }
  214. if ((turn == 1) && (win == 0)) {
  215. turn = 0;
  216. // _mText1 = "O";
  217. computerTurn();
  218. win = checkWinner();
  219. checkGameOver(win);
  220. displayBoard();
  221. }
  222. }
  223. });
  224. }
  225.  
  226. @override
  227. Widget build(BuildContext context) {
  228. return new Scaffold(
  229. appBar: new AppBar(
  230. leading: new Icon(Icons.grid_on),
  231. title: new Text("Tic Tac Toe"),
  232. actions: <Widget>[
  233. new IconButton(
  234. icon: new Icon(Icons.new_releases),
  235. onPressed: () {
  236. _newGame();
  237. }),
  238.  
  239. body: new Container(
  240. padding: EdgeInsets.all(14.0),
  241. child: Column(
  242. mainAxisAlignment: MainAxisAlignment.start,
  243. children: <Widget>[
  244. Row(
  245. //Start of the 1st Row
  246. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  247. children: <Widget>[
  248. // Button 0 in 1st Row
  249. RaisedButton(
  250. padding: const EdgeInsets.all(20.0),
  251. onPressed: () {
  252. _button0();
  253. },
  254. child: new Text(" ",
  255. textAlign: TextAlign.center,
  256. style: new TextStyle(
  257. fontWeight: FontWeight.bold,
  258. fontSize: 40.0,
  259. fontFamily: 'Roboto',
  260. )),
  261. color: Colors.grey[300],
  262. ),
  263.  
  264. //Button 1 in 1st Row
  265. RaisedButton(
  266. padding: const EdgeInsets.all(20.0),
  267. onPressed: () {
  268. _button1();
  269. },
  270. child: new Text(" ",
  271. textAlign: TextAlign.center,
  272. style: new TextStyle(
  273. fontWeight: FontWeight.bold,
  274. fontSize: 40.0,
  275. fontFamily: 'Roboto',
  276. ))),
  277.  
  278. //Button 2 in 1st Row
  279. RaisedButton(
  280. padding: const EdgeInsets.all(20.0),
  281. onPressed: () {
  282. _button2();
  283. },
  284. child: new Text(" ",
  285. textAlign: TextAlign.center,
  286. style: new TextStyle(
  287. fontWeight: FontWeight.bold,
  288. fontSize: 40.0,
  289. fontFamily: 'Roboto',
  290. ))),
  291. ]),
  292. // end 1st Row
  293.  
  294.  
  295.  
  296. Padding(padding: const EdgeInsets.all(7.0)),
  297.  
  298. Text(
  299. //Info Label
  300. _text,
  301. textAlign: TextAlign.center,
  302. overflow: TextOverflow.ellipsis,
  303. style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25.0),
  304. ),
  305.  
  306. Padding(padding: const EdgeInsets.all(35.0)),
  307.  
  308. Row(
  309. //Start of the New Game button Row
  310. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  311. children: <Widget>[
  312. // New Game button
  313. new RaisedButton(
  314. padding:
  315. const EdgeInsets.fromLTRB(50.0, 40.0, 50.0, 40.0),
  316. color: Colors.grey[400],
  317. onPressed: () {
  318. _newGame();
  319. },
  320. child: new Text("New Game",
  321. textAlign: TextAlign.center,
  322. style: new TextStyle(
  323. color: Colors.black,
  324. fontWeight: FontWeight.bold,
  325. fontSize: 20.0,
  326. fontFamily: 'Roboto',
  327. ))),
  328. ])
  329. ])),
  330. );
  331. }
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement