-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_logic.dart
222 lines (201 loc) · 5.73 KB
/
game_logic.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import 'dart:math';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:rock_paper_scissors_demo_app/inference.dart';
enum Move {
rock,
paper,
scissors,
lizard,
spock;
// key: winner, value: (loser, interaction)
static const Map<Move, List<({Move move, String interaction})>>
moveInteractions = {
Move.rock: [
(move: Move.scissors, interaction: "crushes"),
(move: Move.lizard, interaction: "crushes")
],
Move.paper: [
(move: Move.rock, interaction: "covers"),
(move: Move.spock, interaction: "disproves")
],
Move.scissors: [
(move: Move.paper, interaction: "cuts"),
(move: Move.lizard, interaction: "decapitates")
],
Move.lizard: [
(move: Move.paper, interaction: "eats"),
(move: Move.spock, interaction: "poisons")
],
Move.spock: [
(move: Move.rock, interaction: "vaporizes"),
(move: Move.scissors, interaction: "smashes")
],
};
get emoji {
switch (this) {
case Move.rock:
return "🪨";
case Move.paper:
return "📄";
case Move.scissors:
return "✂️";
case Move.lizard:
return "🦎";
case Move.spock:
return "🖖";
}
}
static Move? fromGestureOrNull(Gesture gesture, bool lizardSpockEnabled) {
switch (gesture) {
case Gesture.call:
if (lizardSpockEnabled) {
return spock;
}
return null;
case Gesture.ok:
if (lizardSpockEnabled) {
return lizard;
}
return null;
case Gesture.fist:
return rock;
case Gesture.stop:
case Gesture.stopInverted:
case Gesture.palm:
return paper;
case Gesture.peace:
case Gesture.peaceInverted:
return scissors;
default:
return null;
}
}
static Move getRandomMove(bool lizardSpockEnabled) {
final moves = List<Move>.from(Move.values);
if (!lizardSpockEnabled) {
moves.removeWhere((element) => element == spock || element == lizard);
}
return moves[Random().nextInt(moves.length)];
}
}
enum RoundOutcome {
playerWins,
tie,
playerLoses;
}
({RoundOutcome outcome, String message}) determineRoundResult(
Move playerMove, Move computerMove) {
if (playerMove == computerMove) {
return (
outcome: RoundOutcome.tie,
message: "both picked ${playerMove.name} - it's a tie!"
);
}
final playerWins = Move.moveInteractions[playerMove]!
.where((element) => element.move == computerMove)
.firstOrNull;
if (playerWins != null) {
return (
outcome: RoundOutcome.playerWins,
message:
"${playerMove.name} ${playerWins.interaction} ${computerMove.name} - you win!"
);
}
final playerLoses = Move.moveInteractions[computerMove]!
.where((element) => element.move == playerMove)
.first;
return (
outcome: RoundOutcome.playerLoses,
message:
"${computerMove.name} ${playerLoses.interaction} ${playerMove.name} - you lose!"
);
}
class GameState {
final bool lizardSpockEnabled;
final int roundNumber;
final int playerScore;
final int computerScore;
final Move? computerMove;
final Move? playerMove;
final RoundOutcome? roundOutcome;
final String? roundMessage;
GameState({
required this.lizardSpockEnabled,
required this.roundNumber,
required this.playerScore,
required this.computerScore,
this.computerMove,
this.playerMove,
this.roundOutcome,
this.roundMessage,
});
GameState copyWith({
bool? lizardSpockEnabled,
int? roundNumber,
int? playerScore,
int? computerScore,
Move? computerMove,
Move? playerMove,
RoundOutcome? roundOutcome,
String? roundMessage,
}) {
return GameState(
lizardSpockEnabled: lizardSpockEnabled ?? this.lizardSpockEnabled,
roundNumber: roundNumber ?? this.roundNumber,
playerScore: playerScore ?? this.playerScore,
computerScore: computerScore ?? this.computerScore,
computerMove: computerMove ?? this.computerMove,
playerMove: playerMove ?? this.playerMove,
roundOutcome: roundOutcome ?? this.roundOutcome,
roundMessage: roundMessage ?? this.roundMessage,
);
}
}
final gameStateNotifierProvider =
NotifierProvider<GameStateNotifier, GameState>(() => GameStateNotifier());
class GameStateNotifier extends Notifier<GameState> {
@override
GameState build() {
ref.listen(gestureDetectionResultProvider, (previous, next) {
next.whenData((gesture) {
final playerMove =
Move.fromGestureOrNull(gesture, state.lizardSpockEnabled);
if (playerMove == null) {
state =
state.copyWith(roundMessage: "Invalid gesture: ${gesture.name}");
return;
}
playRound(playerMove);
});
});
return GameState(
lizardSpockEnabled: false,
roundNumber: 0,
playerScore: 0,
computerScore: 0);
}
void playRound(Move playerMove) async {
final computerMove = Move.getRandomMove(state.lizardSpockEnabled);
final result = determineRoundResult(playerMove, computerMove);
state = state.copyWith(
roundNumber: state.roundNumber + 1,
playerMove: playerMove,
computerMove: computerMove,
roundOutcome: result.outcome,
roundMessage: result.message,
);
switch (result.outcome) {
case RoundOutcome.playerWins:
state = state.copyWith(playerScore: state.playerScore + 1);
break;
case RoundOutcome.playerLoses:
state = state.copyWith(computerScore: state.computerScore + 1);
break;
case RoundOutcome.tie:
break;
}
}
void toggleLizardSpock() {
state = state.copyWith(lizardSpockEnabled: !state.lizardSpockEnabled);
}
}