Mã:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Game
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" ABC");
print ran = new print();
ran.start();
ran.rangame();
}
}
class print
{
public int[,] myArr ={ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
public int[,] panduan ={ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
public int[,] aa = new int[3, 3];
public int bestmove, move;
public string playername;
public print()
{
move = 0;
bestmove = 999;
playername = "NOBABY";
}
public void start()
{
string[] files = Directory.GetFiles("D:\\");
if (files.Length == 0)
{
writetxt(bestmove, playername);
readtxt();
}
else
{
for (int i = 0; i < files.Length; i++)
{
if (Path.GetFileNameWithoutExtension(files ) == "message")
{
readtxt();
break;
}
else
{
writetxt(bestmove, playername);
readtxt();
break;
}
}
}
}
public void rangame()
{
Random suiji = new Random();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
int randId1 = suiji.Next(0, 3 - i);
int randId2 = suiji.Next(0, 3 - j);
aa[i, j] = myArr[randId1, randId2];
myArr[randId1, randId2] = myArr[2 - i, 2 - j];
}
}
printgame(aa);
startgame(aa);
}
public void printgame(int[,] aa)
{
Console.WriteLine("+===+===+===+");
Console.WriteLine("+ {0} + {1} + {2} +", aa[0, 0], aa[0, 1], aa[0, 2]);
Console.WriteLine("+ {0} + {1} + {2} +", aa[1, 0], aa[1, 1], aa[1, 2]);
Console.WriteLine("+ {0} + {1} + {2} +", aa[2, 0], aa[2, 1], aa[2, 2]);
Console.WriteLine("+===+===+===+");
Console.WriteLine("Enter your Move [L,R,U,D,E]");
}
public void startgame(int[,] aa)
{
int aai, aaj, i, j;
int jishu, x, y, temp;
string get;
jishu = aai = aaj = i = j = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (aa[i, j] == 0)
{
aai = i;
aaj = j;
break;
}
}
}
get = Console.ReadLine();
switch (get)
{
case "L":
move++;
y = aaj - 1;
if (y < 0)
{
Console.WriteLine("error!Please enter key again!");
startgame(aa);
}
else
{
temp = aa[aai, aaj];
aa[aai, aaj] = aa[aai, y];
aa[aai, y] = temp;
Console.Clear();
readtxt();
printgame(aa);
startgame(aa);
}
break;
case "R":
move++;
y = aaj + 1;
if (y > 2)
{
Console.WriteLine("error!Please enter key again!");
startgame(aa);
}
else
{
temp = aa[aai, aaj];
aa[aai, aaj] = aa[aai, y];
aa[aai, y] = temp;
Console.Clear();
readtxt();
printgame(aa);
startgame(aa);
}
break;
case "U":
move++;
x = aai - 1;
if (x < 0)
{
Console.WriteLine("error!Please enter key again!");
startgame(aa);
}
else
{
temp = aa[aai, aaj];
aa[aai, aaj] = aa[x, aaj];
aa[x, aaj] = temp;
Console.Clear();
readtxt();
printgame(aa);
startgame(aa);
}
break;
case "D":
move++;
x = aai + 1;
if (x > 2)
{
Console.WriteLine("error!Please enter key again!");
startgame(aa);
}
else
{
temp = aa[aai, aaj];
aa[aai, aaj] = aa[x, aaj];
aa[x, aaj] = temp;
Console.Clear();
readtxt();
printgame(aa);
startgame(aa);
}
break;
case "E":
move++;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (aa[i, j] == panduan[i, j])
{
jishu++;
}
}
}
if (jishu == 9)
{
Console.WriteLine("Xin chuc mung vuot qua!");
if (move < bestmove)
{
Console.WriteLine("Ban da vuot qua ky luc!");
Console.WriteLine("Please input your name");
String str = Console.ReadLine();
bestmove = move;
writetxt(bestmove, str);
Console.WriteLine("If you play game again! [Y/N]");
YandN();
}
else
{
Console.WriteLine("If you play game again! [Y/N]");
YandN();
}
}
else
{
Console.WriteLine("error!Please enter key again!");
startgame(aa);
break;
}
break;
case "l":
move++;
y = aaj - 1;
if (y < 0)
{
Console.WriteLine("error!Please enter key again!");
startgame(aa);
}
else
{
temp = aa[aai, aaj];
aa[aai, aaj] = aa[aai, y];
aa[aai, y] = temp;
Console.Clear();
readtxt(); readtxt ();
printgame(aa);
startgame(aa);
}
break;
case "r":
move++;
y = aaj + 1;
if (y > 2)
{
Console.WriteLine("error!Please enter key again!");
startgame(aa);
}
else
{
temp = aa[aai, aaj];
aa[aai, aaj] = aa[aai, y];
aa[aai, y] = temp;
Console.Clear();
readtxt();
printgame(aa);
startgame(aa);
}
break;
case "u":
move++;
x = aai - 1;
if (x < 0)
{
Console.WriteLine("error!Please enter key again!");
startgame(aa);
}
else
{
temp = aa[aai, aaj];
aa[aai, aaj] = aa[x, aaj];
aa[x, aaj] = temp;
Console.Clear();
readtxt();
printgame(aa);
startgame(aa);
}
break;
case "d":
move++;
x = aai + 1;
if (x > 2)
{
Console.WriteLine("error!Please enter key again!");
startgame(aa);
}
else
{
temp = aa[aai, aaj];
aa[aai, aaj] = aa[x, aaj];
aa[x, aaj] = temp;
Console.Clear();
readtxt();
printgame(aa);
startgame(aa);
}
break;
case "e":
move++;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (aa[i, j] == panduan[i, j])
{
jishu++;
}
}
}
if (jishu == 9)
{
Console.WriteLine("Xin chuc mung da qua!");
if (move < bestmove)
{
Console.WriteLine("ban da doat ky luc moi!");
Console.WriteLine("Please input your name");
String str = Console.ReadLine();
bestmove = move;
writetxt(bestmove, str);
Console.WriteLine("If you play game again! [Y/N]");
YandN();
}
else
{
Console.WriteLine("If you play game again! [Y/N]");
YandN();
}
}
else
{
Console.WriteLine("error!Please enter key again!");
startgame(aa);
break;
}
break;
default:
Console.WriteLine("error!Please enter key again!");
startgame(aa);
break;
}
}
public void YandN()
{
string again;
again = Console.ReadLine();
switch (again)
{
case "Y":
{
print ran = new print();
ran.start();
ran.rangame();
break;
}
case "N":
{
return;
}
case "y":
{
print ran = new print(); ran.start(); ran.rangame(); break;
}
case "n":
{
return;
}
default:
Console.WriteLine("error!Please enter key again!");
YandN();
break;
}
}
public void writetxt(int bastmove, string playername)
{
FileStream fs = new FileStream(@"d:\message.txt", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
string str = playername;
sw.WriteLine(str);
sw.Flush();
string newstr = bestmove.ToString();
sw.WriteLine(newstr);
sw.Flush();
sw.Close();
fs.Close();
}
public void readtxt()
{
FileStream fs = null;
StreamReader sr = null;
try
{
fs = new FileStream(@"d:\message.txt", FileMode.Open, FileAccess.Read);
sr = new StreamReader(fs, Encoding.Default);
string str = sr.ReadLine();
string a = str;
Console.WriteLine("Record Holder:{0}", a);
str = sr.ReadLine();
string b = str;
Console.WriteLine("Key Presses:{0}", b);
bestmove = Convert.ToInt32(b);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
sr.Close();
fs.Close();
}
}
}
}
Với sự đa dạng và phong phú về mẫu mã và giá cả, choxeonline.vn hứa hẹn sẽ là địa chỉ tin cậy cho những ai đang tìm kiếm chiếc xe ô tô ưng ý. Từ những mẫu xe hạng sang đến những chiếc xe gia đình...
Khám Phá Đa Dạng Sản Phẩm Xe Ô Tô...