下面是自己寫的飛行棋的小程序,代碼寫的簡單,希望各路大神多多指教----話不多說,直接上代碼
一共有三個類,第一個GameManager:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace GameDemo 7 { 8 class GameManager 9 { 10 Random r = new Random(); 11 int[] playerPos = { 0,0 }; 12 13 string[] playerName = new string[2]; 14 public void Start() 15 { 16 17 GameMap myMap = new GameMap(); 18 int step = 0; //這個變量用於存放要走的步數 19 20 myMap.GameSet(); 21 22 myMap.ShowTitle(); 23 24 Console.WriteLine("請輸入玩家A的姓名:"); 25 while ((playerName[0] = Console.ReadLine()) == "") 26 { 27 Console.WriteLine("您輸入的玩家A的姓名無效,請重新輸入!"); 28 } 29 30 Console.WriteLine ("請輸入玩家B的姓名:"); 31 while ((playerName[1] = Console.ReadLine()) == "") 32 { 33 Console.WriteLine("您輸入的玩家A的姓名無效,請重新輸入!"); 34 } 35 36 while (playerName[0] == playerName[1]) 37 { 38 Console.WriteLine("你輸入的玩家B的姓名與玩家A的姓名相同,請重新輸入!"); 39 playerName[1] = Console.ReadLine(); 40 } 41 42 myMap.ShowTitle(); 43 Console.WriteLine("對戰開始:"); 44 Console.WriteLine("{0}士兵用A來表示",playerName[0]); 45 Console.WriteLine("{0}士兵用A來表示", playerName[1]); 46 myMap.DrawMap(playerPos[0], playerPos[1]); 47 48 //假如我們的游戲誰先到最后一格,就勝利 49 50 bool isStopA = false; 51 bool isStopB = false; 52 while (playerPos[0] < 99 && playerPos[1] < 99) //玩家A所走的位置和玩家B所走的位置都要小於99 53 { 54 string msg = ""; 55 //A開始擲骰子的代碼 56 #region//A開始擲骰子的代碼 57 if (isStopA==false ) 58 { 59 Console.WriteLine("{0}請按任意鍵開始擲骰子。。。", playerName[0]); 60 Console.ReadKey(true); 61 step = r.Next(1, 7); 62 Console.WriteLine("{0}擲出了:{1}", playerName[0], step); 63 Console.WriteLine("按任意鍵開始行動。。。"); 64 Console.ReadKey(true); 65 66 playerPos[0] = playerPos[0] + step; 67 68 if (playerPos[0] == playerPos[1]) 69 { 70 msg = string.Format("{0}踩到{1}了,{1}退回到原點", playerName[0], playerName[1]); 71 playerPos[1] = 0; 72 } 73 else 74 { 75 switch (myMap.Map[playerPos[0]]) 76 { 77 case 1://幸運輪盤 78 Console.WriteLine("1----交換位置 2----轟炸"); 79 int userSelect = myMap.ReadInt(1, 2); 80 switch (userSelect) 81 { 82 case 1: 83 int temp = 0; 84 temp = playerPos[0]; 85 playerPos[0] = playerPos[1]; 86 playerPos[1] = temp; 87 msg = string.Format("{0}和{1}交換位置", playerName[0], playerName[1]); 88 break; 89 case 2: 90 playerPos[1] = playerPos[1] - 6; 91 if (playerPos[1] < 0) 92 playerPos[1] = 0; 93 msg = string.Format("{0}轟炸了{1},{1}向后倒退6步", playerName[0], playerName[1]); 94 break; 95 } 96 break; 97 case 2://地雷 98 playerPos[0] = playerPos[0] - 10; 99 if (playerPos[0] < 0) 100 playerPos[0] = 0; 101 msg = string.Format("{0}踩到地雷了!退后10步,oh~~no", playerName[0]); 102 break; 103 case 3://暫停 104 isStopA = true; 105 msg = string.Format("{0}玩家停止一次擲骰子",playerName[0]); 106 break; 107 case 4://時空隧道 108 playerPos[0] = playerPos[0] + 10; 109 msg = string.Format("{0}玩家進入了時空隧道,向前飛了10步", playerName[0]); 110 break; 111 default://普通 112 break; 113 } 114 } 115 126 Console.Clear(); 127 128 myMap.DrawMap(playerPos[0], playerPos[1]); 129 Console.WriteLine(msg); 130 Console.WriteLine("{0}玩家的位置為:{1}", playerName[0], playerPos[0]); 131 Console.WriteLine("{0}玩家的位置為:{1}", playerName[1], playerPos[1]); 132 133 Console.WriteLine(); 134 } 135 else 136 { 137 isStopA = false ; 138 } 139 #endregion 140 endGame(); 141 142 143 //B開始擲骰子的代碼 144 #region//B開始擲骰子的代碼 145 if (isStopB == false) 146 { 147 msg = ""; 148 Console.WriteLine("{0}請按任意鍵開始擲骰子。。。", playerName[1]); 149 Console.ReadKey(true); 150 step = r.Next(1, 7); 151 Console.WriteLine("{0}擲出了:{1}", playerName[1], step); 152 Console.WriteLine("按任意鍵開始行動。。。"); 153 Console.ReadKey(true); 154 155 playerPos[1] = playerPos[1] + step; 156 // if (playerPos[1] >= 99) 157 // { 158 endGame(); 159 //} 160 if (playerPos[0] == playerPos[1]) 161 { 162 msg = string.Format("{0}踩到{1}了,{1}退回到原點", playerName[1], playerName[0]); 163 playerPos[0] = 0; 164 } 165 else 166 { 167 switch (myMap.Map[playerPos[1]]) 168 { 169 case 1://幸運輪盤 170 Console.WriteLine("1----交換位置 2----轟炸"); 171 int userSelect = myMap.ReadInt(1, 2); 172 switch (userSelect) 173 { 174 case 1: 175 int temp = 0; 176 temp = playerPos[0]; 177 playerPos[0] = playerPos[1]; 178 playerPos[1] = temp; 179 msg = string.Format("{0}和{1}交換位置", playerName[1], playerName[0]); 180 break; 181 case 2: 182 playerPos[0] = playerPos[0] - 6; 183 if (playerPos[0] < 0) 184 playerPos[0] = 0; 185 msg = string.Format("{0}轟炸了{1},{1}向后倒退6步", playerName[1], playerName[0]); 186 break; 187 } 188 break; 189 case 2://地雷 190 playerPos[1] = playerPos[1] - 10; 191 if (playerPos[1] < 0) 192 playerPos[1] = 0; 193 msg = string.Format("{0}踩到地雷了!退后10步,oh~~no", playerName[1]); 194 break; 195 case 3://暫停 196 isStopB = true; 197 msg = string.Format("{0}玩家停止一次擲骰子", playerName[1]); 198 break; 199 case 4://時空隧道 200 playerPos[1] = playerPos[1] + 10; 201 msg = string.Format("{0}玩家進入了時空隧道,向前飛了10步", playerName[1]); 202 break; 203 default://普通 204 break; 205 } 206 } 207 #region//沒用的代碼 208 //playerPos[0] = playerPos[0] + JudgeGame(step,playerPos[0],playerPos[1]); 209 //if (step!= JudgeGame(step, playerPos[0], playerPos[1])) 210 //{ 211 // playerPos[0] = playerPos[0] + JudgeGame(step, playerPos[0], playerPos[1]); 212 // if (playerPos[0] < 0) 213 // playerPos[0] = 0; 214 //} 215 216 //playerPos[1]=stepOtherAtoB(ref playerPos[0], ref playerPos[1]); 217 #endregion 218 Console.Clear(); 219 220 myMap.DrawMap(playerPos[0], playerPos[1]); 221 Console.WriteLine(msg); 222 Console.WriteLine("{0}玩家的位置為:{1}", playerName[0], playerPos[0]); 223 Console.WriteLine("{0}玩家的位置為:{1}", playerName[1], playerPos[1]); 224 225 Console.WriteLine(); 226 } 227 else 228 { 229 isStopB = false; 230 } 231 #endregion 232 233 endGame(); 234 235 } 236 237 238 } 239 /// <summary> 240 /// 游戲結束的代碼 241 /// </summary> 242 public void endGame() 243 { 244 if (playerPos[0] >= 99 || playerPos[1] >= 99) 245 { 246 if (playerPos[0] >= 99) 247 { 248 Console.Clear(); 249 Console.WriteLine("{0}玩家贏得了比賽!", playerName[0]); 250 } 251 else 252 { 253 Console.Clear(); 254 Console.WriteLine("{0}玩家贏得了比賽!", playerName[1]); 255 } 256 Console.WriteLine("*************************************************"); 257 Console.WriteLine("* *"); 258 Console.WriteLine("* GAME OVER *"); 259 Console.WriteLine("* *"); 260 Console.WriteLine("*************************************************"); 261 return; 262 } 263 264 } 265 357 } 358 }
第二個類:GameMap
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace GameDemo 7 { 8 class GameMap 9 { 10 //在這個數組,存儲我們的每一格的地圖類型(機關) 11 //數組中的每一格,對應游戲地圖上的一格. 12 //下標為[0]的數組元素,對應游戲地圖上的第一格 13 14 // 1:幸運輪盤◎ 2:地雷☆ 3: 暫停▲ 4:時空隧道卐 0:普通□ 15 16 int[] map = new int[100]; 17 18 public int[] Map 19 { 20 get { return map; } 21 set { map = value; } 22 } 23 24 //public int[] Map 25 //{ 26 // get { return map; } 27 // set { map = value; } 28 //} 29 30 31 public GameMap() 32 { 33 this.InitalMap(); 34 } 35 public int[] luckTurn = new int[6] { 6, 23, 40, 55, 69, 83 }; //聲明數組 36 public int[] landMine = new int[9] { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; 37 public int[] pause = new int[4] { 9, 27, 60, 93 }; 38 public int[] timeTunnel = new int[7] { 20, 25, 45, 63, 72, 88, 90 }; 39 public int InitalMap() 40 { 41 //用於存儲在地圖中為地雷的下標 42 //int[] luckTurn = { 6, 23, 40, 55, 69, 83 }; // 幸運輪盤◎ 43 //int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆ 44 //int[] pause = { 9, 27, 60, 93 };//暫停▲ 45 //int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//時空隧道卐 46 47 //初始化地圖中的幸運輪盤位置 48 for (int i = 0; i < luckTurn.Length; i++) 49 { 50 Map[luckTurn[i]]=1; 51 } 52 //初始化地圖中的地雷位置 53 for (int i = 0; i < landMine.Length; i++) 54 { 55 Map[landMine[i]]=2; 56 } 57 //初始化地圖中的暫停位置 58 for (int i = 0; i < pause.Length; i++) 59 { 60 Map[pause[i]] = 3; 61 } 62 //初始化地圖中的時空隧道位置 63 for (int i = 0; i < timeTunnel.Length; i++) 64 { 65 Map[timeTunnel[i]] = 4; 66 } 67 return 0; 68 } 69 70 71 // //畫地圖時注意,我們是從0-99依次畫地圖. 72 //以畫第一格為例,需要考慮到的情況如下: 73 //1. A和B 是否同時都在這一格上.如果在,則畫一個 <> 74 //2. A是否在當前要畫的這一格上,如果在,則畫一個 A 75 //3. B是否在當前要畫的這一格上,如果在,則畫一個 B 76 //4.如果上面的都不成立,我們就要考慮這一格是什么機關.比如我們當前畫的是第一格,那么我們就看一下 77 //map[0]中存的值是幾.如果是0,表示普通,則畫一個□ 如果是1,表示輪盤,則畫一個◎ 78 // 如果是2,則表示地雷,畫一個☆ 如果是3,則表示暫停,畫一個▲ 79 //如果是4,則表示時空隧道,則畫一個卐 80 81 82 /// <summary> 83 /// 得到第pos個位置應該畫的圖型 84 /// </summary> 85 /// <param name="pos">我們要畫的地圖的那一格的下標</param> 86 /// <param name="player1Pos">玩家A當前所在的地址位置,下標</param> 87 /// <param name="player2Pos">玩家B當前所在的地址位置,下標</param> 88 /// <returns></returns> 89 90 public string GetMapString(int pos, int player1Pos, int player2Pos) 91 { 92 string mystring = ""; 93 if (player1Pos == player2Pos && player1Pos == pos) 94 { 95 mystring="<>"; 96 } 97 else if (player1Pos == pos) 98 { 99 mystring="A"; 100 } 101 else if (player2Pos == pos) 102 { 103 mystring = "B"; 104 } 105 else 106 { 107 switch (Map[pos]) 108 { 109 case 1: 110 mystring = "◎"; 111 break; 112 case 2: 113 mystring = "☆"; 114 break; 115 case 3: 116 mystring = "▲"; 117 break; 118 case 4: 119 mystring = "卐"; 120 break; 121 default: 122 mystring = "□"; 123 break; 124 } 125 } 126 return mystring; 127 128 } 129 130 /// <summary> 131 /// 132 /// </summary> 133 /// <param name="player1Pos">A所在地圖的下標</param> 134 /// <param name="player2Pos">B所在地圖的下標</param> 135 public void DrawMap(int player1Pos, int player2Pos) 136 { 137 //輸出示例 138 Console.WriteLine("圖例:幸運輪盤--◎ 地雷--☆ 暫停--▲ 時空隧道--卐 普通--□"); 139 //輸出第一行 140 for (int i = 0; i <= 29; i++) 141 { 142 Console.Write(GetMapString(i,player1Pos,player2Pos)); 143 } 144 Console.WriteLine(); 145 146 //輸出第一列 147 for(int i=30;i<=34;i++) 148 { 149 for(int j=0;j<29;j++) 150 { 151 Console.Write(" "); 152 } 153 Console.WriteLine(GetMapString(i,player1Pos,player2Pos)); 154 } 155 156 157 //輸出第二行 158 for (int i = 64; i >= 35; i--) 159 { 160 Console.Write(GetMapString(i,player1Pos,player2Pos)); 161 } 162 Console.WriteLine(); 163 164 //輸出第二列 165 for (int j = 65; j <= 69; j++) 166 { 167 Console.WriteLine(GetMapString(j, player1Pos, player2Pos)); 168 } 169 170 171 //輸出第三行 172 for (int i = 70; i <= 99; i++) 173 { 174 Console.Write(GetMapString(i,player1Pos,player2Pos)); 175 } 176 Console.WriteLine(); 177 } 178 179 /// <summary> 180 /// 調用此方法,清屏,然后輸出有戲名稱 181 /// </summary> 182 public void ShowTitle() 183 { 184 //繪制歡迎界面 185 Console.Clear(); 186 Console.WriteLine("*************************************************"); 187 Console.WriteLine("* *"); 188 Console.WriteLine("* 騎 士 飛 行 棋 *"); 189 Console.WriteLine("* *"); 190 Console.WriteLine("*************************************************"); 191 } 192 193 /// <summary> 194 ///游戲設置,包括開始,背景色,前景色,字體等等 195 /// </summary> 196 public void GameSet() 197 { 198 int userInput = 0; 199 Console.WriteLine("1-----------游戲開始"); 200 Console.WriteLine("2-----------設置背景色"); 201 Console.WriteLine("3-----------設置字體"); 202 Console.WriteLine("請選擇:"); 203 userInput = ReadInt(1, 3); 204 //userInput = ReadInt(Console.Read(),Console.Read()); 205 switch (userInput) 206 { 207 case 2: 208 Console.WriteLine("1-------------白色"); 209 Console.WriteLine("2-------------紅色"); 210 Console.WriteLine("3-------------黑色"); 211 userInput = ReadInt(1, 3); 212 //userInput = ReadInt(Console.Read(), Console.Read()); 213 switch (userInput) 214 { 215 case 1: 216 Console.BackgroundColor = ConsoleColor.White; 217 break; 218 case 2: 219 Console.BackgroundColor = ConsoleColor.Red; 220 break; 221 case 3: 222 Console.BackgroundColor = ConsoleColor.Black; 223 break; 224 } 225 break; 226 case 3: 227 Console.WriteLine("1-------------白色"); 228 Console.WriteLine("2-------------紅色"); 229 Console.WriteLine("3-------------黑色"); 230 userInput=ReadInt(1,3); 231 //userInput = ReadInt(Console.Read(), Console.Read()); 232 userInput = ReadInt(Console.Read(), Console.Read()); 233 switch (userInput) 234 { 235 case 1: 236 Console.ForegroundColor = ConsoleColor.White; 237 break; 238 case 2: 239 Console.ForegroundColor = ConsoleColor.Red; 240 break; 241 case 3: 242 Console.ForegroundColor = ConsoleColor.Black; 243 break; 244 } 245 break; 246 default : 247 break; 248 249 } 250 251 } 252 253 public int ReadInt(int min, int max) 254 { 255 int userInput = 0; 256 while (int.TryParse(Console.ReadLine(), out userInput) == false //把輸入的字符轉換成32位有符號整數,並把值賦給userInput,然后返回它。 257 || (userInput >= min && userInput <= max) == false) 258 { 259 Console.WriteLine("輸入有誤,請重新輸入!"); 260 } 261 return userInput; 262 263 } 264 265 266 } 267 }
第三個類Program
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace GameDemo 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 GameManager gm = new GameManager(); 13 gm.Start(); 14 Console.ReadKey(); 15 16 } 17 } 18 }