C#控制臺(tái)實(shí)現(xiàn)飛行棋游戲
本文實(shí)例為大家分享了C#實(shí)現(xiàn)飛行棋游戲的具體代碼,供大家參考,具體內(nèi)容如下
游戲截圖:
管理類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class ProgramManage { //隨機(jī)數(shù) public static Random rd = new Random(); //定義地圖大小 public static int[] Maps = new int[100]; // public static bool[] PlayerPause = {false,false }; //定義玩家A和玩家B的坐標(biāo),0是玩家A的坐標(biāo);1是玩家B的坐標(biāo) public static int[] PlayerPos = new int[2]; //存儲(chǔ)兩個(gè)玩家的姓名 public static string[] PlayerName = new string[2]; //定義名字錯(cuò)誤類型 public static string[] Error = {"正確","不能有數(shù)字","重名了","不能為空" }; //畫游戲頭 public static void GanmeShow() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("*************************"); Console.WriteLine("********飛行棋游戲*******"); Console.WriteLine("*************************"); Console.ForegroundColor = ConsoleColor.Green; } //定義地圖關(guān)卡布局 public static void InitailMap() { int[] luckturn = { 6, 23, 40, 55, 69, 83 };//幸運(yùn)輪盤◎ for (int i = 0; i < luckturn.Length; i++) { Maps[luckturn[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 };//時(shí)空隧道卍 for (int i = 0; i < timeTunnel.Length; i++) { Maps[timeTunnel[i]] = 4; } } //定義地圖元素。輸入坐標(biāo)值,方法內(nèi)部判斷當(dāng)前坐標(biāo)應(yīng)當(dāng)放什么符號(hào),返回符號(hào)值 public static string MapElement(int i) { string Element = "0"; //如果玩家AB坐標(biāo)相同并都在當(dāng)前位置則畫一個(gè)“<>” if (PlayerPos[0] == i && PlayerPos[1] == i) { Element="<>"; } //有A畫A,有B畫B,都沒有畫地圖 else if (PlayerPos[0] == i) { Element="A"; } else if (PlayerPos[1] == i) { Element="B"; } else { switch (Maps[i]) { case 0: Element = "□"; break; case 1: Element = "◎"; break; case 2: Element = "×"; break; case 3: Element = "▲"; break; case 4: Element = "卍"; break; } } return Element; } //繪制地圖 public static void DrawMap() { Console.ForegroundColor = ConsoleColor.White; //第一橫行 for (int i = 0; i < 30; i++) { Console.Write(MapElement(i)); } //第一豎行 for (int i = 30; i < 35; i++) { Console.WriteLine(); Console.Write(" "); Console.Write(MapElement(i)); } //第二橫行 Console.WriteLine(); for (int i = 64; i > 34; i--) { Console.Write(MapElement(i)); } //第二豎行 for (int i = 65; i < 70; i++) { Console.WriteLine(); Console.Write(MapElement(i)); } //第三橫行 Console.WriteLine(); for (int i = 70; i < 100; i++) { Console.Write(MapElement(i)); } Console.WriteLine(); } //判斷姓名是否符合要求 public static int NameJudge(string str) { int result = 0; //判斷是否為空 if (str == "") { result = 3; return result; } else { result = 0; } //判斷是否是字符串 for (int i = 0; i < str.Length; i++) { if (char.IsNumber(str,i)) { result = 1; return result; } else { result = 0; } } //判斷是否重名 for (int i = 0; i < PlayerName.Length; i++) { if (PlayerName[i]==str) { result = 2; return result; } else { result = 0; } } return result; } //投骰子 public static void Play(int player) { PlayerPos[player] += rd.Next(1, 7); } //坐標(biāo)不能出地圖 public static void Limit(int player) { if (PlayerPos[player] < 0) { PlayerPos[player] = 0; } else if (PlayerPos[player] > 99) { PlayerPos[player] = 99; } } //執(zhí)行規(guī)則 public static void GameRule(int player) { //踩到對(duì)手 if (PlayerPos[player]==PlayerPos[1-player]) { PlayerPos[1 - player] -= 6; Console.WriteLine("{0}踩到對(duì)手,對(duì)手退6格", PlayerName[player]); } //什么也沒踩到 else if (Maps[PlayerPos[player]] == 0) { Console.WriteLine("{0}玩家前進(jìn)了,什么也沒踩到",PlayerName[player]); } //幸運(yùn)輪盤 else if (Maps[PlayerPos[player]] == 1) { Console.WriteLine("1,與對(duì)手交換位置。2,讓對(duì)手退回原點(diǎn)"); string chose; while (true) { chose = Console.ReadLine(); if (chose == "1") { int tem = PlayerPos[player]; PlayerPos[player] = PlayerPos[1 - player]; PlayerPos[1 - player] = tem; Console.WriteLine("{0}與對(duì)手交換了位置", PlayerName[player]); break; } else if (chose == "2") { PlayerPos[1 - player] = 0; Console.WriteLine("{0}把對(duì)手打回原位", PlayerName[player]); break; } else { Console.WriteLine("請(qǐng)重新輸入"); } } } //地雷 else if (Maps[PlayerPos[player]] == 2) { PlayerPos[player] -= 6; Console.WriteLine("{0}踩到地雷了,倒退6格", PlayerName[player]); } //暫停 else if (Maps[PlayerPos[player]] == 3) { PlayerPause[player] = true; Console.WriteLine("{0}暫停一回合", PlayerName[player]); } //時(shí)空隧道 else if (Maps[PlayerPos[player]] == 4) { PlayerPos[player] += 10; Console.WriteLine("{0}前進(jìn)10格", PlayerName[player]); } } //判斷輸贏 public static string Winner() { if (PlayerPos[0]>PlayerPos[1]) { return PlayerName[0]; } else { return PlayerName[1]; } } } }
實(shí)現(xiàn)類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { //初始化標(biāo)題 ProgramManage.GanmeShow(); //初始化地圖關(guān)卡 ProgramManage.InitailMap(); //輸入名字 string name; for (int i = 0; i < ProgramManage.PlayerName.Length; i++) { Console.WriteLine("請(qǐng)輸入玩家{0}的名字", i+1); name = Console.ReadLine(); while (ProgramManage.NameJudge(name)!=0) { Console.WriteLine("名字格式不對(duì),{0},請(qǐng)重新輸入", ProgramManage.Error[ProgramManage.NameJudge(name)]); name = Console.ReadLine(); } ProgramManage.PlayerName[i] = name; } //刷新界面 Console.Clear(); ProgramManage.GanmeShow(); Console.WriteLine("玩家A名字是{0},玩家B名字{1}", ProgramManage.PlayerName[0], ProgramManage.PlayerName[1]); ProgramManage.DrawMap(); //游戲主循環(huán) //初始化玩家A開始 int player = 0; //兩個(gè)玩家都沒在終點(diǎn)的時(shí)候不停的玩游戲 while (ProgramManage.PlayerPos[0]<99&&ProgramManage.PlayerPos[1]<99) { //判斷當(dāng)前玩家是否暫停,如果暫停了就停一回合 if (ProgramManage.PlayerPause[player]==false) { Console.WriteLine("輪到玩家{0}敲回車投骰子", ProgramManage.PlayerName[player]); Console.ReadKey(); //玩家player投骰子 ProgramManage.Play(player); ProgramManage.Limit(player); //清空界面 Console.Clear(); //執(zhí)行規(guī)則,如果踩到了關(guān)卡,一直執(zhí)行,直到踩到空白點(diǎn)位置 while (true) { if (ProgramManage.Maps[ProgramManage.PlayerPos[player]] != 0) { ProgramManage.GameRule(player); ProgramManage.Limit(player); } ProgramManage.GameRule(player); break; } ProgramManage.Limit(player); //變化玩家 player = 1 - player; //刷新地圖 ProgramManage.DrawMap(); } else { //當(dāng)前玩家恢復(fù)暫停 ProgramManage.PlayerPause[player] = false; //下一回合讓給另一個(gè)玩家 player = 1 - player; continue; } } //游戲結(jié)束 Console.WriteLine("玩家{0}贏了", ProgramManage.Winner()); Console.ReadKey(); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#存儲(chǔ)相同鍵多個(gè)值的Dictionary實(shí)例詳解
在本篇文章里小編給大家整理的是關(guān)于C#存儲(chǔ)相同鍵多個(gè)值的Dictionary實(shí)例內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-03-03C#中參數(shù)個(gè)數(shù)可變的方法實(shí)例分析
這篇文章主要介紹了C#中參數(shù)個(gè)數(shù)可變的方法,以一個(gè)簡(jiǎn)單實(shí)例分析了C#中參數(shù)個(gè)數(shù)可變的方法,主要是使用params關(guān)鍵字來實(shí)現(xiàn)的,是C#編程中比較實(shí)用的技巧,需要的朋友可以參考下2014-11-11在C#語言里對(duì)NULL的技術(shù)處理小結(jié)
在 C# 中處理 null 值是編寫可靠且可靠的代碼的一個(gè)重要方面,在本文中,我將討論一些在 C# 中處理 null 值的最常用技術(shù),感興趣的朋友跟隨小編一起看看吧2024-05-05關(guān)于C#操作文件路徑(Directory)的常用靜態(tài)方法詳解
這篇文章主要給大家介紹了關(guān)于C#操作文件路徑(Directory)的常用靜態(tài)方法,Directory類位于System.IO 命名空間,Directory類提供了在目錄和子目錄中進(jìn)行創(chuàng)建移動(dòng)和列舉操作的靜態(tài)方法,需要的朋友可以參考下2021-08-08C#版免費(fèi)離線人臉識(shí)別之虹軟ArcSoft?V3.0(推薦)
本文只是簡(jiǎn)單介紹了如何使用虹軟的離線SDK,進(jìn)行人臉識(shí)別的方法,并且是圖片的方式,本地離線識(shí)別最大的好處就是沒有延遲,識(shí)別結(jié)果立馬呈現(xiàn),對(duì)C#離線人臉識(shí)別虹軟相關(guān)知識(shí)感興趣的朋友一起看看吧2021-12-12C# Winform 子窗體訪問父級(jí)窗體的控件和屬性
本文主要介紹兩種子窗體訪問父窗體控件和屬性的方法,大家可以參考一下,本人比較偏向第二種,把父窗體作為屬性傳遞,一勞永逸,想訪問父窗體的什么控件屬性都可以。2016-05-05