控制台程序源碼-飛行棋
using System ;
using System. Collections. Generic ;
using System. Linq ;
using System. Text ;
using System. Threading. Tasks ;
namespace test03
{
class Program
{
public static int [ ] Maps = new int [ 100 ] ;
static int [ ] PlayerPos = new int [ 2 ] ;
static string [ ] PlayerNames = new string [ 2 ] ;
static bool [ ] Falgs = new bool [ 2 ] ;
static void Main ( string [ ] args)
{
Show ( ) ;
#region 輸入玩家姓名
Console. WriteLine ( "請輸入玩家A的姓名" ) ;
PlayerNames[ 0 ] = Console. ReadLine ( ) ;
while ( PlayerNames[ 0 ] == " " )
{
Console. WriteLine ( "玩家的姓名不能為空,請再次輸入" ) ;
PlayerNames[ 0 ] = Console. ReadLine ( ) ;
}
Console. WriteLine ( "請輸入玩家B的姓名" ) ;
PlayerNames[ 1 ] = Console. ReadLine ( ) ;
while ( PlayerNames[ 1 ] == " " || PlayerNames[ 1 ] == PlayerNames[ 0 ] )
{
if ( PlayerNames[ 1 ] == " " )
{
Console. WriteLine ( "玩家的姓名不能為空,請再次輸入" ) ;
PlayerNames[ 0 ] = Console. ReadLine ( ) ;
}
Console. WriteLine ( "兩名玩家的姓名不能一樣,再從新輸入玩家B的姓名" ) ;
PlayerNames[ 0 ] = Console. ReadLine ( ) ;
}
#endregion
Console. Clear ( ) ;
Console. WriteLine ( "{0}的戰士用A表示,{1}的戰士用B表示" , PlayerNames[ 0 ] , PlayerNames[ 1 ] ) ;
InitailMap ( ) ;
DrawMap ( ) ;
while ( PlayerPos[ 0 ] < 99 && PlayerPos[ 1 ] < 99 )
{
if ( Falgs[ 0 ] == false )
{
PalyGame ( 0 ) ;
}
else
{
Falgs[ 0 ] = false ;
}
if ( PlayerPos[ 0 ] >= 99 )
{
Console. WriteLine ( " " ) ;
Console. WriteLine ( "玩家{0}無恥的贏了玩家{1}" , PlayerNames[ 0 ] , PlayerNames[ 1 ] ) ;
break ;
}
if ( Falgs[ 0 ] == false )
{
PalyGame ( 1 ) ;
}
else
{
Falgs[ 1 ] = false ;
}
if ( PlayerPos[ 1 ] >= 99 )
{
Console. WriteLine ( " " ) ;
Console. WriteLine ( "玩家{0}無恥的贏了玩家{1}" , PlayerNames[ 1 ] , PlayerNames[ 0 ] ) ;
break ;
}
}
Console. WriteLine ( "GAME OVER" ) ;
Console. ReadKey ( ) ;
}
public static void Show ( )
{
Console. ForegroundColor = ConsoleColor. Yellow;
Console. WriteLine ( "***************************" ) ;
Console. ForegroundColor = ConsoleColor. Red;
Console. WriteLine ( "***************************" ) ;
Console. ForegroundColor = ConsoleColor. Blue;
Console. WriteLine ( "*****快樂的飛行棋小遊戲****" ) ;
Console. ForegroundColor = ConsoleColor. Green;
Console. WriteLine ( "***************************" ) ;
Console. ForegroundColor = ConsoleColor. DarkBlue;
Console. WriteLine ( "***************************" ) ;
Console. ForegroundColor = ConsoleColor. Green;
}
public static void InitailMap ( )
{
int [ ] luckyturn = { 6 , 23 , 40 , 55 , 69 , 83 } ;
for ( int i = 0 ; i < luckyturn. Length; i++ )
{
Maps[ luckyturn[ i] ] = 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 , 45 , 63 , 72 , 88 , 90 } ;
for ( int i = 0 ; i < timeTunnel. Length; i++ )
{
Maps[ timeTunnel[ i] ] = 4 ;
}
}
public static void DrawMap ( )
{
Console. WriteLine ( "普通路:□ 幸運輪盤:☆ 地雷:● 暫停:▲ 時空隧道:◎" ) ;
#region
for ( int i = 0 ; i < 30 ; i++ )
{
Console. Write ( DrawStringMap ( i) ) ;
}
Console. WriteLine ( ) ;
#endregion
#region
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) ) ;
}
Console. WriteLine ( ) ;
#endregion
#region
for ( int i = 65 ; i < 70 ; i++ )
{
Console. Write ( DrawStringMap ( i) ) ;
Console. WriteLine ( ) ;
}
#endregion
#region
for ( int i = 70 ; i < 100 ; i++ )
{
Console. Write ( DrawStringMap ( i) ) ;
}
#endregion
}
public static string DrawStringMap ( int i)
{
string str = "" ;
if ( PlayerPos[ 0 ] == PlayerPos[ 1 ] && PlayerPos[ 0 ] == i)
{
str = "<>" ;
}
else if ( PlayerPos[ 0 ] == i)
{
str = "A" ;
}
else if ( PlayerPos[ 1 ] == i)
{
str = "B" ;
}
else
{
switch ( Maps[ i] )
{
case 0 :
Console. ForegroundColor = ConsoleColor. Yellow;
str = "□" ;
break ;
case 1 :
Console. ForegroundColor = ConsoleColor. DarkCyan;
str = "☆" ;
break ;
case 2 :
Console. ForegroundColor = ConsoleColor. Red;
str = "●" ;
break ;
case 3 :
Console. ForegroundColor = ConsoleColor. DarkGreen;
str = "▲" ;
break ;
case 4 :
Console. ForegroundColor = ConsoleColor. DarkMagenta;
str = "◎" ;
break ;
}
}
return str;
}
public static void PalyGame ( int playerNumber)
{
Random r = new Random ( ) ;
int rNumber = r. Next ( 1 , 7 ) ;
Console. WriteLine ( " " ) ;
Console. WriteLine ( "{0}按任意鍵開始擲骰子" , PlayerNames[ playerNumber] ) ;
Console. ReadKey ( true ) ;
Console. WriteLine ( "{0}擲出了{1}" , PlayerNames[ playerNumber] , rNumber) ;
PlayerPos[ playerNumber] += rNumber;
ChangPos ( ) ;
Console. ReadKey ( true ) ;
Console. WriteLine ( "{0}按任意鍵開始移動" , PlayerNames[ playerNumber] ) ;
Console. ReadKey ( true ) ;
Console. WriteLine ( "{0}移動完成" , PlayerNames[ playerNumber] ) ;
Console. ReadKey ( true ) ;
if ( PlayerPos[ playerNumber] == PlayerPos[ 1 - playerNumber] )
{
Console. WriteLine ( "玩家{0}踩到了玩家{1},玩家{2}退六格" , PlayerNames[ playerNumber] , PlayerNames[ 1 - playerNumber] , PlayerNames[ playerNumber] ) ;
PlayerPos[ 1 - playerNumber] -= 6 ;
ChangPos ( ) ;
Console. ReadKey ( true ) ;
}
else
{
switch ( Maps[ PlayerPos[ playerNumber] ] )
{
case 0 :
Console. WriteLine ( "玩家{0}踩到了方塊,安全" , PlayerNames[ playerNumber] ) ;
Console. ReadKey ( true ) ;
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 = PlayerPos[ playerNumber] ;
PlayerPos[ playerNumber] = PlayerPos[ 1 - playerNumber] ;
PlayerPos[ 1 - playerNumber] = temp;
Console. WriteLine ( "交換完成,按任意鍵進行遊戲" ) ;
Console. ReadKey ( true ) ;
break ;
}
else if ( input == "2" )
{
Console. WriteLine ( "玩家{0}選擇轟炸玩家{1},玩家{2}退6格" , PlayerNames[ playerNumber] , PlayerNames[ 1 - playerNumber] , PlayerPos[ 1 - playerNumber] ) ;
Console. ReadKey ( true ) ;
PlayerPos[ 1 - playerNumber] -= 6 ;
ChangPos ( ) ;
Console. WriteLine ( "玩家{0}退了6格" , PlayerPos[ 1 - playerNumber] ) ;
Console. ReadKey ( true ) ;
break ;
}
else
{
Console. WriteLine ( "只能輸入1或者2 1---交換位置 2---轟炸對方" ) ;
input = Console. ReadLine ( ) ;
}
}
break ;
case 3 :
Console. WriteLine ( "玩家{0}踩到了暫停,休息一回合" , PlayerNames[ playerNumber] ) ;
Falgs[ playerNumber] = true ;
Console. ReadKey ( true ) ;
break ;
case 4 :
Console. WriteLine ( "玩家{0}踩到了時空隧道,前進10格" , PlayerNames[ playerNumber] ) ;
Console. ReadKey ( true ) ;
PlayerPos[ playerNumber] += 10 ;
ChangPos ( ) ;
break ;
}
}
ChangPos ( ) ;
Console. Clear ( ) ;
DrawMap ( ) ;
}
public static void ChangPos ( )
{
if ( PlayerPos[ 0 ] < 0 )
{
PlayerPos[ 0 ] = 0 ;
}
if ( PlayerPos[ 0 ] >= 99 )
{
PlayerPos[ 0 ] = 99 ;
}
if ( PlayerPos[ 1 ] < 0 )
{
PlayerPos[ 1 ] = 0 ;
}
if ( PlayerPos[ 1 ] >= 99 )
{
PlayerPos[ 1 ] = 99 ;
}
}
}
}