コード例 #1
0
        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;
        }
コード例 #2
0
        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));
            }
        }
コード例 #3
0
 public String TurnToString(Turn t)
 {
     return String.Format("TURN: Type: {0}, FDU: {1},{2},{3}", t.MoveType, t.FromTile, t.Direction, t.EmptyTile);
 }
コード例 #4
0
ファイル: ICommunication.cs プロジェクト: Bakkes/Karo
 /// <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);
     }
 }
コード例 #5
0
        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;
        }
コード例 #6
0
ファイル: ICommunication.cs プロジェクト: Bakkes/Karo
 /// <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);
     }
 }
コード例 #7
0
ファイル: ICommunication.cs プロジェクト: Bakkes/Karo
 /// <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);
     }
 }
コード例 #8
0
ファイル: ICommunication.cs プロジェクト: Bakkes/Karo
 //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());
 }
コード例 #9
0
ファイル: ICommunication.cs プロジェクト: Bakkes/Karo
 /// <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);
 }
コード例 #10
0
ファイル: ICommunication.cs プロジェクト: Bakkes/Karo
 /// <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);
 }
コード例 #11
0
        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;
        }
コード例 #12
0
 void Communication_SentMoveInvalid(Turn t)
 {
     Debug.WriteLine("Opponent says our move is wrong.");
 }
コード例 #13
0
        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;
        }
コード例 #14
0
 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();
         }
     }
 }