How to write the script for C # to realize the classic flight chess game?
Today, I would like to share with you the relevant knowledge of how to write the script of the classic flight chess game in C#. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
Effect display
Main function static void Main (string [] args) {int w = 50; int h = 30; ConsoleInit (w, h); E_SceneType nowSceneType = E_SceneType.Begin While (true) {switch (nowSceneType) {case E_SceneType.Begin: Console.Clear (); GameEndOrBegin (w, h, ref nowSceneType); break Case E_SceneType.Game: Console.Clear (); GameScene (w, h, ref nowSceneType); break; case E_SceneType.End: Console.Clear (); GameEndOrBegin (w, h, ref nowSceneType) Break; default: break } enumeration of scene types enum E_SceneType {Begin, Game, End,} console basic Settings static void ConsoleInit (int w, int h) {/ / console Settings Console.CursorVisible = false; Console.SetWindowSize (w, h) Console.SetBufferSize (w, h);} start and end scene logic static void GameEndOrBegin (int w, int h, ref E_SceneType nowSceneType) {Console.ForegroundColor = ConsoleColor.White; Console.SetCursorPosition (nowSceneType = = E_SceneType.Begin? W / 2-3: W / 2-4,8); Console.Write (nowSceneType = = E_SceneType.Begin? "Flying Chess": "Game over"); / / the number of the current option int count = 0; bool IsOver = false; while (true) {Console.SetCursorPosition (nowSceneType = = E_SceneType.Begin? Wmax 2-4:w/2-5,11); Console.ForegroundColor = count = = 0? ConsoleColor.Red: ConsoleColor.White; Console.Write (nowSceneType = = E_SceneType.Begin? "Game start": "back to the main menu"); Console.SetCursorPosition (w _ count 2-4, 13); Console.ForegroundColor = count = = 1? ConsoleColor.Red: ConsoleColor.White; Console.Write ("quit the game"); switch (Console.ReadKey (true) .key) {case ConsoleKey.W:-- count; if (count
< 0) { count = 0; } break; case ConsoleKey.S: ++count; if (count >1) {count = 1;} break; case ConsoleKey.J: if (count = = 0) {nowSceneType = nowSceneType = = E_SceneType.Begin? IsOver = true;} else {Environment.Exit (0);} break } if (IsOver) break;}} Game scene Logic static void GameScene (int w, int h, ref E_SceneType nowSceneType) {DrawWall (w, h); Map map = new Map (14,3,80); map.Draw () Player player = new Player (0, E_Player_Type.Player); Player computer = new Player (0, E_Player_Type.Computer); DrawPlayer (map, player, computer); while (true) {if (PlayerRandomMove (w, h, ref player, ref computer, map, ref nowSceneType)) {break } if (PlayerRandomMove (w, h, ref computer, ref player, map, ref nowSceneType)) {break } static bool PlayerRandomMove (int w, int h, ref Player p, ref Player otherP, Map map, ref E_SceneType nowSceneType) {/ / Game logic after / / player throwing dice logic / / check input Console.ReadKey (true) / / logical bool isGameOver of throwing dice = RandomMove (w, h, ref p, ref otherP, map); / / mapping map.Draw (); / / drawing player DrawPlayer (map, p, otherP) / / decide whether to end the game if (isGameOver) {/ / jam the program and let the player press any key Console.ReadKey (true); nowSceneType = evolving SceneType.end;} return isGameOver } fixed printed information static void DrawWall (int w, int h) {Console.ForegroundColor = ConsoleColor.Red; / / horizontal wall for (int I = 0; I
< w; i+=2) { //最上面一行 Console.SetCursorPosition(i, 0); Console.Write("■"); //中间一行 Console.SetCursorPosition(i, h-6); Console.Write("■"); Console.SetCursorPosition(i, h - 11); Console.Write("■"); //最下面一行 Console.SetCursorPosition(i, h-1); Console.Write("■"); } //竖着的墙 for(int i = 0; i < h; i++) { //左边的墙 Console.SetCursorPosition(0, i); Console.Write("■"); //右边的墙 Console.SetCursorPosition(w-2, i); Console.Write("■"); } Console.SetCursorPosition(2, h - 5); Console.ForegroundColor = ConsoleColor.White; Console.Write("按任意键开始扔色子"); Console.SetCursorPosition(2, h - 10); Console.Write("□:普通格子"); Console.SetCursorPosition(2, h - 9); Console.ForegroundColor = ConsoleColor.Blue; Console.Write("■:暂停,一回合不动"); Console.SetCursorPosition(22,h - 9); Console.ForegroundColor = ConsoleColor.Red; Console.Write("●:炸弹,倒退5格"); Console.SetCursorPosition(2, h - 8); Console.ForegroundColor = ConsoleColor.White; Console.Write("×:时空隧道,随机倒退,暂停,交换位置"); Console.SetCursorPosition(2, h - 7); Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("★:玩家 "); Console.SetCursorPosition(11, h - 7); Console.ForegroundColor = ConsoleColor.Magenta; Console.Write("▲:电脑 "); Console.SetCursorPosition(20, h - 7); Console.ForegroundColor = ConsoleColor.Blue; Console.Write("◎:玩家电脑重合"); }格子类型枚举和格子结构体 enum E_Grid_Type { Normal, Boom, Pause, Tunnel, } /// /// 位置信息结构体 /// struct Vector2 { public int x; public int y; public Vector2(int x, int y) { this.x = x; this.y = y; } } struct Grid { //格子的类型 public E_Grid_Type _type; //格子的位置 public Vector2 pos; //构造函数 public Grid(int x, int y, E_Grid_Type type) { pos.x = x; pos.y = y; _type = type; } //画一个格子 public void Draw() { Console.SetCursorPosition(pos.x, pos.y); switch(_type) { case E_Grid_Type.Normal: Console.ForegroundColor = ConsoleColor.White; Console.Write("□"); break; case E_Grid_Type.Boom: Console.ForegroundColor = ConsoleColor.Red; Console.Write("●"); break; case E_Grid_Type.Pause: Console.ForegroundColor = ConsoleColor.Blue; Console.Write("■"); break; case E_Grid_Type.Tunnel: Console.ForegroundColor = ConsoleColor.White; Console.Write("×"); break; } } }地图结构体 struct Map { public Grid[] grids; public Map(int x, int y, int num) { grids = new Grid[num]; int indexX = 0; int indexY = 0; int stepNum = 2; Random r = new Random(); int randomNum; for(int i = 0; i < num; i++) { randomNum = r.Next(0, 101); if(randomNum < 85 || i == 0 || i == num - 1) { //普通格子 grids[i]._type = E_Grid_Type.Normal; } else if(randomNum < 90 && randomNum >= 85) {/ / bomb grids [I]. _ type = estranged Gridged Type.Boom;} else if (randomNum
< 95 && randomNum >= 90) {/ / pause grids [I]. _ type = estranged Gridged Type. Pause.} else {/ / space-time tunnel grids [I]. _ type = E_Grid_Type.Tunnel } grids [I] .pos = new Vector2 (x, y); if (indexX = = 10) {y + = 1; indexY++; if (indexY = = 2) {indexX = 0 IndexY = 0; stepNum =-stepNum;}} else {x + = stepNum; indexX++ } public void Draw () {for (int I = 0; I)
< grids.Length; i++) { grids[i].Draw(); } } }玩家和电脑结构体 enum E_Player_Type { Player, Computer, } struct Player { public E_Player_Type type; public int nowIndex; //是否暂停的标识 public bool isPause; public Player(int index, E_Player_Type type) { nowIndex = index; this.type = type; isPause = false; } public void Draw(Map mapInfo) { //从传入的地图中得到格子信息 Grid grid = mapInfo.grids[nowIndex]; Console.SetCursorPosition(grid.pos.x, grid.pos.y); switch(type) { case E_Player_Type.Player: Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("★"); break; case E_Player_Type.Computer: Console.ForegroundColor = ConsoleColor.Magenta; Console.Write("▲"); break; } } }绘制玩家 static void DrawPlayer(Map map, Player player, Player computer) { //重合时 if(player.nowIndex == computer.nowIndex) { //得到重合的位置 Grid grid = map.grids[player.nowIndex]; Console.SetCursorPosition(grid.pos.x, grid.pos.y); Console.ForegroundColor = ConsoleColor.DarkGreen; Console.Write("◎"); } //不重合时 else { player.Draw(map); computer.Draw(map); } }扔骰子逻辑 //擦除提示的函数 static void ClearInfo(int h) { Console.SetCursorPosition(2, h - 5); Console.Write(" "); Console.SetCursorPosition(2, h - 4); Console.Write(" "); Console.SetCursorPosition(2, h - 3); Console.Write(" "); Console.SetCursorPosition(2, h - 2); Console.Write(" "); } /// /// 扔色子函数 /// >/ window width / / window height / object throwing dice / Map Information / / returned by default false represents no end of static bool RandomMove (int w, int h, ref Player p, ref Player otherP, Map map) {/ / prompt message displayed before erasing ClearInfo (h) / / according to the type of player throwing dice, determine the color of the message Console.ForegroundColor = p.type = = E_Player_Type.Player? ConsoleColor.Cyan: ConsoleColor.Magenta; / / determine whether the player is paused before throwing dice if (p.isPause) {Console.SetCursorPosition (2, h-5); Console.Write ("paused, {0} needs to be paused for a round", p.type = = E_Player_Type.Player? "you": "computer"); Console.SetCursorPosition (2, h-4); Console.Write ("Please press any key and let {0} start throwing dice", p.type = = E_Player_Type.Player? "computer": "you"); / / stop pausing p.isPause = false; return false;} / / the purpose of throwing dice is to change the position of the player or computer to calculate the position change / / throw dice randomly with a number from 1 to 6, plus to Random r = new Random () Int randomNum = r.Next (1,7); p.nowIndex + = randomNum; / / print thrown points Console.SetCursorPosition (2, h-5); Console.Write ("{0} points thrown: {1}", p.type = = E_Player_Type.Player? "you": "computer", randomNum); / / first determine whether the finish line has been reached if (p.nowIndex > = map.grids.Length-1) {p.nowIndex = map.grids.Length-1; Console.SetCursorPosition (2, h-4) If (p.type = = E_Player_Type.Player) {Console.Write ("Congratulations, you have reached the finish line first");} else {Console.Write ("Unfortunately, the computer has reached the finish line first") } Console.SetCursorPosition (2, h-3); Console.Write ("Please press any key to end"); return true } else {/ / determine what type of lattice the current object is in before it reaches the end point Grid grid = map.grids [p.nowIndex] Switch (grid._type) {case E_Grid_Type.Normal: Console.SetCursorPosition (2, h-4); Console.Write ("{0} reached a safe location", p.type = = E_Player_Type.Player? "you": "computer"); Console.SetCursorPosition (2, h-3); Console.Write ("Please press any key and let {0} start throwing dice", p.type = = E_Player_Type.Player? "computer": "you"); break; case E_Grid_Type.Boom: p.nowIndex-= 5; if (p.nowIndex < 0) {p.nowIndex = 0 } Console.SetCursorPosition (2, h-4); Console.Write ("{0} stepped on the bomb, step back 5 squares", p.type = = E_Player_Type.Player? "you": "computer"); Console.SetCursorPosition (2, h-3); Console.Write ("Please press any key and let {0} start throwing dice", p.type = = E_Player_Type.Player? "computer": "you") break; case E_Grid_Type.Pause: p.isPause = true; Console.SetCursorPosition (2, h-4) Console.Write ("{0} has reached the pause point, you need to pause a round", p.type = = E_Player_Type.Player? "you": "computer"); Console.SetCursorPosition (2, h-3); Console.Write ("Please press any key and let {0} start throwing dice", p.type = = E_Player_Type.Player? "computer": "you"); break; case E_Grid_Type.Tunnel: Console.SetCursorPosition (2, h-4); Console.Write ("{0} stepped on the time tunnel", p.type = = E_Player_Type.Player? "you": "computer"); / / Random randomNum = r.Next (1,91); if (randomNum