private Rack ReadRack(IList <char> list, Player current) { string newRack = null; while (!Rack.Check(newRack)) { using (DisposableColor.Prompt) Console.Write("rack? "); if (list.Count > 0) { Console.Write("["); using (PlayerScore.GetCurrentColor(current)) Console.Write(new string(list.ToArray())); Console.Write("] "); } using (PlayerScore.GetCurrentColor(current)) newRack = GetNewRack(graph, list, ReadLine()); } using (new DisposableColor(ConsoleColor.White)) Console.Write("rack:"); using (PlayerScore.GetCurrentColor(current)) Console.WriteLine(" " + newRack); Console.WriteLine(); return(new Rack(newRack)); }
public void TestPlay(string word, int points, int stars) { var part = new WordPart(word, new Cell(4, 3), Direction.Right); var score = PlayerScore.Play(part); Assert.AreEqual(points, score.Points); Assert.AreEqual(stars, score.Stars); }
public void Write(Player currentPlayer) { using (PlayerScore.GetCurrentColor(currentPlayer)) Console.Write(current); Console.Write(" / "); using (PlayerScore.GetOtherColor(currentPlayer)) Console.Write(other); }
[Test] public void TestSkip() { var first = new PlayerScore(1, 2); var second = new PlayerScore(3, 4); var score = new Score(first, second); Assert.AreEqual(first, score.Current); Assert.AreEqual(second, score.Other); score = score.Skip(); Assert.AreEqual(second, score.Current); Assert.AreEqual(first, score.Other); }
[Test] public void TestSkip() { var board = new Board(); var part1 = new WordPart("TRICHE", new Cell(4, 0), Direction.Right); board = board.Play(part1); var first = new PlayerScore(6, 1); var second = new PlayerScore(); Assert.AreEqual(second, board.Score.Current); Assert.AreEqual(first, board.Score.Other); board = board.Skip(); Assert.AreEqual(first, board.Score.Current); Assert.AreEqual(second, board.Score.Other); }
public Board Play(WordPart part, IList <LetterPlay> played, WordPartCollection extras) { var start = new ConstantSet <Cell>(GetStartCells()); var list = new List <Cell>(); list.Add(part.First); var current = part.First; for (int i = 1; i < part.Word.Length; i++) { if (!current.TryGetNext(part.Direction, out current)) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Cannot play {0} at {1}", part.Word, part.First)); } list.Add(current); } if (!Contains(start, list)) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Cannot play {0} at {1}", part.Word, part.First)); } var newBoard = BuildBoard(); var newOwner = BuildOwner(); for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { newBoard[i][j] = board[i][j]; newOwner[i][j] = owner[i][j]; } } var newPlayer = player == Player.First ? Player.Second : Player.First; int taken = 0; var newCells = new HashSet <Cell>(cells); for (int i = 0; i < part.Word.Length; i++) { Cell c = list[i]; char letter = part.Word[i]; if (newBoard[c.Row][c.Column] == Empty || newOwner[c.Row][c.Column] != player) { if (newOwner[c.Row][c.Column] == newPlayer) { taken++; } newBoard[c.Row][c.Column] = letter; newOwner[c.Row][c.Column] = player; newCells.Add(c); if (CellUpdated != null) { CellUpdated(this, new CellUpdatedEventArgs(c, letter)); } } } int points = played.Count; int stars = 0; bool vortex = false; foreach (LetterPlay lp in played) { if (lp.Cell.IsStar) { stars++; } else if (lp.Cell.IsVortex) { vortex = true; } } foreach (WordPart extra in extras) { var cell = extra.First; for (int i = 0; i < extra.Word.Length; i++) { if (newOwner[cell.Row][cell.Column] != player) { newOwner[cell.Row][cell.Column] = player; taken++; } if (i < extra.Word.Length - 1) { switch (extra.Direction) { case Direction.Down: cell = cell.Down; break; case Direction.Right: cell = cell.Right; break; } } } } var newOther = new PlayerScore(score.Current.Points + points + taken + (vortex ? score.Current.Stars + stars : 0), vortex ? 0 : score.Current.Stars + stars); var newCurrent = new PlayerScore(score.Other.Points - taken, vortex ? 0 : score.Other.Stars); var newScore = new Score(newCurrent, newOther); var result = new Board(newBoard, newOwner, newCells.ToConstant(), newPlayer, newScore); result.CellUpdated = CellUpdated; return(result); }
public Score(PlayerScore current, PlayerScore other) { this.current = current; this.other = other; }
public void Run() { Rack rack = null; while (true) { try { bool skipped = false; var pending = new List <char>(); while (!board.Score.Other.Wins) { board.Write(); Console.WriteLine(); if (!skipped) { rack = ReadRack(pending, board.Current); } skipped = false; PlayPath move = ReadMove(board, rack); if (move == null) { board = board.Skip(); skipped = true; continue; } using (PlayerScore.GetCurrentColor(board.Current)) move.Write(); board = board.Play(move); pending.Clear(); pending.AddRange(move.Pending); bool vortex = false; foreach (LetterPlay lp in move.Played) { vortex |= lp.Cell.IsVortex; } if (vortex) { board.Write(); Console.WriteLine(); board = board.Clear(); pending.Clear(); } } board.Write(); rack = null; using (new DisposableColor(PlayerScore.GetColor(board.Other))) Console.WriteLine("{0} wins", board.Other); Console.WriteLine(); } catch (GiveUpException) { if (board.IsEmpty && rack == null) { return; } rack = null; } board = new Board(); } }
private PlayPath ReadMove(Board board, Rack rack) { var regex = new Regex(@"^\s*(?<word>[a-z]+)\s*(?<row>\d),(?<column>\d)\s*(?<direction>(down|right|))\s*$", RegexOptions.IgnoreCase); using (DisposableColor.Prompt) Console.Write("move? "); Console.Write("[word r,c down|right] [play|guess|skip] "); while (true) { try { string line = null; using (new DisposableColor(PlayerScore.GetColor(board.Current))) line = ReadLine().Trim(); if (line == "skip") { return(null); } if (line == "play" || line == "guess") { if (board.IsEmpty) { var game = new Game(random, graph); WordPart part = game.GetFirstWord(rack); Console.WriteLine(part); Console.WriteLine(); var played = part.GetPlayed(); var pending = new List <char>(rack.Letters); foreach (LetterPlay lp in played) { pending.Remove(lp.Letter); } var path = new PlayPath(part, new WordPartCollection(), part.GetPlayed(), pending.ToConstant()); if (line == "play") { return(path); } } else { PlayPath path = FindMove(rack); if (line == "play") { return(path); } } } Match m = regex.Match(line); if (m.Success) { string word = m.Groups["word"].Value; int row = int.Parse(m.Groups["row"].Value, CultureInfo.InvariantCulture); int column = int.Parse(m.Groups["column"].Value, CultureInfo.InvariantCulture); Direction?direction = Parse(m.Groups["direction"].Value); PlayPath path = GetPath(board, rack, word, row, column, direction); if (path != null) { Console.WriteLine(); return(path); } } Console.Write("? "); } catch (FormatException e) { var color = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{0} : {1}", e.GetType().Name, e.Message); Console.ForegroundColor = color; Console.Write("? "); } } }