-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
♻️ 5章の修正案 #9
Closed
Closed
♻️ 5章の修正案 #9
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f11d485
第5章の初期版実装
okaryo 34e0250
:recycle: フォーマット修正(機能変更なし)
chippy-ao 93c8cc0
:+1: リポジトリをクラス&Provider化(機能変更なし)
chippy-ao 7e78deb
:recycle: streamの名称とProviderの定義方法を修正
chippy-ao 93b4ad4
:sparkles: updateをProvider化
chippy-ao 767482d
:recycle: UIでProvider化したupdateを使用
chippy-ao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"file_generated_by": "FlutterFire CLI", | ||
"purpose": "FirebaseAppID & ProjectID for this Firebase app in this directory", | ||
"GOOGLE_APP_ID": "1:807943088200:ios:125d7bfaa751582d21907d", | ||
"FIREBASE_PROJECT_ID": "tic-tac-toe-handson", | ||
"GCM_SENDER_ID": "807943088200" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// File generated by FlutterFire CLI. | ||
// ignore_for_file: lines_longer_than_80_chars, avoid_classes_with_only_static_members | ||
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions; | ||
import 'package:flutter/foundation.dart' | ||
show defaultTargetPlatform, kIsWeb, TargetPlatform; | ||
|
||
/// Default [FirebaseOptions] for use with your Firebase apps. | ||
/// | ||
/// Example: | ||
/// ```dart | ||
/// import 'firebase_options.dart'; | ||
/// // ... | ||
/// await Firebase.initializeApp( | ||
/// options: DefaultFirebaseOptions.currentPlatform, | ||
/// ); | ||
/// ``` | ||
class DefaultFirebaseOptions { | ||
static FirebaseOptions get currentPlatform { | ||
if (kIsWeb) { | ||
return web; | ||
} | ||
switch (defaultTargetPlatform) { | ||
case TargetPlatform.android: | ||
return android; | ||
case TargetPlatform.iOS: | ||
return ios; | ||
case TargetPlatform.macOS: | ||
return macos; | ||
case TargetPlatform.windows: | ||
throw UnsupportedError( | ||
'DefaultFirebaseOptions have not been configured for windows - ' | ||
'you can reconfigure this by running the FlutterFire CLI again.', | ||
); | ||
case TargetPlatform.linux: | ||
throw UnsupportedError( | ||
'DefaultFirebaseOptions have not been configured for linux - ' | ||
'you can reconfigure this by running the FlutterFire CLI again.', | ||
); | ||
default: | ||
throw UnsupportedError( | ||
'DefaultFirebaseOptions are not supported for this platform.', | ||
); | ||
} | ||
} | ||
|
||
static const FirebaseOptions web = FirebaseOptions( | ||
apiKey: 'AIzaSyAoY0U1veRmqWWMmFBULkerX6X5HYjMmIs', | ||
appId: '1:807943088200:web:eda1735cfd79d15e21907d', | ||
messagingSenderId: '807943088200', | ||
projectId: 'tic-tac-toe-handson', | ||
authDomain: 'tic-tac-toe-handson.firebaseapp.com', | ||
storageBucket: 'tic-tac-toe-handson.appspot.com', | ||
); | ||
|
||
static const FirebaseOptions android = FirebaseOptions( | ||
apiKey: 'AIzaSyCCWnRieFsedimIvgsjNevAdUSb0dm1faY', | ||
appId: '1:807943088200:android:d989c8a1b020562321907d', | ||
messagingSenderId: '807943088200', | ||
projectId: 'tic-tac-toe-handson', | ||
storageBucket: 'tic-tac-toe-handson.appspot.com', | ||
); | ||
|
||
static const FirebaseOptions ios = FirebaseOptions( | ||
apiKey: 'AIzaSyBZHELrAr4SRfXLYrxc4zF27P0R3sGsbi0', | ||
appId: '1:807943088200:ios:125d7bfaa751582d21907d', | ||
messagingSenderId: '807943088200', | ||
projectId: 'tic-tac-toe-handson', | ||
storageBucket: 'tic-tac-toe-handson.appspot.com', | ||
iosBundleId: 'com.example.ticTacToeHandson', | ||
); | ||
|
||
static const FirebaseOptions macos = FirebaseOptions( | ||
apiKey: 'AIzaSyBZHELrAr4SRfXLYrxc4zF27P0R3sGsbi0', | ||
appId: '1:807943088200:ios:125d7bfaa751582d21907d', | ||
messagingSenderId: '807943088200', | ||
projectId: 'tic-tac-toe-handson', | ||
storageBucket: 'tic-tac-toe-handson.appspot.com', | ||
iosBundleId: 'com.example.ticTacToeHandson', | ||
); | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Players { | ||
final String playerX; | ||
final String playerO; | ||
|
||
Players({ | ||
required this.playerX, | ||
required this.playerO, | ||
}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,109 @@ | ||
import 'package:tic_tac_toe_handson/model/players.dart'; | ||
|
||
class TicTacToe { | ||
final List<List<String>> board; | ||
final Players players; | ||
final String currentPlayer; | ||
|
||
factory TicTacToe.start() { | ||
return TicTacToe._([ | ||
['', '', ''], | ||
['', '', ''], | ||
['', '', ''], | ||
], 'X'); | ||
factory TicTacToe.start({ | ||
playerX = 'X', | ||
playerO = 'O', | ||
}) { | ||
final players = Players( | ||
playerX: playerX, | ||
playerO: playerO, | ||
); | ||
|
||
return TicTacToe( | ||
[ | ||
['', '', ''], | ||
['', '', ''], | ||
['', '', ''], | ||
], | ||
players, | ||
players.playerX, | ||
); | ||
} | ||
|
||
TicTacToe._(this.board, this.currentPlayer); | ||
TicTacToe(this.board, this.players, this.currentPlayer); | ||
|
||
factory TicTacToe.fromJson(Map<String, dynamic> json) { | ||
final flatBoard = List<String>.from(json['board']); | ||
|
||
return TicTacToe( | ||
[ | ||
List<String>.from(flatBoard.sublist(0, 3)), | ||
List<String>.from(flatBoard.sublist(3, 6)), | ||
List<String>.from(flatBoard.sublist(6, 9)), | ||
], | ||
Players( | ||
playerX: json['players']['playerX'], | ||
playerO: json['players']['playerO'], | ||
), | ||
json['currentPlayer'], | ||
); | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
return { | ||
// Firestoreではネストした配列を扱えないため、1次元配列に変換する | ||
'board': [...board[0], ...board[1], ...board[2]], | ||
'players': { | ||
'playerX': players.playerX, | ||
'playerO': players.playerO, | ||
}, | ||
'currentPlayer': currentPlayer, | ||
}; | ||
} | ||
|
||
TicTacToe placeMark(int row, int col) { | ||
if (board[row][col].isEmpty) { | ||
final newBoard = List.of(board); | ||
newBoard[row][col] = currentPlayer; | ||
String nextPlayer = currentPlayer == 'X' ? 'O' : 'X'; | ||
String nextPlayer = | ||
currentPlayer == players.playerX ? players.playerO : players.playerX; | ||
|
||
return TicTacToe._(newBoard, nextPlayer); | ||
return TicTacToe(newBoard, players, nextPlayer); | ||
} | ||
return this; | ||
} | ||
|
||
String getWinner() { | ||
for (int i = 0; i < 3; i++) { | ||
// row = i における横の判定 | ||
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0].isNotEmpty) { | ||
if (board[i][0] == board[i][1] && | ||
board[i][1] == board[i][2] && | ||
board[i][0].isNotEmpty) { | ||
return board[i][0]; | ||
} | ||
// col = i における縦の判定 | ||
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i].isNotEmpty) { | ||
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].isNotEmpty) { | ||
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].isNotEmpty) { | ||
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().isEmpty && board.every((row) => row.every((cell) => cell.isNotEmpty)); | ||
return getWinner().isEmpty && | ||
board.every((row) => row.every((cell) => cell.isNotEmpty)); | ||
} | ||
|
||
TicTacToe resetBoard() { | ||
return TicTacToe.start(); | ||
return TicTacToe.start(playerX: players.playerX, playerO: players.playerO); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:tic_tac_toe_handson/model/tic_tac_toe.dart'; | ||
import 'package:tic_tac_toe_handson/repository/tic_tac_toe_repository.dart'; | ||
|
||
final getTicTacToeProvider = AutoDisposeStreamProvider<TicTacToe>( | ||
(ref) => | ||
// 対戦相手同士のIDを設定する | ||
ref.watch(ticTacToeRepositoryProvider).get( | ||
playerX: 'flutter', | ||
playerO: 'kaigi', | ||
), | ||
); |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:tic_tac_toe_handson/model/tic_tac_toe.dart'; | ||
import 'package:tic_tac_toe_handson/repository/tic_tac_toe_repository.dart'; | ||
|
||
final updateTicTacToeProvider = | ||
AutoDisposeFutureProviderFamily<void, TicTacToe>( | ||
(ref, arg) => ref.watch(ticTacToeRepositoryProvider).update(arg), | ||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:tic_tac_toe_handson/model/tic_tac_toe.dart'; | ||
|
||
final ticTacToeRepositoryProvider = AutoDisposeProvider<TicTacToeRepository>( | ||
(ref) => TicTacToeRepository(), | ||
); | ||
|
||
/// 盤面のデータを管理するリポジトリ | ||
final class TicTacToeRepository { | ||
TicTacToeRepository(); | ||
|
||
final _client = FirebaseFirestore.instance; | ||
static const _collectionKey = 'tic_tac_toe'; | ||
|
||
String _documentKey(String playerX, String playerO) { | ||
return '${playerX}_$playerO'; | ||
} | ||
|
||
CollectionReference<TicTacToe> _colRef() => | ||
_client.collection(_collectionKey).withConverter( | ||
fromFirestore: (doc, _) => TicTacToe.fromJson(doc.data()!), | ||
toFirestore: (entity, _) => entity.toJson(), | ||
); | ||
|
||
/// 盤面のデータを取得する | ||
Stream<TicTacToe> get({ | ||
String playerX = 'X', | ||
String playerO = 'O', | ||
}) { | ||
final documentKey = _documentKey(playerX, playerO); | ||
return _colRef().doc(documentKey).snapshots().map( | ||
(e) => | ||
e.data() ?? | ||
TicTacToe.start( | ||
playerX: playerX, | ||
playerO: playerO, | ||
), | ||
); | ||
} | ||
|
||
/// 盤面のデータを更新する | ||
Future<void> update(TicTacToe ticTacToe) async { | ||
final documentKey = | ||
_documentKey(ticTacToe.players.playerX, ticTacToe.players.playerO); | ||
await _colRef().doc(documentKey).set(ticTacToe); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
いつも
.autoDispose
を指定していてAutoDispose~Provider
初めて知りました!ありがとうございます!:pray: