Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: naval battle game problem help  (Read 11990 times)

0 Members and 1 Guest are viewing this topic.

patio

  • Moderator


  • Genius
  • Maud' Dib
  • Thanked: 1769
    • Yes
  • Experience: Beginner
  • OS: Windows 7
Re: naval battle game problem help
« Reply #15 on: October 22, 2013, 03:15:57 PM »
Quote
There's no 'd' in convoluted. :D   

Check again...
" Anyone who goes to a psychiatrist should have his head examined. "

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: naval battle game problem help
« Reply #16 on: October 22, 2013, 03:23:26 PM »

patio

  • Moderator


  • Genius
  • Maud' Dib
  • Thanked: 1769
    • Yes
  • Experience: Beginner
  • OS: Windows 7
Re: naval battle game problem help
« Reply #17 on: October 22, 2013, 07:08:18 PM »
Foxi you do great work in this section but i couldn't resist...
" Anyone who goes to a psychiatrist should have his head examined. "

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: naval battle game problem help
« Reply #18 on: October 22, 2013, 09:08:04 PM »
Thanks, it serves me right for being a little bit 'smart'. :)  All in good fun.

kyle_engineer



    Intermediate
  • 010010110101100
  • Thanked: 4
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 7
Re: naval battle game problem help
« Reply #19 on: October 23, 2013, 02:23:49 PM »
I'm now trying to put coordinates in variables as someone suggested.

That was me. :P

I was working on a really nice verbose reply, then my blackberry browser crashed and I lost it all... so no more of that. lol!

There are a couple more suggestions I have too...

1) I'd recommend using row numbers 0-9 rather than 1-10. Just personally I think it would be easier to work with and  wouldn't run the risk of a 3rd character ever being an issue.

2) In the normal game Battleship, you can align your ships either vertically (along columns only) or horizontally (along rows only). But you can have some ships one way, and some the other. For simplicity's sake I'd recommend writing your script to only use one of the alignments. This way you don't have to worry so much about ships overlaying each others' coordinates. Although, you will still have that as a problem, it is not as much of an issue only using this method. Plus it makes it easier to write the computer "player's" logic if you're doing that.

3) I don't remember if you can put wildcards into a FOR loop in this way or not (TBH I have kind of avoided FOR for a while cause I didn't really understand it until recently), but I was thinking something like this to check your coordinates (once they are bound to a variable):

Code: [Select]
FOR %%a IN (A*, B*, C*, D*, E*, F*, G*, H*, I*, J*) DO (IF '%%a'=='%shot%' goto %hit%
I think that get's the concept across. However, that only works nicely if A) you can use a wildcard there, and B) if your variables are kept to 3 characters (as a third could cause problems later if not here).

If that didn't work, you could make several small sections like this:

Code: [Select]
:A_checker
REM check column A
set /a row=0
:checker_sub
set coord=A%row%
if '%coord%'=='%shot%' goto HIT
set /a row=%row%+1
if '%row%'=='10' goto B_checker
goto checker_sub

anyway, just some ideas.

Again though, I haven't taken the time to try any of this - so I'll assume if you do try it that you adopt proper syntax. ;)
"Any answer is only as good as it satisfies the question." - Me

0000000100101011000 -
010010010010000001001100010011110100110 001000000010110010100111101010101

orux

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Windows 7
    Re: naval battle game problem help
    « Reply #20 on: October 30, 2013, 03:35:18 PM »
    I think I'm a ok with checking (just used multiple ifs), yet now my lecturer says that there's an error with set command... I Really can't find it. Help would be more than welcome.

    [recovering disk space, attachment deleted by admin]

    Lemonilla



      Apprentice

    • "Too sweet"
    • Thanked: 70
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: naval battle game problem help
    « Reply #21 on: October 30, 2013, 08:12:44 PM »
    well check1 10 will always return false if im not mistaken, and you are using the wrong / on your set /p.
     
    You seem to have a lot of repeating code, try using setlocal enabledelayedexpansion to cut it down a bit. 
    you can do stuff like set /p x!a!=  and echo !%x%_%y%! which is how i`ve been storing my coordinates (forgive me for stealing your project idea)
    Quote from: patio
    God Bless the DOS Helpers...
    Quote
    If it compiles, send the files.

    orux

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Windows 7
      Re: naval battle game problem help
      « Reply #22 on: November 01, 2013, 01:29:22 PM »
      No worries I forgive you ;). I've changed that slash because I thought it was error. Obviously not... I think i do not need to use echo because I don't need to see those coordinates. Yet could you explain why check1 10 would be false? As far as I understand it should be true if it wouldn't be equal to any of previous coordinates. I've used this system to all checks. So maybe that's the reason why I can type more coordinates than needed and those can be the same (A1 A1 A1 more than 10 times). And There still should be that set error somewhere hidden :/.

      P.S. you may check code just until continuegame command, rest part is either almost the same or just blockout of code.

      orux

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Windows 7
        Re: naval battle game problem help
        « Reply #23 on: November 01, 2013, 01:48:26 PM »
        attachment

        [recovering disk space, attachment deleted by admin]

        Lemonilla



          Apprentice

        • "Too sweet"
        • Thanked: 70
        • Computer: Specs
        • Experience: Experienced
        • OS: Windows 7
        Re: naval battle game problem help
        « Reply #24 on: November 01, 2013, 03:01:29 PM »
        No worries I forgive you ;). I've changed that slash because I thought it was error. Obviously not... I think i do not need to use echo because I don't need to see those coordinates. Yet could you explain why check1 10 would be false? As far as I understand it should be true if it wouldn't be equal to any of previous coordinates. I've used this system to all checks. So maybe that's the reason why I can type more coordinates than needed and those can be the same (A1 A1 A1 more than 10 times). And There still should be that set error somewhere hidden :/.

        P.S. you may check code just until continuegame command, rest part is either almost the same or just blockout of code.

        I apologize about the check1_10 bit, I was using my tablet which wasn't showing the "!", so it looked like "if x5==x6" which is always wrong.

        When I run it, it doesn't seem to do much of anything.  Am I missing a component file?  I might be missing something, as I can't read the instructions though.  I'll highlight some of things that stand out to me anyway.


        Code: [Select]
        :check1_10
        set /p x!10!=
        You don't have setlocal EnableDelayedExpansion, so this will always save the veriable as "x!10!" and not x<value of 10>.
        Code: [Select]

        T:\>set /p x!1!=
        2

        T:\>set x
        x!1!=2

        EDIT: I think I've figured it out (maybe) but I keep getting stuck at "set /p x!8!=" or check1_8

        EDIT2: I don't know if this helps you, but here is what I have so far. Maybe you can use some of the ideas/processes to your benefit.
        Code: [Select]
        :: This is designed for windows 7 and is not guaranteed to work on any other OS. ::

        @echo off
        title Naval Game
        color 07
        setlocal EnableDelayedExpansion
        for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
          set "DEL=%%a"
        )
        <nul > X set /p ".=."
        :start

        call :Newgame
        call :displayBoard
        call :move
        :main
        call :CheckWin
        call :displayBoard
        call :move

        goto :main

        :CheckWin
        set ok=0
        set ok.total=0
        set y=1
        :checkwin.loop1
        set x=1
        :checkwin.loop2
        if "!%x%_%y%!"=="X" set /a ok+=1
        if "!B%x%_%y%!"=="X," set /a ok.total+=1
        set /a x+=1
        if %x% LSS 9 goto :checkwin.loop2
        set /a y+=1
        if %y% LSS 9 goto :checkwin.loop1

        if not %ok% EQU %ok.total% goto :eof

        cls
        echo You Won in %turn% turns!
        pause>nul
        exit

        :move
        echo.
        set move=
        set /p move=^>
        if not defined move goto :move
        set /a turn+=1

        if /i "%move%"=="set" set && pause >nul
        if /i "%move%"=="reset" goto :start


        if /i "%move:~0,1%"=="A" set "x=1" && set y=%move:~1,1%
        if /i "%move:~0,1%"=="B" set "x=2" && set y=%move:~1,1%
        if /i "%move:~0,1%"=="C" set "x=3" && set y=%move:~1,1%
        if /i "%move:~0,1%"=="D" set "x=4" && set y=%move:~1,1%
        if /i "%move:~0,1%"=="E" set "x=5" && set y=%move:~1,1%
        if /i "%move:~0,1%"=="F" set "x=6" && set y=%move:~1,1%
        if /i "%move:~0,1%"=="G" set "x=7" && set y=%move:~1,1%
        if /i "%move:~0,1%"=="H" set "x=8" && set y=%move:~1,1%

        if /i "%move:~1,1%"=="A" set "x=1" && set y=%move:~0,1%
        if /i "%move:~1,1%"=="B" set "x=2" && set y=%move:~0,1%
        if /i "%move:~1,1%"=="C" set "x=3" && set y=%move:~0,1%
        if /i "%move:~1,1%"=="D" set "x=4" && set y=%move:~0,1%
        if /i "%move:~1,1%"=="E" set "x=5" && set y=%move:~0,1%
        if /i "%move:~1,1%"=="F" set "x=6" && set y=%move:~0,1%
        if /i "%move:~1,1%"=="G" set "x=7" && set y=%move:~0,1%
        if /i "%move:~1,1%"=="H" set "x=8" && set y=%move:~0,1%

        set cord=%x%_%y%

        REM CHECK CORD
        if "!B%cord%!"=="X," set %cord%=X
        if "!B%cord%!"=="0," set %cord%=M

        goto :eof


        :NewGame
        cls
        echo Loading, Please Wait . . .
        set turn=1
        for /l %%X in (1,1,8) do (
        for /l %%Y in (1,1,8) do (
        set %%X_%%Y=0
        set "B%%X_%%Y=0,"
        )
        )

        REM Create Board
        set ship_Num=0
        :creatShip
        set /a Ship_Num+=1
        set /a ran_x=%random%*8/32768+1
        set /a ran_y=%random%*8/32768+1
        set /a ran_dir=%random%*4/32768+1
        set /a ran_l=%random%*4/32768+2

        set "B%ran_x%_%ran_y%=X,"

        set working_x=%ran_x%
        set working_y=%ran_y%

        :CreatePoints
        if "%ran_dir%"=="1" set /a working_X=%working_X%+1
        if "%ran_dir%"=="2" set /a working_X=%working_X%-1
        if "%ran_dir%"=="3" set /a working_Y=%working_Y%+1
        if "%ran_dir%"=="4" set /a working_Y=%working_Y%-1
        if %working_X% GTR 8 goto :NewGame
        if %working_X% LSS 0 goto :NewGame
        if %working_Y% GTR 8 goto :NewGame
        if %working_Y% LSS 0 goto :NewGame
        if "!B%working_X%_%working_Y%!"=="X," goto :NewGame
        set "B%working_x%_%working_y%=X,"
        set /a ran_l-=1
        if not %ran_l% EQU -1 goto :createPoints

        if %Ship_num% LSS 3 set ship_num-=1 && goto :creatShip

        REM Record Map
        if exist game.txt del game.txt
        set y=1
        :loop_y
        set x=1
        :loop_x
        set line=%line%!B%x%_%y%!
        set /a x+=1
        if %x% LSS 9 goto :loop_x
        echo %line% >>game.txt
        set line=
        set /a y+=1
        if %y% LSS 9 goto :loop_y

        goto :eof

        :displayBoard
        cls
        if "%turn:~1,1%"=="" echo.%turn%  A B C D E F G H
        if not "%turn:~1,1%"=="" echo.%turn% A B C D E F G H
        set y=1
        :loop1
        0>nul set /p"=%y%  "
        set x=1
        :loop2
        if "!%x%_%y%!"=="0" call :color 09 "0 "
        if "!%x%_%y%!"=="X" call :color 0c "X "
        if "!%x%_%y%!"=="M" call :color 0e "0 "
        0>nul set /p"= "
        set /a x+=1
        if %x% LSS 9 goto :loop2
        echo.
        set /a y+=1
        if %y% LSS 9 goto :loop1
        goto :eof

        :color
        set "param=^%~2" !
        set "param=!param:"=\"!"
        findstr /p /A:%1 "." "!param!\..\X" nul
        <nul set /p ".=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
        exit /b
        « Last Edit: November 01, 2013, 03:44:18 PM by Lemonilla »
        Quote from: patio
        God Bless the DOS Helpers...
        Quote
        If it compiles, send the files.

        orux

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Windows 7
          Re: naval battle game problem help
          « Reply #25 on: November 02, 2013, 02:10:27 AM »
          well, yesterday night I worked a bit myself on this. Now everything seems working except that I can still type after entering 10th coordinates. Btw I was missing percentage sign in my ifs' so that was probably that set mistake. Thanks for showing your code. Problem is, i'm not yet familiar with EnableDelayedExpansion command so I'll need to check it out.

          [recovering disk space, attachment deleted by admin]

          orux

            Topic Starter


            Rookie

            • Experience: Beginner
            • OS: Windows 7
            Re: naval battle game problem help
            « Reply #26 on: November 02, 2013, 02:35:08 PM »
            Yep seems I'm stuck with check1_8 as well. So I guess check1_8 check1_9 check1_10 will provide errors as well... wonder why tho... system is the same.

            orux

              Topic Starter


              Rookie

              • Experience: Beginner
              • OS: Windows 7
              Re: naval battle game problem help
              « Reply #27 on: November 02, 2013, 03:53:46 PM »
              I've figured out the problem with chek1_8 i think. Some of my commands were misspelled. Now coordinates works for both players. Next Step: fix the hitting. Cya.

              [recovering disk space, attachment deleted by admin]

              orux

                Topic Starter


                Rookie

                • Experience: Beginner
                • OS: Windows 7
                Re: naval battle game problem help
                « Reply #28 on: November 03, 2013, 03:37:50 AM »
                Now, this version kinda works. Yet it still needs last thing: To check if there are any more ships left and if there aren't to announce winner. The rest will be only details (like instructions (that i need to add), color of background if I figure how to; that i wouldn't need to retype all coordinates if one is wrong, etc)

                [recovering disk space, attachment deleted by admin]

                foxidrive



                  Specialist
                • Thanked: 268
                • Experience: Experienced
                • OS: Windows 8
                Re: naval battle game problem help
                « Reply #29 on: November 03, 2013, 05:16:26 AM »
                Just a comment here - if you make a script with English inside it, then maybe people will be more inclined to offer help.

                It takes a fair amount of effort to analyse a large script when it works properly, and more effort again to analyse a script that is broken, or incomplete.
                If it is in a foreign language then it takes even more work.