import 'dart:async'; import 'dart:math';
import 'package:flutter/material.dart';
void main() { runApp(MathGameApp()); }
class MathGameApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Math Game', theme: ThemeData(primarySwatch: Colors.blue), home: MathGamePage(), ); } }
class MathGamePage extends StatefulWidget { @override _MathGamePageState createState() => _MathGamePageState(); }
class _MathGamePageState extends State<MathGamePage> { final Random _random = Random(); int _num1 = 0; int _num2 = 0; String _operator = '+'; int _correctAnswer = 0; List<int> _options = []; int _score = 0; int _level = 1; int _timeLeft = 10; Timer? _timer;
@override void initState() { super.initState(); _generateQuestion(); }
void _startTimer() { _timer?.cancel(); _timeLeft = 10; _timer = Timer.periodic(Duration(seconds: 1), (timer) { setState(() { _timeLeft--; if (_timeLeft <= 0) { timer.cancel(); _gameOver(); } }); }); }
void _generateQuestion() { _num1 = _random.nextInt(10 * _level) + 1; _num2 = _random.nextInt(10 * _level) + 1; List<String> operators = ['+', '-', '×', '÷']; _operator = operators[_random.nextInt(4)];
switch (_operator) {
case '+':
\_correctAnswer = \_num1 + \_num2;
break;
case '-':
\_correctAnswer = \_num1 - \_num2;
break;
case '×':
\_correctAnswer = \_num1 \* \_num2;
break;
case '÷':
\_correctAnswer = (\_num1 \* \_num2) \~/ \_num2;
\_num1 = \_correctAnswer \* \_num2;
break;
}
_options = [_correctAnswer];
while (_options.length < 4) {
int option = _correctAnswer + _random.nextInt(20) - 10;
if (!_options.contains(option)) {
\_options.add(option);
}
}
_options.shuffle();
_startTimer();
}
void _checkAnswer(int selected) { if (selected == _correctAnswer) { _score++; if (_score % 5 == 0) _level++; _generateQuestion(); } else { _gameOver(); } }
void _gameOver() { timer?.cancel(); showDialog( context: context, barrierDismissible: false, builder: () => AlertDialog( title: Text('Game Over'), content: Text('Score: $_score\nLevel: $_level'), actions: [ TextButton( child: Text('Restart'), onPressed: () { Navigator.of(context).pop(); setState(() { _score = 0; _level = 1; _generateQuestion(); }); }, ) ], ), ); }
@override void dispose() { _timer?.cancel(); super.dispose(); }
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Math Game')), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Level: $_level', style: TextStyle(fontSize: 22)), Text('Score: $_score', style: TextStyle(fontSize: 22)), Text('Time left: $_timeLeft', style: TextStyle(fontSize: 22, color: Colors.red)), SizedBox(height: 40), Text('$_num1 $_operator $_num2 = ?', style: TextStyle(fontSize: 32)), SizedBox(height: 20), ..._options.map((option) => Padding( padding: const EdgeInsets.symmetric(vertical: 8.0), child: ElevatedButton( onPressed: () => _checkAnswer(option), child: Text('$option', styl
e: TextStyle(fontSize: 24)), ), )), ], ), ), ); } }