------- Windows Phone 7手机开发、.Net培训、期待与您交流! -------
最近几天初学C#,看了黑马的教学视频后深有感触,于是决定自己动手写一个面向过程的C#版飞行棋游戏,花费了一晚上时间终于搞定,今天修改了一下决心把源码发出来,以便和我一样初学C#的朋友一起交流学习。
先看成品图:
----------------------------------------------------------------------------------------------------------------------
第一步先分析了一下整个游戏的过程:
第二步写出的源码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Con_PlaneChess
{
class Program
{
//面向过程的飞行棋游戏
const int MAXSIZE=110;
public struct player
{
public int dir; //位置
public string szPlayer; //名字
}
static void Main(string[] args)
{
player[] per=new player[2];
Random rnd = new Random(System.DateTime.Now.Millisecond); //随机种子
int roll;
int[] arrChess = new int[MAXSIZE];
//0==玩家1 1==玩家2
ShowTitle();
per[0].dir = 0; //棋子的位置
per[1].dir = 0;
per[0].szPlayer = InputPlayerName('A'); //玩家的名字
per[1].szPlayer = InputPlayerName('B');
int iCurrent = 0;
Console.Clear();
MakeChess(ref arrChess); //初始化棋盘
ShowChess(arrChess, per[0].dir, per[1].dir); //显示棋盘
//无限循环
while (true)
{
Console.Write("A:{0} ,B:{1} ==> 现在轮到 {2} 掷骰子:",per[0].szPlayer,per[1].szPlayer, per[iCurrent].szPlayer);
Console.ReadKey(); //等待任意键继续
roll= rnd.Next(1, 6);
Console.Write("掷出了{0}点", roll);
MoveTo(ref per[iCurrent].dir, roll);//移动棋子
Console.ReadKey(); //等待任意键继续
if (SomeEvent(arrChess, ref per, iCurrent)) break; //胜利时退出循环
ShowChess(arrChess, per[0].dir, per[1].dir); //刷新棋盘
SwapTurn(ref iCurrent); //交换玩家掷子
}
Console.WriteLine("[游戏结束] 恭喜 {0} 获得胜利!", per[iCurrent].szPlayer);
Console.ReadLine();
}
static string InputPlayerName(char index)
{
Console.Write("请输入玩家{0}的名字:", index);
while (true)
{
string name = Console.ReadLine();
if (name.Length > 0 && name.Length < 8) return name;
Console.Write("输入错误,名字长度(0,8),请重新输入。\n请输入玩家{0}的名字:", index);
}
}//输入玩家名字
static void ShowTitle()
{
Console.WriteLine("*==================================*");
Console.WriteLine("*======基于面向过程的飞行棋游戏====*");
Console.WriteLine("=====支持双人游戏,玩物而不丧志也===*");
Console.WriteLine("*==================================*");
Console.WriteLine("*========欢迎大家提出宝贵意见======*");
Console.WriteLine("*==================================*");
Console.WriteLine("1)A B:表示玩家位置");
Console.WriteLine("2)<> :表示玩家重叠");
Console.WriteLine("3)¤ :表示时光隧道,前进6格");
Console.WriteLine("2)※ :表示天雷,回退10格");
Console.WriteLine("2)★ :表示阿拉丁,1:交换玩家位置或2:随机前进或者后退10格");
Console.WriteLine();
}//显示标题
static void SwapTurn (ref int icur)
{
icur=icur != 1 ? 1:0;
}//交换掷骰子权利
static void MoveTo(ref int index,int roll)
{
index += roll; //移动棋子
if (index < 0) index = 0;
else if (index > MAXSIZE) index = MAXSIZE;
}//判断是否胜利
static bool SomeEvent(int[] arrChess,ref player[] per,int index)
{
if(per[index].dir>=MAXSIZE) return true; //胜利则返回
switch (arrChess[per[index].dir])
{
case 11:
{
Console.WriteLine("哇,时光隧道,前进6格!");
MoveTo(ref per[index].dir,6); //前进10格
}
break;
case 12:
{
Console.WriteLine("天雷!,回退10格!");
MoveTo(ref per[index].dir,-10);
}
break;
case 13:
{
string str;
Console.Write("捡到一个阿拉丁,请许愿:\n1:交换玩家位置.2:随机前进或者后退10格");
do
{
str=Console.ReadLine();
}while(str!="1" && str!="2");
switch (str)
{
case "1":
{
int temp;
temp = per[0].dir;
per[0].dir = per[1].dir;
per[1].dir = temp;
Console.Write("玩家位置已交换");
}break;
case "2":
{
Random rnd = new Random(System.DateTime.Now.Millisecond);
bool go=Convert.ToBoolean(rnd.Next(2));
//向前移动10格
if (go) { MoveTo(ref per[index].dir, 10); Console.Write("获得丁丁相助,前进10格!"); }
//后退10格
else { MoveTo(ref per[index].dir, -10);Console.Write("阿拉丁失败了,你回退10格!"); }
}break;;
default:break;
}
}
break;
default: return false; //没有事件时返回
}
Console.ReadKey();
ShowChess(arrChess, per[0].dir, per[1].dir);//刷新棋盘
SomeEvent(arrChess, ref per, index);//检查移动后的位置
return false;
}//判断是否有意外事件
static void ShowChess(int[] chs,int index_a,int index_b)
{//复制了一份棋盘
int i = 0; //计数
if (index_a > MAXSIZE || index_b > MAXSIZE || index_a < 0 || index_b < 0) return;
int[] chess =(int[])chs.Clone();
if (index_a == index_b) chess[index_a] = 3;
else { chess[index_a] = 1; chess[index_b] = 2; }
Console.Clear(); //清空屏幕上的内容
//画棋盘
for (i = 0; i < 30; ++i)
Console.Write(ConvertStr(chess, i)); Console.Write("\n");
//第二行
for (int j = 0; j < 10; ++j, ++i)
Console.WriteLine(" {0}", ConvertStr(chess, i));
//中间的行
for (int j = 30; j >0; --j)
Console.Write(ConvertStr(chess, i+j-1)); Console.Write("\n");
i += 30;
//中间行后
for (int j = 0; j < 10; ++j, ++i)
Console.WriteLine(ConvertStr(chess, i));
//最后一行
for (int j = 0; j < 30; ++j, ++i)
Console.Write(ConvertStr(chess, i)); Console.Write("\n");
}//显示棋盘
static void MakeChess(ref int[] arr)
{
Random rnd = new Random(System.DateTime.Now.Millisecond);
for (int i = rnd.Next(1, 10); i < MAXSIZE; i += rnd.Next(1, 10))
{
arr[i] = rnd.Next(1, 4) + 10;
}
} //创建随机棋盘
static string ConvertStr(int[] arr,int dir)
{
switch (arr[dir])
{
case 1: return "A ";
case 2: return "B ";
case 3: return "<>";
case 11: return "¤"; //时光隧道
case 12: return "※"; //天雷
case 13: return "★"; //阿拉丁
default: return "□";
}
} //转换成字符
}
}