void UpdateWhosTurn() { // set turn points in UI if (game.CheckForWinner().Length > 0) { // game is over return; } FindViewById <TextView>(Resource.Id.TurnLabel).Text = game.GetCurrentPlayer() + "'s turn"; }
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); }; }
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); }