public Vector2DWrapper CalculateToCell(Turn t) { BoardWrapper b = _game.GetBoard(); Vector2DWrapper goalPosition = ConvertIntToBoardPosition(t.FromTile); int moveSize = t.MoveType == CommunicationProtocol.MoveType.Jump ? 2 : 1; switch (t.Direction) { case Direction.None: break; case Direction.North: goalPosition.Y -= moveSize; break; case Direction.East: goalPosition.X += moveSize; break; case Direction.South: goalPosition.Y += moveSize; break; case Direction.West: goalPosition.X -= moveSize; break; case Direction.NorthEast: goalPosition.X += moveSize; goalPosition.Y -= moveSize; break; case Direction.NorthWest: goalPosition.X -= moveSize; goalPosition.Y -= moveSize; break; case Direction.SouthEast: goalPosition.X += moveSize; goalPosition.Y += moveSize; break; case Direction.SouthWest: goalPosition.X -= moveSize; goalPosition.Y += moveSize; break; } return goalPosition; }
public MoveWrapper ConvertTurnToMove(Turn t) { engine.wrapper.MoveType mt = engine.wrapper.MoveType.INSERT; switch (t.MoveType) { case CommunicationProtocol.MoveType.Insert: mt = engine.wrapper.MoveType.INSERT; break; case CommunicationProtocol.MoveType.Jump: mt = engine.wrapper.MoveType.JUMP; break; case CommunicationProtocol.MoveType.Move: mt = engine.wrapper.MoveType.STEP; break; } Vector2DWrapper toCell = CalculateToCell(t); if (t.EmptyTile == null) { return new MoveWrapper(mt, ConvertIntToBoardPosition(t.FromTile), toCell); } else { return new MoveWrapper(mt, ConvertIntToBoardPosition(t.FromTile), toCell, ConvertIntToBoardPosition(t.EmptyTile)); } }
public String TurnToString(Turn t) { return String.Format("TURN: Type: {0}, FDU: {1},{2},{3}", t.MoveType, t.FromTile, t.Direction, t.EmptyTile); }
/// <summary> /// Methode om het WinDetected event te activeren. /// Dit geeft aan dat de andere partij een winst heeft gevonden na de meegegeven zet. /// </summary> /// <param name="t">De winnende zet</param> /// <param name="p">De winnende speler, normaal Player.Me</param> /// <seealso cref="Turn"/> /// <seealso cref="Player"/> internal void OnWinDetected(Turn t, Player p) { if (WinDetected != null) { WinDetected(t, p); } }
public Turn ConvertMoveToTurn(MoveWrapper mw) { CommunicationProtocol.MoveType mt = CommunicationProtocol.MoveType.Insert; switch (mw.GetMoveType()) { case engine.wrapper.MoveType.INSERT: mt = CommunicationProtocol.MoveType.Insert; break; case engine.wrapper.MoveType.JUMP: mt = CommunicationProtocol.MoveType.Jump; break; case engine.wrapper.MoveType.STEP: mt = CommunicationProtocol.MoveType.Move; break; } Turn t = new Turn(); t.MoveType = mt; if (mw.HasUsedCell()) { t.EmptyTile = ConvertBoardPositionToInt(mw.GetUsedCell()); } else { t.EmptyTile = null; } if (mw.GetMoveType() == engine.wrapper.MoveType.INSERT) { t.FromTile = ConvertBoardPositionToInt(mw.GetToCell()); t.Direction = Direction.None; } else { t.FromTile = ConvertBoardPositionToInt(mw.GetFromCell()); t.Direction = CalculateDirection(mw.GetToCell(), mw.GetFromCell()); } return t; }
/// <summary> /// Methode om het SentMoveInvalid event te activeren. /// Dit geeft aan de de verzonden zet invalide is bevonden door de andere partij. /// Na dit bericht wordt een disconnect bericht verstuurd en het disconnected event van deze instantie aangeroepen. /// </summary> /// <param name="t">De invalide bevonden zet</param> /// <seealso cref="SentMoveInvalid"/> /// <seealso cref="OnDisconnected"/> internal void OnSentMoveInvalid(Turn t) { if (SentMoveInvalid != null) { SentMoveInvalid(t); DisconnectMessage dc = new DisconnectMessage(); dc.Reason = DisconnectReason.InvalidMove; dc.Type = MessageType.Disconnect; SendMessage(dc); OnDisconnected(dc.Reason); } }
/// <summary> /// Methode om het TurnReceived event te activeren /// </summary> /// <param name="t">Ontvangen zet</param> internal void OnTurnReceived(Turn t) { if (TurnReceived != null) { TurnReceived(t); } }
//Public methoden voor gebruik door game instanties /// <summary> /// Verstuur een zet naar de andere instantie. /// Maakt gebruik van de interne methode SendMessage() /// </summary> /// <param name="t">Een gevulde zet</param> /// <seealso cref="Turn"/> /// <seealso cref="SendMessage"/> public void SendTurn(Turn t) { SendMessage(t.ToMessage()); }
/// <summary> /// Verstuur naar de andere instantie dat er een winnaar gevonden is. /// </summary> /// <param name="winner">Wie er gewonnen heeft, in normale situatie verstuur je hier Player.Me</param> /// <param name="t">Winnende zet</param> /// <seealso cref="WinDetectedMessage"/> /// <seealso cref="Player"/> /// <seealso cref="SendMessage"/> public void SendWinDetected(Player winner, Turn t) { WinDetectedMessage m = new WinDetectedMessage(); m.Type = MessageType.WinDetected; m.Winner = winner; m.Turn = t; SendMessage(m); }
/// <summary> /// Verstuurt naar de andere instantie dat de ontvangen zet ongeldig is. /// </summary> /// <param name="t">De turn die als ongeldig gezien wordt</param> /// <seealso cref="Message"/> /// <seealso cref="SendMessage"/> public void SendMoveInvalid(Turn t) { Message m = new Message(); m.Turn = t; m.Type = MessageType.SentMoveInvalid; SendMessage(m); }
void Communication_TurnReceived(Turn t) { if (CurrentPlayer == Players.Max) { //Not their turn _communication.SendDisconnect(DisconnectReason.InvalidMove); return; } if(!HandleTheirMove(t)){ return; } _turn++; //Handled their move, moving to ours now if (OnBoardUpdated != null) OnBoardUpdated(); //System.Threading.Thread.Sleep(1000); CurrentPlayer = Players.Max; MoveWrapper bm = GetMove(); Turn turn = _conversion.ConvertMoveToTurn(bm); ExecuteMove(bm); _turn++; if (Game.HasWon(Players.Max)) { Debug.WriteLine("We won, sending message to opponent."); _communication.SendWinDetected(Player.Me, turn); } else { Debug.WriteLine("Move made, sending move to opponent."); _communication.SendTurn(turn); } Debug.WriteLine("Move sent to opponent: " + _conversion.MoveWrapperToString(bm)); Debug.WriteLine("Converted move to turn: " + _conversion.TurnToString(turn)); if (OnBoardUpdated != null) OnBoardUpdated(); CurrentPlayer = Players.Min; }
void Communication_SentMoveInvalid(Turn t) { Debug.WriteLine("Opponent says our move is wrong."); }
bool HandleTheirMove(Turn t) { Debug.WriteLine("Opponent took a turn"); if (t == null) { Console.WriteLine("Turn is null, sending back"); _communication.SendMoveInvalid(t); return false; } Debug.WriteLine("Received turn: " + _conversion.TurnToString(t) + " - " + (t.EmptyTile == null)); MoveWrapper received = _conversion.ConvertTurnToMove(t); Debug.WriteLine("Converted turn to move: " + _conversion.MoveWrapperToString(received)); // Get the move with the correct source tile from the last click. Debug.WriteLine("Current player: " + CurrentPlayer); if (!IsMoveLegal(received, CurrentPlayer)) { Console.WriteLine("Move is illegal, sending back"); _communication.SendMoveInvalid(t); return false; } ExecuteMove(received); return true; }
void Communication_WinDetected(Turn t, Player p) { if (p == Player.Me) { Debug.WriteLine("Opponent thinks he won, going to check."); if(!HandleTheirMove(t)){ return; } if (Game.HasWon(Players.Min)) { Debug.WriteLine("Opponent did win, we lose."); _communication.SendWinAccepted(); } else { Debug.WriteLine("Opponent thinks he won but didnt according to us."); _communication.SendWinDisputed(); } } else if (p == Player.You) { Debug.WriteLine("Opponent thinks I won, going to check."); if (Game.HasWon(Players.Max)) { Debug.WriteLine("We actually won without noticing?"); _communication.SendWinAccepted(); } else { Debug.WriteLine("We didnt win according to our calculations."); _communication.SendWinDisputed(); } } }