Exemplo n.º 1
0
    private void HandleServerMessage(CheckersMessage message)
    {
        if (message.ProtocolVersion != 1)
        {
            // don't know how to handle other versions!
            return;
        }
        switch (message.MessageType)
        {
        case MessageType.Join:
            // set player identity
            Player = message.Side;
            break;

        case MessageType.Move:
            // server says a move was done, so represent that in the game
            ApplyMove(new Move(message));
            break;

        case MessageType.PlayerConnected:
            if (message.Side != Player)
            {
                Debugging.Print("opponent connected.");
                OpponentConnected = true;
            }
            break;

        default:
            break;
        }
    }
Exemplo n.º 2
0
        protected virtual unsafe List <Rectangle> _findImageAlpha(DirectBitmap small, Rectangle area, List <SearchRange> ranges, int margin, cancelToken token, ImageSearchingOptions opts)
        {
            opts = opts ?? ImageSearchingOptions.Default;
            var @out       = new List <Rectangle>(0);
            int jump_small = small.PixelSize;
            int jump_large = this.PixelSize;

            byte *scan_large = (byte *)this._scan0;  //[largeX+ smallX, largeY+ smallY];
            byte *scan_small = (byte *)small._scan0; //[smallX, smallY];
            int   c, smallX = 0, smallY = 0, pix = 0;
            byte *s, l;

            //Debugging.Print($"{Thread.CurrentThread.ManagedThreadId} has started");
            for (int largeY = area.Y; largeY < area.Y + area.Height; largeY++)
            {
                if (token.cancel) //here so it wont impact performance so bad
                {
                    return(@out);
                }
                for (int largeX = area.X; largeX < area.X + area.Width; largeX++)
                {
                    foreach (var range in ranges)
                    {
                        for (smallY = range.StartY; smallY <= range.StopY; smallY++)
                        {
                            for (smallX = range.StartX; smallX <= range.StopX; smallX++)
                            {
                                l = scan_large + (((largeX + smallX) + (largeY + smallY) * this.Width) * jump_large);
                                s = scan_small + (smallX + smallY * small.Width) * jump_small;
                                for (pix = 0; pix < 3; pix++)
                                {
                                    c = l[pix] - s[pix]; //c==margin
                                    if (c > margin || c < -margin)
                                    {
                                        goto nextLoop;
                                    }
                                }
                            }
                        }
                    }


                    //If all the pixels match up, then return true and change Point location to the top left co-ordinates where it was found
                    @out.Add(new Rectangle(largeX, largeY, small.Width, small.Height));
                    Debugging.Print($"{Thread.CurrentThread.ManagedThreadId} has found: {new Rectangle(largeX, largeY, small.Width, small.Height)}");
                    if (opts.NumberOfResults != -1 && @out.Count >= opts.NumberOfResults)
                    {
                        return(@out);
                    }
                    //Go to next pixel on large image
nextLoop:
                    ;
                }
            }
            //Return false if image is not found, and set an empty point
            /*            location = Point.Empty;*/
            //Debugging.Print($"{Thread.CurrentThread.ManagedThreadId} has finished");
            return(@out);
        }
Exemplo n.º 3
0
    public void OnMouseDown()
    {
        // send message for move from selected checker to this location
        GameBoard.Move theMove = new GameBoard.Move()
        {
            Owner         = Board.Player,
            StartLocation = Board.SelectedChecker.GetComponent <Checker>().Location,
            EndLocation   = Location
        };

        Board.ServerConnection.SendMessage(theMove.ToMessage());
        Debugging.Print("Should have just sent a message: " + theMove.ToMessage());
        Board.DeselectPiece();
    }
Exemplo n.º 4
0
        public static void PrintTable(string tableName)
        {
            SqliteCommand sqlCommand = new SqliteCommand("select * from " + tableName, dbConnection);

            SqliteDataReader dataReader = sqlCommand.ExecuteReader();

            Debugging.Print("Table: " + tableName);
            while (dataReader.Read())
            {
                string row = "";
                for (int i = 0; i < dataReader.FieldCount; i++)
                {
                    row += dataReader.GetName(i) + ": " + dataReader.GetString(i) + " | ";
                }
                Debugging.Print(row);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Initializes the database, loading the engine associated to <paramref name="connectorType"/>.
        /// </summary>
        /// <parameters>
        /// <param name="connectorType">The engine backing the database.</param>
        /// <param name="connectionOption">Options related to connection to a server.</param>
        /// <param name="isDebugTraceEnabled">True to turn traces on.</param>
        /// </parameters>
        public virtual void Initialize(ConnectorType connectorType, ConnectionOption connectionOption, bool isDebugTraceEnabled)
        {
            Debug.Assert(Connector == null);
            if (Connector != null)
            {
                return;
            }

            ConnectorType    = connectorType;
            ConnectionOption = connectionOption;

            try
            {
                switch (ConnectorType)
                {
                case ConnectorType.MySql:
                    Connector = new MySqlConnector();
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(connectorType));
                }

                IsDebugTraceEnabled = isDebugTraceEnabled;
                if (IsDebugTraceEnabled)
                {
                    Debugging.Print("Connector created: " + Connector.ToString());
                }
            }
#if TRACE
            catch (ApplicationException)
            {
                throw;
            }
#endif
            catch (Exception e)
            {
                if (IsDebugTraceEnabled)
                {
                    Debugging.PrintExceptionMessage(e.Message);
                }
            }
        }
Exemplo n.º 6
0
        // A partial move is either a full move that can be taken by a checker, or a jump among
        // jumps in a chain of jumps, but that is only a single step.
        public bool IsValidPartialMove(Move move)
        {
            if (forceSamePiece != null)               // if player must use same piece as last time because they jumped, enforce this
            {
                if (Board [move.StartLocation] != forceSamePiece)
                {
                    return(false);
                }
            }

            // there should be a checker where the move starts.
            if (!Board.ContainsKey(move.StartLocation))
            {
                Debugging.Print("Invalid move: board doesn't have " + move.StartLocation.ToString());
                return(false);
            }
            Checker checker = Board [move.StartLocation];

            // if there's already a piece where the move ends, the move is invalid.
            if (Board.ContainsKey(move.EndLocation))
            {
                Debugging.Print("Invalid move: landing on another piece.");
                return(false);
            }

            // if starts or ends somewhere nonsensical, move is invalid
            if (!isValidBoardPosition(move.StartLocation) || !isValidBoardPosition(move.EndLocation))
            {
                Debugging.Print("Invalid Move: doesn't correspond to a valid board position.");
                return(false);
            }

            bool isJump = (Math.Abs(move.EndLocation.Row - move.StartLocation.Row) > 1);

            if (isJump)
            {
                BoardLocation capturedLocation = JumpsOver(move);
                if ((object)capturedLocation == null)
                {
                    // jumps must have a captured location that is on the board.
                    return(false);
                }

                bool capturesPiece = Board.ContainsKey(capturedLocation);
                if (!capturesPiece)
                {
                    // captured location must have a piece if it's a jump move
                    return(false);
                }
                else if (Board [capturedLocation].Player ==
                         Board [move.StartLocation].Player)
                {
                    // captured location must be of the opposite color
                    return(false);
                }
            }
            else if (forceSamePiece != null)
            {
                return(false);
            }

            if (checker.IsCrowned)
            {
                // crowned pieces can move anywhere
                bool result = (move.EndLocation.Equals(GetDownLeft(move.StartLocation, isJump)) ||
                               move.EndLocation.Equals(GetDownRight(move.StartLocation, isJump)) ||
                               move.EndLocation.Equals(GetUpLeft(move.StartLocation, isJump)) ||
                               move.EndLocation.Equals(GetUpRight(move.StartLocation, isJump)));
                return(result);
            }
            else if (checker.Player == Side.Red)
            {
                // red uncrowned pieces can only move down
                bool result = (move.EndLocation.Equals(GetDownLeft(move.StartLocation, isJump)) ||
                               move.EndLocation.Equals(GetDownRight(move.StartLocation, isJump)));
                return(result);
            }
            else
            {
                // white uncrowned pieces
                bool result = (move.EndLocation.Equals(GetUpLeft(move.StartLocation, isJump)) ||
                               move.EndLocation.Equals(GetUpRight(move.StartLocation, isJump)));
                return(result);
            }
        }
Exemplo n.º 7
0
 public void SendMessage(CheckersMessage message)
 {
     Debugging.Print("Sending message " + message.ToString());
     ToSend.Enqueue(message);
 }
Exemplo n.º 8
0
    // assume this is only called if the given move is valid (correct player's turn, there's a piece there, etc.)
    public void ApplyMove(Move move)
    {
        bool jump       = false;
        bool turnChange = true;

        // remove any captured piece
        BoardLocation capturedLocation = JumpsOver(move);

        if (capturedLocation != null)
        {
            jump = true;
            GameObject capturedPiece = Board [capturedLocation];
            Board.Remove(capturedLocation);
            Destroy(capturedPiece);
        }
        Board [move.EndLocation] = Board[move.StartLocation];
        Board.Remove(move.StartLocation);
        Board[move.EndLocation].GetComponent <Checker>().Relocate(move.EndLocation);

        // crowning happens first because a continued jump might be in the opposite direction
        if (CrowningPosition(Board[move.EndLocation].GetComponent <Checker>().Player, move.EndLocation))
        {
            Debugging.Print("trying to crown the piece at " + move.EndLocation.ToString() + " for player " +
                            Board[move.EndLocation].GetComponent <Checker>().Player.ToString());
            Board [move.EndLocation].GetComponent <Checker> ().BecomeCrowned();
        }



        if (jump)
        {
            // if another jump follows the jump, don't end the turn. otherwise end the turn.
            List <Move> nextJumpMoves = GetValidPartialMoves(move.EndLocation);
            if (nextJumpMoves.Count > 0)
            {
                Debugging.Print("next jump moves are:");
                foreach (Move nextJumpMove in nextJumpMoves)
                {
                    Debugging.Print(nextJumpMove.ToString());
                }
                turnChange = false;                 // turn only doesn't change if a piece jumped and still has jumps
                if (CurrentTurn == Player)
                {
                    forceSamePiece = Board [move.EndLocation];                     // same player must use this piece next
                    SelectPiece(forceSamePiece);
                }
            }
        }

        if (turnChange)
        {
            forceSamePiece = null;
            DeselectPiece();
            Debugging.Print("changing turns");
            if (CurrentTurn == Side.Red)
            {
                CurrentTurn = Side.White;
            }
            else
            {
                CurrentTurn = Side.Red;
            }
        }
    }