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; }
public void RemovePointFrom(Player p) { if (Scores.ContainsKey(p) && Scores[p].Score > 0) { Scores[p].Score--; } }
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() }); } } }
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)); }
public int ScoreOf(Player p) { if (Scores.ContainsKey(p)) { return Scores[p].Score; } else { return 0; } }
public void AddPointTo(Player p) { if (Scores.ContainsKey(p)) { Scores[p].Score++; } else { Scores.Add(p, new PlayerScore() { Score = 1 }); } }
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; }
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; }
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]); }
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; } }
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; }
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; }
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); }
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; }
public abstract bool CanPlay(Player p);
public int ScoreOf(Player p) { return board.ScoreOf(p); }
private Player getNextPlayer(Player p) { return _players[(Array.IndexOf(_players, p) + 1) % _players.Length]; }
public void MakeCurrentTurnOf(Player p) { Current = p; }
public bool IsPlayerInGame(Player p) { return _players.Contains(p); }
public PlayerQueue(Player p1, Player p2) { _players = new Player[] { p1, p2 }; Current = _players[0]; }
private void PutTokens(Player p, IEnumerable<Coord> coords) { foreach (Coord item in coords) { PutToken(p, item.x, item.y); } }
public void PutToken(Player p, int x, int y) { if (!IsValid(p,x,y)) { throw new ArgumentException(); } board[x, y] = p; }
public bool IsValid(Player p, int x, int y) { return board.IsInBoard(x, y) && board[x,y] != p && playerQueue.IsPlayerInGame(p); }
public RevGame(Player black, Player white) : this(new PlayerQueue(black, white)) { }
protected bool IsValidPlay(Player p, int x, int y) { return board.IsEmpty(x, y) && IsValid(p, x, y); }
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; }
public void LoadBoard(Board b, Player p) { this.board = b; playerQueue.MakeCurrentTurnOf(p); }
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() }); } } }
public void TestPutTokenWithNonValidPlayer() { var board = new RevGame(); var player = new Player(0); board.PutToken(player, 5, 5); }
private bool LineShouldChangeOwnership(int length, OffsetCalculator calc, Player p) { return length > 0 && board[calc.ResultX, calc.ResultY] == p; }