用C#编写猜数、九九乘法表‘、迷宫
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
//数字9 代表墙
//数字0 代表通路
//数字1 代表入口 只有一个
//数字2 代表出口 只有一个
//数字3 代表已走过
//数字8 代表死胡同
//最终按照3代表的路径走出迷宫
int[,] mg = new int[10, 10]
{
{9,9,9,9,9,9,9,9,9,9},
{9,1,0,9,0,9,0,9,0,9},
{9,0,9,9,0,0,0,0,0,9},
{9,0,0,0,0,9,9,0,0,9},
{9,0,9,0,9,0,0,0,0,9},
{9,0,0,0,9,0,0,9,9,9},
{9,0,9,0,0,9,0,0,0,9},
{9,0,9,9,9,0,0,9,0,9},
{9,9,0,0,0,0,0,9,2,9},
{9,9,9,9,9,9,9,9,9,9}
};
static void Main(string[] args)
{
//*********九九乘法表********
////for (int i = 1; i <10; i++)
////{
//// for (int j =1; j <10; j++)
//// {
//// if (j <= i)
//// {
//// Console.Write("{0}*{1}={2}\t", i, j, i * j);
//// }
//// }
//// Console.WriteLine();
////}
////Console.ReadKey();
//Helloworld tet = new Helloworld();
//tet.wrie();
//*********九九乘法表********
//*********猜数游戏********
//bool temp = true;
//Random rd = new Random();
//int i = rd.Next(1, 100), j;
//Console.WriteLine("欢迎来玩猜数游戏");
//while(temp == true)
//{
// Console.Write("开始游戏,请输入数字:");
// j = Convert.ToInt32(Console.ReadLine());
// if(j > i)
// {
// Console.WriteLine("你输入的数字太大了,请再来一次!");
// continue;
// }
// else if (j < i)
// {
// Console.WriteLine("你输入的数字太小了,请重新输入:");
// continue;
// }
// Console.WriteLine("恭喜你猜对了!");
// temp = false;
//}
//*********猜数游戏********
//*********加减乘除计算器
// while (true)
// {
// calcu();
// Console.WriteLine("是否继续执行循环?Y/N");
// string s = Console.ReadLine();
// if(s.ToUpper() == "Y")
// {
// calcu();
// continue;
// }
// break;
// }
// }
//static void calcu()
// {
// Console.WriteLine("请输入第一个数字:");
// float num1 = 0;
// float num2 = 0;
// string fuhao = "";
// while (true)
// {
// try
// {
// num1 = Convert.ToSingle(Console.ReadLine());
// break;
// }
// catch (Exception)
// {
// num1 = 0;
// Console.WriteLine("输入错误,请重新输入第一个数字:");
// }
// }
// Console.WriteLine("请输入第二个数字:");
// while (true)
// {
// try
// {
// num2 = Convert.ToSingle(Console.ReadLine());
// break;
// }
// catch (Exception)
// {
// num1 = 0;
// Console.WriteLine("输入错误,请重新输入第二个数字:");
// }
// }
// Console.WriteLine("请输入运算符(+ - * /):");
// while (true)
// {
// try
// {
// fuhao = Console.ReadLine();
// if (fuhao == "+" || fuhao == "-" || fuhao == "*" || fuhao == "/")
// break;
// fuhao = "";
// Console.WriteLine("运算符输入错误,请重新输入:");
// }
// catch (Exception)
// {
// fuhao = "";
// Console.WriteLine("运算符输入错误,请重新驶入");
// }
// }
// float num = 0;
// switch (fuhao)
// {
// case "+": num = num1 + num2; break;
// case "-": num = num1 - num2; break;
// case "*": num = num1 * num2; break;
// case "/": num = num1 / num2; break;
// }
// Console.WriteLine("运算结果出炉:" + num1 + fuhao + num2 + "=" + num);
//*********加减乘除计算器
//*******迷宫游戏
Program pm = new Program();
Console.WriteLine("原始迷宫:");
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Console.Write(pm.mg[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
//走出迷宫
bool bl = pm.getEntrance();
if (bl == false)
{
Console.Write("未指明迷宫入口! ");
return;
}
point cpt = pm.stk.Pop();
pm.stk.Push(cpt);
while (cpt.var != 2)
{
if (pm.scanUp(cpt))
{
cpt = pm.stk.Pop();
pm.stk.Push(cpt);
continue;
}
if (pm.scanDown(cpt))
{
cpt = pm.stk.Pop();
pm.stk.Push(cpt);
continue;
}
if (pm.scanLeft(cpt))
{
cpt = pm.stk.Pop();
pm.stk.Push(cpt);
continue;
}
if (pm.scanRight(cpt))
{
cpt = pm.stk.Pop();
pm.stk.Push(cpt);
continue;
}
pm.mg[cpt.row, cpt.cul] = 8;
cpt = pm.stk.Pop();
}
Console.WriteLine("走出迷宫(数字3 代表出迷宫路径):");
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Console.Write(pm.mg[i, j] + " ");
}
Console.WriteLine();
}
Console.Write("END");
}
public struct point
{
public int row;
public int cul;
public int var;
}
//路径栈
Stack<point> stk = new Stack<point>();
//得到入口 从入口进入
public bool getEntrance()
{
point ent = new point();
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (mg[i, j] == 1)
{
ent.row = i;
ent.cul = j;
mg[i, j] = 3;
ent.var = mg[i, j];
stk.Push(ent);
return true;
}
}
}
return false;
}
//向上扫描
public bool scanUp(point cpt)
{
int cr = cpt.row - 1;
int cc = cpt.cul;
if (cr < 0 || mg[cr, cc] > 2)
{
return false;
}
if (mg[cr, cc] != 2)
mg[cr, cc] = 3;
point npt = new point();
npt.row = cr;
npt.cul = cc;
npt.var = mg[cr, cc];
stk.Push(npt);
return true;
}
//向下扫描
public bool scanDown(point cpt)
{
int cr = cpt.row + 1;
int cc = cpt.cul;
if (cr > 10 || mg[cr, cc] > 2)
{
return false;
}
if (mg[cr, cc] != 2)
mg[cr, cc] = 3;
point npt = new point();
npt.row = cr;
npt.cul = cc;
npt.var = mg[cr, cc];
stk.Push(npt);
return true;
}
//向左扫描
public bool scanLeft(point cpt)
{
int cr = cpt.row;
int cc = cpt.cul - 1;
if (cc < 0 || mg[cr, cc] > 2)
{
return false;
}
if (mg[cr, cc] != 2)
mg[cr, cc] = 3;
point npt = new point();
npt.row = cr;
npt.cul = cc;
npt.var = mg[cr, cc];
stk.Push(npt);
return true;
}
//向右扫描
public bool scanRight(point cpt)
{
int cr = cpt.row;
int cc = cpt.cul + 1;
if (cc > 10 || mg[cr, cc] > 2)
{
return false;
}
if (mg[cr, cc] != 2)
mg[cr, cc] = 3;
point npt = new point();
npt.row = cr;
npt.cul = cc;
npt.var = mg[cr, cc];
stk.Push(npt);
return true;
}
//**********迷宫游戏
}
}
//*********九九乘法表********
//public class Helloworld
//{
// public void wrie()
// {
// for (int i = 1; i < 10; i++)
// {
// for (int j = 1; j < 10; j++)
// {
// if (j <= i)
// {
// Console.Write("{0}*{1}={2}\t", i, j, i * j);
// }
// }
// Console.WriteLine();
// }
// Console.ReadKey();
// }
//}
//*********九九乘法表********