Skip to content

Commit

Permalink
refactor: コードをより簡潔に分かりやすいように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
okaryo committed Sep 12, 2023
1 parent bbc0b8e commit 56bc595
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class MyApp extends StatelessWidget {
theme: ThemeData(
primarySwatch: Colors.blue,
),
darkTheme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(
title: const Text('FlutterKaigi2023 TicTacToe'),
Expand Down
18 changes: 11 additions & 7 deletions lib/model/tic_tac_toe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class TicTacToe {
TicTacToe._(this.board, this.currentPlayer);

TicTacToe placeMark(int row, int col) {
if (board[row][col] == '') {
final newBoard = board.map<List<String>>((row) => List.from(row)).toList();
if (board[row][col].isEmpty) {
final newBoard = List.of(board);
newBoard[row][col] = currentPlayer;
String nextPlayer = currentPlayer == 'X' ? 'O' : 'X';

Expand All @@ -25,24 +25,28 @@ class TicTacToe {

String getWinner() {
for (int i = 0; i < 3; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != '') {
// row = i における横の判定
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0].isNotEmpty) {
return board[i][0];
}
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != '') {
// col = i における縦の判定
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i].isNotEmpty) {
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != '') {
// 左上から右下への斜めの判定
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0].isNotEmpty) {
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != '') {
// 右上から左下への斜めの判定
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2].isNotEmpty) {
return board[0][2];
}
return '';
}

bool isDraw() {
return getWinner() == '' && board.every((row) => row.every((cell) => cell != ''));
return getWinner().isEmpty && board.every((row) => row.every((cell) => cell.isNotEmpty));
}

TicTacToe resetBoard() {
Expand Down
11 changes: 6 additions & 5 deletions lib/view/board.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Board extends ConsumerWidget {
padding: const EdgeInsets.all(16),
child: Text(
_statusMessage(ticTacToe),
style: const TextStyle(fontSize: 24),
style: Theme.of(context).textTheme.headlineSmall,
),
),
GridView.builder(
Expand All @@ -35,7 +35,7 @@ class Board extends ConsumerWidget {
return GestureDetector(
onTap: () {
final winner = ticTacToe.getWinner();
if (mark == '' && winner == '') {
if (mark.isEmpty && winner.isEmpty) {
ref.read(ticTacToeProvider.notifier).placeMark(row, col);
}
},
Expand All @@ -53,8 +53,9 @@ class Board extends ConsumerWidget {
);
},
),
Padding(
padding: const EdgeInsets.all(16),
const SizedBox(height: 16),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () {
ref.read(ticTacToeProvider.notifier).resetBoard();
Expand All @@ -71,7 +72,7 @@ class Board extends ConsumerWidget {
final winner = ticTacToe.getWinner();
final isDraw = ticTacToe.isDraw();

if (winner != '') {
if (winner.isNotEmpty) {
return '$winnerの勝ち';
} else if (isDraw) {
return '引き分けです';
Expand Down

0 comments on commit 56bc595

Please sign in to comment.