两人简易飞行棋
using System;
namespace feixingqi
{
class Program
{
//用静态字段模拟全局变量
public static int[] Maps = new int[100];
//存储玩家AB的坐标
static int[] PlayerPose = new int[2];
//两个玩家的标记 默认都是false
static bool[] Flags = new bool[2];
static string[] PlayerNames = new string[2];
static void Main(string[] args)
{
GameShow();
#region 输入玩家姓名
Console.WriteLine("输入1姓名");
PlayerNames[0] = Console.ReadLine();
while (PlayerNames[0] == "")
{
Console.WriteLine("玩家的姓名不能为空");
PlayerNames[0] = Console.ReadLine();
}
Console.WriteLine("请输入玩家2姓名");
PlayerNames[1] = Console.ReadLine();
while (PlayerNames[1]==PlayerNames[0] || PlayerNames[1] == "")
{
Console.WriteLine("请重新输入");
PlayerNames[1] = Console.ReadLine();
}
#endregion
//玩家姓名输入ok后清屏
Console.Clear();
Console.WriteLine("{0}的士兵用A表示", PlayerNames[0]);
Console.WriteLine("{0}的士兵用B表示", PlayerNames[1]);
InitialMap();
DrawMap();
while (PlayerPose[0] < 99 && PlayerPose[1] < 99)
{
if (Flags[0]==false)
{
PlayGames(0);
}
else
{
Flags[0] = false;
}
if (PlayerPose[0]>=99)
{
Console.WriteLine("玩家{0}赢了玩家{1}",PlayerNames[0],PlayerNames[1]);
break;
}
if (Flags[1] == false)
{
PlayGames(1);
}
else
{
Flags[1] = false;
}
if (PlayerPose[1] >= 99)
{
Console.WriteLine("玩家{0}赢了玩家{1}",PlayerNames[0],PlayerNames[1]);
break;
}
}
Console.ReadKey();
}
/// <summary>
/// 玩游戏
/// </summary>
public static void PlayGames(int playerNumber)
{
Random r = new Random();
int rNumber = r.Next(1, 7);
Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[playerNumber]);
Console.ReadKey(true); //让按下的键不显示再控制台中
Console.WriteLine("{0}掷出了{1}", PlayerNames[playerNumber],rNumber);
PlayerPose[playerNumber] = PlayerPose[playerNumber] + rNumber;
ChangePose();
Console.ReadKey(true);
Console.WriteLine("{0}按任意键开始行动", PlayerNames[playerNumber]);
Console.ReadKey(true);
Console.WriteLine("{0}行动完了", PlayerNames[playerNumber]);
Console.ReadKey(true);
//玩家互踩,方块,幸运轮盘,地雷,暂停,时空隧道
if (PlayerPose[playerNumber] == PlayerPose[1- playerNumber])
{
Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退6格", PlayerNames[playerNumber], PlayerNames[1- playerNumber], PlayerNames[1- playerNumber]);
PlayerPose[1- playerNumber] = PlayerPose[1- playerNumber] - 6;
Console.ReadKey();
}
else
{
switch (Maps[PlayerPose[playerNumber]])
{
case 0:
Console.WriteLine("玩家{0}踩到了方块", PlayerNames[playerNumber]);
Console.ReadKey();
break;
case 1:
Console.WriteLine("玩家{0}踩到了幸运轮盘 请选择1--交换位置 2--轰炸对象",PlayerNames[playerNumber]);
string input = Console.ReadLine();
while (true)
{
if (input == "1")
{
Console.WriteLine("玩家{0}和玩家{1}交换位置", PlayerNames[playerNumber], PlayerNames[1- playerNumber]);
Console.ReadKey(true);
int temp = PlayerPose[playerNumber];
PlayerPose[playerNumber] = PlayerPose[1- playerNumber];
PlayerPose[1- playerNumber] = temp;
Console.WriteLine("交换成功!!继续游戏");
Console.ReadKey(true);
break;
}
else if (input == "2")
{
Console.WriteLine("玩家{0}准备轰炸玩家{1},玩家{2}退六格", PlayerPose[playerNumber], PlayerPose[1- playerNumber], PlayerPose[1- playerNumber]);
Console.ReadKey();
PlayerPose[1- playerNumber] = PlayerPose[1- playerNumber] - 6;
ChangePose();
Console.WriteLine("玩家{0}退了六格", PlayerNames[1- playerNumber]);
Console.ReadKey(true);
break;
}
}
break;
case 2:
Console.WriteLine("玩家{0}踩到了地雷", PlayerNames[playerNumber]);
Console.ReadKey(true);
PlayerPose[playerNumber] = PlayerPose[playerNumber] - 6;
ChangePose();
break;
case 3:
Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerNames[playerNumber]);
ChangePose();
Flags[playerNumber] = true;
Console.ReadKey(true);
break;
case 4:
Console.WriteLine("玩家踩到了时空隧道,前进十格", PlayerNames[playerNumber]);
PlayerPose[playerNumber] = PlayerPose[playerNumber] + 10;
ChangePose();
Console.ReadKey(true);
break;
}//switch
}//else
Console.Clear();
DrawMap();
}
//画游戏头
public static void GameShow()
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("*****************************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("*****************************");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("*****************************");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("*********飞行棋**************");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("*****************************");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("*****************************");
}
//初始化地图(将整数数组中的数字显示特殊字符串的过程就是初始化地图的过程)
public static void InitialMap()
{
int[] luckyTurn = { 6, 23, 40, 55, 69, 83 }; //幸运轮盘◎
for (int i = 0; i < luckyTurn.Length; i++)
{
int index = luckyTurn[i];
Maps[index] = 1;
}
int[] landMine = { 5, 13, 17, 33, 38, 50 ,64 ,80 ,94 }; //地雷★
for (int i = 0; i < landMine.Length; i++)
{
Maps[landMine[i]] = 2;
}
int[] pause = { 9, 27, 60, 93 }; //暂停▲
for (int i = 0; i < pause.Length; i++)
{
Maps[pause[i]] = 3;
}
int[] timeTunnel = { 20, 25, 40, 45, 63, 72 , 88 , 90 }; //时空隧道卍
for (int i = 0; i < timeTunnel.Length; i++)
{
Maps[timeTunnel[i]] = 4;
}
}
public static void DrawMap()
{
Console.WriteLine("幸运轮盘◎,地雷★,暂停▲,时空隧道卍");
//坐标相同,画<>,不同是,该画A画A,该画B画B
#region 0—29 第一横行
for (int i = 0; i < 30; i++)
{
Console.Write(DrawstringMap(i));
}//for
#endregion
Console.WriteLine();
#region 30-34 第一竖行
for (int i = 30; i < 35; i++)
{
for (int j = 0; j <= 28; j++)
{
Console.Write(" ");
}
Console.Write(DrawstringMap(i));
Console.WriteLine();
}
#endregion
#region 第二横行
for (int i = 64; i >= 35 ; i--)
{
Console.Write(DrawstringMap(i));
}
#endregion
Console.WriteLine();
#region 第二竖行
for (int i = 65; i < 69; i++)
{
Console.WriteLine(DrawstringMap(i));
}
#endregion
#region 第三横行
for (int i = 70; i <= 99; i++)
{
Console.Write(DrawstringMap(i));
}
#endregion
Console.WriteLine();
}
/// <summary>
/// 从画地图的方法中抽象出的方法
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public static string DrawstringMap(int i)
{
string str = " ";
if (PlayerPose[0] == PlayerPose[1] && PlayerPose[1] == i)
{
str = "<>";
}
else if (PlayerPose[0] == i)
{
str = "A";
}
else if (PlayerPose[1] == i)
{
str = "B";
}
else
{
switch (Maps[i])
{
case 0:
Console.ForegroundColor = ConsoleColor.Red;
str = "□" ;
break;
case 1:
Console.ForegroundColor = ConsoleColor.Yellow;
str = "◎" ;
break;
case 2:
Console.ForegroundColor = ConsoleColor.Blue;
str = "★" ;
break;
case 3:
Console.ForegroundColor = ConsoleColor.Gray;
str = "▲" ;
break;
case 4:
Console.ForegroundColor = ConsoleColor.Cyan;
str = "卍";
break;
}//switch
}//else
return str;
}
public static void ChangePose()
{
if (PlayerPose[0]<0)
{
PlayerPose[0] = 0;
}
if (PlayerPose[0]>99)
{
PlayerPose[0] = 99;
}
if (PlayerPose[1] < 0)
{
PlayerPose[1] = 0;
}
if (PlayerPose[1] > 99)
{
PlayerPose[1] = 99;
}
}
}
}
属性
属性的作用就是保护字段,对字段的赋值和取值进行限定,属性应该写在类中。
属性的本质就是两个方法,一个是get(),一个是set()
public string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
属性只是一个过渡,对字段的再处理,添加属性后,不能通过字段赋值,只能通过属性赋值
当我们给属性赋值时,会执行get(),当我们给属性取值时,会执行set()
对age进行赋值限定:
public int Age
{
get { return _age; }
//加入限制条件
set {
if (value<0||value>100)
{
value = 0;
}
_age = value; }
}
对gender进行取值限定
public char Gender
{
get {
if (_gender!='男'&&_gender!='女')
{
return _gender = '男';
}
return _gender; }
set { _gender = value; }
}
静态与非静态
在非静态类中,既可以有实例(非静态)成员,也可以有静态成员
public class Person
{
public void M1()
{
Console.WriteLine("非静态");
}
public static void M2()
{
Console.WriteLine("静态");
}
}
在调用非静态成员的时候,需要使用对象名.实例名,静态成员调用是类名.静态名
坦白来说,非静态成员是属于对象的东西,是具体的成员,用对象名调用。而静态成员与类直接相关,使用类名去调用。
Person p = new Person();
p.M1();//调用实例成员
Person.M2();//调用静态成员
另外,在静态函数中,只能访问静态成员,不能访问实例成员,实例函数中,静态成员与实例成员都能访问。
静态类不允许创建对象。
什么时候用静态类,什么时候用非静态类
如果把类当作一个工具类去使用,可以将类写成一个静态类,比如Console,在调用时就非常方便,能直接调用
静态类在整个项目中资源共享,类是不占内存的,对象是占内存的
构造函数
我们输出以下字列
我叫李昉,我今年20岁,我是男生
我叫李昉,我总成绩是287,平均成绩是95
我叫卓琳,我今年20岁,我是女生
我叫卓琳,我总成绩是298,平均成绩是99
首先写一个方法
public class Student
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
public int Age
{
get { return _age; }
set {
if (_age<0||_age>100)
{
value = 0;
}
_age = value; }
}
private char _gender;
public char Gender
{
get {
if (_gender != '男' && _gender != '女')
{
_gender = '男';
}
return _gender; }
set { _gender = value; }
}
private int _English;
public int English
{
get { return _English; }
set { _English = value; }
}
private int _math;
public int Math
{
get { return _math; }
set { _math = value; }
}
private int _chinese;
public int Chinese
{
get { return _chinese; }
set { _chinese = value; }
}
public void SayHello()
{
Console.WriteLine("我叫{0},我今年{1}岁,我是{2}生",this.Name,this.Age,this.Gender);
}
public void ShowScore()
{
Console.WriteLine("我叫{0},我总成绩是{1},平均成绩是{2}",this.Name,this.Chinese+this.English+this.Math,(this.Chinese + this.English + this.Math)/3);
}
}
然后创建对象调用
class Program
{
static void Main(string[] args)
{
Student 李昉 = new Student();
李昉.Name = "李昉";
李昉.Gender = '男';
李昉.Age = 20;
李昉.Chinese = 99;
李昉.English = 89;
李昉.Math = 99;
李昉.SayHello();
李昉.ShowScore();
Student zolin = new Student();
zolin.Name = "卓琳";
zolin.Gender = '女';
zolin.Age = 20;
zolin.Chinese = 100;
zolin.English = 99;
zolin.Math = 99;
zolin.SayHello();
zolin.ShowScore();
Console.ReadKey();
}
}
我们可以看到,在调用时是非常冗余的,可以构造函数帮助对象初始化(给对象的每个属性依次赋值)
构造函数没有返回值,连void也不能写
构造函数的名称必须和类名一样
在创建对象的时候,会执行构造函数
我们在类中创建一个函数如下
public Student(string name,int age,int chinese,int math,int english,char gender)
{
this.Name = name;
this.Age = age;
this.Gender = gender;
this.Chinese = chinese;
this.Math = math;
this.English = english;
}
主函数改成
class Program
{
static void Main(string[] args)
{
Student 李昉 = new Student("李昉",20,99,89,99,'男');
Student zolin = new Student("zolin",21,100,100,100,'女');
zolin.SayHello();
zolin.ShowScore();
Console.ReadKey();
}
}
代码简化了许多
构造函数是可以有重载的,可以有多个参数不同的构造函数
构造函数是用来创建对象的特殊方法,可以有参数,构造函数会把参数传递给对象的每一个属性来初始化对象,可以方便快捷的来初始化对象