用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();
// }
//}
//*********九九乘法表********
用C#编写猜数、九九乘法表‘、迷宫的更多相关文章
- python编写九九乘法表代码
打印九九乘法表 代码: #!/usr/bin/env python # -*- coding: UTF-8 -*- # 项目二: # 1.要求:编写九九乘法表 # 2.分析: # 根据九九乘法表的样式 ...
- 用Python编写九九乘法表考虑print自动换行问题
编写了一个简单的小程序九九乘法表,代码如下: for i in range(1,10): for j in range(1,i+1): print(" %d*%d=%d" % (j ...
- Python学习笔记系列——九九乘法表&猜大小
再重新捡起Python,数据库短时间之内已经没啥看的了,不知道今年结束之前能不能Python入门,一直认为自己是没有编程思想的... 1.九九乘法表 #九九乘法表实现的一种方式之一 def Multi ...
- 使用VS2017进行Python代码的编写并打印出九九乘法表
我们来盘一盘怎么使用VS2017进行python代码的编写并打印出九九乘法表. 使用Visual Studio 2017进行Python编程不需要太复杂的工作,只需要vs2017安装好对Python的 ...
- 使用 JavaScript 用循环嵌套输出乘法表。外循环控制行数,内循环控制当前行要输出的乘法表达式,在页面上输出九九乘法表
查看本章节 查看作业目录 需求说明: 在页面上输出九九乘法表,实现效果如图所示 实现思路: 创建HTML页面 在页面中嵌入 <script type="text/javascript& ...
- 编写Java程序,使用循环结构打印出九九乘法表
编写Java程序,使用循环结构打印出九九乘法表 效果如下: 实现代码: public class Multiplication99 { public static void main(String[] ...
- 编写一个jsp页面,输出九九乘法表。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- 九九乘法表,全js编写,放入table表格带入页面渲染出来
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用lambda编写九九乘法表
Java 8 出来有一段时间了,支持lambda表达式 非常的赞. lambda表达式 即匿名方法,属于一种轻量级的封装 lambda表达式的语法由参数列表.箭头符号->和函数体组成.函数体既可 ...
随机推荐
- php 字符串截取
$str="3,22,11,444,33,1,3455,33,22,444,55,66,77,88,99,554336,"; echo substr($str,0,strlen($ ...
- ThinkPHP与EasyUI整合之三(searchbox):在datagrid中查询指定记录
在datagrid中toolbar添加searchbox查询框,根据列范围查询数据,先看效果图: 1. searchbox采用easyui的Demo例子,再加以js扩展,根据datagrid中的列数据 ...
- cv resource
http://blog.sina.com.cn/s/blog_5086c3e20101kdy5.html
- VBA 字符串操作
Trim(string) 去掉string左右两端空白 Ltrim(string) 去掉string左端空白 Rtrim(string) 去掉string右端空白 Len(string) 计算stri ...
- js array filter pop push shift unshift方法
JavaScript Array filter() 方法 JavaScript Array 对象 实例 返回数组 ages 中所有元素都大于 18 的元素: var ages = [32, 33, ...
- Android-ViewPagerIndicator框架使用——Circle
前言:Circle适用于应用新功能的展示页和商品的多张图片的展示功能. 1.定义布局文件:SampleCirclesDefault中添加了一个布局:simple_circles. 布局中定义一个Lin ...
- try finally 执行顺序
class Exc{ int a; int b; } public class Except { @SuppressWarnings("finally") static int c ...
- Laravel5.1 数据库-查询构建器
今儿个咱说说查询构建器.它比运行原生SQL要简单些,它的操作面儿也是比较广泛的. 1 查询结果 先来看看它的语法: public function getSelect() { $result = DB ...
- Python 爬取盗墓笔记的标题,章节,章节名称
# coding:utf-8import requestsimport jsonfrom bs4 import BeautifulSoup user_agent = 'Mozilla/5.0 (Win ...
- Android系统中Parcelable和Serializable的区别,自动化实现Parcelable接口的插件
Parcelable和Serializable的区别 参考地址:http://greenrobot.me/devpost/android-parcelable-serializable/ 由于最终的区 ...