Click here to Skip to main content
15,893,508 members
Articles / Programming Languages / C#

Borg Knowledge Assimilator

Rate me:
Please Sign up or sign in to vote.
4.73/5 (20 votes)
24 Jul 200513 min read 79.9K   583   37  
A simple game that accumulates facts and appears to learn by conversing with players asking yes and no questions.
using System;
using System.Runtime.InteropServices;

namespace Borg {
	public class ConsoleEx {
      [DllImport("kernel32.dll", SetLastError=true)]
      public static extern bool SetConsoleTextAttribute(int hConsoleOutput, short wAttributes);
      [DllImport("kernel32.dll", EntryPoint="GetStdHandle", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
      private static extern int  GetStdHandle(Int32 stdHandle);

      private  const	   Int32	   STD_OUTPUT_HANDLE = -11;
      private  static   int      consoleHandle     = int.MinValue;

      /// <summary>
      /// Colour primitives enumeration.
      /// </summary>
      [Flags]
      private enum CP : short {
         Black   = 0,
         Blue        = 1,
         Green       = 2,
         Red         = 4,
         Intensity   = 8
      }

      /// <summary>
      /// An enumeration of colours available for console output.  The decimal
      /// numbers represent binary bit flags.
      /// </summary>
      public enum Colour : short {
   //                  Black        Blue        Green       Red      Intensity
   /*00*/Black       = CP.Black, 
   /*01*/DarkBlue    =              CP.Blue,
   /*02*/DarkGreen   =                          CP.Green,
   /*03*/DarkCyan    =              CP.Blue |   CP.Green,
   /*04*/DarkRed     =                                      CP.Red,
   /*05*/DarkMagenta =              CP.Blue |               CP.Red,
   /*06*/DarkYellow  =                          CP.Green |  CP.Red,
   /*07*/Gray        =              CP.Blue |   CP.Green |  CP.Red,
   /*08*/DarkGray    =                                               CP.Intensity,
   /*09*/Blue        =              CP.Blue |                        CP.Intensity,
   /*10*/Green       =                          CP.Green |           CP.Intensity,
   /*11*/Cyan        =              CP.Blue |   CP.Green |           CP.Intensity,
   /*12*/Red         =                                      CP.Red | CP.Intensity,    
   /*13*/Magenta     =              CP.Blue |               CP.Red | CP.Intensity,
   /*14*/Yellow      =                          CP.Green |  CP.Red | CP.Intensity,
   /*15*/White       =              CP.Blue |   CP.Green |  CP.Red | CP.Intensity 
      }

      /// <summary>
      /// Type constructor called to determine the handle to the output
      /// window.  The handle is needed for all subsequent API calls
      /// to console output functions.
      /// </summary>
      static ConsoleEx() {
         consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
      }

      /// <summary>
      /// Sets the colour of the text sent to the console back to white.
      /// </summary>
      private static void ResetColour() {
         SetConsoleTextAttribute(consoleHandle, (short)Colour.White);
      }

      /// <summary>
      /// Reads a line of text from the user in a particular colour.
      /// </summary>
      /// <param name="colour">The Colour to use in which to display
      /// the solicited data.</param>
      /// <returns>The string entered by the user.</returns>
      public static string ReadLine(Colour colour) {
         SetConsoleTextAttribute(consoleHandle, (short)colour);
         string data = Console.ReadLine();
         ResetColour();

         return data;
      }

      public static void WriteLine(string message, Colour colour) {
         SetConsoleTextAttribute(consoleHandle, (short)colour);
         System.Console.WriteLine(message);
         ResetColour();
      }

      public static void Write(string message, Colour colour) {
         SetConsoleTextAttribute(consoleHandle, (short)colour);
         System.Console.Write(message);
         ResetColour();
      }
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Technical Lead
Canada Canada
I'm a graduate of the University of Toronto with a degree in zoology. I'm currently a software development manager with a large Canadian financial institution, and a passionate squash player.

I am a proud daddy to Alex and Sarah.

Comments and Discussions