Skip to content
Snippets Groups Projects
Select Git revision
  • 18a9c4c9b1345130cb4f439ff679418e214e4338
  • master default protected
  • 58-upgrade-framework-and-dependencies
  • 49-fix-end-game
  • 40-improve-app-metadata
  • 12-improve-layout
  • 4-add-animations
  • Release_0.9.0_46 protected
  • Release_0.8.2_45 protected
  • Release_0.8.1_44 protected
  • Release_0.8.0_43 protected
  • Release_0.7.0_42 protected
  • Release_0.6.0_41 protected
  • Release_0.5.0_40 protected
  • Release_0.4.2_39 protected
  • Release_0.4.1_38 protected
  • Release_0.4.0_37 protected
  • Release_0.3.1_36 protected
  • Release_0.3.0_35 protected
  • Release_0.2.1_34 protected
  • Release_0.2.0_33 protected
  • Release_0.1.2_32 protected
  • Release_0.1.1_31 protected
  • Release_0.1.0_30 protected
  • Release_0.0.29_29 protected
  • Release_0.0.28_28 protected
  • Release_0.0.27_27 protected
27 results

game_utils.dart

Blame
  • board.dart 3.36 KiB
    import 'package:flutter/material.dart';
    
    import '../provider/data.dart';
    import '../utils/game_utils.dart';
    
    class Board {
      static Container buildGameBoard(Data myProvider) {
        String skin = myProvider.skin;
    
        int maxGuessesCount = myProvider.maxGuessesCount;
        int wordLength = int.parse(myProvider.length);
    
        Widget buildCellWidget(
            String cellValue, String cellTip, bool hasFocus, String foundLetter) {
          Color textColor = Colors.white;
          Color focusBorderColor = Colors.yellow.shade700;
          Color defaultBorderColor = Colors.white;
    
          String cellImage = 'empty';
          if (cellTip != '') {
            cellImage = cellTip;
          }
    
          if ((foundLetter != ' ') && (cellValue == ' ')) {
            cellValue = foundLetter;
            cellImage = 'good';
          }
    
          Image imageWidget = Image(
            image: AssetImage('assets/skins/' + skin + '_' + cellImage + '.png'),
            fit: BoxFit.fill,
          );
    
          Widget cellBackground = Container(
            decoration: BoxDecoration(
              border: Border.all(
                width: 4.0,
                color: hasFocus ? focusBorderColor : defaultBorderColor,
                style: BorderStyle.solid,
              ),
            ),
            child: imageWidget,
          );
    
          Text textWidget = Text(
            cellValue,
            style: TextStyle(
              color: textColor,
              fontSize: 40.0,
              fontWeight: FontWeight.w900,
            ),
            textAlign: TextAlign.center,
          );
    
          return Stack(
            alignment: Alignment.center,
            children: <Widget>[
              cellBackground,
              Center(child: textWidget),
            ],
          );
        }
    
        List<String> guesses = myProvider.guesses;
    
        List<TableRow> tableRows = [];
        for (int lineIndex = 0; lineIndex < maxGuessesCount; lineIndex++) {
          String word = '';
          if (lineIndex < guesses.length) {
            word = guesses[lineIndex];
          } else if (lineIndex == guesses.length) {
            word = myProvider.currentGuess;
          }
    
          List<String> tips = GameUtils.getTips(myProvider, word);
    
          List<TableCell> tableCells = [];
          for (int colIndex = 0; colIndex < wordLength; colIndex++) {
            String cellValue = ' ';
            if (word.length > colIndex) {
              cellValue = word[colIndex];
            }
    
            String cellTip = '';
            if (lineIndex < guesses.length) {
              cellTip = tips[colIndex];
            }
    
            bool hasFocus = (!myProvider.gameWon) &&
                (lineIndex == guesses.length) &&
                (colIndex == word.length);
    
            String foundLetter = ((!myProvider.gameWon) && (lineIndex == guesses.length))
                ? myProvider.foundLetters.substring(colIndex, colIndex + 1)
                : ' ';
    
            tableCells.add(TableCell(
              child: buildCellWidget(cellValue, cellTip, hasFocus, foundLetter),
            ));
          }
    
          tableRows.add(TableRow(children: tableCells));
        }
    
        double horizontalMargins = 20;
        if (wordLength < 6) {
          horizontalMargins = 40;
          if (wordLength < 5) {
            horizontalMargins = 60;
          }
        }
    
        return Container(
          margin: EdgeInsets.symmetric(horizontal: horizontalMargins),
          padding: EdgeInsets.all(2),
          child: Table(
            defaultVerticalAlignment: TableCellVerticalAlignment.middle,
            border: TableBorder.all(
              color: Colors.white,
              style: BorderStyle.none,
            ),
            children: tableRows,
          ),
        );
      }
    }