-
March 16th, 2012, 01:46 PM
#1
Batteships Game
im trying to make a game of battleships and im currently stuck trying to get a way to be able to take a shot.
so far there is a table on the database and it will contain all 0's i will then query the table and fill a table on the page if there is a 0 it will put a blank image. then when you click the button for the location of the shot you wish to take i want it to update the table value to a 1 to indicate that that grid location has been used so then when i repopulate the table it will put a different image.
im using ajax to refresh the page every few seconds this is the code from the div that gets refreshed. the code works fine right now but im stuck on what to do next ive tried giving each button a unique id but that means ill need to have 100 functions for the sql. whats the best way to go about this problem so i can take a shot and it will change a value in the table thats stored on the database
PHP Code:
$conn=mysql_connect($host,$username,$password);
@mysql_select_db($db_name) or die ("<b>Unable to select database - named: ".$db_name);
$query="SELECT * FROM Grid"; $result=mysql_query($query); $num = 10; $i=0; echo "<table border=5>";
while ($i <$num)
{ echo "<tr>"; for( $col = 1; $col <=10; $col+=1) { $query = mysql_result($result, $i, $col);
if($query > 0) { if($query ==1) { echo '<td><button onclick="SubmitShot();"><img src ="miss.JPG"/></button></td>'; } else echo '<td><button onclick="SubmitShot();"><img src ="hit.JPG"/></button></td>'; } else echo '<td><button onclick="SubmitShot();"><img src ="white.JPG"/></button></td>';
$counter++; } echo "</tr>"; $i++; }
echo "</table>";
?>
Last edited by PeejAvery; March 17th, 2012 at 08:52 PM.
Reason: Added PHP tags
-
March 17th, 2012, 02:55 AM
#2
Re: Batteships Game
Please use CODE or PHP tags when posting code.
Anyways, are you sure you want to Update and refresh the DB so many times?? Sounds crazy IMHO. I'd have made use of a different way, for example a text file or something which stores this info, then read and write from and to it.
-
March 17th, 2012, 10:51 AM
#3
Re: Batteships Game
no it has to use a database because it needs to be a 2 player game played over the internet. ive no idea how im gonna do it but i need to get over this problem before i can move onto the next 1
-
March 17th, 2012, 04:31 PM
#4
Re: Batteships Game
Have you ever played, say Quake Live? If you think about it, during gameplay, it doesn't transfer a lot of data over the connection - otherwise, it would not be playable. It doesn't excange models, textures, and stuff like that. It only sends small packets of data, enough for both sides to stay in sinc. Each side has it's own set of resources. Both sides have to download the same map. If something less significant is missing (say, a texture), it can be replaced by a proxy, and the game would go on. And both sides run the same application. All each side needs to know is by how much a player moved, in what direction a shot was fired, what weapon a player currently carries... That's just a bunch of numbers, and maybe strings.
There's no "central database". The two sides are simply kept synchronized by being constantly updated by small sets of incremental data.
So, each side is, basically, running its own game, in a sense.
With battleships, the data you need to send to the other side is minimal. You just wait for your turn, make your move, send some coordinates across, and that's it.
On the other side, you wait for the data to arrive, use it to simulate gameplay (as if another person was really playing), and let the app logic on that side do the necessary updates.
The point is, the same gameplay logic resides on both sides, so by doing same things, you get the same results on both ends.
-
March 18th, 2012, 01:06 PM
#5
Re: Batteships Game
To add to that...
In this case, a central database is perfectly fine. However, your structure is completely off. Battleship is a game of coordinates. So make your columns reflect that. You also have 2 players, so you need to distinguish. For maximum performance, you're going to want an index. So your table is basically going to look like the following.
- id - primary key for performance and indexing
- game - id for storing multiple games
- player - differentiate each player's move
- coords - coordinates called by the players
- hit - mark with 1 if it's hit, 0 if it's not
Now, don't create a whole set of queries until after each player moves. You don't need a pre-populated database.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 19th, 2012, 08:11 AM
#6
Re: Batteships Game
my plan was at first to do what ye say i was using the onclick feature of each button which had a unique id using the for loop counter so when you click it it would send the location of the button clicked and would then update the button with either the hit or miss image. i was using bools for this so if hit was true this would happen so it wasnt using the database to constantly check but that led to me having a function for each button and 100s of bools which wasnt making sense at all because thats 100 functions just to deal with the clicking of the buttons and then loads more to update and all that so it seemed wrong.
-
March 19th, 2012, 02:19 PM
#7
Re: Batteships Game
Originally Posted by Dano1066
because thats 100 functions just to deal with the clicking of the buttons and then loads more to update and all that so it seemed wrong.
Then you'd definitely be doing it wrong. What I've described would be a simple 1 query to check and 1 query to update. There would be 1 if/then statement, perhaps 2. You are definitely over thinking this one.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 20th, 2012, 10:44 AM
#8
Re: Batteships Game
well how to i do it so that i only need the 1 function? there are 100 buttons how do i make them all submit something different without needing code for each 1?
-
March 20th, 2012, 01:27 PM
#9
Re: Batteships Game
Give each button an ID (the coordinates).
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
April 3rd, 2012, 12:51 PM
#10
Re: Batteships Game
I would just have the game build an array at the beginning, then reference key-value pairs throughout the game. The only time you have to write to the database is for recording permanent changes, such as player registration or a high score.
So, for a 10x10 board, it would be:
Code:
Key A B C D E F G H I J
1
2
3
4
5
6
7
8
9
10
With a function that generates a second array set in session variables for each player with their ship positions, i.e.
A1, B1 for a smaller craft.
By comparing a value passed via GET (which would be a page refresh) you can check the array for a match and register a hit.
Basically, it's like having a contact form and a thank you page, but they are the same code, just with a refresh option (via Javascript) that would get the current hit and pass the next hit. When you submit, it takes you to the second page where you wait for a response, then it lets you submit and it takes you back to the first page, repeating until the game is done.
At least, that's how I think it could be done, without writing to a db way too much.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|