主類:
using System;
namespace NumGuess
{
/// <summary>
/// Guess 的摘要說明。
/// </summary>
class Guess
{
ColorInput colorText = new ColorInput(); //控制臺彩色輸出類
private string choose = null; //對於 菜單的 選擇
private int[] random = new int[4]; //隨機產生的 壹個 4位數
private int[] guess = new int[4]; //存儲 用戶 猜測的 4位數
private bool again = false; //是否是 繼續遊戲 而不是開始新遊戲
//構造方法
public Guess()
{
}
//顯示遊戲開始的壹些信息
void Game()
{
colorText.SetTextColor((int) ColorInput.Foreground.Blue); //設置控制臺輸出字符的顏色
Console.WriteLine("\t\t\t\t猜數字遊戲\n\n");
colorText.SetTextColor((int) ColorInput.Foreground.Green);
Console.Write("\t遊戲說明:遊戲開始,您要猜隨機產生的壹個四位數,"
+ "數字的");
colorText.SetTextColor((int) ColorInput.Foreground.Red);
Console.Write("位置");
colorText.SetTextColor((int) ColorInput.Foreground.Green);
Console.Write("和");
colorText.SetTextColor((int) ColorInput.Foreground.Red);
Console.Write("大小");
colorText.SetTextColor((int) ColorInput.Foreground.Green);
Console.Write("都要猜對才算贏哦,有點像彩票哦,不過您每次只有");
colorText.SetTextColor((int) ColorInput.Foreground.Red);
Console.Write("10");
colorText.SetTextColor((int) ColorInput.Foreground.Green);
Console.Write("次機會哦~~!!!\n");
colorText.SetTextColor((int) ColorInput.Foreground.Purple);
Console.WriteLine("\t\t\t\t\t\t\t----By H_Q");
colorText.SetTextColor((int) ColorInput.Foreground.White);
Console.WriteLine("\t\t\t\t菜單(請選擇)");
Console.WriteLine("\t\t\t\t 1.開始");
Console.WriteLine("\t\t\t\t 0.退出");
Init();
}// Game
void Init()
{
choose = Console.ReadLine();
//判斷 用戶的 選擇
while (choose != "0")
{
if (choose.Equals("1"))
{
InitGame();
break;
}
//當 10次 中沒有猜對 判斷是否要繼續遊戲
if (choose.Equals("2") && again)
{
Input();
again = false;
}
else
{
Console.Write("請按遊戲菜單,輸入 1 或 0: ");
choose = Console.ReadLine();
continue;
}
} // while
} // Init()
//遊戲開始
void InitGame()
{
Random r = new Random();
int ran = r.Next(1000, 9999); //隨機產生壹個 4位數 下限1000 上限9999
//將該 4位數 存儲到數組中
for (int i =3; i >= 0; i --)
{
random[i] = ran%10;
ran /= 10;
}
Input();
} // InitGame()
//接受用戶的 輸入的 數字
void Input()
{
int count = 0; //用於記錄 用戶猜測的次數 10次 沒有猜對為輸
bool isFormat = false;
//若輸入的不正確 循環要求用戶輸入
do
{
Console.Write("請您輸入您猜測的4位數字: ");
string inputNum = Console.ReadLine();
//檢查用戶 輸入的合法性
if (inputNum.Length == 4)
{
int j = 0;
foreach (char ch in inputNum)
{
if (Char.IsNumber(ch))
{
j ++;
}
}
if (j == 4)
{
//將正確的輸入 存入 猜測數組中
for (int i = 0; i < 4; i ++)
{
guess[i] = int.Parse(inputNum.Substring(i, 1));
}
count ++;
isFormat = Compare(count);
}
else
{
isFormat = false;
}
}
else
{
isFormat = false;
}
}while (count < 10 && isFormat == false);
}//Input()
//比較 用戶猜測的 數字 與 隨機產生的數字 的位置 及 每位數的大小
bool Compare(int count)
{
string sameG = null; //用於標識 用戶猜測的數字中 已經 判等過的
string sameR = null; //用於標識 隨機產生的數字中 已經 被判等過的
int s = 0; //用於標識 用戶猜對大小的數字的個數
int p = 0; //用於標識 用戶猜對位置的數字的個數
//順序判等 用來 排除壹次就猜對的情況
for (int i = 0; i < 4; i ++)
{
if (guess[i] == random[i])
{
p ++;
s ++;
sameG += i;
sameR += i;
}
}
//壹次就猜對的情況
if (p == 4 && s == 4)
{
Win();
return true;
}
else
{
for (int g = 0; g < 4; g ++)
{
for (int r = 0; r < 4; r ++)
{
//如果順序比較中沒有壹個猜對那麽 sameG sameR都為null
//如果不為空 那麽通過 IndexOf 方法 將 已經判等過的 排除了 不須再次比較
//註: IndexOf(string) 此方法 從 string 中查找相同字符 若沒找到 返回-1
if (sameG == null || sameR == null ||
(sameR.IndexOf(r.ToString()) == -1 && sameG.IndexOf(g.ToString()) == -1))
{
if (guess[g] == random[r])
{
s ++;
sameG += g;
sameR += r;
break;
}
}
}
}
//在10次內猜對的情況
if (s == 4 && p == 4)
{
Win();
return true;
}
else
{
if (count >= 10)
{
Lost();
return true;
}
else
{
Console.WriteLine("猜對數字的個數為:{0}, 猜對位置的個數為:{1}, 您還有{2}次機會\n", s, p,10 - count);
return false;
}
}
}
} //Compare()
//贏咯~
void Win()
{
colorText.SetTextColor((int)ColorInput.Foreground.Green);
Console.WriteLine("\t\t\t\t您贏了\n");
colorText.ResetColor(); //還原控制臺 默認的 輸出顏色
Console.WriteLine("\t\t\t\t 菜單");
Console.WriteLine("\t\t\t\t1.新遊戲");
Console.WriteLine("\t\t\t\t0.退出");
colorText.SetTextColor((int)ColorInput.Foreground.White);
Init();
}
//再接再厲
void Lost()
{
colorText.SetTextColor((int)ColorInput.Foreground.Red);
Console.WriteLine("\t\t\t\t您輸了");
colorText.ResetColor();
Console.WriteLine("\t\t\t\t 菜單");
Console.WriteLine("\t\t\t\t1.新遊戲");
Console.WriteLine("\t\t\t\t2.繼續");
Console.WriteLine("\t\t\t\t0.退出");
again = true; //標識 是繼續 這樣不重新產生隨機的4位數
colorText.SetTextColor((int)ColorInput.Foreground.White);
Init();
}
public static void Main(string[] args)
{
Guess game = new Guess();
game.Game();
}
} //class
} // namespace
控制 控制臺 彩色輸出類:
using System;
using System.Runtime.InteropServices;
namespace NumGuess
{
//用於改變控制臺輸出顏色
public class ColorInput
{
private int hConsoleHandle;
private const int STD_OUTPUT_HANDLE = -11;
[DllImport("kernel32.dll")]
private static extern int GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
private static extern int SetConsoleTextAttribute(int hConsoleOutput,
int wAttributes);
// 構造方法
public ColorInput()
{
hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
}
//自己試出來的高亮顏色
public enum Foreground
{
Green = 10,
Blue,
Red,
Purple,
Golder,
White
}
public void SetTextColor(int color)
{
SetConsoleTextAttribute(hConsoleHandle, color);
}
public void ResetColor()
{
SetConsoleTextAttribute(hConsoleHandle, 7);
}
}//class
}//namespace
.Net 2003 中 C# 復雜控制臺清屏 類
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace NumGuess
{
class StdHandleEnum
{
public const int STD_INPUT_HANDLE = -10;
public const int STD_OUTPUT_HANDLE = -11;
public const int STD_ERROR_HANDLE = -12;
};
// This sructure contains a screen coordinate.
class cls
{
internal struct COORD
{
public short X;
public short Y;
}
// 屏幕緩沖區信息
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct CONSOLE_SCREEN_BUFFER_INFO
{
public COORD Size;
public COORD p1;
public short a1;
public short w1;
public short w2;
public short w3;
public short w4;
public COORD m1;
}
/*
* Kernel32.dll 中 4個函數
*/
//返回 handle 給 任何標準輸入輸出
[DllImport("kernel32.dll")]
public static extern int GetStdHandle(int nStdHandle);
//返回 控制臺 緩沖區 信息
[DllImport("kernel32.dll")]
public static extern bool GetConsoleScreenBufferInfo(int hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
//放置光標
[DllImport("kernel32.dll")]
public static extern bool SetConsoleCursorPosition(int hConsoleOutput, COORD dwCursorPosition);
// 不知道該如何翻譯
// The FillConsoleOutputCharacter() allows us to place
// any character on the console screen. Using a space
// clears the display area.
[DllImport("kernel32.dll",SetLastError=true,CharSet=CharSet.Auto)]
public static extern bool FillConsoleOutputCharacter(int hConsoleOutput, short cCharacter, int nLength, COORD WriteCoord, out int lpNumberOfCharsWritten);
[STAThread]
public static void Clear()
{
// Needed ask Windows about the console screen
// buffer settings.
CONSOLE_SCREEN_BUFFER_INFO CSBI;
// Handle to the output device.
int hOut;
// Number of characters written to the screen.
int CharOut;
// Home cursor position.
COORD Home;
// Clear the screen.
// Begin by getting a handle to the console screen.
hOut = GetStdHandle(StdHandleEnum.STD_OUTPUT_HANDLE);
// Get the required console screen buffer information.
GetConsoleScreenBufferInfo(hOut, out CSBI );
// Set the home position for the cursor.
Home.X = 0;
Home.Y = 0;
// Fill the console with spaces.
FillConsoleOutputCharacter(hOut,
(short) ' ',
CSBI.Size.X * CSBI.Size.Y,
Home,
out CharOut);
// Place the cursor in the upper left corner.
SetConsoleCursorPosition(hOut, Home);
Console.WriteLine("\t\t\t\t遊戲開始咯~\n");
} // cls
}// class
}//namespace