public void Move(ref Player player, ref Map map) { Random r = new Random(); int num = r.Next(1, 7); string msg = ""; Console.WriteLine("{0}按任意键掷筛子", name); Console.ReadKey(true); Console.WriteLine("{0}掷出了{1}", name, num); Console.WriteLine("{0}按任意键开始行动......", name); Console.ReadKey(true); index += num; CheckPos(); if (this.index == player.index) { msg = string.Format("玩家{0}踩到玩家{1},玩家{2}退6格", this.name, player.name, player.name); player.index -= 6; CheckPos(); } else { MapEvent(ref player); } Console.Clear(); if (piece.type == GameObjectType.Player1) { map.DrawMap(ref this, ref player); } else { map.DrawMap(ref player, ref this); } Console.WriteLine(msg); }
private void btnStart_Click(object sender, RoutedEventArgs e) { try { if (txtIP.Visibility == Visibility.Visible) { txtIP.Visibility = txtPort.Visibility = btnListen.Visibility = tbMsg.Visibility = btnSend.Visibility = btnLink.Visibility = Visibility.Collapsed; } if (btnStart.Content.ToString() == "开始游戏") { #region 判空 if (String.IsNullOrEmpty(pi1.txtPlayerName.Text) || String.IsNullOrWhiteSpace(pi1.txtPlayerName.Text)) { MessageBox.Show("昵称不能为空,请重新输入。"); return; } #endregion #region 更改按钮状态 btnStart.Content = "结束游戏"; btnStart.Background = new SolidColorBrush() { Color = Color.FromArgb(255, 255, 100, 50) }; #endregion #region 初始化 //初始化地图 _Map = new Map() { landMine = new int[] { 5, 13, 17, 33, 38, 50, 64, 80, 94 }, luckyturn = new int[] { 6, 23, 40, 55, 69, 83 }, pause = new int[] { 9, 27, 60, 93 }, timeTunnel = new int[] { 20, 25, 45, 63, 72, 88, 90 } }; _Map.InitailMap(); if (flagMap != true) { Map.DrawMap(_Map.Maps, gdMap); flagMap = true; } else { foreach (var o in gdMap.Children) { if (o is TextBox) { (o as TextBox).Visibility = Visibility.Visible; } } } //初始化玩家 pi1.txtPlayerName.IsEnabled = false; pi2.txtPlayerName.IsEnabled = false; if (flagMode == false) { pi1.ellAvatar.Fill = new SolidColorBrush() { Color = Color.FromArgb(255, 255, 0, 0) }; pi2.ellAvatar.Fill = new SolidColorBrush() { Color = Color.FromArgb(255, 0, 0, 255) }; } btnPlay.IsEnabled = true; _Player1 = new Player() { PlayerName = pi1.txtPlayerName.Text, PlayerPo = 0, Flag = 0, PlayerUI = ellPlayer1 }; _Player2 = new Player() { PlayerName = pi2.txtPlayerName.Text, PlayerPo = 0, Flag = 1, PlayerUI = ellPlayer2 }; ellPlayer1.Visibility = Visibility.Visible; ellPlayer2.Visibility = Visibility.Visible; Grid.SetZIndex(ellPlayer1, 2); Grid.SetZIndex(ellPlayer2, 2); #endregion #region 主机客机不同的操作 if (flagMode == true && flagEnd == true) //主机 { //发送开局信息 var list = new List <byte>(); list.Add(11); list.AddRange(Encoding.UTF8.GetBytes("游戏开始")); socketSend.Send(list.ToArray()); } else if (flagMode == true && flagEnd == false) //客机 { btnPlay.IsEnabled = false; _Player1 = new Player() { PlayerName = pi2.txtPlayerName.Text.Trim(), PlayerPo = 0, Flag = 0, PlayerUI = ellPlayer1 }; _Player2 = new Player() { PlayerName = pi1.txtPlayerName.Text.Trim(), PlayerPo = 0, Flag = 1, PlayerUI = ellPlayer2 }; } #endregion output("游戏开始"); } else { #region 切换按钮状态 btnStart.Content = "开始游戏"; btnStart.Background = new SolidColorBrush() { Color = Color.FromArgb(255, 100, 255, 50) }; #endregion if (txtIP.Visibility == Visibility.Collapsed) { txtIP.Visibility = txtPort.Visibility = btnListen.Visibility = tbMsg.Visibility = btnSend.Visibility = btnLink.Visibility = Visibility.Visible; } pi1.txtPo.Text = pi2.txtPo.Text = "0"; #region 隐藏地图,角色归位,清空变量 foreach (var o in gdMap.Children) { if (o is TextBox) { (o as TextBox).Visibility = Visibility.Hidden; } } pi1.txtPlayerName.IsEnabled = true; btnPlay.IsEnabled = false; Grid.SetColumn(ellPlayer1, 0); Grid.SetRow(ellPlayer1, 0); Grid.SetColumn(ellPlayer2, 0); Grid.SetRow(ellPlayer2, 0); ellPlayer1.Visibility = Visibility.Hidden; ellPlayer2.Visibility = Visibility.Hidden; _Map = null; _Player1 = null; _Player2 = null; if (flagMode == false) { pi2.txtPlayerName.IsEnabled = true; } #endregion #region 主机客机不同的操作 if (flagMode == true && flagEnd == true) { //发送结束信息 byte[] buffer = Encoding.UTF8.GetBytes("游戏结束"); List <byte> list = new List <byte>(); list.Add(11); list.AddRange(buffer); socketSend.Send(list.ToArray()); } else if (flagMode == true && flagEnd == false) { pi2.txtPlayerName.IsEnabled = false; } #endregion output("游戏结束"); } } catch (Exception ex) { if (!File.Exists("errorlogs.txt")) { File.CreateText("errorlogs.txt").Close(); } var fs = new FileStream("errorlogs.txt", FileMode.Append); var sw = new StreamWriter(fs, Encoding.Default); sw.Write(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + ":" + ex.ToString() + "\r\n"); sw.Close(); fs.Close(); } }
static void Main(string[] args) { ShowUI(); Map map = new Map(); Player a = new Player(); a.piece.type = GameObjectType.Player1; Player b = new Player(); b.piece.type = GameObjectType.Player2; map.InitMap(); a.map = map; b.map = map; Console.WriteLine("请输入玩家A的姓名"); a.name = Console.ReadLine(); while (a.name == "") { Console.WriteLine("玩家A的姓名不能为空,请重新输入"); a.name = Console.ReadLine(); } Console.WriteLine("请输入玩家B的姓名"); b.name = Console.ReadLine(); while (b.name == a.name || b.name == "") { if (b.name == a.name) { Console.WriteLine("玩家B和玩家A的姓名{0}不能相同,请重新输入玩家B的姓名", a.name); } else { Console.WriteLine("玩家B的姓名为空,请重新输入"); } b.name = Console.ReadLine(); } Console.Clear(); Console.WriteLine("对战开始......"); Console.WriteLine("{0}的士兵用A表示", a.name); Console.WriteLine("{0}的士兵用B表示", b.name); Console.Clear(); map.DrawMap(ref a, ref b); while (a.index <= 99 && b.index <= 99) { if (a.isPause == false) { a.Move(ref b, ref map); } else { a.isPause = false; } if (a.index == 99) { Console.WriteLine("恭喜{0}胜利了!", a.name); break; } if (b.isPause == false) { b.Move(ref a, ref map); } else { b.isPause = false; } if (b.index == 99) { Console.WriteLine("恭喜{0}胜利了!", b.name); break; } } Win(); Console.ReadKey(); }