protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); int roll = 0; var editTextPlayer1 = FindViewById <EditText>(Resource.Id.editTextPlayer1); var editTextPlayer2 = FindViewById <EditText>(Resource.Id.editTextPlayer2); var textViewPlayersTurn = FindViewById <TextView>(Resource.Id.textViewPlayersTurn); if (savedInstanceState == null) //if null, initialization { // Create the quote collection and load quotes game = new PigLogic(); game.Player1Name = editTextPlayer1.Text; game.Player2Name = editTextPlayer2.Text; textViewPlayersTurn.Text = game.Player1Name + "'s Turn"; } else //I tried to restore data using xml serilization but it doesn't work currently { // Deserialized the saved object state string xmlGame = savedInstanceState.GetString("PigGame"); XmlSerializer x = new XmlSerializer(typeof(PigLogic)); game = (PigLogic)x.Deserialize(new StringReader(xmlGame)); } //When the names of players are changed editTextPlayer1.AfterTextChanged += delegate { game.Player1Name = editTextPlayer1.Text; if (game.Turn == 1) { textViewPlayersTurn.Text = game.Player1Name + "'s Turn"; } }; editTextPlayer2.AfterTextChanged += delegate { game.Player2Name = editTextPlayer2.Text; if (game.Turn == 2) { textViewPlayersTurn.Text = game.Player2Name + "'s Turn"; } }; var buttonRollDie = FindViewById <Button>(Resource.Id.buttonRollDie); var textViewPoint4ThisTurn = FindViewById <TextView>(Resource.Id.textViewPoint4ThisTurn); var imageViewDie = FindViewById <ImageView>(Resource.Id.imageViewDie); var textViewPlayer1Score = FindViewById <TextView>(Resource.Id.textViewPlayer1Score); var textViewPlayer2Score = FindViewById <TextView>(Resource.Id.textViewPlayer2Score); DisplayScores(textViewPlayer1Score, textViewPlayer2Score, textViewPlayersTurn); DisplayTurnScore(roll, textViewPoint4ThisTurn, imageViewDie); buttonRollDie.Click += delegate { roll = game.RollDie(); DisplayTurnScore(roll, textViewPoint4ThisTurn, imageViewDie); //when the roll is a bad number, it changes turn with giving 0 score to current player. if (roll == PigLogic.BAD_NUMBER) { game.ChangeTurn(); DisplayScores(textViewPlayer1Score, textViewPlayer2Score, textViewPlayersTurn); // Check whether points are same or over the point's limit // only after both player's playing same amount of rolling if (game.Turn == 1) { if (game.CheckForWinner() != "") { CheckWinner(textViewPlayersTurn); ResetGame(textViewPlayer1Score, textViewPlayer2Score); } } } }; var buttonEndTurn = FindViewById <Button>(Resource.Id.buttonEndTurn); buttonEndTurn.Click += delegate { game.ChangeTurn(); DisplayScores(textViewPlayer1Score, textViewPlayer2Score, textViewPlayersTurn); DisplayTurnScore(roll, textViewPoint4ThisTurn, imageViewDie); // Check whether points are same or over the point's limit // only after both player's playing same amount of rolling if (game.Turn == 1) { if (game.CheckForWinner() != "") { CheckWinner(textViewPlayersTurn); ResetGame(textViewPlayer1Score, textViewPlayer2Score); } } }; var buttonNewGame = FindViewById <Button>(Resource.Id.buttonNewGame); buttonNewGame.Click += delegate { ResetGame(textViewPlayer1Score, textViewPlayer2Score); }; }
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(); }; }
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Use this to return your custom view for this Fragment // return inflater.Inflate(Resource.Layout.YourFragment, container, false); View view = inflater.Inflate(Resource.Layout.Fragment2, container, false); int roll = 0; var textViewPlayer1 = view.FindViewById <TextView>(Resource.Id.textPlayer1Actual); var textViewPlayer2 = view.FindViewById <TextView>(Resource.Id.textPlayer2Actual); var textViewPlayersTurn = view.FindViewById <TextView>(Resource.Id.textViewPlayersTurn); if (savedInstanceState == null) { game = new PigLogic(); SetPlayersName("player1", "player2"); } //SetPlayersName(); /*if (Arguments != null) * { * game.Player1Name = Arguments.GetString("player1name"); * game.Player2Name = Arguments.GetString("player2name"); * }*/ textViewPlayersTurn.Text = game.Player1Name + "'s Turn"; textViewPlayer1.Text = game.Player1Name; textViewPlayer2.Text = game.Player2Name; var buttonRollDie = view.FindViewById <Button>(Resource.Id.buttonRollDie); var textViewPoint4ThisTurn = view.FindViewById <TextView>(Resource.Id.textViewPoint4ThisTurn); var imageViewDie = view.FindViewById <ImageView>(Resource.Id.imageViewDie); var textViewPlayer1Score = view.FindViewById <TextView>(Resource.Id.textViewPlayer1Score); var textViewPlayer2Score = view.FindViewById <TextView>(Resource.Id.textViewPlayer2Score); DisplayScores(textViewPlayer1Score, textViewPlayer2Score, textViewPlayersTurn); DisplayTurnScore(roll, textViewPoint4ThisTurn, imageViewDie); buttonRollDie.Click += delegate { roll = game.RollDie(); DisplayTurnScore(roll, textViewPoint4ThisTurn, imageViewDie); //when the roll is a bad number, it changes turn with giving 0 score to current player. if (roll == PigLogic.BAD_NUMBER) { game.ChangeTurn(); DisplayScores(textViewPlayer1Score, textViewPlayer2Score, textViewPlayersTurn); Android.Widget.Toast.MakeText(Activity, game.GetCurrentPlayer() + " rolled 0! Turn changed", Android.Widget.ToastLength.Short).Show(); // Check whether points are same or over the point's limit // only after both player's playing same amount of rolling if (game.Turn == 1) { if (game.CheckForWinner() != "") { CheckWinner(); ResetGame(); } } } }; var buttonEndTurn = view.FindViewById <Button>(Resource.Id.buttonEndTurn); buttonEndTurn.Click += delegate { game.ChangeTurn(); DisplayScores(textViewPlayer1Score, textViewPlayer2Score, textViewPlayersTurn); DisplayTurnScore(roll, textViewPoint4ThisTurn, imageViewDie); // Check whether points are same or over the point's limit // only after both player's playing same amount of rolling if (game.Turn == 1) { if (game.CheckForWinner() != "") { CheckWinner(); ResetGame(); } } }; var buttonNewGame = view.FindViewById <Button>(Resource.Id.buttonNewGame); buttonNewGame.Click += delegate { ResetGame(); }; return(view); //return base.OnCreateView(inflater, container, savedInstanceState); }