Hi, I’m trying to make a battleship program for class and can’t seam to get it to work right. the problems I’m running into are that first off everytime i hit the shoot button it seams to reset the board arrays. By adding the $currentPlayer variable to a if loop i thought it would fix this but it doesn’t for some reason. after i hit the shoot button it again evaluates it as null and resets the boards for each player. The next problem is related to the first as the attack function doesn’t seam to be run or work properly and i can’t seam to figure out why. any help at all would be greatly appreciated as I have looked through the book PHP5/MySQL programming for the absolute beginner and still can’t find out whats wrong. thanks in advance the entire code of my program is below.
[code]
Battleship <? global $board1, $board2, $height, $width; $height = 10; $width = 10; $sizeOfBattleship = 5; $board1 = array(); $board2 = array(); $hitCounter1 = 0; $hitCounter2 = 0; //print the form print <<<HERE height: width:HERE; if(!$currentPlayer){ setNewBoards(10,10); // create and place boards randomly positioning the battleship $currentPlayer = 1; //sets current player to 1 so setNewBoards isn't ran again }else{ attack($attackPositionX, $attackPositionY); $currentPlayer = 1; }//end else print "Current Player = "; print $currentPlayer; print "
"; function attack($attackPositionX, $attackPositionY){ //function to handle the attack by the player global $currentPlayer; if ($currentPlayer==1){ //attack for player 1 if($board1[$attackPositionX][$attackPositionY]== "B"){ //checks if a battleship location was attacked print "You Hit the battleship!"; $board1[$attackPositionX][$attackPositionY] = "H"; //replaces the battleship location with a Hit $hitCounter1++; //incriments the number of hits }else{ print "You Missed"; $board1[$attackPositionX][$attackPositionY] = "M"; //replaces the battleship location with a miss }//end else if ($hitCounter1 ==5){ //checks if the player won print "You Win!"; }//end win }//end player if //changes the current player $currentPlayer=2; if ($currentPlayer==2){ if($board2[$attackPositionX][$attackPositionY]== "B"){ //checks if a battleship location was attacked print "You Hit the battleship!"; $board2[$attackPositionX][$attackPositionY] = "H"; //replaces the battleship location with a Hit $hitCounter2++; //incriments the number of hits }else{ print "You Missed!"; $board2[$attackPositionX][$attackPositionY] = "M"; //replaces the battleship location with a miss }//end else if ($hitCounter2 ==5){ //checks if the player won print "You Win!"; }//end win }//end player if //changes the current player $currentPlayer = 1; }//function end //prints the board for player 1 for($i=0;$i<10;$i++){ for($j=0;$j<10;$j++){ print $board1[$i][$j]; } print"
"; } //prints the board for player 2 print "
"; for($i=0;$i<10;$i++){ for($j=0;$j<10;$j++){ print $board2[$i][$j]; } print"
"; } function setNewBoards($height,$width){ //sets the boards positioning the ships in random locations global $board1; global $board2; $row = rand(0,5);//random starting location for row $col = rand(0,5);//random starting location for column $dirPlayer1 = rand(0,1); //random start direction for ship to be placed for player 1 $dirPlayer2 = rand(0,1); //random start direction for ship to be placed for player 2 //fills the board with X's for($i=0;$i<10;$i++){ for($j=0;$j<10;$j++){ $board1[$i][$j] = "X"; }//end for }//end for //checks direction of the ship to be placed and places the ship if($dirPlayer1 == 0){ $count=0; for($row = rand(0,5);$count < 5;$count++){ $board1[$row][$col] = "B"; $row++; }//for end }else{ $count=0; for($col = rand(0,5);$count<5;$count++){ $board1[$row][$col] = "B"; $col++; }//for end }//else end //fills player 2's board with X's for($i=0;$i<10;$i++){ for($j=0;$j<10;$j++){ $board2[$i][$j] = "X"; }//end for }//end for //checks direction of the ship to be placed and places the ship if($dirPlayer2 == 0){ $count=0; for($row = rand(0,5);$count < 5;$count++){ $board2[$row][$col] = "B"; $row++; }//for end }else{ $count=0; for($col = rand(0,5);$count<5;$count++){ $board2[$row][$col] = "B"; $col++; }//for end }//else end }//function end ?> [/code]
MOD EDIT: Added code tags