You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
5.2 KiB

2 years ago
  1. import 'dart:developer';
  2. import 'package:flutter/material.dart';
  3. import 'package:my_flutter_puzzle/models/user_info.dart';
  4. import 'package:my_flutter_puzzle/widgets/puzzle_widgets/puzzle_widgets.dart';
  5. class PuzzleScreen extends StatefulWidget {
  6. const PuzzleScreen({
  7. Key? key,
  8. required this.initialList,
  9. required this.id,
  10. required this.myInfo,
  11. }) : super(key: key);
  12. final List<int> initialList;
  13. final String id;
  14. final UserData myInfo;
  15. @override
  16. _PuzzleScreenState createState() => _PuzzleScreenState();
  17. }
  18. class _PuzzleScreenState extends State<PuzzleScreen> {
  19. late final List<int> myList;
  20. late final List<int> opponentList;
  21. int _moves = 0;
  22. onClick(index) {
  23. log('-----------------------');
  24. log('Tapped index: $index');
  25. int emptyTilePosIndex = myList.indexOf(0);
  26. int emptyTilePosRow = emptyTilePosIndex ~/ 4;
  27. int emptyTilePosCol = emptyTilePosIndex % 4;
  28. int currentTileRow = index ~/ 4;
  29. int currentTileCol = index % 4;
  30. //current element moves up
  31. if ((currentTileRow - 1 == emptyTilePosRow) && (currentTileCol == emptyTilePosCol)) {
  32. setState(() {
  33. myList[emptyTilePosIndex] = myList[index];
  34. myList[index] = 0;
  35. _moves++;
  36. });
  37. }
  38. //current element moves down
  39. else if ((currentTileRow + 1 == emptyTilePosRow) && (currentTileCol == emptyTilePosCol)) {
  40. setState(() {
  41. myList[emptyTilePosIndex] = myList[index];
  42. myList[index] = 0;
  43. _moves++;
  44. });
  45. }
  46. //current element moves left
  47. else if ((currentTileRow == emptyTilePosRow) && (currentTileCol + 1 == emptyTilePosCol)) {
  48. setState(() {
  49. myList[emptyTilePosIndex] = myList[index];
  50. myList[index] = 0;
  51. _moves++;
  52. });
  53. }
  54. //current element moves right
  55. else if ((currentTileRow == emptyTilePosRow) && (currentTileCol - 1 == emptyTilePosCol)) {
  56. setState(() {
  57. myList[emptyTilePosIndex] = myList[index];
  58. myList[index] = 0;
  59. _moves++;
  60. });
  61. } else {
  62. if (currentTileCol == emptyTilePosCol) {
  63. int low;
  64. int high;
  65. // multiple elements move up
  66. if (emptyTilePosRow < currentTileRow) {
  67. low = emptyTilePosRow;
  68. high = currentTileRow;
  69. int i = low;
  70. while (i < high) {
  71. setState(() {
  72. myList[(i * 4) + emptyTilePosCol] = myList[(((i + 1) * 4) + emptyTilePosCol)];
  73. });
  74. i += 1;
  75. }
  76. setState(() {
  77. myList[(high * 4) + emptyTilePosCol] = 0;
  78. _moves++;
  79. });
  80. }
  81. //multiple elements move down
  82. else {
  83. low = emptyTilePosRow;
  84. high = currentTileRow;
  85. int i = low;
  86. while (i > high) {
  87. setState(() {
  88. myList[(i * 4) + emptyTilePosCol] = myList[(((i - 1) * 4) + emptyTilePosCol)];
  89. });
  90. i -= 1;
  91. }
  92. setState(() {
  93. myList[(high * 4) + emptyTilePosCol] = 0;
  94. _moves++;
  95. });
  96. }
  97. }
  98. // multiple elements move left
  99. // multiple elements move right
  100. if (currentTileRow == emptyTilePosRow) {
  101. int low;
  102. int high;
  103. // multiple elements move left
  104. if (emptyTilePosCol < currentTileCol) {
  105. low = emptyTilePosCol;
  106. high = currentTileCol;
  107. int i = low;
  108. while (i < high) {
  109. setState(() {
  110. myList[(emptyTilePosRow * 4) + i] = myList[(emptyTilePosRow * 4) + (i + 1)];
  111. });
  112. i += 1;
  113. }
  114. setState(() {
  115. myList[high + (emptyTilePosRow * 4)] = 0;
  116. _moves++;
  117. });
  118. }
  119. //multiple elements move right
  120. else {
  121. low = emptyTilePosCol;
  122. high = currentTileCol;
  123. int i = low;
  124. while (i > high) {
  125. setState(() {
  126. myList[(i + (emptyTilePosRow * 4))] = myList[(i - 1) + (emptyTilePosRow * 4)];
  127. });
  128. i -= 1;
  129. }
  130. setState(() {
  131. myList[high + (emptyTilePosRow * 4)] = 0;
  132. _moves++;
  133. });
  134. }
  135. }
  136. }
  137. // _databaseClient.updateGameState(
  138. // id: widget.id,
  139. // mydata: widget.myInfo,
  140. // numberList: myList,
  141. // moves: _moves,
  142. // );
  143. log('List: $myList');
  144. log('-----------------------');
  145. }
  146. @override
  147. void initState() {
  148. super.initState();
  149. myList = widget.initialList;
  150. opponentList = widget.initialList;
  151. }
  152. @override
  153. Widget build(BuildContext context) {
  154. return Scaffold(
  155. backgroundColor: Colors.white,
  156. body: Row(
  157. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  158. children: [
  159. // PLAYER 1 puzzle --> my (own)
  160. Column(
  161. children: [
  162. const PlayerText(
  163. displayName: 'PLAYER 1',
  164. ),
  165. // TODO: Change font size
  166. MovesText(
  167. moves: _moves,
  168. fontSize: 60,
  169. ),
  170. ],
  171. ),
  172. Container(
  173. height: MediaQuery.of(context).size.height * 0.6,
  174. width: 2,
  175. color: Colors.black,
  176. ),
  177. ],
  178. ),
  179. );
  180. }
  181. }