Skip to content

Commit 5fed04e

Browse files
thamaraiselvamthamaraiselvam
authored andcommitted
game score algo added
1 parent 219fd7f commit 5fed04e

File tree

6 files changed

+80
-36
lines changed

6 files changed

+80
-36
lines changed

lib/screens/GameScreen/GameScreen.dart

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:numbers/store/BestScore.dart';
33
import 'package:numbers/store/RecentScoreStore.dart';
44
import 'package:numbers/schema/BlockSchema.dart';
55
import 'package:numbers/screens/GameScreen/summaryModel.dart';
6+
import 'package:numbers/utils/Config.dart';
67
import 'package:numbers/widgets/bgGradient.dart';
78
import 'package:numbers/provider/BlockDataStream.dart';
89
import 'package:flutter/material.dart';
@@ -20,9 +21,16 @@ class _GameScreenState extends State<GameScreen> {
2021
List<BlockSchema> blocks;
2122
int currentTotal;
2223
Timer gameTimerObject;
23-
int secCounter = 5;
24+
int secCounter = gameDuration;
2425
bool isTimeUp = false;
25-
Map<String, int> gameHistory = {"total": 0, "success": 0, "fail": 0};
26+
Map<String, int> gameHistory = {
27+
"total": 0,
28+
"success": 0,
29+
"fail": 0,
30+
"score": 0,
31+
"selectedBlocks": 0,
32+
"tmpSelectedBlocks": 0
33+
};
2634
BlockDataStream blockDataStream = BlockDataStream();
2735

2836
@override
@@ -66,18 +74,30 @@ class _GameScreenState extends State<GameScreen> {
6674
setState(() {
6775
this.secCounter--;
6876
if (this.secCounter < 1) {
69-
this.isTimeUp = true;
70-
this.gameTimerObject.cancel();
71-
showSummary(context, gameHistory);
72-
updateScores();
77+
this.postGameWorks();
7378
}
7479
});
7580
});
7681
}
7782

78-
void updateScores(){
79-
RecentScoreStore().updateRecentScore(gameHistory['success']);
80-
BestScoreStore().updateScore(gameHistory['success']);
83+
void postGameWorks() {
84+
this.isTimeUp = true;
85+
this.gameTimerObject.cancel();
86+
showSummary(context, gameHistory);
87+
updateScores();
88+
}
89+
90+
void calculateScore() {
91+
int score = (this.gameHistory['selectedBlocks'] * costs['block']) +
92+
(this.gameHistory['success'] * costs['success']) +
93+
(this.gameHistory['fail'] * costs['fail']);
94+
this.gameHistory['score'] = score > 0 ? (score) : 0;
95+
print(this.gameHistory);
96+
}
97+
98+
void updateScores() {
99+
RecentScoreStore().updateRecentScore(gameHistory['score']);
100+
BestScoreStore().updateScore(gameHistory['score']);
81101
}
82102

83103
void fillBlocksData() {
@@ -109,6 +129,8 @@ class _GameScreenState extends State<GameScreen> {
109129

110130
this.blocks[selectedIndex].isSelected = true;
111131

132+
this.gameHistory['tmpSelectedBlocks']++;
133+
112134
this.currentTotal += blockData['value'];
113135

114136
if (this.currentTotal < this.blockSchema.target) {
@@ -126,13 +148,18 @@ class _GameScreenState extends State<GameScreen> {
126148

127149
void _wrongAnswer(selectedIndex) {
128150
this.gameHistory['fail']++;
151+
this.gameHistory['tmpSelectedBlocks'] = 0; //reset
152+
calculateScore();
129153
_changeBlockColor(selectedIndex, Colors.red);
130154
_showStatusAlert('Wrong !!!', Icons.clear, Colors.red, false);
131155
_closePopUpAndShuffle();
132156
}
133157

134158
void _correctAnswer(selectedIndex) {
135159
this.gameHistory['success']++;
160+
this.gameHistory['selectedBlocks'] += this.gameHistory['tmpSelectedBlocks'];
161+
this.gameHistory['tmpSelectedBlocks'] = 0; //reset
162+
calculateScore();
136163
_changeBlockColor(selectedIndex, Colors.green);
137164
_showStatusAlert('Correct !!!', Icons.check, Colors.green, true);
138165
_closePopUpAndShuffle();

lib/screens/GameScreen/headerInfo.widget.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Row headerInfo(int secCounter, Map<String, int> gameHistory) {
2727
Container(
2828
margin: const EdgeInsets.only(left: 10),
2929
padding: const EdgeInsets.only(left: 15, right: 10),
30-
width: 100,
30+
// width: 200,
3131
color: blackLowOpacity,
3232
child: Row(
3333
children: <Widget>[
@@ -39,7 +39,7 @@ Row headerInfo(int secCounter, Map<String, int> gameHistory) {
3939
textAlign: TextAlign.center,
4040
),
4141
Text(
42-
gameHistory['success'].toString(),
42+
gameHistory['score'].toString(),
4343
style: TextStyle(color: Colors.white),
4444
),
4545
],

lib/screens/GameScreen/summaryModel.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Future<void> showSummary(BuildContext context, Map<String, int> gameHistory, ) a
2020
fontWeight: FontWeight.bold, fontSize: 30),
2121
),
2222
Text(
23-
gameHistory['success'].toString(),
23+
gameHistory['score'].toString(),
2424
style: TextStyle(
2525
fontWeight: FontWeight.bold, fontSize: 30),
2626
),

lib/screens/HomeScreen/HomeScreen.dart

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
import 'package:flutter/material.dart';
22
import 'package:numbers/component/ScoreCard.dart';
3+
import 'package:numbers/store/BestScore.dart';
34
import 'package:numbers/utils/constants.dart';
45
import 'package:numbers/widgets/dashedLine.dart';
56

67
import 'appTitle.widget.dart';
78
import 'bestScore.widget.dart';
89
import 'tutorialBtn.widget.dart';
910

10-
class HomeScreen extends StatelessWidget {
11-
const HomeScreen({Key key}) : super(key: key);
11+
class HomeScreen extends StatefulWidget {
12+
@override
13+
_HomeScreenState createState() => _HomeScreenState();
14+
}
15+
16+
class _HomeScreenState extends State<HomeScreen> {
17+
18+
int bestScore = 0;
19+
20+
@override
21+
void initState() {
22+
super.initState();
23+
this.setBestScore();
24+
}
25+
26+
void setBestScore() async {
27+
int _bestScore = await BestScoreStore().getBestScore();
28+
setState(() {
29+
this.bestScore = _bestScore;
30+
});
31+
}
1232

1333
@override
1434
Widget build(BuildContext context) {
@@ -31,7 +51,7 @@ class HomeScreen extends StatelessWidget {
3151
SizedBox(
3252
height: 40,
3353
),
34-
BestScore(),
54+
buildBestScore(this.bestScore),
3555
dashedLineBreak(Colors.white),
3656
SizedBox(
3757
height: 10,
Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
import 'package:flutter/material.dart';
22

3-
class BestScore extends StatelessWidget {
4-
const BestScore({Key key}) : super(key: key);
5-
6-
@override
7-
Widget build(BuildContext context) {
8-
return ListTile(
9-
leading: Icon(
10-
Icons.golf_course,
11-
size: 40,
12-
color: Colors.white,
13-
),
14-
title: Text(
15-
'Best Score',
16-
style: TextStyle(color: Colors.white, fontSize: 13),
17-
),
18-
subtitle: Text(
19-
'55590',
20-
style: TextStyle(color: Colors.white, fontSize: 16),
21-
),
22-
);
23-
}
3+
ListTile buildBestScore(int bestScore) {
4+
return ListTile(
5+
leading: Icon(
6+
Icons.golf_course,
7+
size: 40,
8+
color: Colors.white,
9+
),
10+
title: Text(
11+
'Best Score',
12+
style: TextStyle(color: Colors.white, fontSize: 13),
13+
),
14+
subtitle: Text(
15+
bestScore.toString(),
16+
style: TextStyle(color: Colors.white, fontSize: 16),
17+
),
18+
);
2419
}

lib/utils/Config.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const int gameDuration = 10;
2+
const Map costs = {'success': 1000, 'fail': -500, 'block': 100};

0 commit comments

Comments
 (0)