Exemplo n.º 1
0
        // function ResetGame
        // reset the game and score display
        public void ResetGame()
        {
            game.ResetGame();

            TextView textViewPlayer1Score = Activity.FindViewById <TextView>(Resource.Id.textViewPlayer1Score);
            TextView textViewPlayer2Score = Activity.FindViewById <TextView>(Resource.Id.textViewPlayer2Score);

            textViewPlayer1Score.Text = textViewPlayer2Score.Text = "0";
        }
        void SetUp(Bundle bundle)
        {
            if (bundle != null)
            {
                // load game
                // get game string
                string gameString = bundle.GetString(BUNLDE_KEY);
                // make serializer
                XmlSerializer cereal = new XmlSerializer(typeof(SaveGame));
                // make reader
                StringReader reader = new StringReader(gameString);
                // get saved game
                SaveGame savedGame = (SaveGame)cereal.Deserialize(reader);

                // remake game
                game = new PigLogic(savedGame.Player1Score, savedGame.Player2Score, savedGame.TurnScore, savedGame.WhosTurn)
                {
                    Player1Name = savedGame.Player1Name, Player2Name = savedGame.Player2Name
                };

                // update ui names
                FindViewById <EditText>(Resource.Id.Player1EditText).Text = savedGame.Player1Name;
                FindViewById <EditText>(Resource.Id.Player2EditText).Text = savedGame.Player2Name;

                // check if turn is bombed
                if (!savedGame.Rollable)
                {
                    DisableRollButton();
                }

                // check if last round was over
                WinOrLose();
            }
            else
            {
                // new game
                game = new PigLogic();
                // set player names
                game.Player1Name = FindViewById <EditText>(Resource.Id.Player1EditText).Text;
                game.Player2Name = FindViewById <EditText>(Resource.Id.Player2EditText).Text;
            }

            // set up events so new names will update the game
            FindViewById <EditText>(Resource.Id.Player1EditText).AfterTextChanged += (sender, args) => { game.Player1Name = FindViewById <EditText>(Resource.Id.Player1EditText).Text; UpdateWhosTurn(); };
            FindViewById <EditText>(Resource.Id.Player2EditText).AfterTextChanged += (sender, args) => { game.Player2Name = FindViewById <EditText>(Resource.Id.Player2EditText).Text; UpdateWhosTurn(); };

            // set up UI
            FlushUI();

            // set up buttons
            // roll button rolls and updates points for turn UI and update dice image and checks if bad number was rolled
            FindViewById <Button>(Resource.Id.RollButton).Click += (sender, args) => { UpdateDiceImage(game.RollDie()); UpdatePointsForTurn(); BombTurn(); };
            // end turn button changes player and updates who's turn UI and updates points for turn UI and updates player score UI and enables roll button and checks for winner
            // check for winner before enabling roll button
            FindViewById <Button>(Resource.Id.EndTurnButton).Click += (sender, args) => { game.ChangeTurn(); FlushUI(); if (WinOrLose())
                                                                                          {
                                                                                              return;
                                                                                          }
                                                                                          ; EnableRollButton(); };
            // new game turn button resets the game and updates who's turn UI and updates points for turn UI and enables roll button/ new game button
            FindViewById <Button>(Resource.Id.NewGameButton).Click += (sender, args) => { game.ResetGame(); FlushUI(); EnableRollButton(); EnableEndTurnButton(); };
        }
Exemplo n.º 3
0
 // function ResetGame
 // reset the game and score display
 void ResetGame(TextView textViewPlayer1Score, TextView textViewPlayer2Score)
 {
     game.ResetGame();
     textViewPlayer1Score.Text = textViewPlayer2Score.Text = "0";
 }