/** I apologise for this messy code! It's because I'm a complete beginner, but I'd really appreciate any advice in improving it/cleaning it up! The majority has been gathered from various internet search results, and so I'm making this available for any other learners wanting to play around with the code/app - Remember to replace occurrances of "ball" with the name of your video file - The background video wouldn't run for me on the emulator, so try running directly to your android device - Place your video in a "raw" folder that sits within the "res" folder. */ package com.example.android.tabletennisscore; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import android.widget.VideoView; public class MainActivity extends AppCompatActivity { int scorePlayerA = 0; int scorePlayerB = 0; int setsPlayerA = 0; int setsPlayerB = 0; int gamesPlayerA = 0; String winner = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final VideoView videoview = findViewById(R.id.videoView); Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.ball); videoview.setVideoURI(uri); videoview.start(); videoview.setOnCompletionListener ( new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { videoview.start(); } }); displayForPlayerA(0); displayForPlayerB(0); } /** ~~~~ to add: Once game total reaches 5 compare this to other player and if it's more than 2 points above oponent * then delcare the winner. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * ~~~~~~Once game reaches end then reset everything automatically * * declares loser also if player is less than oponent?::::::::::::::::::::::::::: * if (setsPlayerA < setsPlayerB) { winner = "Pants"; displayForPlayerAResult(winner); }~~~~~~~~~~~~~~~~~~~~~~~~ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /** * Increase the Points score for Player A by 1 point. */ public void addPointForPlayerA(View v) { scorePlayerA = scorePlayerA + 1; displayForPlayerA(scorePlayerA); if (scorePlayerA < 11) { displayForPlayersResults(""); displayForPlayerASets(setsPlayerA); } if (scorePlayerA == 11) { winner = "Player A wins Set!"; setsPlayerA = setsPlayerA + 1; displayForPlayersResults(winner); displayForPlayerASets(setsPlayerA); scorePlayerA = 0; } if (setsPlayerA == 5) { /**gamesPlayerA = gamesPlayerA + 1; displayForPlayerAGames(gamesPlayerA);*/ winner = "Player A wins match!"; displayForPlayersResults(winner); /**displayForPlayerA(0);*/ setsPlayerA = 0; } } /** * Displays the current points for Player A. */ public void displayForPlayerA(int score) { TextView scoreView = (TextView) findViewById(R.id.player_a_score); scoreView.setText(String.valueOf(score)); } public void displayForPlayersResults(String i) { TextView r = (TextView) findViewById(R.id.playersResults); r.setText("" + i); } /** * Displays the sets for Player A. */ public void displayForPlayerASets(int i) { TextView r = (TextView) findViewById(R.id.player_a_sets); r.setText("" + i + " of 5"); } /** * Displays the games for Player A. public void displayForPlayerAGames(int i) { TextView r = (TextView) findViewById(R.id.player_a_games); r.setText("" + i); } */ /** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /** * Increase the score for Player B by 1 point. */ public void addPointForPlayerB(View v) { scorePlayerB = scorePlayerB + 1; displayForPlayerB(scorePlayerB); if (scorePlayerB < 11) { displayForPlayersResults(""); displayForPlayerBSets(setsPlayerB); } if (scorePlayerB == 11) { winner = "Player B Wins Set!"; setsPlayerB = setsPlayerB + 1; displayForPlayersResults(winner); displayForPlayerBSets(setsPlayerB); scorePlayerB = 0; } if (setsPlayerB == 5) { /**gamesPlayerB = gamesPlayerB + 1; displayForPlayerBGames(gamesPlayerB);*/ winner = "Player B wins match!"; displayForPlayersResults(winner); /**displayForPlayerB(0);*/ setsPlayerB = 0; } } /** * Displays the current points for Player A. */ public void displayForPlayerB(int score) { TextView scoreView = (TextView) findViewById(R.id.player_b_score); scoreView.setText(String.valueOf(score)); } /** * Displays the result for Player A. //wins */ /**public void displayForPlayerBResult(String i) { TextView r = (TextView) findViewById(R.id.player_b_result); r.setText("" + i); } * Displays the sets for Player A. */ public void displayForPlayerBSets(int i) { TextView r = (TextView) findViewById(R.id.player_b_sets); r.setText("" + i + " of 5"); } /** * Displays the games for Player A. public void displayForPlayerBGames(int i) { TextView r = (TextView) findViewById(R.id.player_b_games); r.setText("" + i); }*/ /** * Reset the score for Players B and A */ public void Reset(View v) { scorePlayerB = 0; scorePlayerA = 0; winner = ""; setsPlayerA = 0; setsPlayerB = 0; displayForPlayerB(scorePlayerB); displayForPlayerA(scorePlayerA); displayForPlayersResults(winner); displayForPlayerASets(setsPlayerA); displayForPlayerBSets(setsPlayerB); } }