Example #1
0
 public Gameplay(PlayStrategy ai)
 {
     _ai = ai;
     _ai.SetMark(BoardMark.O);
     _whoGoesNow = BoardMark.X;
     _board = new BoardState();
 }
Example #2
0
 public GameTurn(string playerId, string gameId, int turn, BoardMark boardMark)
 {
     PlayerId = playerId;
     GameId = gameId;
     Turn = turn;
     BoardMark = boardMark;
 }
Example #3
0
 public IEnumerable<int> GetPositions(BoardMark mark)
 {
     return _board
         .Select((m, p) => new {Mark = m, Position = p})
         .Where(b => b.Mark == mark)
         .Select(b => b.Position);
 }
Example #4
0
        private static IEnumerable<BobDecisionTree> AllDecisionsFromHere(BoardState board, BoardMark whosTurn, int decisionsCount)
        {
            IEnumerable<int> positions2Go = new BoardSymmetry(board).GetFreeUniquePositions();
            if (CanOptimizeTreeBuilding(decisionsCount, positions2Go))
            {
                positions2Go = positions2Go.AsParallel();
            }

            return positions2Go
                .Select(p => new { NewBoard = board.Set(p, whosTurn), Position = p })
                .Select(p => new BobDecisionTree(p.NewBoard, whosTurn.OpponentsMark(), p.Position, decisionsCount + 1))
                .ToArray();
        }
Example #5
0
        private BobDecisionTree(BoardState board, BoardMark whosTurn, int position, int decisionsCount)
        {
            _board = board;
            _position = position;
            _decisionsCount = decisionsCount;

            _children = board.HasCompleteLine()
                            ? Enumerable.Empty<BobDecisionTree>()
                            : AllDecisionsFromHere(board, whosTurn, decisionsCount);
            _endsFromHere = IsEndDecision
                                ? new[] {this}
                                : CollectEndDecisions();
            _treeSize = _children.Select(ch => ch.TreeSize).Sum() + 1;
        }
Example #6
0
 //Set the mark type, that will be kept in the tile until the tile receives the OnUnSelected event
 public void SetTemporaryMarkType(BoardMark.MarkType type)
 {
     mark.SetType(type);
 }
Example #7
0
 //Set the mark type, and it will be set as the permanent mark type, that is, the one to change to when the tile receives the OnUnSelected event
 public void SetPermanentMarkType(BoardMark.MarkType type)
 {
     permanentMarkType = type;
     mark.SetType(type);
 }
Example #8
0
 public IEnumerable<int> GetPositionsToCompleteLine(BoardMark mark)
 {
     return _winPositions
         .Where(line => line.Count(GetPositions(mark).Contains) == 2)
         .SelectMany(line => line)
         .Where(p => _board.ElementAt(p) == BoardMark._);
 }
Example #9
0
 private bool CompleteLine(IEnumerable<int> winLine, BoardMark mark)
 {
     return winLine.All(GetPositions(mark).Contains);
 }
Example #10
0
 public BoardState Set(int position, BoardMark mark)
 {
     if (_board.ElementAt(position) != BoardMark._)
     {
         throw new InvalidOperationException();
     } else
     {
         return new BoardState(_board.Select((oldMark, p) => p == position ? mark : oldMark));
     }
 }
Example #11
0
 private double WinForecast(BoardMark whosTurn, int movesToGetTurn)
 {
     return WinEndsFor(whosTurn).Select(e => e.Weight(movesToGetTurn)).Sum();
 }
Example #12
0
 public void Reset()
 {
     _whoGoesNow = BoardMark.X;
     _board = new BoardState();
     OnChanged();
 }
Example #13
0
 private static BoardState SetUpBoard(IEnumerable<int> line, BoardMark mark)
 {
     return line.Aggregate(new BoardState(), (current, position) => current.Set(position, mark));
 }
Example #14
0
 public double GetWinFactor(BoardMark whosTurn)
 {
     return WinForecast(whosTurn, 0) - WinForecast(whosTurn.OpponentsMark(), 1);
 }
Example #15
0
 public BobDecisionTree(BoardState board, BoardMark whosTurn)
     : this(board, whosTurn, -1, 0)
 {
 }
Example #16
0
 //Called at when creating the tile, it instantiates the mark
 private void CreateMark()
 {
     GameObject go = GameObject.Instantiate(Resources.Load("Board/BoardMark/BoardMark"), worldPos, Quaternion.identity) as GameObject;
     mark = go.GetComponent<BoardMark>();
 }
Example #17
0
 public void SetMark(BoardMark mark)
 {
     OwnMark = mark;
 }
Example #18
0
 private IEnumerable<int> GetForkPositions(BoardMark mark)
 {
     return _board.FreePositions
         .Where(p => _board.Set(p, mark).GetPositionsToCompleteLine(mark).Count() > 1);
 }
Example #19
0
 private IEnumerable<BobDecisionTree> WinEndsFor(BoardMark player)
 {
     return _endsFromHere.Where(d => d._board.HasCompleteLine(player));
 }
Example #20
0
 private void SetAMark(int position)
 {
     if (!Board.FreePositions.Any(p => p == position))
     {
         throw new InvalidOperationException(String.Format("Cannot go to position {0}!", position));
     }
     else
     {
         _board = _board.Set(position, WhoGoesNow);
         _whoGoesNow = WhoGoesNow.OpponentsMark();
     }
 }
Example #21
0
 public bool HasCompleteLine(BoardMark mark)
 {
     return _winPositions.Any(line => CompleteLine(line, mark));
 }