示例#1
0
 public void Write()
 {
     Console.Write("  ");
     using (new DisposableColor(ConsoleColor.White))
         for (int c = 0; c < Width; c++)
         {
             Console.Write("{0} ", c);
         }
     Console.WriteLine();
     for (int r = 0; r < Height; r++)
     {
         using (new DisposableColor(ConsoleColor.White))
             Console.Write("{0} ", r);
         for (int c = 0; c < Width; c++)
         {
             var cell = new Cell(r, c);
             using (new ColoredCell(board[r][c], PlayerScore.GetColor(owner[r][c])))
                 Console.Write("{0} " + (c == Width - 1 ? "\n" : ""), board[r][c] == Empty ? (cell.IsVortex ? '@' : (cell.IsStar ? '*' : '.')) : board[r][c]);
         }
     }
     using (new DisposableColor(ConsoleColor.White))
         Console.Write("score: ");
     score.Write(player);
     Console.WriteLine();
 }
示例#2
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();
            }
        }
示例#3
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("? ");
                }
            }
        }