Esempio n. 1
0
        public void SetupPage()
        {
            //turn on the display of the board and models
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            //Run the detection separate from the update
            dispatcherTimer.Start();

            // Create a timer for this page
            timer.Start();

            //see if user was placed into check
            if ((GameStateManager.getInstance().getGameState().black.in_check&& GameStateManager.getInstance().getCurrentPlayer() == ChessPiece.Color.BLACK) || (GameStateManager.getInstance().getGameState().white.in_check&& GameStateManager.getInstance().getCurrentPlayer() == ChessPiece.Color.WHITE))
            {
                handleError("You have been placed into check by your opponent.");
            }

            //if the state of the game has not yet been loaded, load it now before displaying board
            if (!stateHasBeenLoaded)
            {
                GameState.getInstance().loadState(GameStateManager.getInstance().getGameState());
                stateHasBeenLoaded = true;
            }

            //Initialize the camera
            photoCamera              = new PhotoCamera();
            photoCamera.Initialized += new EventHandler <CameraOperationCompletedEventArgs>(photoCamera_Initialized);
            ViewFinderBrush.SetSource(photoCamera);

            setupFinished = true;
        }
Esempio n. 2
0
        private void waitForOpponent()
        {
            TeardownPage();
            var bw = new BackgroundWorker();

            bw.DoWork += (s, args) =>
            {
                response = new NetworkTask().getGameState();
                while (response.is_game_over == false && response.is_current_players_turn == false)
                {
                    Thread.Sleep(10000);
                    response = new NetworkTask().getGameState();
                }
            };
            bw.RunWorkerCompleted += (s, args) =>
            {
                alreadyCalledWait = false;

                if (response.is_game_over == false)
                {
                    GameStateManager.getInstance().setShouldWait(false);
                    GameStateManager.getInstance().setGameState(response.game_state);
                    stateHasBeenLoaded = false;
                    SetupPage();
                    hidePopup();
                }
                else
                {
                    string myColor = GameState.getInstance().getMyColor() == ChessPiece.Color.BLACK ?
                                     "black" : "white";

                    if (response.winner == myColor)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            NavigationService.Navigate(new Uri("/WonPage.xaml", UriKind.Relative));
                        });
                    }
                    else
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            NavigationService.Navigate(new Uri("/LostPage.xaml", UriKind.Relative));
                        });
                    }
                }
            };
            bw.RunWorkerAsync();
            showPopup("Waiting");
        }
Esempio n. 3
0
        private void sendMove()
        {
            //send the move to the central server so that it can be sent to the opponents phone
            var bw = new BackgroundWorker();

            bw.DoWork += (s, args) =>
            {
                new NetworkTask().sendGameState(GameState.getInstance().toCurrentGameState());
            };
            bw.RunWorkerCompleted += (s, args) =>
            {
                GameStateManager.getInstance().setShouldWait(true);
            };
            bw.RunWorkerAsync();
        }
Esempio n. 4
0
 // Simple button Click event handler to take us to the second page
 private void Button_Click(object sender, RoutedEventArgs e)
 {
     GameStateManager.getInstance().setGameState(response.game_state);
     if (response.game_in_progress && response.is_current_players_turn)
     {
         GameStateManager.getInstance().setCurrentPlayer(response.current_player);
         GameStateManager.getInstance().setShouldWait(false);
     }
     else if (response.game_in_progress && !response.is_current_players_turn)
     {
         GameStateManager.getInstance().setCurrentPlayer((response.current_player == "black" ? "white" : "black"));
         GameStateManager.getInstance().setShouldWait(true);
     }
     NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
 }
Esempio n. 5
0
        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            //Renders your Silverlight content
            uiRenderer.Render();
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black);

            //Draw your ui renderer onto a texture feed texture
            spriteBatch.Begin();
            spriteBatch.Draw(uiRenderer.Texture, Vector2.Zero, Microsoft.Xna.Framework.Color.White);
            spriteBatch.End();

            //Draw game
            try
            {
                GameState.getInstance().Draw();

                bool doesAPawnNeedPromotion = false;

                string color         = GameStateManager.getInstance().getCurrentPlayer().ToString().ToLower();
                bool   white_and_end = color == "white" && GameState.getInstance().getSelectedPiece() != null && GameState.getInstance().getSelectedPiece().getMasqueradeType() == "pawn" && GameState.getInstance().getSelectedPiece().getPosition().X == 7;
                bool   black_and_end = color == "black" && GameState.getInstance().getSelectedPiece() != null && GameState.getInstance().getSelectedPiece().getMasqueradeType() == "pawn" && GameState.getInstance().getSelectedPiece().getPosition().X == 0;

                doesAPawnNeedPromotion = (white_and_end || black_and_end);

                //check to see if pawn promotion is needed
                if (doesAPawnNeedPromotion && !alreadyCalledPromote)
                {
                    TeardownPage();
                    showPopup("Pawn Promote");
                    alreadyCalledPromote = true;
                }
            }
            catch (Exception ex)
            {
                handleError(ex.Message);
            }

            //we have made our move, now wait for the opponent to make theirs
            if (GameStateManager.getInstance().getShouldWait() && setupFinished && !alreadyCalledWait)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    waitForOpponent();
                });
                alreadyCalledWait = true;
            }
        }
Esempio n. 6
0
        void _client_RecognizeSpeechCompleted(object sender, RecognizeSpeechCompletedEventArgs e)
        {
            hidePopup();
            bool good = false;

            try
            {
                //attempt to process voice recognition
                VoiceCommandFuzzyProcessing.process(e.Result);

                //determine if there is a pawn that needs promotion
                bool doesAPawnNeedPromotion = false;

                string color         = GameStateManager.getInstance().getCurrentPlayer().ToString().ToLower();
                bool   white_and_end = color == "white" && GameState.getInstance().getSelectedPiece() != null && GameState.getInstance().getSelectedPiece().getMasqueradeType() == "pawn" && GameState.getInstance().getChosenPosition().X == 7;
                bool   black_and_end = color == "black" && GameState.getInstance().getSelectedPiece() != null && GameState.getInstance().getSelectedPiece().getMasqueradeType() == "pawn" && GameState.getInstance().getChosenPosition().X == 0;

                doesAPawnNeedPromotion = (white_and_end || black_and_end);

                //check to see if pawn promotion is needed
                if (doesAPawnNeedPromotion && !alreadyCalledPromote)
                {
                    showPopup("Pawn Promote");
                    alreadyCalledPromote = true;
                }
                else
                {
                    SetupPage();
                }
                good = true;
            }
            catch (Exception ex)
            {
                handleError(ex.Message);
            }
            finally
            {
                if (!good)
                {
                    SetupPage();
                }
            }
        }
Esempio n. 7
0
        public void loadState(CurrentGameState state)
        {
            if (state != null)
            {
                mCurrentState = state;

                chessPieces["black_pawn1"].setPosition(new Vector2((float)state.black.pawn1.x, (float)state.black.pawn1.y));
                chessPieces["black_pawn2"].setPosition(new Vector2((float)state.black.pawn2.x, (float)state.black.pawn2.y));
                chessPieces["black_pawn3"].setPosition(new Vector2((float)state.black.pawn3.x, (float)state.black.pawn3.y));
                chessPieces["black_pawn4"].setPosition(new Vector2((float)state.black.pawn4.x, (float)state.black.pawn4.y));
                chessPieces["black_pawn5"].setPosition(new Vector2((float)state.black.pawn5.x, (float)state.black.pawn5.y));
                chessPieces["black_pawn6"].setPosition(new Vector2((float)state.black.pawn6.x, (float)state.black.pawn6.y));
                chessPieces["black_pawn7"].setPosition(new Vector2((float)state.black.pawn7.x, (float)state.black.pawn7.y));
                chessPieces["black_pawn8"].setPosition(new Vector2((float)state.black.pawn8.x, (float)state.black.pawn8.y));
                chessPieces["black_rook1"].setPosition(new Vector2((float)state.black.rook1.x, (float)state.black.rook1.y));
                chessPieces["black_rook2"].setPosition(new Vector2((float)state.black.rook2.x, (float)state.black.rook2.y));
                chessPieces["black_bishop1"].setPosition(new Vector2((float)state.black.bishop1.x, (float)state.black.bishop1.y));
                chessPieces["black_bishop2"].setPosition(new Vector2((float)state.black.bishop2.x, (float)state.black.bishop2.y));
                chessPieces["black_knight1"].setPosition(new Vector2((float)state.black.knight1.x, (float)state.black.knight1.y));
                chessPieces["black_knight2"].setPosition(new Vector2((float)state.black.knight2.x, (float)state.black.knight2.y));
                chessPieces["black_queen"].setPosition(new Vector2((float)state.black.queen.x, (float)state.black.queen.y));
                chessPieces["black_king"].setPosition(new Vector2((float)state.black.king.x, (float)state.black.king.y));

                chessPieces["black_pawn1"].setMasqueradesAs(state.black.pawn1.masquerading_as);
                chessPieces["black_pawn2"].setMasqueradesAs(state.black.pawn2.masquerading_as);
                chessPieces["black_pawn3"].setMasqueradesAs(state.black.pawn3.masquerading_as);
                chessPieces["black_pawn4"].setMasqueradesAs(state.black.pawn4.masquerading_as);
                chessPieces["black_pawn5"].setMasqueradesAs(state.black.pawn5.masquerading_as);
                chessPieces["black_pawn6"].setMasqueradesAs(state.black.pawn6.masquerading_as);
                chessPieces["black_pawn7"].setMasqueradesAs(state.black.pawn7.masquerading_as);
                chessPieces["black_pawn8"].setMasqueradesAs(state.black.pawn8.masquerading_as);
                chessPieces["black_rook1"].setMasqueradesAs(state.black.rook1.masquerading_as);
                chessPieces["black_rook2"].setMasqueradesAs(state.black.rook2.masquerading_as);
                chessPieces["black_bishop1"].setMasqueradesAs(state.black.bishop1.masquerading_as);
                chessPieces["black_bishop2"].setMasqueradesAs(state.black.bishop2.masquerading_as);
                chessPieces["black_knight1"].setMasqueradesAs(state.black.knight1.masquerading_as);
                chessPieces["black_knight2"].setMasqueradesAs(state.black.knight2.masquerading_as);
                chessPieces["black_queen"].setMasqueradesAs(state.black.queen.masquerading_as);
                chessPieces["black_king"].setMasqueradesAs(state.black.king.masquerading_as);

                chessPieces["white_pawn1"].setPosition(new Vector2((float)state.white.pawn1.x, (float)state.white.pawn1.y));
                chessPieces["white_pawn2"].setPosition(new Vector2((float)state.white.pawn2.x, (float)state.white.pawn2.y));
                chessPieces["white_pawn2"].setPosition(new Vector2((float)state.white.pawn2.x, (float)state.white.pawn2.y));
                chessPieces["white_pawn3"].setPosition(new Vector2((float)state.white.pawn3.x, (float)state.white.pawn3.y));
                chessPieces["white_pawn4"].setPosition(new Vector2((float)state.white.pawn4.x, (float)state.white.pawn4.y));
                chessPieces["white_pawn5"].setPosition(new Vector2((float)state.white.pawn5.x, (float)state.white.pawn5.y));
                chessPieces["white_pawn6"].setPosition(new Vector2((float)state.white.pawn6.x, (float)state.white.pawn6.y));
                chessPieces["white_pawn7"].setPosition(new Vector2((float)state.white.pawn7.x, (float)state.white.pawn7.y));
                chessPieces["white_pawn8"].setPosition(new Vector2((float)state.white.pawn8.x, (float)state.white.pawn8.y));
                chessPieces["white_rook1"].setPosition(new Vector2((float)state.white.rook1.x, (float)state.white.rook1.y));
                chessPieces["white_rook2"].setPosition(new Vector2((float)state.white.rook2.x, (float)state.white.rook2.y));
                chessPieces["white_bishop1"].setPosition(new Vector2((float)state.white.bishop1.x, (float)state.white.bishop1.y));
                chessPieces["white_bishop2"].setPosition(new Vector2((float)state.white.bishop2.x, (float)state.white.bishop2.y));
                chessPieces["white_knight1"].setPosition(new Vector2((float)state.white.knight1.x, (float)state.white.knight1.y));
                chessPieces["white_knight2"].setPosition(new Vector2((float)state.white.knight2.x, (float)state.white.knight2.y));
                chessPieces["white_queen"].setPosition(new Vector2((float)state.white.queen.x, (float)state.white.queen.y));
                chessPieces["white_king"].setPosition(new Vector2((float)state.white.king.x, (float)state.white.king.y));

                chessPieces["white_pawn1"].setMasqueradesAs(state.white.pawn1.masquerading_as);
                chessPieces["white_pawn2"].setMasqueradesAs(state.white.pawn2.masquerading_as);
                chessPieces["white_pawn3"].setMasqueradesAs(state.white.pawn3.masquerading_as);
                chessPieces["white_pawn4"].setMasqueradesAs(state.white.pawn4.masquerading_as);
                chessPieces["white_pawn5"].setMasqueradesAs(state.white.pawn5.masquerading_as);
                chessPieces["white_pawn6"].setMasqueradesAs(state.white.pawn6.masquerading_as);
                chessPieces["white_pawn7"].setMasqueradesAs(state.white.pawn7.masquerading_as);
                chessPieces["white_pawn8"].setMasqueradesAs(state.white.pawn8.masquerading_as);
                chessPieces["white_rook1"].setMasqueradesAs(state.white.rook1.masquerading_as);
                chessPieces["white_rook2"].setMasqueradesAs(state.white.rook2.masquerading_as);
                chessPieces["white_bishop1"].setMasqueradesAs(state.white.bishop1.masquerading_as);
                chessPieces["white_bishop2"].setMasqueradesAs(state.white.bishop2.masquerading_as);
                chessPieces["white_knight1"].setMasqueradesAs(state.white.knight1.masquerading_as);
                chessPieces["white_knight2"].setMasqueradesAs(state.white.knight2.masquerading_as);
                chessPieces["white_queen"].setMasqueradesAs(state.white.queen.masquerading_as);
                chessPieces["white_king"].setMasqueradesAs(state.white.king.masquerading_as);

                mMyColor       = GameStateManager.getInstance().getCurrentPlayer();
                mSelectedPiece = null;
                mMoveMade      = false;
                setPieceMoves();
            }
        }
Esempio n. 8
0
        private static Vector2 findClosestPiece(Vector2 chosenLocation, ChessPiece.Piece chosenPiece)
        {
            Dictionary <string, ChessPiece> chessPieces = GameState.getInstance().getPieces();
            string  color           = GameStateManager.getInstance().getCurrentPlayer().ToString().ToLower() + "_";
            Vector2 closestLocation = new Vector2();

            for (int i = 1; i <= 8; ++i)
            {
                if (chessPieces[color + "pawn" + i].getMasqueradeType() == chosenPiece.ToString().ToLower())
                {
                    if (Vector2.Distance(chosenLocation, chessPieces[color + "pawn" + i].getPosition()) < Vector2.Distance(chosenLocation, closestLocation))
                    {
                        closestLocation = chessPieces[color + "pawn" + i].getPosition();
                    }
                }
            }
            for (int i = 1; i <= 2; ++i)
            {
                if (chessPieces[color + "rook" + i].getMasqueradeType() == chosenPiece.ToString().ToLower())
                {
                    if (Vector2.Distance(chosenLocation, chessPieces[color + "rook" + i].getPosition()) < Vector2.Distance(chosenLocation, closestLocation))
                    {
                        closestLocation = chessPieces[color + "rook" + i].getPosition();
                    }
                }
            }
            for (int i = 1; i <= 2; ++i)
            {
                if (chessPieces[color + "knight" + i].getMasqueradeType() == chosenPiece.ToString().ToLower())
                {
                    if (Vector2.Distance(chosenLocation, chessPieces[color + "knight" + i].getPosition()) < Vector2.Distance(chosenLocation, closestLocation))
                    {
                        closestLocation = chessPieces[color + "knight" + i].getPosition();
                    }
                }
            }
            if (chessPieces[color + "king"].getMasqueradeType() == chosenPiece.ToString().ToLower())
            {
                if (Vector2.Distance(chosenLocation, chessPieces[color + "king"].getPosition()) < Vector2.Distance(chosenLocation, closestLocation))
                {
                    closestLocation = chessPieces[color + "king"].getPosition();
                }
            }
            if (chessPieces[color + "queen"].getMasqueradeType() == chosenPiece.ToString().ToLower())
            {
                if (Vector2.Distance(chosenLocation, chessPieces[color + "queen"].getPosition()) < Vector2.Distance(chosenLocation, closestLocation))
                {
                    closestLocation = chessPieces[color + "queen"].getPosition();
                }
            }
            for (int i = 1; i <= 2; ++i)
            {
                if (chessPieces[color + "bishop" + i].getMasqueradeType() == chosenPiece.ToString().ToLower())
                {
                    if (Vector2.Distance(chosenLocation, chessPieces[color + "bishop" + i].getPosition()) < Vector2.Distance(chosenLocation, closestLocation))
                    {
                        closestLocation = chessPieces[color + "bishop" + i].getPosition();
                    }
                }
            }

            return(closestLocation);
        }