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 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 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 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() }); } } }
private IEnumerable<Coord> ListChangesInLine(int length, OffsetCalculator calc) { for (int i = length - 1; i > 0; i--) { calc.MakeOffset(i); yield return new Coord(calc.ResultX, calc.ResultY); } }
private bool LineShouldChangeOwnership(int length, OffsetCalculator calc, Player p) { return length > 0 && board[calc.ResultX, calc.ResultY] == p; }
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; }