//------------------------------------------------------------------------- public void UndoCommand(Command Cmd) { UChess chess = this[Cmd.To]; // 记录老坐标 Point lastChessPoint = chess.point; // 计算新坐标 Point newChessPoint = ToChessPoint(Cmd.From, chess.campType); // 将棋子从原始位置移除 this[chess.ToChessboardPoint()] = null; // 设置新坐标 chess.point = newChessPoint; // 移动位置 this[chess.ToChessboardPoint()] = chess; // 坐标还原 chess.ResetPosition(); // 如果吃掉了棋子,还原棋子 if (Cmd.EatedHistory != null) { AddChess(Cmd.EatedHistory); Cmd.EatedHistory = null; } // 切换回合 ToggleTun(); History.RemoveAt(History.Count - 1); }
//------------------------------------------------------------------------- // 胜负判定 bool Judge(UChess EatedChess) { if (EatedChess.chessType == EChessType.Shuai) { Winner = EatedChess.campType == ECampType.Red ? ECampType.Black : ECampType.Red; return(true); } return(false); }
//------------------------------------------------------------------------- // 添加一个棋子到棋盘上(如果指定坐标已经有棋子了,吃掉棋子) public void AddChess(UChess c) { if (this[c.ToChessboardPoint()] != null) { RemoveChess(this[c.ToChessboardPoint()]); } this[c.ToChessboardPoint()] = c; c.onAdded(); }
public override List <Point> GetAvailablePoints() { List <Point> R = new List <Point>(); Point pt1 = new Point(point.x - 1, point.y - 1); Point pt2 = new Point(point.x + 1, point.y - 1); Point pt3 = new Point(point.x - 1, point.y + 1); Point pt4 = new Point(point.x + 1, point.y + 1); UChess chess1 = null; UChess chess2 = null; UChess chess3 = null; UChess chess4 = null; if (ValidPoint(pt1)) { chess1 = chessboard[chessboard.ToChessboardPoint(pt1, campType)]; } if (IsValidPoint(pt2)) { chess2 = chessboard[chessboard.ToChessboardPoint(pt2, campType)]; } if (IsValidPoint(pt3)) { chess3 = chessboard[chessboard.ToChessboardPoint(pt3, campType)]; } if (IsValidPoint(pt4)) { chess4 = chessboard[chessboard.ToChessboardPoint(pt4, campType)]; } Point ptTarget1 = new Point(point.x - 2, point.y - 2); Point ptTarget2 = new Point(point.x + 2, point.y - 2); Point ptTarget3 = new Point(point.x - 2, point.y + 2); Point ptTarget4 = new Point(point.x + 2, point.y + 2); if (chess1 == null && ValidPoint(ptTarget1)) { R.Add(ptTarget1); } if (chess2 == null && ValidPoint(ptTarget2)) { R.Add(ptTarget2); } if (chess3 == null && ValidPoint(ptTarget3)) { R.Add(ptTarget3); } if (chess4 == null && ValidPoint(ptTarget4)) { R.Add(ptTarget4); } return(ModifyChessPoint(R)); }
public override List <Point> GetAvailablePoints() { Point pt1 = new Point(point.x - 1, point.y); Point pt2 = new Point(point.x + 1, point.y); Point pt3 = new Point(point.x, point.y + 1); Point pt4 = new Point(point.x, point.y - 1); List <Point> R = new List <Point>(); if (ValidPoint(pt1)) { R.Add(pt1); } if (ValidPoint(pt2)) { R.Add(pt2); } if (ValidPoint(pt3)) { R.Add(pt3); } if (ValidPoint(pt4)) { R.Add(pt4); } // 查看是否可以喝酒 Point PT = point; // X轴负向 while (PT.y < 10) { PT.y++; Point cbPoint = chessboard.ToChessboardPoint(PT, campType); UChess chess = chessboard[cbPoint]; if (chess != null) { if (chess.chessType == EChessType.Shuai) { R.Add(PT); break; } else { break; } } } return(ModifyChessPoint(R)); }
// 返回可吃掉别人棋子的点 public List <Point> GetEatPoints() { List <Point> R = new List <Point>(); List <Point> AvailablePoints = GetAvailablePoints(); for (int i = 0; i < AvailablePoints.Count; i++) { UChess chess = chessboard[chessboard.ToChessboardPoint(AvailablePoints[i], campType)]; if (chess != null && chess.campType != campType) { R.Add(AvailablePoints[i]); } } return(R); }
//------------------------------------------------------------------------- public void DoCommand(Command Cmd) { UChess chess = this[Cmd.From]; // 记录老坐标 Point lastChessPoint = chess.point; // 计算新坐标 Point newChessPoint = ToChessPoint(Cmd.To, chess.campType); // 吃掉新坐标的棋子 if (this[Cmd.To] != null && this[Cmd.To] != chess) { Cmd.EatedHistory = this[Cmd.To]; RemoveChess(this[Cmd.To]); // 胜负判定 if (!game_over) { if (Judge(Cmd.EatedHistory)) { game_over = true; if (onGameOver != null) { onGameOver(Winner); } } } } // 将棋子从原始位置移除 this[chess.ToChessboardPoint()] = null; // 设置新坐标 chess.point = newChessPoint; // 移动位置 this[chess.ToChessboardPoint()] = chess; // 事件通知 chess.ResetPosition(); // 切换玩家 ToggleTun(); // 记录 History.Add(Cmd); }
// 辅助函数,修正可移动点中我方棋子占据的点,并且移除当前位置的点 protected List <Point> ModifyChessPoint(List <Point> allAvailablePoints) { List <Point> R = new List <Point>(); foreach (var pt in allAvailablePoints) { if (pt == point) { continue; } UChess ptChess = chessboard[chessboard.ToChessboardPoint(pt, campType)]; if (ptChess != null && ptChess.campType == campType) { continue; } R.Add(pt); } return(R); }
//------------------------------------------------------------------------- // 创建棋盘 void Init() { game_start_timer = 0.0f; // 初始化棋盘 map = new UChess[9, 10]; for (int i = 0; i < StartChessboard.Length; i++) { UChess chess = System.Activator.CreateInstance(ChessTypeFactory[StartChessboard[i].chessType]) as UChess; chess.InitData(StartChessboard[i], this); AddChess(chess); } // 默认为红方视角 SetPlayerView(ECampType.Red); // 红方起手 SetNowTun(ECampType.Red); }
public Command Update(UChess input) { return(null); }
//------------------------------------------------------------------------- // 从棋盘移除一个棋子 public void RemoveChess(UChess c) { this[c.ToChessboardPoint()] = null; c.onRemoved(); }
public override List <Point> GetAvailablePoints() { List <Point> R = new List <Point>(); Point PT = point; // X轴负向 while (PT.x > 1) { PT.x--; Point cbPoint = chessboard.ToChessboardPoint(PT, campType); UChess chess = chessboard[cbPoint]; if (chess != null) { // 射击点 while (PT.x > 1) { PT.x--; UChess chess2 = chessboard[chessboard.ToChessboardPoint(PT, campType)]; if (chess2 != null) { R.Add(PT); break; } } break; } R.Add(PT); } // X轴正向 PT = point; while (PT.x < 9) { PT.x++; Point cbPoint = chessboard.ToChessboardPoint(PT, campType); UChess chess = chessboard[cbPoint]; if (chess != null) { // 射击点 while (PT.x < 9) { PT.x++; UChess chess2 = chessboard[chessboard.ToChessboardPoint(PT, campType)]; if (chess2 != null) { R.Add(PT); break; } } break; } R.Add(PT); } // Y轴负向 PT = point; while (PT.y > 1) { PT.y--; Point cbPoint = chessboard.ToChessboardPoint(PT, campType); UChess chess = chessboard[cbPoint]; if (chess != null) { // 射击点 while (PT.y > 1) { PT.y--; UChess chess2 = chessboard[chessboard.ToChessboardPoint(PT, campType)]; if (chess2 != null) { R.Add(PT); break; } } break; } R.Add(PT); } // Y轴正向 PT = point; while (PT.y < 10) { PT.y++; Point cbPoint = chessboard.ToChessboardPoint(PT, campType); UChess chess = chessboard[cbPoint]; if (chess != null) { // 射击点 while (PT.y < 10) { PT.y++; UChess chess2 = chessboard[chessboard.ToChessboardPoint(PT, campType)]; if (chess2 != null) { R.Add(PT); break; } } break; } R.Add(PT); } return(ModifyChessPoint(R)); }
public override List <Point> GetAvailablePoints() { List <Point> R = new List <Point>(); UChess leftchess = null; UChess rightchess = null; UChess upchess = null; UChess downchess = null; // 日字型 Point pt_left = new Point(point.x - 1, point.y); Point pt_right = new Point(point.x + 1, point.y); Point pt_up = new Point(point.x, point.y + 1); Point pt_down = new Point(point.x, point.y - 1); if (IsValidPoint(pt_left)) { leftchess = chessboard[chessboard.ToChessboardPoint(pt_left, campType)]; } if (IsValidPoint(pt_right)) { rightchess = chessboard[chessboard.ToChessboardPoint(pt_right, campType)]; } if (IsValidPoint(pt_up)) { upchess = chessboard[chessboard.ToChessboardPoint(pt_up, campType)]; } if (IsValidPoint(pt_down)) { downchess = chessboard[chessboard.ToChessboardPoint(pt_down, campType)]; } if (leftchess == null) { Point left1 = new Point(point.x - 2, point.y - 1); Point left2 = new Point(point.x - 2, point.y + 1); if (IsValidPoint(left1)) { R.Add(left1); } if (IsValidPoint(left2)) { R.Add(left2); } } if (rightchess == null) { Point right1 = new Point(point.x + 2, point.y - 1); Point right2 = new Point(point.x + 2, point.y + 1); if (IsValidPoint(right1)) { R.Add(right1); } if (IsValidPoint(right2)) { R.Add(right2); } } if (upchess == null) { Point up1 = new Point(point.x - 1, point.y + 2); Point up2 = new Point(point.x + 1, point.y + 2); if (IsValidPoint(up1)) { R.Add(up1); } if (IsValidPoint(up2)) { R.Add(up2); } } if (downchess == null) { Point down1 = new Point(point.x - 1, point.y - 2); Point down2 = new Point(point.x + 1, point.y - 2); if (IsValidPoint(down1)) { R.Add(down1); } if (IsValidPoint(down2)) { R.Add(down2); } } return(ModifyChessPoint(R)); }
public override void Update() { //H键切换视图 if (Input.GetKeyDown(KeyCode.H)) { if (ViewType == ECampType.Red) { ViewType = ECampType.Black; } else { ViewType = ECampType.Red; } Gamer.Chessboard.SetPlayerView(ViewType); } switch (MyOperateStep) { case EOpStep.Select: { //选择棋子 if (Input.GetMouseButtonDown(0)) { RaycastHit HitInfo; if (Physics.Raycast(Gamer.Chessboard.GetViewCamera().ScreenPointToRay(Input.mousePosition), out HitInfo)) { UChess HitChess = Gamer.Chessboard[Gamer.Chessboard.WorldToPos(HitInfo.point)]; if (HitChess != null && HitChess.campType == MyCamp) { selectedChess = HitChess; MyOperateStep = EOpStep.Push; ShowMoveTargets(); } } } } break; case EOpStep.Push: { RaycastHit HitInfo; if (Physics.Raycast(Gamer.Chessboard.GetViewCamera().ScreenPointToRay(Input.mousePosition), out HitInfo)) { if (selectedChess.gameObject != null) { selectedChess.gameObject.transform.position = HitInfo.point; } if (Input.GetMouseButtonDown(0)) { Point NewPoint = Gamer.Chessboard.WorldToPos(HitInfo.point); if (selectedChess.GetAvailablePoints().Contains(Gamer.Chessboard.ToChessPoint(NewPoint, selectedChess.campType))) { Command Cmd = new Command(); Cmd.Camp = MyCamp; Cmd.From = selectedChess.ToChessboardPoint(); Cmd.To = NewPoint; Gamer.Chessboard.DoCommand(Cmd); } else { selectedChess.ResetPosition(); } ClearMoveTargets(); MyOperateStep = EOpStep.Select; } } } break; default: break; } }