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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2004
    Location
    Maryland
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Please Help!! Battleship Code

    Okay my teacher wants me and my class to do a simple battleship program, i mean i got everything except i cant change the value of my 2-D array cell...for example one of my cell contain "2,4" the cordinates for the shiP!
    I want to change the the cell to a "H" or "M" if i missed it. Youll see

    he said i need something to like intialize my array and something that display it, and he said the StartGame Function contain both. And once I seperate the two my Value can change

    Plz Help!! If you really smart this shouldnt take anymore that 5 mins, but its taking me forever.... ill give you the rubric too if you want.


    Rubric:
    Your objective is to create a Battleship-type game. The grid should be a minimum of 20 by 20 (400 possible locations) and you should have at least 5 ships. Each ship is assigned to one location. You must display the grid on the screen by writing it out. The game should meet the following minimum requirements:


    ____/10: 1. The grid should be at least 20 by 20.

    ____/10: 2. Each grid location should indicate either1) not guessed, (2) miss, or (3) hit.

    ____/10: 3. The computer should randomly assign at least 5 ships to the grid.

    ____/10: 4. The computer should keep track of and display the number of guesses.

    ____/10: 5. The computer should indicate if your guess is a hit or a miss.

    ____/10: 6. An image should be displayed depending on whether your guess is a hit or a miss. You can be creative by adding a sinking ship or similar. Alternatively, you could add sound to indicate a hit or a miss.

    ____/10: 7. When the player has sunk all the ships, the number of hits and misses should be displayed.

    ____/10: 8. A reset button should enable the player to start a new game.

    ____/10: 9. The user should guess by entering the row and column number. You must show the row and column numbers on the grid.

    ____/10: 10. A "cheat" button should enable the user to display the locations of the ships.



    You will receive extra credit for any features beyond those minimum requirements. for example, you could indicate how far from the target the guess is.




    My Code -
    <html>
    <head>
    <title> Khiem Nguyen BattleShip Project </title>
    <script>
    var r1 = new Array()
    var r2 = new Array()
    var r3 = new Array()
    var r4 = new Array()
    var r5 = new Array()
    var r6 = new Array()
    var r7 = new Array()
    var r8 = new Array()
    var r9 = new Array()
    var r10 = new Array()
    var r11 = new Array()
    var r12 = new Array()
    var r13 = new Array()
    var r14 = new Array()
    var r15 = new Array()
    var r16 = new Array()
    var r17 = new Array()
    var r18 = new Array()
    var r19 = new Array()
    var r20 = new Array()
    var r21 = new Array()
    var myGrid = new Array(r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20,r21)
    var i
    var j
    var theGrid = ""
    //Grid Function
    function StartGame(){
    theGrid = theGrid + "<table border='1'>"
    for(i=0;i<21;i++){
    theGrid = theGrid + "<tr>"
    for(j=0;j<21;j++){
    myGrid[i][j] = j + "," + i
    theGrid = theGrid + "<td>" + myGrid[i][j] + "</td>"
    }
    theGrid = theGrid + "</tr>"
    }
    theGrid = theGrid + "</table>"
    YoGrid.innerHTML = theGrid
    }
    //EndGrid Function

    //Randomize Function

    function randomize(N1, N2) {
    return Math.floor((N2 - N1 + 1)* Math.random() + N1)
    }
    var iRN = new Array()
    var jRN = new Array()
    var mKay = ""
    for(x=0;x<5;x++){
    iRN[x] = randomize(0,20);
    jRN[x] = randomize(0,20);
    mKay = mKay + iRN[x] + "," + jRN[x] + "\n"
    }
    //End Randomize Function

    var Cheater
    //Cheater Function
    function Cheater(){
    alert(mKay)
    }
    //End Cheater Function

    //New Game Function

    function NewGame(){
    window.location="3.html"
    }
    //End New Game Function
    //Function Intialize
    var gGuess = 0
    var gHitShip = 0
    var gMissShip = 0
    var k
    var m
    function HitMiss(){
    gGuess++
    fatcat.innerHTML = gGuess
    if(txt1.value == ""|| txt2.value == ""){
    alert("Please Fill in Both Boxes Monkey!!")
    }
    for(k=0;k<5;k++){
    if(iRN[k] == txt1.value && jRN[k] == txt2.value){
    gHitShip++
    alert("Hit")
    sassycat.innerHTML = gHitShip
    return("Hit")
    }
    }
    for(m=0;m<5;m++){
    if(iRN[m] != txt1.value || jRN[m] != txt2.value){
    gMissShip++
    alert("Miss")
    coolcat.innerHTML = gMissShip
    return("Miss")
    }
    }
    }

    //End Function Intialize
    </script>
    </head>
    <body onload="StartGame()">
    <div id="YoGrid"></div>
    <br>
    <br>
    <br>
    <div>You Guessed This Many Times :<span id="fatcat"></span></div>
    <div>You Guessed This Many Hit :<span id="sassycat"></span></div>
    <div>You Guessed This Many Missed :<span id="coolcat"></span></div>
    <input type="text" id="txt1">
    <input type="text" id="txt2">
    <input type="button" id="Cheater" value="Cheater" onclick="Cheater()">
    <input type="button" id="NewGame" value="New Game" onclick="NewGame()">
    <input type="button" id="HitMiss" value="Fire in the Hole!!!" onclick="HitMiss()">
    </body>
    </html>[/FONT]
    Attached Files Attached Files
    Last edited by skyblueff0; 05-05-2004 at 01:40 AM.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    This will probably not solve any problem that you are having, but in javascript a semicolon at the end of a statement is a must. You've also got an extraneous [font] at the end. Do realize, that we do not do people's homework assignments here. We can give some pointers, but your best bet is to ask your instructor. I hope that one of the javascript gurus will be along to shortly to give you a push in the right direction.
    Code:
    var i
    var j
    var theGrid = ""
    to
    Code:
    var i;
    var j;
    var theGrid = "";

  • #3
    Regular Coder
    Join Date
    Feb 2004
    Location
    WA
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    another minor fix.

    Code:
    for(i=0;i<21;i++){
    theGrid = theGrid + "<tr>"
    The looping condition is wrong, you actually go through the loop 21 times instead of the 20 you were expecting.
    if you count
    0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
    - That's 21!
    Nobody is Perfect. I am Nobody.

  • #4
    Regular Coder
    Join Date
    Jan 2004
    Location
    Maryland
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts
    GAH! Khiem I gave you my code! How can it not work. I got a 110/100.

  • #5
    New to the CF scene
    Join Date
    Feb 2004
    Location
    Maryland
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Dave you go on this forum...lol oh well


  •  

    Posting Permissions

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