コード例 #1
0
        public string DrawBoardToString(GenericTicTacToeGame game)
        {
            // Since potentially we could be dealing with a huge number of squares (there is no
            // limit on board size), then use a string builder to save memory
            StringBuilder str_sp = new StringBuilder();

            for (int i = 0; i < game.BoardWidth; i++)
            {
                for (int j = 0; j < game.BoardHeight; j++)
                {
                    Space space = game.SpaceAt(i, j);

                    switch (space.State)
                    {
                    case SpaceState.O:
                        str_sp.Append("O"); break;

                    case SpaceState.X:
                        str_sp.Append("X"); break;

                    default:
                        str_sp.Append("-"); break;
                    }
                }
                str_sp.Append(Environment.NewLine);
            }

            return(str_sp.ToString());
        }
コード例 #2
0
        public Space NextMove(GenericTicTacToeGame game)
        {
            Random r = new Random();

            // Select a random square
            List <Space> available = game.GetAvailableSpaces();

            if (available.Count == 0) // None left
            {
                return(null);
            }
            else // Get a random space from the remaining set
            {
                return(available.ElementAt(r.Next(0, available.Count - 1)));
            }
        }
コード例 #3
0
 public void DrawBoardToOutput(GenericTicTacToeGame game)
 {
     Console.Write(DrawBoardToString(game));
 }
コード例 #4
0
 public bool HasFinished(GenericTicTacToeGame game)
 {
     return((from s in game.Spaces where !s.HasBeenPlayed() select s).FirstOrDefault() == default(Space));
 }