示例#1
0
文件: Program.cs 项目: dedale/wordox
        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));
        }
示例#2
0
        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);
        }
示例#3
0
文件: Score.cs 项目: dedale/wordox
 public void Write(Player currentPlayer)
 {
     using (PlayerScore.GetCurrentColor(currentPlayer))
         Console.Write(current);
     Console.Write(" / ");
     using (PlayerScore.GetOtherColor(currentPlayer))
         Console.Write(other);
 }
示例#4
0
        [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);
        }
示例#5
0
文件: Board.cs 项目: dedale/wordox
        [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);
        }
示例#6
0
        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);
        }
示例#7
0
文件: Score.cs 项目: dedale/wordox
 public Score(PlayerScore current, PlayerScore other)
 {
     this.current = current;
     this.other   = other;
 }
示例#8
0
文件: Program.cs 项目: dedale/wordox
        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();
            }
        }
示例#9
0
文件: Program.cs 项目: dedale/wordox
        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("? ");
                }
            }
        }