![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#41 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
Re: back to online battleship
cwl
I will be back to explain in detail how to do these things tomorrow.. however I'm studying for an exam (that is tomorrow lol) right now so I don't have time. In the meantime check out this resource, it will explain some of your concerns. http://java.sun.com/docs/books/tutor...ect/index.html Btw in order for the players to 'know about' what each other did, player1 will take his turn and update the Action Request object. Then he will send the object over to player2 using the ObjectOutputStream. This is done by out.writeObject(yourObjecthere) and Object requestObject = in.readObject(); The link I provided above will explain why it's possible for one Player to know what the other player is sending him. Also note that readObject() will just sit there until it reads in an object. So its possible for player1 to wait for player2's response. |
|
|
|
|
|
#42 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 346
Rep Power: 4
![]() |
Re: back to online battleship
thank you i will have a look at that and see what i can get from it. Thanks and good luck on your exam.
|
|
|
|
|
|
#43 | ||
|
Professional Programmer
Join Date: Feb 2005
Posts: 346
Rep Power: 4
![]() |
Re: back to online battleship
Fall back son,
so I have not had a chance to look at that link as thoroughly as i would like to yet but I have changed my player class so now the constructor makes the board, and sets the players ships. So now to create a player with a name and a board full of ships you just create a new player. Now you mention an action request object that changes state when a player moves. So, i had a method inside the Player class that would take the turn for the player. I assume this method would be replaced by the action request object so to take 1 turn it is a whole different class and then using the stuff in that link you sent me it would tell the server or other client or whoever what happened. I guess im confused on how i would use that reflection stuff to send moves and other messages from one client across a server to another client. And then based off of this: Quote:
Quote:
Also, now that I am working full time again my time to work on this is pretty much limited to weekends but i really want to know about this reflection stuff and get it working. Last edited by cwl157; May 21st, 2008 at 1:18 AM. |
||
|
|
|
|
|
#44 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
Re: back to online battleship
You only need one class to represent the board, sorry if I made it sound different.
Reflection: Take for example where you said Object myObj = in.readObject(). Reflection is a mechanism in Java that allows you to determine things about that object (myObj). These things include: The class the object is from, the methods the object has, etc. AND reflection allows you to not only know these things, but to make use of them - you can also call the object's methods using reflection. When do you need this? You need this in cases where you aren't sure exactly what type of object you will have at runtime. Say, for example, I am writing a program that has to do calculus. I have a class called Derivable and I have another class called Integrable. I have a Server which has to read in objects that are either of those two things. Then it has to evaluate them on an interval. How is the Server supposed to know whether it just read in an Integral or a Derivative? Reflection is how it can do this. Now, I don't think you really have to use reflection for this project, since every time the Server receives an object, it will be an ActionRequest object. Instead, you can just make sure that both the "Action Request" class and the "Board" class is available to each player. Then, when each player receives an Action Request, it will be like was said above... Object myObj = in.readObject() and you can just cast the object that you read in. ActionRequest newRequest = (ActionRequest)myObj; then you can call whatever methods are necessary, pass it back to the other player, and repeat. |
|
|
|
|
|
#45 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 346
Rep Power: 4
![]() |
Re: back to online battleship
so would i still be using the server / client model or would it all be one program that has players and then the ActionRequest object is how the players pass moves and other data back and forth to each other? If thats the case how does this work over a network?
|
|
|
|
|
|
#46 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
Re: back to online battleship
You will still need the client/server model. The client and server will be passing the ActionRequests back and forth, and will be calling whatever methods are necessary to update the boards. Basically the client and server shouldn't itself contain the code necessary for running the program, they should just create whatever objects are necessary to run the program and call whatever methods are necessary. So client would create one player's board and server would create the other players board.
|
|
|
|
|
|
#47 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 346
Rep Power: 4
![]() |
Re: back to online battleship
o so your saying one of the players is the server and the other player is the client and then they pass the info back and forth to each other to play battleship, so then how does the actionRequest object fit in? So i would still have 2 programs, 1 client and 1 server and then they both initialize a name and board, so now player 1 is the server with a name and board with ships placed on the board and player 2 is a client with a name and board with ships placed on the board. Then player 1 (server) sends a move to player 2 (client). The move would be something to do with the actionRequest object? Then player 2 (client) sends a move back to player 1 (server) and this is something with the actionRequest object as well. Then the players would be checking when the game is over and do some sort of update to the actionRequest object for that as well. So does that sound like how the game is supposed to go? I'm still confused with how the actionRequestObject fits into it. Would it just have some booleans like if it hits a ship say true, or if the game is over say true?
|
|
|
|
|
|
#48 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 346
Rep Power: 4
![]() |
Re: back to online battleship
I've tried to do something with it and don't know what to do. I made an ActionRequest object it looks like this java Syntax (Toggle Plain Text)
It just returns true if the player is attempting a move, is that what you had in mind for it to do? Also, i have a takeTurn method that takes a row and column along with the board and all the ships and sees what is hit where, this info will have to be passed to the other player as well though and this method takes all the ships and board as parameters so do i just make those global variables to my Player class because right now i have them being created in the constructor of the Player class. Here is my Player class java Syntax (Toggle Plain Text)
|