Skip to content
Snippets Groups Projects
Select Git revision
  • 0fa5cd2e0d4d3ea039e48e3e00d5bdef3b3fa42d
  • master default protected
  • 12-improve-ai
  • 14-improve-app-metadata
  • Release_0.8.1_26 protected
  • Release_0.8.0_25 protected
  • Release_0.7.2_24 protected
  • Release_0.7.1_23 protected
  • Release_0.7.0_22 protected
  • Release_0.6.0_21 protected
  • Release_0.5.0_20 protected
  • Release_0.4.0_19 protected
  • Release_0.3.2_18 protected
  • Release_0.3.1_17 protected
  • Release_0.3.0_16 protected
  • Release_0.2.1_15 protected
  • Release_0.2.0_14 protected
  • Release_0.1.1_13 protected
  • Release_0.1.0_12 protected
  • Release_0.0.11_11 protected
  • Release_0.0.10_10 protected
  • Release_0.0.9_9 protected
  • Release_0.0.8_8 protected
  • Release_0.0.7_7 protected
24 results

game.dart

Blame
  • game.dart 3.93 KiB
    import 'package:awale/models/game/board.dart';
    import 'package:awale/models/settings/settings_game.dart';
    import 'package:awale/models/settings/settings_global.dart';
    import 'package:awale/utils/tools.dart';
    
    class Game {
      Game({
        // Settings
        required this.gameSettings,
        required this.globalSettings,
    
        // State
        this.isRunning = false,
        this.isStarted = false,
        this.isFinished = false,
        this.animationInProgress = false,
    
        // Base data
        required this.board,
    
        // Game data
        required this.currentPlayer,
        required this.scores,
      });
    
      // Settings
      final GameSettings gameSettings;
      final GlobalSettings globalSettings;
    
      // State
      bool isRunning;
      bool isStarted;
      bool isFinished;
      bool animationInProgress;
    
      // Base data
      final Board board;
    
      // Game data
      int currentPlayer;
      List<int> scores;
    
      factory Game.createNull() {
        return Game(
          // Settings
          gameSettings: GameSettings.createDefault(),
          globalSettings: GlobalSettings.createDefault(),
          // Base data
          board: Board.createNull(),
          // Game data
          currentPlayer: 0,
          scores: [0, 0],
        );
      }
    
      factory Game.createNew({
        GameSettings? gameSettings,
        GlobalSettings? globalSettings,
      }) {
        final GameSettings newGameSettings = gameSettings ?? GameSettings.createDefault();
        final GlobalSettings newGlobalSettings = globalSettings ?? GlobalSettings.createDefault();
    
        const int boardSize = 12;
    
        final BoardCells cells = [];
        for (int index = 0; index < boardSize; index++) {
          cells.add(4);
        }
    
        return Game(
          // Settings
          gameSettings: newGameSettings,
          globalSettings: newGlobalSettings,
          // State
          isRunning: true,
          // Base data
          board: Board.createNew(cells: cells),
          // Game data
          currentPlayer: 0,
          scores: [0, 0],
        );
      }
    
      bool get canBeResumed => isStarted && !isFinished;
    
      int getNextCellIndex(int cellIndex, int firstCellIndex) {
        final int nextCellIndex = (cellIndex + 1) % board.cells.length;
    
        if (nextCellIndex == firstCellIndex) {
          return getNextCellIndex(nextCellIndex, firstCellIndex);
        }
    
        return nextCellIndex;
      }
    
      int getPreviousCellIndex(int cellIndex) {
        return (cellIndex - 1) % board.cells.length;
      }
    
      bool isCurrentPlayerHouse(int cellIndex) {
        const allowedCellIndexes = [
          [0, 1, 2, 3, 4, 5],
          [6, 7, 8, 9, 10, 11],
        ];
        return allowedCellIndexes[currentPlayer].contains(cellIndex);
      }
    
      bool isOpponentHouse(int cellIndex) {
        return !isCurrentPlayerHouse(cellIndex);
      }
    
      // Ensure move is allowed, from cell seeds count
      bool isMoveAllowed(int cellIndex) {
        final int seedsCount = board.cells[cellIndex];
    
        int finalCellIndex = cellIndex;
        for (int i = 0; i < seedsCount; i++) {
          finalCellIndex = getNextCellIndex(finalCellIndex, cellIndex);
          if (isOpponentHouse(finalCellIndex)) {
            return true;
          }
        }
    
        return false;
      }
    
      void dump() {
        printlog('');
        printlog('## Current game dump:');
        printlog('');
        printlog('$Game:');
        printlog('  Settings');
        gameSettings.dump();
        globalSettings.dump();
        printlog('  State');
        printlog('    isRunning: $isRunning');
        printlog('    isStarted: $isStarted');
        printlog('    isFinished: $isFinished');
        printlog('    animationInProgress: $animationInProgress');
        board.dump();
        printlog('  Game data');
        printlog('    scores: $scores');
        printlog('');
      }
    
      @override
      String toString() {
        return '$Game(${toJson()})';
      }
    
      Map<String, dynamic>? toJson() {
        return <String, dynamic>{
          // Settings
          'gameSettings': gameSettings.toJson(),
          'globalSettings': globalSettings.toJson(),
          // State
          'isRunning': isRunning,
          'isStarted': isStarted,
          'isFinished': isFinished,
          'animationInProgress': animationInProgress,
          // Base data
          'board': board.toJson(),
          // Game data
          'scores': scores,
        };
      }
    }