Advertisement
PatPeter

Big Daddy#9514 HW2

Oct 11th, 2019
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.78 KB | None | 0 0
  1. package hw2;
  2.  
  3. public class TennisMatch {
  4. //private String ballFromName;
  5. //private String score;
  6. //private String ballTo; // who the ball is going to
  7. //private String gameScore;
  8. //private String playerName;
  9. //private String serverName;
  10. //private String p1Score;
  11. //private String p2Score;
  12. //private String winnerName;
  13. //private String serverPlayer;
  14. //private String receiver;
  15. //private String name;
  16. //private String setScore;
  17.  
  18.  
  19. private boolean ballInPlay;
  20. private boolean ballServed;
  21. private boolean bestOfThree;
  22. private boolean gameOver;
  23. private boolean faultStatus;
  24. private boolean deuce;
  25.  
  26. //private int p1End;
  27. //private int p2End;
  28. private int fault;
  29. private int ballDirection;
  30. private int server;
  31. private int serverEnd;
  32.  
  33. private TennisPlayer P1, P2;
  34. private TennisPlayer playerServer;
  35. private TennisPlayer playerReceiver;
  36. private TennisPlayer playerBallTo;
  37. private TennisPlayer playerBallFrom;
  38.  
  39. public TennisMatch(String p1Name, String p2Name, boolean playBestOfThree, int initialServer, int initialServerEnd) {
  40. if (initialServer == 1 && initialServerEnd == 1) {
  41. P1 = new TennisPlayer(p1Name, 1);
  42. P2 = new TennisPlayer(p2Name, 2);
  43. } else if (initialServer == 1 && initialServerEnd == 2) {
  44. P1 = new TennisPlayer(p1Name, 2);
  45. P2 = new TennisPlayer(p2Name, 1);
  46. } else if (initialServer == 2 && initialServerEnd == 1) {
  47. P1 = new TennisPlayer(p1Name, 2);
  48. P2 = new TennisPlayer(p2Name, 1);
  49. } else {
  50. P1 = new TennisPlayer(p1Name, 1);
  51. P2 = new TennisPlayer(p2Name, 2);
  52. }
  53. if (playBestOfThree) {
  54. this.bestOfThree = true;
  55. } else {
  56. this.bestOfThree = false;
  57. }
  58. server = initialServer;
  59. if (server == 1) {
  60. playerServer = P1;
  61. playerReceiver = P2;
  62. playerBallTo = P1;
  63. playerBallFrom = P2;
  64. } else {
  65. playerServer = P2;
  66. playerReceiver = P1;
  67. playerBallTo = P2;
  68. playerBallFrom = P1;
  69. }
  70. }
  71.  
  72. public void changeEnds() {
  73. int p1End = P1.getEnd();
  74. int p2End = P2.getEnd();
  75. P2.setEnd(p1End);
  76. P1.setEnd(p2End);
  77. }
  78.  
  79. public void changeServer() {// the server changes to other teams server
  80. if (server == 1) {
  81. server = 2;
  82. playerServer = P2;
  83. playerReceiver = P1;
  84. playerBallTo = P1;
  85. playerBallFrom = P2;
  86. } else {
  87. server = 1;
  88. playerServer = P1;
  89. playerReceiver = P2;
  90. playerBallTo = P2;
  91. playerBallFrom = P1;
  92. }
  93. }
  94.  
  95. public void setGameScore(int p1Game, int p2Game) {
  96. P1.setGamePoints(p1Game);
  97. P2.setGamePoints(p2Game);
  98. }
  99.  
  100. public void fault() {
  101. if (ballServed) {
  102. return;
  103. }
  104.  
  105. faultStatus = true;
  106. fault++;
  107. if (fault == 2) {
  108. if (playerBallFrom.getName() == P2.getName()) {
  109. P1.incrementGamePoints();
  110. } else {
  111. P2.incrementGamePoints();
  112. }
  113. }
  114. }
  115.  
  116. public String getBallFrom() {
  117. return playerBallFrom.getName();
  118. }
  119.  
  120. public boolean getBallInPlay() {
  121. return ballInPlay;
  122. }
  123.  
  124. public boolean getBallServed() {
  125. return ballServed;
  126. }
  127.  
  128. public String getBallTo() {
  129. return playerBallTo.getName();
  130. }
  131.  
  132. public boolean getBestOfThree() {
  133. return bestOfThree;
  134. }
  135.  
  136. public boolean getGameOver() {
  137. return gameOver;
  138. }
  139.  
  140. public String getGameScore() {
  141. return P1.getGamePoints() + "-" + P2.getGamePoints();
  142. }
  143.  
  144. public String getMatchScore() {
  145. return P1.getMatchPoints() + "-" + P2.getMatchPoints();
  146. }
  147.  
  148. public String getName(int player) {
  149. if (player == 1) {
  150. return P1.getName();
  151. } else {
  152. return P2.getName();
  153. }
  154. }
  155.  
  156. public int getP1End() {
  157. return P1.getEnd();
  158. }
  159.  
  160. public int getP2End() {
  161. return P2.getEnd();
  162. }
  163.  
  164. public String getReceiver() {
  165. return playerReceiver.getName();
  166. }
  167.  
  168. public String getScore() {
  169. return P1.getGamePoints() + "-" + P2.getGamePoints();
  170. }
  171.  
  172. public boolean getServefault() {
  173. return faultStatus;
  174. }
  175.  
  176. public String getServer() {
  177. if (server == 1) {
  178. return P1.getName();
  179. } else {
  180. return P2.getName();
  181. }
  182. }
  183.  
  184. public String getSetScore() {
  185. return Integer.toString(P1.getGamePoints()) + Integer.toString(P2.getGamePoints());
  186. }
  187.  
  188. public String getWinner() {
  189. if (P1.getMatchPoints() > P2.getMatchPoints()) {
  190. return P1.getName();
  191. } else {
  192. return P2.getName();
  193. }
  194. }
  195.  
  196. public void incrementGamePoints(TennisPlayer addTo, TennisPlayer noAdd) {
  197. ballDirection = 1;
  198. if (addTo == P1) {
  199. faultStatus = false;
  200. ballInPlay = false;
  201. P1.incrementGamePoints();
  202. if ((P1.getGamePoints() >= 4 && deuce == false)
  203. || (P1.getGamePoints() >= 4 && P1.getGamePoints() >= (P2.getGamePoints() + 2))) {
  204. P1.setGamePoints(0);
  205. P2.setGamePoints(0);
  206. deuce = false;
  207. }
  208. }
  209. if (addTo == P2) {
  210. faultStatus = false;
  211. ballInPlay = false;
  212. P2.incrementGamePoints();
  213. if ((P2.getGamePoints() >= 4 && deuce == false)
  214. || (P2.getGamePoints() >= 4 && P2.getGamePoints() >= (P1.getGamePoints() + 2))) {
  215. P1.setGamePoints(0);
  216. P2.setGamePoints(0);
  217. deuce = false;
  218. }
  219. }
  220. }
  221.  
  222. public void incrementMatchPoints(TennisPlayer addTo, TennisPlayer noAdd) {
  223. if (addTo == P1) {
  224. P1.incrementGamePoints();
  225. }
  226. if (addTo == P2) {
  227. P2.incrementGamePoints();
  228. }
  229. }
  230.  
  231. public void incrementSetPoints(TennisPlayer addTo, TennisPlayer noAdd) {
  232. changeServer();
  233. if (addTo == P1) {
  234. P1.incrementGamePoints();
  235. if (P1.getGamePoints() >= 6) {
  236. P1.setGamePoints(0);
  237. P2.setGamePoints(0);
  238. incrementMatchPoints(P1, P2);
  239. }
  240. }
  241. if (addTo == P2) {
  242. P2.incrementGamePoints();
  243. if (P2.getGamePoints() >= 6) {
  244. P1.setGamePoints(0);
  245. P2.setGamePoints(0);
  246. deuce = false;
  247. }
  248. }
  249. }
  250.  
  251. public void let() {
  252. ballInPlay = false;
  253. }
  254.  
  255. public void returnBall() {
  256. if (!ballInPlay) {
  257. return;
  258. }
  259.  
  260. TennisPlayer ballFrom = playerBallFrom;
  261. TennisPlayer ballTo = playerBallTo;
  262. this.playerBallTo = ballFrom;
  263. this.playerBallFrom = ballTo;
  264.  
  265. if (ballDirection == 1) {
  266. ballDirection = 0;
  267. ballServed = false;
  268. } else {
  269. ballDirection = 1;
  270. ballServed = false;
  271. }
  272. }
  273.  
  274. public void serve() {
  275. if (gameOver) {
  276. ballServed = false;
  277. } else {
  278. ballServed = true;
  279. }
  280. }
  281.  
  282. public void failedReturn() {
  283. ballInPlay = false;
  284.  
  285. TennisPlayer ballFrom = playerBallFrom;
  286. TennisPlayer ballTo = playerBallTo;
  287. this.playerBallTo = ballFrom;
  288. this.playerBallFrom = ballTo;
  289.  
  290. if (ballDirection == 1) {
  291. P2.incrementGamePoints();
  292. } else {
  293. P1.incrementGamePoints();
  294. }
  295.  
  296. }
  297.  
  298. public void setScore(int p1Game, int p2Game, int p1Set, int p2Set, int p1Match, int p2Match) {
  299. P1.setGamePoints(p1Game);
  300. P2.setGamePoints(p2Game);
  301. P1.setSetPoints(p1Set);
  302. P2.setSetPoints(p2Set);
  303. P1.setMatchPoints(p1Match);
  304. P2.setMatchPoints(p2Match);
  305. }
  306.  
  307. public void setServe(int player) {
  308. server = player; // setting that player to serve
  309. if (player == 1) {
  310.  
  311. }
  312. }
  313.  
  314. public void setServerEnd(int end) {
  315. serverEnd = end;
  316. }
  317.  
  318. public void setSetScore(int p1Set, int p2Set) {
  319. P1.setSetPoints(p1Set);
  320. P2.setSetPoints(p2Set);
  321. }
  322.  
  323. public void setMatchScore(int p1Match, int p2Match) {
  324. P1.setMatchPoints(p1Match);
  325. P2.setMatchPoints(p2Match);
  326. }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement