Skip to content
Snippets Groups Projects
Select Git revision
  • 5e60eed82cb206a801034953046bcad3a4c42892
  • master default protected
  • 38-upgrade-framework-and-dependencies
  • 20-improve-app-metadata
  • Release_0.8.0_32 protected
  • Release_0.7.2_31 protected
  • Release_0.7.1_30 protected
  • Release_0.7.0_29 protected
  • Release_0.6.0_28 protected
  • Release_0.5.0_27 protected
  • Release_0.4.0_26 protected
  • Release_0.3.2_25 protected
  • Release_0.3.1_24 protected
  • Release_0.3.0_23 protected
  • Release_0.2.1_22 protected
  • Release_0.2.0_21 protected
  • Release_0.1.2_20 protected
  • Release_0.1.1_19 protected
  • Release_0.1.0_18 protected
  • Release_0.0.17_17 protected
  • Release_0.0.16_16 protected
  • Release_0.0.15_15 protected
  • Release_0.0.14_14 protected
  • Release_0.0.13_13 protected
24 results

game_board.dart

Blame
  • game_board.dart 6.63 KiB
    // Copyright 2018 The Chromium Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style license that can be
    // found in the LICENSE file.
    
    enum PieceType {
      empty,
      black,
      white,
    }
    
    /// This method flips a black piece to a white one, and vice versa. I'm still
    /// unsure about having it as a global function, but don't know where else to
    /// put it.
    PieceType getOpponent(PieceType player) =>
        (player == PieceType.black) ? PieceType.white : PieceType.black;
    
    /// A position on the reversi board. Just an [x] and [y] coordinate pair.
    class Position {
      final int x;
      final int y;
    
      const Position(this.x, this.y);
    }
    
    /// An immutable representation of a reversi game's board.
    class GameBoard {
      static const int height = 8;
      static const int width = 8;
      final List<List<PieceType>> rows;
    
      // Because calculating out all the available moves for a player can be
      // expensive, they're cached here.
      final _availableMoveCache = <PieceType, List<Position>>{};
    
      /// Default constructor, which creates a board with pieces in starting
      /// position.
      GameBoard() : rows = _emptyBoard;
    
      /// Copy constructor.
      GameBoard.fromGameBoard(GameBoard other)
          : rows = List.generate(height, (i) => List.from(other.rows[i]));
    
      /// Retrieves the type of piece at a location on the game board.
      PieceType getPieceAtLocation(int x, int y) {
        assert(x >= 0 && x < width);
        assert(y >= 0 && y < height);
        return rows[y][x];
      }
    
      /// Gets the total number of pieces of a particular type.
      int getPieceCount(PieceType pieceType) {
        return rows.fold(
          0,
          (s, e) => s + e.where((e) => e == pieceType).length,
        );
      }
    
      /// Calculates the list of available moves on this board for a player. These
      /// moves are calculated for the first call and cached for any subsequent
      /// ones.
      List<Position> getMovesForPlayer(PieceType player) {
        if (player == PieceType.empty) {
          return [];
        }
    
        if (_availableMoveCache.containsKey(player)) {
          return _availableMoveCache[player]!;
        }
    
        final legalMoves = <Position>[];