464 lines
14 KiB
Dart
464 lines
14 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:sudoku_app/models/sudoku_game_dto.dart';
|
|
import 'package:sudoku_app/models/sudoku_theme.dart';
|
|
import 'package:sudoku_app/models/unified_rank_dto.dart';
|
|
import 'package:sudoku_app/services/puzzle_service.dart';
|
|
import 'package:sudoku_app/widgets/ad_banner_widget.dart';
|
|
import 'package:sudoku_app/widgets/number_pad.dart';
|
|
import 'package:sudoku_app/widgets/sudoku_board.dart';
|
|
|
|
class GameScreen extends StatefulWidget {
|
|
final SudokuGameDto gameData;
|
|
final String themeName;
|
|
|
|
const GameScreen({
|
|
super.key,
|
|
required this.gameData,
|
|
required this.themeName,
|
|
});
|
|
|
|
@override
|
|
State<GameScreen> createState() => _GameScreenState();
|
|
}
|
|
|
|
class _GameScreenState extends State<GameScreen> {
|
|
final PuzzleService _puzzleService = PuzzleService();
|
|
|
|
late final int blockSize;
|
|
late final int gridSize;
|
|
late final SudokuTheme activeTheme;
|
|
|
|
late List<int> puzzleCells;
|
|
late List<int> solutionCells;
|
|
late List<int> originalCells;
|
|
|
|
int? selectedIndex;
|
|
int score = 5;
|
|
int secondsElapsed = 0;
|
|
Timer? timer;
|
|
int? selectedNumberPad;
|
|
Set<int> incorrectCells = {};
|
|
bool isValidating = false;
|
|
|
|
// "A" -> 10 (파싱용)
|
|
int _charToInt(String char) {
|
|
if (char == '0') return 0;
|
|
if (char.codeUnitAt(0) >= '1'.codeUnitAt(0) && char.codeUnitAt(0) <= '9'.codeUnitAt(0)) {
|
|
return int.parse(char);
|
|
}
|
|
if (char.codeUnitAt(0) >= 'A'.codeUnitAt(0) && char.codeUnitAt(0) <= 'Z'.codeUnitAt(0)) {
|
|
return char.codeUnitAt(0) - 'A'.codeUnitAt(0) + 10;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// 10 -> "A" (전송용)
|
|
String _intToChar(int num) {
|
|
if (num == 0) return '0';
|
|
if (num >= 1 && num <= 9) return num.toString();
|
|
if (num >= 10 && num <= 35) return String.fromCharCode('A'.codeUnitAt(0) + (num - 10));
|
|
return '?';
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
blockSize = widget.gameData.blockSize;
|
|
gridSize = widget.gameData.gridSize;
|
|
activeTheme = AppThemes.buildGameTheme(widget.themeName, gridSize);
|
|
|
|
puzzleCells = widget.gameData.question.split('').map(_charToInt).toList();
|
|
solutionCells = widget.gameData.solution.split('').map(_charToInt).toList();
|
|
originalCells = widget.gameData.question.split('').map(_charToInt).toList();
|
|
|
|
startTimer();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
timer?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
void startTimer() {
|
|
timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
|
setState(() {
|
|
secondsElapsed++;
|
|
});
|
|
});
|
|
}
|
|
|
|
void onCellTapped(int index) {
|
|
if (originalCells[index] == 0) {
|
|
setState(() {
|
|
selectedIndex = index;
|
|
|
|
if (selectedNumberPad != null) {
|
|
|
|
// 오답 블로킹
|
|
if (incorrectCells.isNotEmpty && !incorrectCells.contains(index)) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('틀린 값을 먼저 수정해주세요. (되돌리기 ↩)'),
|
|
duration: Duration(seconds: 1),
|
|
),
|
|
);
|
|
return; // 입력 처리 중단
|
|
}
|
|
|
|
final int numberValue = selectedNumberPad!;
|
|
puzzleCells[index] = numberValue;
|
|
|
|
// 정답과 비교
|
|
if (numberValue != solutionCells[index]) {
|
|
// 점수 차감
|
|
if (!incorrectCells.contains(index)) {
|
|
if (score > 0) {
|
|
score--;
|
|
}
|
|
incorrectCells.add(index);
|
|
}
|
|
} else {
|
|
incorrectCells.remove(index);
|
|
}
|
|
|
|
_checkIfBoardIsFull();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
void _checkIfBoardIsFull() {
|
|
if (!puzzleCells.contains(0) && !isValidating) {
|
|
_validateGame();
|
|
}
|
|
}
|
|
|
|
void onNumberTapped(int numberValue) {
|
|
setState(() {
|
|
if (selectedNumberPad == numberValue) {
|
|
selectedNumberPad = null;
|
|
} else {
|
|
selectedNumberPad = numberValue;
|
|
}
|
|
});
|
|
}
|
|
|
|
void onUndoTapped() {
|
|
setState(() {
|
|
if (selectedIndex != null && originalCells[selectedIndex!] == 0) {
|
|
puzzleCells[selectedIndex!] = 0;
|
|
incorrectCells.remove(selectedIndex);
|
|
}
|
|
});
|
|
}
|
|
|
|
void onHintTapped() {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('힌트 기능은 준비 중입니다.')),
|
|
);
|
|
}
|
|
|
|
Future<void> _validateGame() async {
|
|
// ... (기존과 동일)
|
|
if (isValidating) return;
|
|
setState(() { isValidating = true; });
|
|
timer?.cancel();
|
|
String currentAnswer = puzzleCells.map(_intToChar).join('');
|
|
try {
|
|
final bool result = await _puzzleService.validateSolution(
|
|
widget.gameData.question, currentAnswer, blockSize,
|
|
);
|
|
if (result) {
|
|
if(mounted) _showRankingDialog();
|
|
} else {
|
|
if(mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('🤔 틀린 부분이 있습니다.')),
|
|
);
|
|
}
|
|
startTimer();
|
|
}
|
|
} catch (e) {
|
|
if(mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('오류: $e')),
|
|
);
|
|
}
|
|
startTimer();
|
|
} finally {
|
|
if(mounted) {
|
|
setState(() { isValidating = false; });
|
|
}
|
|
}
|
|
}
|
|
|
|
void _showRankingDialog() {
|
|
// ... (기존과 동일)
|
|
final nameController = TextEditingController();
|
|
bool isSubmitting = false;
|
|
final String contextId = "SUDOKU_${gridSize}x${gridSize}_L${_difficultyLevel(widget.gameData.question)}";
|
|
showDialog(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (ctx) {
|
|
return StatefulBuilder(
|
|
builder: (context, setDialogState) {
|
|
return AlertDialog(
|
|
title: const Text('🎉 성공! 기록을 남겨주세요.'),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text('($contextId)'),
|
|
Text('완료 시간: $secondsElapsed 초'),
|
|
const SizedBox(height: 20),
|
|
TextField(
|
|
controller: nameController,
|
|
decoration: const InputDecoration(
|
|
labelText: '이름 (10자 이내)',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
maxLength: 10,
|
|
),
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(ctx).pop();
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: const Text('닫기'),
|
|
),
|
|
isSubmitting
|
|
? const Padding(
|
|
padding: EdgeInsets.all(8.0),
|
|
child: CircularProgressIndicator(),
|
|
)
|
|
: ElevatedButton(
|
|
onPressed: () async {
|
|
final playerName = nameController.text.trim();
|
|
if (playerName.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('이름을 입력해주세요.')),
|
|
);
|
|
return;
|
|
}
|
|
setDialogState(() { isSubmitting = true; });
|
|
final rankDto = UnifiedRankDto(
|
|
gameType: 'SUDOKU',
|
|
contextId: contextId,
|
|
playerName: playerName,
|
|
primaryScore: secondsElapsed,
|
|
secondaryScore: null,
|
|
);
|
|
try {
|
|
await _puzzleService.submitRank(rankDto);
|
|
if (!mounted) return;
|
|
Navigator.of(ctx).pop();
|
|
Navigator.of(context).pop();
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('랭킹이 등록되었습니다!')),
|
|
);
|
|
} catch (e) {
|
|
setDialogState(() { isSubmitting = false; });
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(e.toString())),
|
|
);
|
|
}
|
|
},
|
|
child: const Text('랭킹 등록'),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
int _difficultyLevel(String question) {
|
|
int holes = question.split('').where((c) => c == '0').length;
|
|
double holeRatio = holes / (gridSize * gridSize);
|
|
if (holeRatio <= 0.51) return 1;
|
|
if (holeRatio <= 0.58) return 2;
|
|
if (holeRatio <= 0.63) return 3;
|
|
if (holeRatio <= 0.68) return 4;
|
|
return 5;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// 🔽 [수정] 타이머 텍스트를 AppBar로 이동시키기 위해 build 메서드 상단으로 이동
|
|
String formattedTime =
|
|
'${(secondsElapsed ~/ 60).toString().padLeft(2, '0')}:${(secondsElapsed % 60).toString().padLeft(2, '0')}';
|
|
|
|
final Map<int, int> numberCounts = {};
|
|
for (int i = 1; i <= gridSize; i++) { numberCounts[i] = 0; }
|
|
for (int cellValue in puzzleCells) {
|
|
if (cellValue > 0) {
|
|
numberCounts[cellValue] = (numberCounts[cellValue] ?? 0) + 1;
|
|
}
|
|
}
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Sudoku'), // 👈 [수정] 테마 이름 제거
|
|
actions: [
|
|
// 🔽 [수정] AppBar 우측에 타이머 추가
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 20.0),
|
|
child: Center(
|
|
child: Text(
|
|
formattedTime,
|
|
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
children: [
|
|
// 1. 게임 콘텐츠 영역
|
|
Expanded(
|
|
child: LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
bool isLandscape = constraints.maxWidth > constraints.maxHeight;
|
|
if (isLandscape) {
|
|
return _buildLandscapeLayout(context, numberCounts);
|
|
} else {
|
|
return _buildPortraitLayout(context, numberCounts);
|
|
}
|
|
},
|
|
),
|
|
),
|
|
|
|
// 2. 광고 배너
|
|
const AdBannerWidget(),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
// 🔽 [수정] formattedTime 파라미터 제거
|
|
Widget _buildPortraitLayout(BuildContext context, Map<int, int> numberCounts) {
|
|
return Center(
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: 600),
|
|
child: SingleChildScrollView(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
children: [
|
|
_buildGameInfoWidget(), // 👈 [수정] 파라미터 제거
|
|
const SizedBox(height: 15),
|
|
_buildSudokuBoardWidget(),
|
|
const SizedBox(height: 15),
|
|
_buildNumberPadWidget(context, numberCounts, isLandscape: false),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
// 🔽 [수정] formattedTime 파라미터 제거
|
|
Widget _buildLandscapeLayout(BuildContext context, Map<int, int> numberCounts) {
|
|
return Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Expanded(
|
|
flex: 6,
|
|
child: Center(
|
|
child: AspectRatio(
|
|
aspectRatio: 1.0,
|
|
child: _buildSudokuBoardWidget(),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 16),
|
|
Expanded(
|
|
flex: 4,
|
|
child: SingleChildScrollView(
|
|
child: Column(
|
|
children: [
|
|
_buildGameInfoWidget(), // 👈 [수정] 파라미터 제거
|
|
const SizedBox(height: 20),
|
|
_buildNumberPadWidget(context, numberCounts, isLandscape: true),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
// 🔽 [수정] 상단 정보 (점수, 힌트, 되돌리기) - 타이머 제거
|
|
Widget _buildGameInfoWidget() {
|
|
return Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
// 1. 점수
|
|
Text('SCORE: $score', style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
|
|
|
|
// 2. 버튼 그룹 (힌트, 되돌리기)
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
IconButton(
|
|
onPressed: onHintTapped,
|
|
icon: const Icon(Icons.lightbulb_outline, color: Colors.orange),
|
|
iconSize: 30,
|
|
),
|
|
IconButton(
|
|
onPressed: onUndoTapped,
|
|
icon: const Icon(Icons.undo, color: Colors.red),
|
|
iconSize: 30,
|
|
),
|
|
],
|
|
)
|
|
],
|
|
);
|
|
}
|
|
|
|
// 게임 보드 (변경 없음)
|
|
Widget _buildSudokuBoardWidget() {
|
|
return SudokuBoard(
|
|
blockSize: blockSize,
|
|
theme: activeTheme,
|
|
cells: puzzleCells,
|
|
originalCells: originalCells,
|
|
selectedIndex: selectedIndex,
|
|
selectedNumberPad: selectedNumberPad,
|
|
incorrectCells: incorrectCells,
|
|
onCellTapped: onCellTapped,
|
|
);
|
|
}
|
|
|
|
// 숫자 패드 (변경 없음)
|
|
Widget _buildNumberPadWidget(BuildContext context, Map<int, int> numberCounts, {required bool isLandscape}) {
|
|
double? maxWidth = !isLandscape
|
|
? 600 * 0.6
|
|
: null;
|
|
|
|
return Center(
|
|
child: ConstrainedBox(
|
|
constraints: BoxConstraints(maxWidth: maxWidth ?? double.infinity),
|
|
child: NumberPad(
|
|
blockSize: blockSize,
|
|
theme: activeTheme,
|
|
numberCounts: numberCounts,
|
|
selectedNumber: selectedNumberPad,
|
|
onNumberTapped: onNumberTapped,
|
|
isLandscape: isLandscape,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |