Flutter로 구현한 Tic Tae Toe 게임
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
// Remove the debug banner | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.amber, | |
), | |
title: 'coolsharp', | |
home: const TicTacToe(), | |
); | |
} | |
} | |
class TicTacToe extends StatefulWidget { | |
const TicTacToe({super.key}); | |
@override | |
State<TicTacToe> createState() => _TicTacToeState(); | |
} | |
class _TicTacToeState extends State<TicTacToe> { | |
final List<List<String>> _board = [ | |
['', '', ''], | |
['', '', ''], | |
['', '', ''] | |
]; | |
String _currentPlayer = 'X'; | |
String _result = 'Tic Tac Toe'; | |
void _play(int row, int col) { | |
setState(() { | |
if(!_checkWin() && '' == _board[row][col]) { | |
_board[row][col] = _currentPlayer; | |
if (_checkWin()) { | |
_result = '$_currentPlayer 승'; | |
} else if (_checkEndGame()) { | |
_result = '비김'; | |
} | |
else { | |
_currentPlayer = _currentPlayer == 'X' ? 'O' : 'X'; | |
_result = '$_currentPlayer 차례'; | |
} | |
} | |
}); | |
} | |
bool _checkWin() { | |
for (int i = 0; i < _board.length; i++) { | |
if (_currentPlayer == _board[i][0] && _currentPlayer == _board[i][1] && _currentPlayer == _board[i][2]) { | |
return true; | |
} | |
if (_currentPlayer == _board[0][i] && _currentPlayer == _board[1][i] && _currentPlayer == _board[2][i]) { | |
return true; | |
} | |
} | |
if (_currentPlayer == _board[0][0] && _currentPlayer == _board[1][1] && _currentPlayer == _board[2][2]) { | |
return true; | |
} | |
if (_currentPlayer == _board[0][2] && _currentPlayer == _board[1][1] && _currentPlayer == _board[2][0]) { | |
return true; | |
} | |
return false; | |
} | |
bool _checkEndGame() { | |
for (int i = 0; i < _board.length; i++) { | |
for (int j = 0; j < _board[0].length; j++) { | |
if ('' == _board[i][j]) { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
void _reset() { | |
for (int i = 0; i < _board.length; i++) { | |
for (int j = 0; j < _board[0].length; j++) { | |
_board[i][j] = ''; | |
} | |
} | |
setState(() { | |
_currentPlayer = 'X'; | |
_result = 'Tic Tac Toe'; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text(_result), | |
actions: [IconButton(onPressed: () {_reset();}, icon: Icon(Icons.refresh))],), | |
body: Column( | |
children: [ | |
Expanded( | |
child: GridView.builder( | |
itemCount: 9, | |
padding: const EdgeInsets.all(10), | |
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( | |
crossAxisCount: 3, | |
childAspectRatio: 1, | |
crossAxisSpacing: 10, | |
mainAxisSpacing: 10), | |
itemBuilder: (context, index) { | |
int row = index ~/ 3; | |
int col = index % 3; | |
Color colFace = Colors.white; | |
Color colBorder = Colors.black; | |
switch (_board[row][col]) { | |
case 'X': | |
colFace = Colors.blue; | |
colBorder = Colors.blueAccent; | |
break; | |
case 'O': | |
colFace = Colors.red; | |
colBorder = Colors.redAccent; | |
break; | |
} | |
return GestureDetector( | |
onTap: () { | |
_play(row, col); | |
print('row : $row, col : $col'); | |
}, | |
child: Container( | |
decoration: BoxDecoration( | |
border: Border.all(width: 2, color: colBorder), | |
color: colFace, | |
boxShadow: const [ | |
BoxShadow( | |
color: Colors.grey, | |
spreadRadius: 2, | |
blurRadius: 3, | |
offset: Offset(0, 5)) | |
]), | |
child: Center( | |
child: Text(_board[row][col]), | |
), | |
), | |
); | |
}), | |
), | |
], | |
), | |
); | |
} | |
} |
댓글
댓글 쓰기