20.3 C
New York
Sunday, October 19, 2025

single participant – The Block puzzle


import 'package deal:flutter/materials.dart';
import 'dart:math';

void essential() => runApp(BlockPuzzleApp());

class BlockPuzzleApp extends StatelessWidget {
  @override
  Widget construct(BuildContext context) {
    return MaterialApp(
      title: 'Block Puzzle Recreation',
      theme: ThemeData.darkish(),
      residence: BlockPuzzleGame(),
    );
  }
}

class BlockPuzzleGame extends StatefulWidget {
  @override
  _BlockPuzzleGameState createState() => _BlockPuzzleGameState();
}

class _BlockPuzzleGameState extends State {
  static const int gridSize = 10;
  Checklist> grid =
      Checklist.generate(gridSize, (_) => Checklist.stuffed(gridSize, false));

  Checklist>> items = [
    [[1, 1, 1]], // Horizontal
    [[1], [1], [1]], // Vertical
    [[1, 1], [1, 0]], // L
    [[1, 1], [1, 1]], // Sq.
  ];

  Checklist>? selectedPiece;
  int rating = 0;

  void placePiece(int gx, int gy) {
    if (selectedPiece == null) return;

    bool canPlace = true;
    for (int y = 0; y < selectedPiece!.size; y++) {
      for (int x = 0; x < selectedPiece![y].size; x++) {
        if (selectedPiece![y][x] == 1) {
          int px = gx + x;
          int py = gy + y;
          if (px >= gridSize || py >= gridSize || grid[py][px]) {
            canPlace = false;
            break;
          }
        }
      }
    }

    if (canPlace) {
      setState(() {
        for (int y = 0; y < selectedPiece!.size; y++) {
          for (int x = 0; x < selectedPiece![y].size; x++) {
            if (selectedPiece![y][x] == 1) {
              grid[gy + y][gx + x] = true;
            }
          }
        }
        selectedPiece = null;
        clearLines();
      });
    }
  }

  void clearLines() {
    int cleared = 0;

    // Clear full rows
    grid.removeWhere((row) {
      if (row.each((cell) => cell)) {
        cleared++;
        return true;
      }
      return false;
    });

    // Add empty rows on prime
    whereas (grid.size < gridSize) {
      grid.insert(0, Checklist.stuffed(gridSize, false));
    }

    // Add rating
    if (cleared > 0) {
      rating += cleared * 10;
    }
  }

  void resetGame() {
    setState(() {
      grid = Checklist.generate(gridSize, (_) => Checklist.stuffed(gridSize, false));
      rating = 0;
      selectedPiece = null;
    });
  }

  @override
  Widget construct(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Textual content('Block Puzzle - Rating: $rating'),
        actions: [
          IconButton(
            icon: Icon(Icons.refresh),
            onPressed: resetGame,
          )
        ],
      ),
      physique: Column(
        kids: [
          Expanded(
            child: GridView.builder(
              padding: EdgeInsets.all(4),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: gridSize,
              ),
              itemCount: gridSize * gridSize,
              itemBuilder: (context, index) {
                int x = index % gridSize;
                int y = index ~/ gridSize;
                return GestureDetector(
                  onTap: () => placePiece(x, y),
                  child: Container(
                    margin: EdgeInsets.all(1),
                    color: grid[y][x] ? Colours.blue : Colours.gray[800],
                  ),
                );
              },
            ),
          ),
          Divider(),
          Padding(
            padding: const EdgeInsets.all(8.0),
            baby: selectedPiece == null
                ? ElevatedButton(
                    baby: Textual content('New Piece'),
                    onPressed: () {
                      setState(() {
                        selectedPiece =
                            items[Random().nextInt(pieces.length)];
                      });
                    },
                  )
                : buildPiecePreview(),
          ),
        ],
      ),
    );
  }

  Widget buildPiecePreview() {
    return Column(
      kids: selectedPiece!
          .map((row) => Row(
                mainAxisSize: MainAxisSize.min,
                kids: row
                    .map((cell) => Container(
                          width: 20,
                          peak: 20,
                          margin: EdgeInsets.all(1),
                          colour:
                              cell == 1 ? Colours.inexperienced : Colours.clear,
                        ))
                    .toList(),
              ))
          .toList(),
    );
  }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles