hackypro Posted September 21, 2008 Share Posted September 21, 2008 hey i just got assigned a project to use $_SERVER variables to basically put together a battleship type game to run in your browser. it should randomly map out spaces for the ships to occupy on a board and update the board every time a ship is hit. thats the first part of the assignment but theres a lot more. i was just wondering if you see this assignment, what commands come to mind as to how to produce this, with loops to control everything. it needs to keep track of losses and basically all information that the user would want to know while playing the game (where the past hits were, what ships have been sunk, with names based on the length of each ship on the board. i recognize these are a lot of demands for the assignment) so if you have read this far and know anything about any individual task or a good place to check out for information on this kinda thing it'd be greatly accepted. this class makes me nervous ??? ??? Link to comment https://forums.phpfreaks.com/topic/125158-solved-big-assignment-make-a-battleship-game-need-lots-of-help/ Share on other sites More sharing options...
JasonLewis Posted September 21, 2008 Share Posted September 21, 2008 You should take things one step at a time. First, why do you have to use $_SERVER variables, that doesn't make sense to me. Onto the question. Start at the start (oh how genius). You need to generate a board, you could do this with a for loop. A lot of things will end up taking place inside this loop, like placing ships and hits and misses in. So once you have got the loop constructed to generate a table with X*Y size then you need to start on the processing of the information. You'll probably want to create a session variable called something like "game_in_progress" or something similar. If the session is not set, then you can provide the user with a form where he/she can put in ship co-ordinates. When they post it, you can store these co-ordinates in another session as an array. Then in the for loop that you constructed earlier you would need to check if the current tile you are creating in the loop is the location of a ship, if it is, put the ship in. Once you get all that setup you should be able to do the rest. Just plan it out, use pen and paper if need be. Think about how it could be achieved. Good luck with the assignment. Link to comment https://forums.phpfreaks.com/topic/125158-solved-big-assignment-make-a-battleship-game-need-lots-of-help/#findComment-646904 Share on other sites More sharing options...
chronister Posted September 21, 2008 Share Posted September 21, 2008 WOW, thats a big project and will be interesting for ya. I am not sure about the $_SERVER variable part either. I cannot think of any of the $_SERVER vars that would be of great use here. I can see $_SESSION, $_GET and $_POST being used quite a bit as well as loops and arrays and probably many if statements. I find it is best to break things down into step by step pieces. Use pseudo code if you need to and map it out little by little.... e.g. <?php //use a loop to create board //define what ships are going to be used //define the dimensions of each ship //determine SESSION vars to store information //display form for user to place ships ?> This is a crude example and does not add much more if anything that ProjectFear has not already stated, but hopefully it gives you a place to start. plan plan and plan some more. I used to just jump in and start coding stuff, still do a lot of times. But when it comes to complicated things like this I try to break it down into each tiny piece that is going to need to be done to solve the whole problem and start working off of my pieces list. Hope I helped at least a little bit. Nate Link to comment https://forums.phpfreaks.com/topic/125158-solved-big-assignment-make-a-battleship-game-need-lots-of-help/#findComment-646908 Share on other sites More sharing options...
hackypro Posted September 21, 2008 Author Share Posted September 21, 2008 ah i see my n00bocity is showing, you're both correct, it wasn't $_SERVER but $_SESSION that i'm supposed to use. my mistake. and yeah i'm supposed to use $_POST as well. thanks for helping me for my first steps of breaking the project down; on assignments like this i usually end up jumping into the deep end and making lots of mistakes along the way back. i'll check back with problems i'm having in the next couple weeks. thanks again guys Link to comment https://forums.phpfreaks.com/topic/125158-solved-big-assignment-make-a-battleship-game-need-lots-of-help/#findComment-647063 Share on other sites More sharing options...
hackypro Posted September 22, 2008 Author Share Posted September 22, 2008 ok first real post with problem while coding... i'm creating a 10x10 two dimensional array that randomly generates a number 1-100 for each key. i'm doing this so that the ships can be randomly placed each time and thus the game will be different each time you access it. my problem lies in that 1. i'm not really sure how i will prevent my random number generator from generating the same number twice 2. even if i get a random number without any duplicates, i still need my ships to be horizontally/vertically random, which requires the keys with other random numbers adjacent to it to be grouped as one 'ship' so i could solve these problems and do a random number, or if you have an easier solution for random spaces on the array, i just need to set 5 ships ranging from 2-5 spaces taken each. with no overlapping and must completely on the grid. i guess this is made easier with the two dimensional array as i could just go $array_space[0][0], [0][1], [0][2] for a three space ship going vertically. but i'm not sure how this could be randomized. please help! thanks! Link to comment https://forums.phpfreaks.com/topic/125158-solved-big-assignment-make-a-battleship-game-need-lots-of-help/#findComment-647367 Share on other sites More sharing options...
hackypro Posted September 23, 2008 Author Share Posted September 23, 2008 ok new update: i've coded my whole table with radio buttons in a 2-d for-loop with a counter that can be referenced by a variable $location. i also have started with a function to determine if the location that was hit contains a ship. i again used counters here through if statements to code. function ishit($loc) { if($ship1==$loc) { if($shiphit1!=2) { $shiphit1++; return; } else return; } elseif($ship2==$loc) { if($shiphit2!=3) { $shiphit2++; return; } else return; } elseif($ship3==$loc) { if($shiphit3!=3) { $shiphit3++; return; } else return; } each statement shows how many spaces the ship takes before it goes down, and counts each time a ship is hit. the only problem with this that i see is that the ships would need to be arrays, which would mean each hit needs to iterate. furthermore i haven't even started with session variables or the random placing of the ships. i would assume i'd need to use a rand(1,100) method and set limits so the ships cannot go out of bounds (i.e. 10-11). i'm pretty sure this could be solved by arbitrarily like: enter edge in a if statement if edge(right) = true rand(1,3); where the three remaining sides are up, down and left for the ship to fill their other spaces the problem is if there's other ships, can i arbitrarily number all 100 spaces and do random numbers around them? is there an easier way to use variables like my $location above? am i going OUT OF MY MIND? i eagerly await your answers sorry i'm burnt out on schoolwork Link to comment https://forums.phpfreaks.com/topic/125158-solved-big-assignment-make-a-battleship-game-need-lots-of-help/#findComment-648993 Share on other sites More sharing options...
discomatt Posted September 23, 2008 Share Posted September 23, 2008 Here's what I'd to <pre><?php error_reporting( E_ALL ); # $ships['Ship Name'] = size (int); $ships = array( 'Carrier' => 5, 'Battleship' => 4, 'Destroyer' => 3, 'Submarine' => 3, 'Patrol Boat' => 2 ); # $grid[x-coord][y-coord] $grid = array(); # Loop through ships for placement foreach( $ships as $name => $size ) { # Begin an infinite loop ( dangerous, but we can break it when # the ship is happily placed ) while ( TRUE ) { # Determine direction of ship # x- horizontal, y- vertical $axis = ( mt_rand(0,1) == 1 ? 'x' : 'y' ); # Maximum values on grid $max = array( 'x' => 10, 'y' => 10 ); # Subtract $size from the max value to compensate for ship size $max[ $axis ] -= $size; # Generate random placement $x = mt_rand( 1, $max['x'] ); $y = mt_rand( 1, $max['y'] ); # Check to see if the grid is empty by checking $size squares in $axis direction for ( $i = 0; $i < $size; $i++ ) { # Create a temporary holder for our coordinates $curr = array( 'x' => $x, 'y' => $y ); # Add current grid position to the direction we're going $curr[ $axis ] += $i; # Check to see if the grids populated if ( isset( $grid[ $curr['x'] ][ $curr['y'] ] ) ) # If it is, start at the beginning of the while loop and find new coordinates continue 2; } # If the for loop didn't run into a collision, then we know the grid space is empty # and we can break out of the infinite loop! break; } # Now that we have a position for the ship, write it into the grid! for ( $i = 0; $i < $size; $i++ ) { # Create a temporary holder for our coordinates $curr = array( 'x' => $x, 'y' => $y ); # Add current grid position to the direction we're going $curr[ $axis ] += $i; # Add the first letter of the ships name to the grid ( just for example purposes ) $grid[ $curr['x'] ][ $curr['y'] ] = substr( $name, 0, 1 ); } } # Display the grid echo '<table width="300" height="300" border="1">'; for ( $row = 1; $row <= 10; $row++ ) { echo '<tr>'; for( $col = 1; $col <= 10; $col++ ) { echo '<td width="30" align="center">'; echo ( isset($grid[$row][$col]) ? $grid[$row][$col] : ' ' ); echo '</td>'; } echo '</tr>'; } echo '</table>'; ?></pre> You may want to have a retry limit on the infinite loop - if it fails to generate a location in 100 tries, reset all... this shouldn't be an issue with a standard set of BattleShip ships. Digest it a little and post back for some help... should get you off to a good start though Link to comment https://forums.phpfreaks.com/topic/125158-solved-big-assignment-make-a-battleship-game-need-lots-of-help/#findComment-649046 Share on other sites More sharing options...
hackypro Posted September 28, 2008 Author Share Posted September 28, 2008 wow thats great, it makes a lot of sense to me. i guess i'm just having trouble incorporating the abstract position of the ships into the actual grid full of radio buttons, but maybe i need to spend a few more hours to figure it out. is $ships an array of ship arrays? because they have to keep location for whether or not they're being hit, and to do that would probably take 2 arrays to get all the squares locations for each ship logged. i think i might just need to rewrite my code from the start with the script you gave incorporated in order to check if hits have been posted and all the session stuff i need to figure out, because right now i'm not sure that the way i wrote my grid works well with the placing ships code. thanks again for your help though, it saved me a lot of time Link to comment https://forums.phpfreaks.com/topic/125158-solved-big-assignment-make-a-battleship-game-need-lots-of-help/#findComment-652419 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.