Beispiel #1
0
        public IEnumerable<Coord> MakeListOfConvertedTokens(Player p, int x, int y)
        {
            var playChanges = new List<Coord>();

            OffsetCalculator calc = new OffsetCalculator(x, y);

            for (int dir = 0; dir < 9; dir++)
            {
                calc.setDirection(dir);
                int length = CountLine(p,calc);
                calc.MakeOffset(length);

                if (LineShouldChangeOwnership(length, calc, p))
                {
                    var lineChanges = ListChangesInLine(length, calc);
                    playChanges.AddRange(lineChanges);
                }
            }

            if(playChanges.Any())
            {
                playChanges.Add(new Coord(x, y));
            }

            return playChanges;
        }
Beispiel #2
0
 public void RemovePointFrom(Player p)
 {
     if (Scores.ContainsKey(p) && Scores[p].Score > 0)
     {
         Scores[p].Score--;
     }
 }
Beispiel #3
0
        public void Play(Player p, Coord start, Coord end)
        {
            if (CanSelectToPlay(p, start.x, start.y) && board.IsEmpty(end.x, end.y))
            {
                var distance = end - start;
                int travelLength = Math.Abs(distance.x) + Math.Abs(distance.y);

                //Move -> Remove original
                if (travelLength == 2)
                {
                    board[start.x, start.y] = null;
                }

                //Copy (or Move) -> Put token
                PutToken(p, end.x, end.y);
                var offsetCalc = new OffsetCalculator(end.x, end.y);
                for (int i = 0; i < 9; i++)
                {
                    offsetCalc.setDirection(i);
                    offsetCalc.MakeOffset(1);

                    if (board.CanOvertakeToken(p, offsetCalc.ResultX, offsetCalc.ResultY))
                    {
                        PutToken(p, offsetCalc.ResultX, offsetCalc.ResultY);
                    }
                }

                HandlePlayerChange();

                if (SnapshotContainer != null)
                {
                    SnapshotContainer.TakeSnapShot(new TwoPartTurn() { Start = start, End = end, PlayerThatPlayed = p, PlayerToPlay = CurrentPlayer, Board = board.Clone() });
                }
            }
        }
Beispiel #4
0
        public void TestPutTokenOnEmptySpace()
        {
            var board = new Board();
            Player b = new Player(Token.Black);
            Player w = new Player(Token.White);

            board[0, 0] = b;
            Assert.AreEqual(1, board.ScoreOf(b));
            Assert.AreEqual(0, board.ScoreOf(w));
        }
Beispiel #5
0
 public int ScoreOf(Player p)
 {
     if (Scores.ContainsKey(p))
     {
         return Scores[p].Score;
     }
     else
     {
         return 0;
     }
 }
Beispiel #6
0
 public void AddPointTo(Player p)
 {
     if (Scores.ContainsKey(p))
     {
         Scores[p].Score++;
     }
     else
     {
         Scores.Add(p, new PlayerScore() { Score = 1 });
     }
 }
Beispiel #7
0
 public override bool CanPlay(Player p)
 {
     SpiralCoordEnumerator enumerator = new SpiralCoordEnumerator(8,1);
     while(enumerator.MoveNext())
     {
         var coord = enumerator.Current;
         if (CanPlayThere(p, coord.x, coord.y))
         {
             return true;
         }
     }
     return false;
 }
Beispiel #8
0
 public override bool CanPlay(Player p)
 {
     for (int i = 0; i < 8; i++)
     {
         for (int j = 0; j < 8; j++)
         {
             if (CanSelectToPlay(p, i, j))
             {
                 return true;
             }
         }
     }
     return false;
 }
Beispiel #9
0
        public void TestCloneBoard()
        {
            var board = new Board();
            var player = new Player(Token.Black);
            board[0, 0] = player;

            var clonedBoard = board.Clone();
            Assert.AreEqual(player, clonedBoard[0, 0]);

            var white = new Player(Token.White);
            clonedBoard[4, 7] = white;

            Assert.AreNotEqual(board[4, 7], clonedBoard[4, 7]);
        }
Beispiel #10
0
 public bool CanPlayThere(Player p, Coord start, int x, int y)
 {
     if (board.IsInBoard(x,y) && board.IsEmpty(x,y))
     {
         Coord end = new Coord(x, y);
         var distance = end - start;
         int travelLength = Math.Abs(distance.x) + Math.Abs(distance.y);
         return travelLength == 1 || travelLength == 2;
     }
     else
     {
         return false;
     }
 }
Beispiel #11
0
        protected Dictionary<Coord, int> MakeListOfPossiblePlays(Player p, RevGame game)
        {
            int col = 8;
            int row = 8;
            Dictionary<Coord,int> d = new Dictionary<Coord, int>();

            for (int i = 0; i < col; i++)
            {
                for (int j = 0; j < row; j++)
                {
                    if (game.CanPlayThere(p, i, j))
                    {
                        var tokens = game.MakeListOfConvertedTokens(p, i, j);
                        d[new Coord(i,j)] = tokens.Count();
                    }
                }
            }
            return d;
        }
Beispiel #12
0
        public bool CanSelectToPlay(Player p, int x, int y)
        {
            var offcalc = new OffsetCalculator(x, y);

            if(board[x,y] == p){
                for (int i = 0; i < 9; i++)
                {
                    offcalc.setDirection(i);
                    for (int offset = 1; offset <= 2; offset++)
                    {
                        offcalc.MakeOffset(offset);
                        if (board.IsInBoard(offcalc.ResultX, offcalc.ResultY) && board.IsEmpty(offcalc.ResultX, offcalc.ResultY))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
Beispiel #13
0
        public void TestIsEmpty()
        {
            Player p = new Player(Token.Black);

            Board board = new Board();
            Action<int, int, bool> test = (x, y, expected) =>
            {
                bool actual = board.IsEmpty(x, y);
                Assert.AreEqual(expected, actual);
            };

            test(0, 0, true);

            board[0, 0] = p;

            test(0, 0, false);

            board[0, 0] = null;

            test(0, 0, true);
        }
Beispiel #14
0
        public bool CanPlayThere(Player p, int x, int y)
        {
            if (!IsValidPlay(p,x,y))
            {
                return false;
            }

            var calc = new OffsetCalculator(x, y);
            for (int dir = 0; dir < 9; dir++)
            {
                calc.setDirection(dir);
                int offset = CountLine(p,calc);

                if (offset > 1)
                {
                    return true;
                }
            }

            return false;
        }
Beispiel #15
0
 public abstract bool CanPlay(Player p);
Beispiel #16
0
 public int ScoreOf(Player p)
 {
     return board.ScoreOf(p);
 }
Beispiel #17
0
 private Player getNextPlayer(Player p)
 {
     return _players[(Array.IndexOf(_players, p) + 1) % _players.Length];
 }
Beispiel #18
0
 public void MakeCurrentTurnOf(Player p)
 {
     Current = p;
 }
Beispiel #19
0
 public bool IsPlayerInGame(Player p)
 {
     return _players.Contains(p);
 }
Beispiel #20
0
 public PlayerQueue(Player p1, Player p2)
 {
     _players = new Player[] { p1, p2 };
     Current = _players[0];
 }
Beispiel #21
0
 private void PutTokens(Player p, IEnumerable<Coord> coords)
 {
     foreach (Coord item in coords)
     {
         PutToken(p, item.x, item.y);
     }
 }
Beispiel #22
0
        public void PutToken(Player p, int x, int y)
        {
            if (!IsValid(p,x,y))
            {
                throw new ArgumentException();
            }

            board[x, y] = p;
        }
Beispiel #23
0
 public bool IsValid(Player p, int x, int y)
 {
     return board.IsInBoard(x, y) && board[x,y] != p && playerQueue.IsPlayerInGame(p);
 }
Beispiel #24
0
 public RevGame(Player black, Player white)
     : this(new PlayerQueue(black, white))
 {
 }
Beispiel #25
0
 protected bool IsValidPlay(Player p, int x, int y)
 {
     return board.IsEmpty(x, y) && IsValid(p, x, y);
 }
Beispiel #26
0
        private int CountLine(Player p, OffsetCalculator calc)
        {
            int offset = 0;

            do
            {
                offset++;
                calc.MakeOffset(offset);
            } while (board.CanOvertakeToken(p, calc.ResultX, calc.ResultY));

            if (!board.IsInBoard(calc.ResultX, calc.ResultY) || board[calc.ResultX, calc.ResultY] != p)
            {
                offset = 0;
            }

            return offset;
        }
Beispiel #27
0
 public void LoadBoard(Board b, Player p)
 {
     this.board = b;
     playerQueue.MakeCurrentTurnOf(p);
 }
Beispiel #28
0
        public void Play(Player p, int x, int y)
        {
            if (!IsValidPlay(p, x, y))
            {
                return;
            }

            var list = MakeListOfConvertedTokens(p, x, y);
            if (list.Any())
            {
                PutTokens(p, list);
                HandlePlayerChange();
                if (SnapshotContainer != null)
                {
                    SnapshotContainer.TakeSnapShot(new OnePartTurn() { Coord = new Coord(x, y), PlayerThatPlayed = p, PlayerToPlay = CurrentPlayer, Board = board.Clone() });
                }
            }
        }
Beispiel #29
0
        public void TestPutTokenWithNonValidPlayer()
        {
            var board = new RevGame();
            var player = new Player(0);

            board.PutToken(player, 5, 5);
        }
Beispiel #30
0
 private bool LineShouldChangeOwnership(int length, OffsetCalculator calc, Player p)
 {
     return length > 0 && board[calc.ResultX, calc.ResultY] == p;
 }