Advertisement
Guest User

puzzle.cs

a guest
Nov 10th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace WordFinderGame
  7. {
  8.     /// <summary>
  9.     /// The Puzzle class keeps track of the puzzle grid. The Game uses it to draw
  10.     /// the initial grid to the screen, and the WordFinder class uses it to check if the
  11.     /// player's input contains only letters from the grid.
  12.     /// </summary>
  13.     class Puzzle
  14.     {
  15.         /// <summary>
  16.         /// Randomizer
  17.         /// </summary>
  18.         private Random random = new Random();
  19.  
  20.         /// <summary>
  21.         /// Consonants (including Y)
  22.         /// </summary>
  23.         public static readonly char[] Consonants = { 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K',
  24.                'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z' };
  25.  
  26.         /// <summary>
  27.         /// Vowels (including Y)
  28.         /// </summary>
  29.         public static readonly char[] Vowels = { 'A', 'E', 'I', 'O', 'U', 'Y' };
  30.  
  31.         /// <summary>
  32.         /// Backing field for Letters property
  33.         /// </summary>
  34.         char[] letters;
  35.  
  36.         /// <summary>
  37.         /// Get the letters in the puzzle
  38.         /// </summary>
  39.         public IEnumerable<char> Letters
  40.         {
  41.             get { return letters; }
  42.         }
  43.  
  44.         /// <summary>
  45.         /// The number of letters in the puzzle
  46.         /// </summary>
  47.         private int puzzleLength;
  48.  
  49.         /// <summary>
  50.         /// Puzzle Constructor
  51.         /// </summary>
  52.         /// <param name="puzzleLength">The number of letters in the puzzle</param>
  53.         /// <param name="vowelEvery">Every nth letter is a vowel</param>
  54.         public Puzzle(int puzzleLength, int vowelEvery)
  55.         {
  56.             this.puzzleLength = puzzleLength;
  57.  
  58.             letters = new char[puzzleLength];
  59.  
  60.             for(int i = 0; i< puzzleLength; i++)
  61.             {
  62.                 if(i % vowelEvery == 0)
  63.                     letters[i] = Vowels[random.Next(Vowels.Length)];
  64.                 else
  65.                     letters[i] = Consonants[random.Next(Consonants.Length)];
  66.             }
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Draw the puzzle at a specific point on the screen
  71.         /// </summary>
  72.         /// <param name="left">The column position of the cursor</param>
  73.         /// <param name="top">The row position of the cursor</param>
  74.         public void Draw(int left, int top)
  75.         {
  76.             int oldTop = Console.CursorTop;
  77.             int oldLeft = Console.CursorLeft;
  78.  
  79.             Console.BackgroundColor = ConsoleColor.Gray;
  80.  
  81.             // Create the random puzzle using random letters and print them
  82.             for (int i = 0; i < puzzleLength; i++)
  83.             {
  84.                 // Use cursor movement to draw the rows of the square puzzle grid
  85.                 if(i % Math.Floor(Math.Sqrt(puzzleLength)) == 0)
  86.                 {
  87.                     Console.CursorTop = top++;
  88.                     Console.CursorLeft = left;
  89.                 }
  90.                 if(Vowels.Contains(letters[i]))
  91.                     Console.ForegroundColor = ConsoleColor.DarkRed;
  92.                 else
  93.                     Console.ForegroundColor = ConsoleColor.DarkBlue;
  94.  
  95.                 Console.Write(" {0} ", letters[i]);
  96.             }
  97.             Console.ResetColor();
  98.  
  99.             Console.CursorTop = oldTop;
  100.             Console.CursorLeft = oldLeft;
  101.         }
  102.  
  103.  
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement