Skip to content

Instantly share code, notes, and snippets.

@FrankWu100
Created November 15, 2015 12:57
Show Gist options
  • Save FrankWu100/015283c365e833d9c323 to your computer and use it in GitHub Desktop.
Save FrankWu100/015283c365e833d9c323 to your computer and use it in GitHub Desktop.
using System;
class Untitled
{
static void Main(string[] args)
{
int _count = 15;
int countA = 0, countB= 0;
bool win = false;
bool needReInput = false;
int[] ans = new int[4];
Random rand = new Random();
for (int i = 0; i < 4; ++i)
{
ans[i] = rand.Next(10);
//Console.WriteLine(i + " - " + ans[i]);
for (int j = 0; j < i; ++j)
{
if (ans[i] == ans[j])
{
--i;
}
}
}
Console.WriteLine(String.Format("ANS:{0}{1}{2}{3}", ans[0], ans[1], ans[2], ans[3]));
for (int i = 0; i < _count; ++i)
{
countA = 0;
countB = 0;
needReInput = false;
Console.Write("{0:d2} 輸入數字:", i+1);
var inputStr = Console.ReadLine();
//Console.WriteLine(inputStr);
if (inputStr.Length != 4)
{
needReInput = true;
Console.WriteLine("!請輸入四位數!");
}
else
{
for (int m = 1; m < 4 && !needReInput; ++m)
{
if (!Char.IsNumber(inputStr[m]))
{
needReInput = true;
Console.WriteLine("!請輸入數字!");
}
else
{
for (int n = 0; n < m && !needReInput; ++n)
{
if (inputStr[m] == inputStr[n])
{
needReInput = true;
Console.WriteLine("!請輸入不重複之四位數!");
}
}
}
}
}
if (needReInput)
{
--i;
continue;
}
for (int m = 0; m < 4; ++m)
{
for (int n = 0; n < 4; ++n)
{
if(ans[m] == (inputStr[n] - '0'))
//if (ans[m] == int.Parse(inputStr[n].ToString()))
{
if (m == n)
{
++countA;
}
else
{
++countB;
}
}
}
}
Console.WriteLine("{0}A{1}B", countA, countB);
if (countA == 4)
{
win = true;
Console.WriteLine("Win");
break;
}
}
if(!win)
{
Console.WriteLine("Gameover");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment