Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hw2;
- public class TennisMatch {
- //private String ballFromName;
- //private String score;
- //private String ballTo; // who the ball is going to
- //private String gameScore;
- //private String playerName;
- //private String serverName;
- //private String p1Score;
- //private String p2Score;
- //private String winnerName;
- //private String serverPlayer;
- //private String receiver;
- //private String name;
- //private String setScore;
- private boolean ballInPlay;
- private boolean ballServed;
- private boolean bestOfThree;
- private boolean gameOver;
- private boolean faultStatus;
- private boolean deuce;
- //private int p1End;
- //private int p2End;
- private int fault;
- private int ballDirection;
- private int server;
- private int serverEnd;
- private TennisPlayer P1, P2;
- private TennisPlayer playerServer;
- private TennisPlayer playerReceiver;
- private TennisPlayer playerBallTo;
- private TennisPlayer playerBallFrom;
- public TennisMatch(String p1Name, String p2Name, boolean playBestOfThree, int initialServer, int initialServerEnd) {
- if (initialServer == 1 && initialServerEnd == 1) {
- P1 = new TennisPlayer(p1Name, 1);
- P2 = new TennisPlayer(p2Name, 2);
- } else if (initialServer == 1 && initialServerEnd == 2) {
- P1 = new TennisPlayer(p1Name, 2);
- P2 = new TennisPlayer(p2Name, 1);
- } else if (initialServer == 2 && initialServerEnd == 1) {
- P1 = new TennisPlayer(p1Name, 2);
- P2 = new TennisPlayer(p2Name, 1);
- } else {
- P1 = new TennisPlayer(p1Name, 1);
- P2 = new TennisPlayer(p2Name, 2);
- }
- if (playBestOfThree) {
- this.bestOfThree = true;
- } else {
- this.bestOfThree = false;
- }
- server = initialServer;
- if (server == 1) {
- playerServer = P1;
- playerReceiver = P2;
- playerBallTo = P1;
- playerBallFrom = P2;
- } else {
- playerServer = P2;
- playerReceiver = P1;
- playerBallTo = P2;
- playerBallFrom = P1;
- }
- }
- public void changeEnds() {
- int p1End = P1.getEnd();
- int p2End = P2.getEnd();
- P2.setEnd(p1End);
- P1.setEnd(p2End);
- }
- public void changeServer() {// the server changes to other teams server
- if (server == 1) {
- server = 2;
- playerServer = P2;
- playerReceiver = P1;
- playerBallTo = P1;
- playerBallFrom = P2;
- } else {
- server = 1;
- playerServer = P1;
- playerReceiver = P2;
- playerBallTo = P2;
- playerBallFrom = P1;
- }
- }
- public void setGameScore(int p1Game, int p2Game) {
- P1.setGamePoints(p1Game);
- P2.setGamePoints(p2Game);
- }
- public void fault() {
- if (ballServed) {
- return;
- }
- faultStatus = true;
- fault++;
- if (fault == 2) {
- if (playerBallFrom.getName() == P2.getName()) {
- P1.incrementGamePoints();
- } else {
- P2.incrementGamePoints();
- }
- }
- }
- public String getBallFrom() {
- return playerBallFrom.getName();
- }
- public boolean getBallInPlay() {
- return ballInPlay;
- }
- public boolean getBallServed() {
- return ballServed;
- }
- public String getBallTo() {
- return playerBallTo.getName();
- }
- public boolean getBestOfThree() {
- return bestOfThree;
- }
- public boolean getGameOver() {
- return gameOver;
- }
- public String getGameScore() {
- return P1.getGamePoints() + "-" + P2.getGamePoints();
- }
- public String getMatchScore() {
- return P1.getMatchPoints() + "-" + P2.getMatchPoints();
- }
- public String getName(int player) {
- if (player == 1) {
- return P1.getName();
- } else {
- return P2.getName();
- }
- }
- public int getP1End() {
- return P1.getEnd();
- }
- public int getP2End() {
- return P2.getEnd();
- }
- public String getReceiver() {
- return playerReceiver.getName();
- }
- public String getScore() {
- return P1.getGamePoints() + "-" + P2.getGamePoints();
- }
- public boolean getServefault() {
- return faultStatus;
- }
- public String getServer() {
- if (server == 1) {
- return P1.getName();
- } else {
- return P2.getName();
- }
- }
- public String getSetScore() {
- return Integer.toString(P1.getGamePoints()) + Integer.toString(P2.getGamePoints());
- }
- public String getWinner() {
- if (P1.getMatchPoints() > P2.getMatchPoints()) {
- return P1.getName();
- } else {
- return P2.getName();
- }
- }
- public void incrementGamePoints(TennisPlayer addTo, TennisPlayer noAdd) {
- ballDirection = 1;
- if (addTo == P1) {
- faultStatus = false;
- ballInPlay = false;
- P1.incrementGamePoints();
- if ((P1.getGamePoints() >= 4 && deuce == false)
- || (P1.getGamePoints() >= 4 && P1.getGamePoints() >= (P2.getGamePoints() + 2))) {
- P1.setGamePoints(0);
- P2.setGamePoints(0);
- deuce = false;
- }
- }
- if (addTo == P2) {
- faultStatus = false;
- ballInPlay = false;
- P2.incrementGamePoints();
- if ((P2.getGamePoints() >= 4 && deuce == false)
- || (P2.getGamePoints() >= 4 && P2.getGamePoints() >= (P1.getGamePoints() + 2))) {
- P1.setGamePoints(0);
- P2.setGamePoints(0);
- deuce = false;
- }
- }
- }
- public void incrementMatchPoints(TennisPlayer addTo, TennisPlayer noAdd) {
- if (addTo == P1) {
- P1.incrementGamePoints();
- }
- if (addTo == P2) {
- P2.incrementGamePoints();
- }
- }
- public void incrementSetPoints(TennisPlayer addTo, TennisPlayer noAdd) {
- changeServer();
- if (addTo == P1) {
- P1.incrementGamePoints();
- if (P1.getGamePoints() >= 6) {
- P1.setGamePoints(0);
- P2.setGamePoints(0);
- incrementMatchPoints(P1, P2);
- }
- }
- if (addTo == P2) {
- P2.incrementGamePoints();
- if (P2.getGamePoints() >= 6) {
- P1.setGamePoints(0);
- P2.setGamePoints(0);
- deuce = false;
- }
- }
- }
- public void let() {
- ballInPlay = false;
- }
- public void returnBall() {
- if (!ballInPlay) {
- return;
- }
- TennisPlayer ballFrom = playerBallFrom;
- TennisPlayer ballTo = playerBallTo;
- this.playerBallTo = ballFrom;
- this.playerBallFrom = ballTo;
- if (ballDirection == 1) {
- ballDirection = 0;
- ballServed = false;
- } else {
- ballDirection = 1;
- ballServed = false;
- }
- }
- public void serve() {
- if (gameOver) {
- ballServed = false;
- } else {
- ballServed = true;
- }
- }
- public void failedReturn() {
- ballInPlay = false;
- TennisPlayer ballFrom = playerBallFrom;
- TennisPlayer ballTo = playerBallTo;
- this.playerBallTo = ballFrom;
- this.playerBallFrom = ballTo;
- if (ballDirection == 1) {
- P2.incrementGamePoints();
- } else {
- P1.incrementGamePoints();
- }
- }
- public void setScore(int p1Game, int p2Game, int p1Set, int p2Set, int p1Match, int p2Match) {
- P1.setGamePoints(p1Game);
- P2.setGamePoints(p2Game);
- P1.setSetPoints(p1Set);
- P2.setSetPoints(p2Set);
- P1.setMatchPoints(p1Match);
- P2.setMatchPoints(p2Match);
- }
- public void setServe(int player) {
- server = player; // setting that player to serve
- if (player == 1) {
- }
- }
- public void setServerEnd(int end) {
- serverEnd = end;
- }
- public void setSetScore(int p1Set, int p2Set) {
- P1.setSetPoints(p1Set);
- P2.setSetPoints(p2Set);
- }
- public void setMatchScore(int p1Match, int p2Match) {
- P1.setMatchPoints(p1Match);
- P2.setMatchPoints(p2Match);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement