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 11958 times)

0 Members and 1 Guest are viewing this topic.

orux

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Windows 7
    naval battle game problem help
    « on: October 20, 2013, 08:26:24 AM »
    Hey there, my first post here.

    I'm making a little naval battle game for uni (u know, that two coordinates based game). txt files contains coordinates of firstplayers "pirmaszaidejas.txt", secondplayer "antraszaidejas",  coordinates are like this: one line for one coordinate (letter and number), and empty line between ships. I need to change background color when someone hits and to delete guessed line and write it to another file.

    Now it says neteisingos kordinates which means  wrong coordinates even tho I'm picking right coords. Also it doesn't change between players.

    P.S. At first I want to check whether there are such coordinates and if there are to continue. (t stands for yeas, and n stands for no).

    I'm adding all necessary files (I hope). If you have any questions in order to help me please ask I'll try to explain.

    [recovering disk space, attachment deleted by admin]

    Salmon Trout

    • Guest
    Re: naval battle game problem help
    « Reply #1 on: October 20, 2013, 12:50:20 PM »
    You need to study syntax some more, especially FOR...

    One example: at line 81 (This is all one line! 661 chars!) (I have split it to make this post possible!)

    for %%U in (R1,E1,S1,P1,U1,B1,L1,I1,K1,A1,R2,E2,S2,P2,U2,B2,L2,I2,K2,A2,R3,E3,S3,
    P3,U3,B3,L3,I3,K3,A3,R4,E4,S4,P4,U4,B4,L4,I4,K4,A4,R5,E5,S5,P5,U5,B5,L5,I5,K5,A5,
    R6,E6,S6,P6,U6,B6,L6,I6,K6,A6,R7,E7,S7,P7,U7,B7,L7,I7,K7,A7,R8,E8,S8,P8,U8,B8,L8,
    I8,K8,A8,R9,E9,S9,P9,U9,B9,I9,K9,A9,R10,E10,S10,P10,U10,B10,L10,I10,K10,A10)
    do findstr /C:"R1 E1 S1 P1 U1 B1 L1 I1 K1 A1 R2 E2 S2 P2 U2 B2 L2 I2 K2 A2 R3 E3
    S3 P3 U3 B3 L3 I3 K3 A3 R4 E4 S4 P4 U4 B4 L4 I4 K4 A4 R5 E5 S5 P5 U5 B5 L5 I5 K5
    A5 R6 E6 S6 P6 U6 B6 L6 I6 K6 A6 R7 E7 S7 P7 U7 B7 L7 I7 K7 A7 R8 E8 S8 P8 U8 B8
    L8 I8 K8 A8 R9 E9 S9 P9 U9 B9 I9 K9 A9 R10 E10 S10 P10 U10 B10 L10 I10 K10 A10" AntrasZaidejas.txt

    That is a one-line FOR command. You are now outside the loop and %%U is no longer a loop variable (it is the literal string "%%U")
    Execution will probably (!) never go to hit1


    if %%U == %a%%b% goto hit1
    goto missed1
    « Last Edit: October 20, 2013, 01:48:10 PM by Salmon Trout »

    orux

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Windows 7
      Re: naval battle game problem help
      « Reply #2 on: October 21, 2013, 06:56:56 AM »
      Thanks for the answer. This is probably my biggest problem... How to check all these coordinates?? I was thinking about using for + findstr, but I'm not really sure...

      kyle_engineer



        Intermediate
      • 010010110101100
      • Thanked: 4
        • Yes
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 7
      Re: naval battle game problem help
      « Reply #3 on: October 21, 2013, 09:48:00 PM »
      Personally, I think I would store the coordinates in variables, rather than .txt files. I think it would be easier to argue the coordinates that way. Also if you wanted to 'save' the coordinates, you could just dump them to a .txt file which will be loaded next time.

      From what I can tell, you're basically trying to make something like Battle Ship, but for DOS. If that's the case, I'm sure you couuld find some snippets to help you.
      "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 #4 on: October 22, 2013, 01:21:11 AM »
        Yep that's probably battle ship. How should I store coordinates in variables?

        orux

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Windows 7
          Re: naval battle game problem help
          « Reply #5 on: October 22, 2013, 03:01:02 AM »
          Do you mean that I should do something like this (see attachment)? lets say I'm doing like that. still how should i check whether players typed correct coords or not  ( for example if we have a grid with word respublika (10 letters) and numbers from 1 to 10).
          I mean if they typed something like "m15" instead of lets say "r9" you got the idea. I assume I should use for command. something like: for %%U in (all those coordinates) if %%U == %all those sets from attachment%.

          Or I'm thinking in a wrong way at all? because it may be the case :)

          [recovering disk space, attachment deleted by admin]

          orux

            Topic Starter


            Rookie

            • Experience: Beginner
            • OS: Windows 7
            Re: naval battle game problem help
            « Reply #6 on: October 22, 2013, 10:41:34 AM »
            Here's a bit of an update. I'm making it simpler because my deadline is coming... Now each of ships takes just one coordinate. And when player picks coordinates, as far as I'm understanding it should go through all possible variations (r1-r10,a1-a10) and see if given coordinate matches previous player coordinates. (pirm zaid stands for first player and antrzaid stands for secondplayer).
            @echo off
            echo laivu musis.
            echo 2 zaidejai.
            echo Ar norite pradeti?
            echo jei taip spauskite, bet kuri klavisa, jei ne ctrl+c.
            pause>null

            TITLE NAVAL BATTLE

            echo pasirinkite:
            echo T - Pradeti zaidima
            echo N - Baigti zaidima

            CHOICE/c:TN
            if errorlevel 255 goto end
            if errorlevel 2 goto end
            goto start

            :start
            echo.
            goto firstplayer
            goto secondplayer

            :firstplayer
            echo pirmas zaidejas iveda savo 10 laivu. 1 koordinate = 1 laivas
            set /p x1=
            set /p x2=
            set /p x3=
            set /p x4=
            set /p x5=
            set /p x6=
            set /p x7=
            set /p x8=
            set /p x9=
            set /p x10=

            :continuegame
            echo pasirinkite ar testi?
            choice/c:TN
            if errorlevel 255 goto end
            if errorlevel 2 goto end
            goto clean

            :clean
            cls
            goto secondplayer

            :secondplayer
            echo antras zaidejas iveda savo 10 laivu. 1 koordinate = 1 laivas
            set /p y1=
            set /p y2=
            set /p y3=
            set /p y4=
            set /p y5=
            set /p y6=
            set /p y7=
            set /p y8=
            set /p y9=
            set /p y10=

            :continuegame
            echo pasirinkite ar testi?
            choice/c:TN
            if errorlevel 255 goto end
            if errorlevel 2 goto end
            goto cls

            :cls
            cls
            goto PirmZaid

            :PirmZaid
            echo Pirmo zaidejo eile.
            echo pasirinkite koordinates. Pirma raide ir tada skaiciu.
            set /p a=

            for %%U in (R1,E1,S1,P1,U1,B1,L1,I1,K1,A1,R2,E2,S2,P2,U2,B2,L2,I2,K2,A2,R3,E3,S3,
            P3,U3,B3,L3,I3,K3,A3,R4,E4,S4,P4,U4,B4,L4,I4,K4,A4,R5,E5,S5,P5,U5,B5,L5,I5,K5,A5,
            R6,E6,S6,P6,U6,B6,L6,I6,K6,A6,R7,E7,S7,P7,U7,B7,L7,I7,K7,A7,R8,E8,S8,P8,U8,B8,L8,
            I8,K8,A8,R9,E9,S9,P9,U9,B9,I9,K9,A9,R10,E10,S10,P10,U10,B10,L10,I10,K10,A10) do if %a% == %y1%%y2%%y3%%y4%%y5%%y6%%y7%%8%%y9%%y10% goto hit1
            goto AntrZaid

            :hit1
            goto setcolor1
            :setcolor1
            color 4f
            goto shootagain1

            :shootagain1
            color 0f
            goto PirmZaid

            :AntrZaid
            echo Antro zaidejo eile.
            echo pasirinkite koordinates. Pirma raide ir tada skaiciu.
            set /p a=

            for %%U in (R1,E1,S1,P1,U1,B1,L1,I1,K1,A1,R2,E2,S2,P2,U2,B2,L2,I2,K2,A2,R3,E3,S3,
            P3,U3,B3,L3,I3,K3,A3,R4,E4,S4,P4,U4,B4,L4,I4,K4,A4,R5,E5,S5,P5,U5,B5,L5,I5,K5,A5,
            R6,E6,S6,P6,U6,B6,L6,I6,K6,A6,R7,E7,S7,P7,U7,B7,L7,I7,K7,A7,R8,E8,S8,P8,U8,B8,L8,
            I8,K8,A8,R9,E9,S9,P9,U9,B9,I9,K9,A9,R10,E10,S10,P10,U10,B10,L10,I10,K10,A10) do if %a% == %x1%%x2%%x3%%x4%%x5%%x6%%x7%%x8%%x9%%x10% goto hit2
            goto PirmZaid

            :hit2
            goto setcolor2
            :setcolor2
            color 4f
            goto shootagain2

            :shootagain2
            color 0f
            goto AntrZaid
             
            :end


            now doesn't matter what I'm inputing, I'm only changing from first player to second player forever. Help is more than welcome.

            foxidrive



              Specialist
            • Thanked: 268
            • Experience: Experienced
            • OS: Windows 8
            Re: naval battle game problem help
            « Reply #7 on: October 22, 2013, 11:16:48 AM »
            It's difficult to see what is being done, and what you expect.


            orux

              Topic Starter


              Rookie

              • Experience: Beginner
              • OS: Windows 7
              Re: naval battle game problem help
              « Reply #8 on: October 22, 2013, 11:30:51 AM »
              well, How I'm imagining it :) : At first player chooses wether to continue or not. If yes (or T) players need to write coordinates. firstplayer starts by entering coordinates of 10 ships (for example one ship L5, because they're playing on a grid). Then program asks do players want to continue? if so screen is being cleaned and second player puts its' fleet coordinates. Then again program asks shall it continue? if so, screen is being cleaned lastly and it's firstplayer move. He should choose random coordinate from grid (will show grid at the end of post) and if that random coordinate matches any of the second players' ship, it should set background color to red and let the first player shoot again. If it doesn't match, then go to the secondplayer turn immediately.

              Grid:
                RESPUBLIKA
              1
              2
              3
              4
              5
              6
              7
              8
              9
              10

              Basically 10 on 10 grid.
              Hope it helps.

              foxidrive



                Specialist
              • Thanked: 268
              • Experience: Experienced
              • OS: Windows 8
              Re: naval battle game problem help
              « Reply #9 on: October 22, 2013, 11:48:05 AM »
              One issue you will have is that you can't change the background colour of just one position on the screen, without using either a third party utility or some convoluted scripting and formatting of your screen output.

              orux

                Topic Starter


                Rookie

                • Experience: Beginner
                • OS: Windows 7
                Re: naval battle game problem help
                « Reply #10 on: October 22, 2013, 11:57:43 AM »
                so, the best option would be to change both background and foreground then?  could you tell something about my main problem? When I'm reaching part of program where first player picks random coordinate. At this point it allows me to write anything (while it should be only combination of letter and number from grid) moreover when I'm hitting enter after typing (no matter what) program gives second player turns. As I said it seems like program ignores that "random coord" I can type something like "mm" and it moves to second player. even if I'm typing proper coordinates (that should hit the target) it still moves.

                What coordinates I'v chosen for testing:
                1stplayer (or  set /p xn=):           2ndplayer(or set/p yn=):
                R1                                                 R1     
                B2                                                 P2
                A4                                                 L3
                P5                                                 A4
                L5                                                 R5
                R6                                                 U5
                K7                                                 B7
                B8                                                 E9
                R10                                               K9
                K10                                              L10

                Lemonilla



                  Apprentice

                • "Too sweet"
                • Thanked: 70
                • Computer: Specs
                • Experience: Experienced
                • OS: Windows 7
                Re: naval battle game problem help
                « Reply #11 on: October 22, 2013, 01:37:37 PM »
                One issue you will have is that you can't change the background colour of just one position on the screen, without using either a third party utility or some convoluted scripting and formatting of your screen output.

                I don't know if this is considered a "convoluded scripting" but here is a way that works on windows 7

                Code: [Select]
                :: Jeb Stack Overflow.com
                :: http://stackoverflow.com/questions/4339649/how-to-have-multiple-colors-in-a-batch-file/5344911#5344911

                @echo off
                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"
                )

                echo say the name of the colors, don't read

                call :ColorText 0a "blue"
                call :ColorText 0C "green"
                call :ColorText 0b "red"
                echo.
                call :ColorText 19 "yellow"
                call :ColorText 2F "black"
                call :ColorText 4e "white"
                echo.
                pause
                goto :Year

                goto :eof

                :ColorText
                echo off
                <nul set /p ".=%DEL%" > "%~2"
                findstr /v /a:%1 /R "^$" "%~2" nul
                del "%~2" > nul 2>&1
                goto :eof













                :Year



                @echo off
                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"
                )

                rem Prepare a file "X" with only one dot
                <nul > X set /p ".=."

                call :color 1a "a"
                call :color 1b "b"
                call :color 1c "^!<>&| %%%%"*?"
                pause
                exit /b

                :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

                To do this you would need a loop and loop through the collums until you get to the end of the line, which 'echo.' would end.

                Quote from: patio
                God Bless the DOS Helpers...
                Quote
                If it compiles, send the files.

                foxidrive



                  Specialist
                • Thanked: 268
                • Experience: Experienced
                • OS: Windows 8
                Re: naval battle game problem help
                « Reply #12 on: October 22, 2013, 02:03:25 PM »
                so, the best option would be to change both background and foreground then?

                Basic batch commands change the entire screen colour, not just one character.

                first player picks random coordinate. At this point it allows me to write anything (while it should be only combination of letter and number from grid)

                You will have to parse each input string and check firstly that it is either 2 or 3 characters, and then that the first character is a lower/upper case letter and the second 1 or 2 character is a digit, and ask for further input if it is not.

                Checking it is possible with a regular expression, but findstr has bugs when checking character classes.
                The alternative is to do it manually with plain batch code, or use VBS to check the characters.



                foxidrive



                  Specialist
                • Thanked: 268
                • Experience: Experienced
                • OS: Windows 8
                Re: naval battle game problem help
                « Reply #13 on: October 22, 2013, 02:07:54 PM »
                I don't know if this is considered a "convoluded scripting" but here is a way that works on windows 7

                There's no 'd' in convoluted. :D   

                That method doesn't handle the full spectrum of characters, but here is a script that works for more characters:

                Code: [Select]
                @Echo Off
                Call :Color A "######" \n E "" C " 21 " E "!" \n B "######" \n
                Pause >Nul
                Exit /B

                :Color
                :: v21
                :: Arguments: hexColor text [\n] ...
                :: \n -> newline ... -> repeat
                :: Supported in windows XP, 7, 8.
                :: In XP extended ascii characters are printed as dots.
                :: For print quotes, use empty text.
                SetLocal EnableExtensions EnableDelayedExpansion
                Subst `: "!Temp!" >Nul &`: &Cd \
                SetLocal DisableDelayedExpansion
                If Not Exist `.7 (
                Echo(|(Pause >Nul &Findstr "^" >`)
                Set /P "=." >>` <Nul
                For /F "delims=;" %%# In (
                '"Prompt $H;&For %%_ In (_) Do Rem"') Do (
                Set /P "=%%#%%#%%#" <Nul >`.3
                Set /P "=%%#%%#%%#%%#%%#" <Nul >`.5
                Set /P "=%%#%%#%%#%%#%%#%%#%%#" <Nul >`.7))
                :__Color
                Set "Text=%~2"
                If Not Defined Text (Set Text=^")
                SetLocal EnableDelayedExpansion
                Set /P "LF=" <` &Set "LF=!LF:~0,1!"
                For %%# in ("!LF!") Do For %%_ In (
                \ / :) Do Set "Text=!Text:%%_=%%~#%%_%%~#!"
                For /F delims^=^ eol^= %%# in ("!Text!") Do (
                If #==#! EndLocal
                If \==%%# (Findstr /A:%~1 . \` Nul
                Type `.3) Else If /==%%# (Findstr /A:%~1 . /.\` Nul
                Type `.5) Else (Echo %%#\..\`>`.dat
                Findstr /F:`.dat /A:%~1 .
                Type `.7))
                If "\n"=="%~3" (Shift
                Echo()
                Shift
                Shift
                If ""=="%~1" Goto :Eof
                Goto :__Color

                orux

                  Topic Starter


                  Rookie

                  • Experience: Beginner
                  • OS: Windows 7
                  Re: naval battle game problem help
                  « Reply #14 on: October 22, 2013, 02:39:23 PM »
                  thanks for answers. I've been working on code by myself a bit. before seeing your answers.. I'm now trying to put coordinates in variables as someone suggested. without findstr at all. also haven't fixed the color issue yet. Trying to get main stuff working at first. will take a look at this tomorrow. It's too late here on my side of the world.

                  [recovering disk space, attachment deleted by admin]