Results 1 to 2 of 2
Thread: Small tennis simulation in Java
- 12-02-2007, 02:00 AM #1Member
- Join Date
- Dec 2007
- Rep Power
Small tennis simulation in Java
Hello, I am a new programmer and I have Several questions about how to implement object oriented features in my tennis simulation being written in Java. Index of /apliques/2007/2/ProjetoTenis/src
First I will tell how my simulation works so that you all can understand better what I need to do, how to do it and especially how to do it in an object oriented way (can have some generic programming too)
This is how the program works
I am doing a little tennis program in which I implement the simulation of a game of a tennis match based on random number generation and probabilities. For example, it chooses a number between 1 to 2, and if the result is 1, the player hits the first serve. If not, then the second one.
Then, when he hits any of the two serves, it sorts a number between 1 to 10. In the first serve case, if the number is anything between 1 to 8, the server wins the point. In the second serve case, the server wins the point if the value choosed is anything between 1 to 5.
I have three big classes, one that has the main method and creates an object that represents a tennis match.
This object calls for a method of the class "Game", the class responsible for 'managing' a game from a tennis match.
For now the only thing this "Game" class is doing, is holding the method responsable for controlling the serves, that as was said in the above paragrapher, are the responsables for deciding who is going to win a point. The method "saque" (serve in english) chooses a serve and then send it to a certain method of the "Pontos" (points) class to select who winned the point.This class contains three methods, each one for one of the three possibilities: first serve in, second serve in and double fault. When the winner of the point is choosed, I would like it to return the two variables that represent the points that each player will have after the point is finished,
back to method "saque" (serve) of the Game class.
This brings me to the first problem: methods cannot return two values. So, can I create an object composed of two fields and return it? If I can, can you guys show how to do it?. If not, what you suggest?. I need to return them because I intend to create in the "Game" class, another method, one that will control the game, or in another words, finish the program when a player collects enough points to win the game.
Then in the "game" class, I would like to send these two values, whether they will be in an object or in another form to the "control" method that I intend to create in the same class. This brings to the second question which is: when these points will be returned, should I create this "control method" in the same class and send the values to it, or should I do something completely different to allow bigger modularization?.
And the last question comes to the surface due to the fact that the point count changes during the game. The way it is now. at each new point 15points will be given to the variable that represents the score of one of the players.
How should I change this value according to occasion? Should I create an object of the type "Point" to do this?
Thanks for all the suggestions and recomendations.
- 12-02-2007, 02:32 AM #2Member
- Join Date
- Dec 2007
- Rep Power
In your problem of a method only returning one value, I would suggest using an array. This way you could have as many values you needed stored in that one array.
I'm not sure about your second question.
0-15-30-40-50 (That's how I remember tennis scoring going)...
I would make a variable, say addTotal, which initially equals 15. Then a condition directly after the line of code where you increment the score like so:
if (totalScore == 15) addTotal -= 5; //which makes it 10
Hope some of that helped...
Last edited by Leprechaun; 12-02-2007 at 02:34 AM. Reason: Changin tag type
- By Saurabh321 in forum New To JavaReplies: 1Last Post: 02-01-2008, 04:38 PM
- By Greenfrog99 in forum AWT / SwingReplies: 0Last Post: 01-27-2008, 09:46 PM
- By atanu.dey in forum New To JavaReplies: 1Last Post: 01-05-2008, 08:48 PM