Last active
March 21, 2018 18:59
-
-
Save jattoabdul/5a723195714e32cc99f35d4c940ab7ba to your computer and use it in GitHub Desktop.
Android app that randomly awards score to paired opponents based on score point and comments out the winner after 20 plays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.goalchallenge; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
// This is an example of an implementation of inheritance | |
public class MainActivity extends AppCompatActivity { | |
//Variables to hold global data that will be accessed by methods | |
int scorePoint[] = {4, 2, 1}; | |
String team[] = {"A", "B"}; | |
int teamScoreA = 0; | |
int teamScoreB = 0; | |
int playCount = 0; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
/* This method randomly selects a team to award score and stops execution at full time*/ | |
public void play(View view) { | |
String result = ""; | |
if (playCount == 21) { | |
//Notify player that the challenge is over | |
Toast.makeText(this, "Game over!", Toast.LENGTH_SHORT).show(); | |
//Statement to display winner on the challenge at full time | |
if (teamScoreA > teamScoreB) { | |
result = "Peeeeee!!!\nChelsea won the goal challenge"; | |
displayResult(result); | |
return; | |
} else if (teamScoreA == teamScoreB) { | |
result = "Peeeeee!!!\nThe goal challenge was a tie"; | |
displayResult(result); | |
return; | |
} else { | |
result = "Peeeeee!!!\nLiverpool won the goal challenge"; | |
displayResult(result); | |
return; | |
} | |
} | |
playCount += 1; | |
int goalType = scorePoint[(int) Math.floor(Math.random() * 3)]; | |
String teamSupply = team[(int) Math.floor(Math.random() * 2)]; | |
//statement to identify the type of goal scored and provides the appropriate commentary | |
switch (teamSupply) { | |
case "A": | |
teamScoreA += scorePoint[(int) Math.floor(Math.random() * 3)]; | |
displayScoreA(teamScoreA); | |
switch (goalType) { | |
case 4: | |
Toast.makeText(this, "Chelsea score from a free kick", Toast.LENGTH_SHORT).show(); | |
break; | |
case 2: | |
Toast.makeText(this, "Chelsea score In play", Toast.LENGTH_SHORT).show(); | |
break; | |
case 1: | |
Toast.makeText(this, "Chelsea score from a penalty", Toast.LENGTH_SHORT).show(); | |
break; | |
} | |
break; | |
case "B": | |
teamScoreB += scorePoint[(int) Math.floor(Math.random() * 3)]; | |
displayScoreB(teamScoreB); | |
switch (goalType) { | |
case 4: | |
Toast.makeText(this, "Liverpool score from a free kick", Toast.LENGTH_SHORT).show(); | |
break; | |
case 2: | |
Toast.makeText(this, "Liverpool score In play", Toast.LENGTH_SHORT).show(); | |
break; | |
case 1: | |
Toast.makeText(this, "Liverpool score from a penalty", Toast.LENGTH_SHORT).show(); | |
break; | |
} | |
break; | |
} | |
} | |
/* This method resets all team scores and full time result */ | |
public void restart(View view) { | |
teamScoreB = 0; | |
teamScoreA = 0; | |
playCount = 0; | |
String result = ""; | |
displayScoreB(teamScoreB); | |
displayScoreA(teamScoreA); | |
displayResult(result); | |
} | |
/* This method displays the current score on screen for teamA */ | |
public void displayScore(int num) { | |
TextView score_text_view = (TextView) findViewById(R.id.score_boardA); | |
score_text_view.setText(String.valueOf(num)); | |
} | |
/* This method displays the current score on screen for any other Team passed as second param (TeamB). | |
This is an example of method overloading */ | |
public void displayScore(int num, String team_board) { | |
TextView score_text_view = (TextView) findViewById(team_board); | |
score_text_view.setText(String.valueOf(num)); | |
} | |
/* This method displays the full time result on screen */ | |
public void displayResult(String message) { | |
TextView score_text_view = (TextView) findViewById(R.id.result_text_view); | |
score_text_view.setText(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment