View Full Version : Battleship type script

07-06-2007, 02:40 AM
Has anyone seen or know of a php game script that is like battleship? I would like something like that to code into a mini-game on my website.

07-06-2007, 09:33 AM
I wouldn't think it wouldn be very difficult to write a script like that. You could use the $_POST function to get where someone wants to put a boat from an input field. For example say I input A 10 into an input field.. Then one fo my boats would be put into that field. And just make as many input fields as you require. Then write a randomize script and put several different possible boat positions. Then call out how ever many boat positions you need at a time. Then make some more input fields for the game player to input his guesses, and give them as many tries as they need. then use the randomize script to pull out random boat positions again. Then use if else statements to see if either the human or computer's guesses match the predefined boat positions. You could have a variable that has a function to count how many hits (guesses that matched the boats positions) each player had. Then use an if else statement, and have it say if computer $total < human echo computer lost else echo computer won..
You can always have the human player put where they want their boats using input fields. then display one input field letting them make guesses each time getting back a message of whether it was a hit or miss... You could alternate turns. Letting the human, then the computer make a guess untill either players ships are all destroyed. I know I didn't explain it with any examples, or alot of detail. If you want some help I'll be glad to answer any questions or write up some examples for you.. (But thinking about this idea... It just sounds like it would be a fun script to code.)

07-06-2007, 05:01 PM
Hmm that makes sense. So in the battleship concept, if it's being hardcoded. Then for a Town War, it could be setup battleship style with the the places to put it, and you place the Town and Soldiers just 2 pieces down and went both are destoryed you lose. How hard would it be to have a graphic of what your doing instead of text only? Thanks

07-06-2007, 05:16 PM
For graphics, a defined area using CSS (width, height).
That <div> would have a background image of your town.

Then using absolute positioning, you put graphics over the
background. Your array of positions would be like a grid.

You would of course need PHP to dynamically position the
images on the background with each page refresh.

07-06-2007, 06:38 PM
Excellent, I understand. Now just have to find someone to code this. Thanks to you two I can explain what exactly is needed to be done.

07-06-2007, 08:39 PM
I would start with the graphics first.

1) Design the town grid as a large .gif or .png image.
Most people now have larger monitors, so you'll have to
decide what size to make it 800X600 or 1024 wide ....

2) Design the things that will be placed on the background.
Again, use .gif or .png because you can specify transparent colors.

3) Find a friend that knows CSS and PHP really good.
Someone who will do it for free.

The way I would approach the logic?

A person goes online and generates a new game.
This creates a random number that is saved both
as a cookie and in a file as an ID. The person then
can put stuff on the grid (using AJAX?). They save
their town ... gets saved on the server with their ID
in the filename. It's an array of the location of pieces
on the town grid.

Whenever two people want to play against each other,
a temporary directory is created that stores the moves
between players (their ID's) and keeps track of whose
turn it is, and which pieces are "hit". That directory is
named as a session number that also gets stored as
a cookie on the user's PC.

Players can leave and come back later if the directory
stays in place ... otherwise, maybe after 24 hours, the
temporary directory is erased ... session is over.

A PHP script handles the back-and-forth between players and
checks each player's ID file to determine hits or misses.

The player's own ID files never get altered, so a player could use the
same town over again with another player. You could even have a
whole series of "pre-made" layouts.

Only the temporary directory gets altered, and is erased after the
game is complete.


The BEST way would actually be to use PHP/MySQL, but you'll need to
find someone that knows SQL ... a bit more advanced, but the same idea.

EDIT: and don't forget a shout-box so you can send snide remarks back and forth!


07-07-2007, 12:06 AM
I have a total of 16 towns on my website already and the database has Owner for them. So there can be a leader of each town (the ones who can play the game).

So I guess it'd be a 90 second timeout if one person doesn't make a move and either loses a turn or the game ends and the other person wins?

07-07-2007, 03:44 AM
I don't know much about gaming myself ...

I guess some Javascripting or AJAX involved would
handle the timing ... pretty much like a chat room thing.