Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts

    Battleship type script

    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.

  • #2
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts

    Maybe

    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.)
    Last edited by stevenmw; 07-06-2007 at 10:25 AM.

  • #3
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    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

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,529
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    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.

  • #5
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    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.

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,529
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    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!

    .
    Last edited by mlseim; 07-06-2007 at 08:44 PM.

  • #7
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    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?

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,529
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •