Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. The first descent code I wrote in my life

The first descent code I wrote in my life

Scheduled Pinned Locked Moved The Lounge
c++game-devalgorithmsdata-structureslounge
40 Posts 27 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Vasily Tserekh

    This is the first descent code I wrote when I began to make programs, it was in C++ Is about an algorithm to arrangle ships in a classic battleship game, first funcion is to check if a ship can be placed at x;y second is for placing the ship and 3th was for placing all the ships. I was so amazed that the code worked that I felt a joy I only felt few times since then. How about you

    //------Check if in square X;Y a ship can be placed------------------

    /* x,y: Board coordinates
    Size: Ship size
    Pos: posicion (1 horizontal vertical)
    board: board number
    */
    bool TGame::Free(int x,int y,int size,int pos,int board )
    {
    int ship[7];
    int side1[7];
    int side2[7];
    bool answer=true;
    if(aBoard[board]->GetInfo(x,y)!=0)
    {
    return answer=false;
    }
    if (pos==1)
    {
    for(int i=0;i<size+2;i++)
    {ship[i]=aBoard[board]->GetInfo(x-1+i,y);
    side1[i]=aBoard[board]->GetInfo(x-1+i,y-1);
    side2[i]=aBoard[board]->GetInfo(x-1+i,y+1);
    }
    }

    else
    { for(int i=0;i<size+2;i++)
    {ship[i]=aBoard[board]->GetInfo(x,y-1+i);
    side1[i]=aBoard[board]->GetInfo(x-1,y-1+i);
    side2[i]=aBoard[board]->GetInfo(x+1,y-1+i);
    }
    }
    if (x==0 && pos==1)
    {ship[0]=0;
    side1[0]=0;
    side2[0]=0;

    }
    for (int i=0;i<size+2;i++)
    {if (ship[i]!= 0)answer=false;
    if (side1[i]!= 0)answer=false;
    if (side2[i]!= 0)answer=false;
    }
    return answer;
    }
    //------Place a ship in the board-------------------------------------

    /* Size: Ship size
    board: Board
    Ship: Place of the ship in the ship array
    Type: ship type
    */
    void TGame:: RandomShip(int size,int type,int board,int ship)
    {
    int pos=random(2);
    randomize();
    int x,y;
    bool answer=false; ;
    if (pos==1)
    {
    while (answer==false)
    {
    x=random(11)-size;
    while(x<0)
    {x=random(11)-size;
    }
    y=random(15);
    answer=Free(x,y,size,pos,board);
    }
    for(int i=0;i<size;i++)
    {aBoard[board]->SetInfo(x+i,y,type);
    }
    }
    else
    {
    while (answer==false)
    {
    x=random(10);
    y=random(16)-size;
    while(y<0)
    {y=random(16)-size;
    }
    answer=Free(x,y,size,pos,board);
    }
    for(int i=0;i<size;i++)
    {aBoard[board]->SetInfo(x,y+i,type);
    }
    }
    if(board==0)
    {
    int ax=Transfo

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #6

    I was able to boil that down to 3 lines. I'd show you the code, but then I'd have to charge you for it. :-D

    C 1 Reply Last reply
    0
    • Richard DeemingR Richard Deeming

      My first descent code was a recursive descent parser[^]. I'm not sure if it was decent, though! ;P


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      M Offline
      M Offline
      mla154
      wrote on last edited by
      #7

      Clever! :) Regards, Mike

      1 Reply Last reply
      0
      • V Vasily Tserekh

        This is the first descent code I wrote when I began to make programs, it was in C++ Is about an algorithm to arrangle ships in a classic battleship game, first funcion is to check if a ship can be placed at x;y second is for placing the ship and 3th was for placing all the ships. I was so amazed that the code worked that I felt a joy I only felt few times since then. How about you

        //------Check if in square X;Y a ship can be placed------------------

        /* x,y: Board coordinates
        Size: Ship size
        Pos: posicion (1 horizontal vertical)
        board: board number
        */
        bool TGame::Free(int x,int y,int size,int pos,int board )
        {
        int ship[7];
        int side1[7];
        int side2[7];
        bool answer=true;
        if(aBoard[board]->GetInfo(x,y)!=0)
        {
        return answer=false;
        }
        if (pos==1)
        {
        for(int i=0;i<size+2;i++)
        {ship[i]=aBoard[board]->GetInfo(x-1+i,y);
        side1[i]=aBoard[board]->GetInfo(x-1+i,y-1);
        side2[i]=aBoard[board]->GetInfo(x-1+i,y+1);
        }
        }

        else
        { for(int i=0;i<size+2;i++)
        {ship[i]=aBoard[board]->GetInfo(x,y-1+i);
        side1[i]=aBoard[board]->GetInfo(x-1,y-1+i);
        side2[i]=aBoard[board]->GetInfo(x+1,y-1+i);
        }
        }
        if (x==0 && pos==1)
        {ship[0]=0;
        side1[0]=0;
        side2[0]=0;

        }
        for (int i=0;i<size+2;i++)
        {if (ship[i]!= 0)answer=false;
        if (side1[i]!= 0)answer=false;
        if (side2[i]!= 0)answer=false;
        }
        return answer;
        }
        //------Place a ship in the board-------------------------------------

        /* Size: Ship size
        board: Board
        Ship: Place of the ship in the ship array
        Type: ship type
        */
        void TGame:: RandomShip(int size,int type,int board,int ship)
        {
        int pos=random(2);
        randomize();
        int x,y;
        bool answer=false; ;
        if (pos==1)
        {
        while (answer==false)
        {
        x=random(11)-size;
        while(x<0)
        {x=random(11)-size;
        }
        y=random(15);
        answer=Free(x,y,size,pos,board);
        }
        for(int i=0;i<size;i++)
        {aBoard[board]->SetInfo(x+i,y,type);
        }
        }
        else
        {
        while (answer==false)
        {
        x=random(10);
        y=random(16)-size;
        while(y<0)
        {y=random(16)-size;
        }
        answer=Free(x,y,size,pos,board);
        }
        for(int i=0;i<size;i++)
        {aBoard[board]->SetInfo(x,y+i,type);
        }
        }
        if(board==0)
        {
        int ax=Transfo

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #8

        Mine took 7.5 million years to execute and simply said "42". :-\

        K R R 3 Replies Last reply
        0
        • V Vasily Tserekh

          This is the first descent code I wrote when I began to make programs, it was in C++ Is about an algorithm to arrangle ships in a classic battleship game, first funcion is to check if a ship can be placed at x;y second is for placing the ship and 3th was for placing all the ships. I was so amazed that the code worked that I felt a joy I only felt few times since then. How about you

          //------Check if in square X;Y a ship can be placed------------------

          /* x,y: Board coordinates
          Size: Ship size
          Pos: posicion (1 horizontal vertical)
          board: board number
          */
          bool TGame::Free(int x,int y,int size,int pos,int board )
          {
          int ship[7];
          int side1[7];
          int side2[7];
          bool answer=true;
          if(aBoard[board]->GetInfo(x,y)!=0)
          {
          return answer=false;
          }
          if (pos==1)
          {
          for(int i=0;i<size+2;i++)
          {ship[i]=aBoard[board]->GetInfo(x-1+i,y);
          side1[i]=aBoard[board]->GetInfo(x-1+i,y-1);
          side2[i]=aBoard[board]->GetInfo(x-1+i,y+1);
          }
          }

          else
          { for(int i=0;i<size+2;i++)
          {ship[i]=aBoard[board]->GetInfo(x,y-1+i);
          side1[i]=aBoard[board]->GetInfo(x-1,y-1+i);
          side2[i]=aBoard[board]->GetInfo(x+1,y-1+i);
          }
          }
          if (x==0 && pos==1)
          {ship[0]=0;
          side1[0]=0;
          side2[0]=0;

          }
          for (int i=0;i<size+2;i++)
          {if (ship[i]!= 0)answer=false;
          if (side1[i]!= 0)answer=false;
          if (side2[i]!= 0)answer=false;
          }
          return answer;
          }
          //------Place a ship in the board-------------------------------------

          /* Size: Ship size
          board: Board
          Ship: Place of the ship in the ship array
          Type: ship type
          */
          void TGame:: RandomShip(int size,int type,int board,int ship)
          {
          int pos=random(2);
          randomize();
          int x,y;
          bool answer=false; ;
          if (pos==1)
          {
          while (answer==false)
          {
          x=random(11)-size;
          while(x<0)
          {x=random(11)-size;
          }
          y=random(15);
          answer=Free(x,y,size,pos,board);
          }
          for(int i=0;i<size;i++)
          {aBoard[board]->SetInfo(x+i,y,type);
          }
          }
          else
          {
          while (answer==false)
          {
          x=random(10);
          y=random(16)-size;
          while(y<0)
          {y=random(16)-size;
          }
          answer=Free(x,y,size,pos,board);
          }
          for(int i=0;i<size;i++)
          {aBoard[board]->SetInfo(x,y+i,type);
          }
          }
          if(board==0)
          {
          int ax=Transfo

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #9

          Back in the 90s, I built a 3d tennis game in Java. I was so proud of myself seeing the tennis ball bounce around the court. For self-gratification, I did new TennisBall() about 20 times and just had all these balls bouncing around the court. From that point on, I was hooked on writing software. I still enjoy it today.

          My Messianic Jewish blog: Kineti L'Tziyon My software blog: Debugger.Break() Judah Himango

          1 Reply Last reply
          0
          • R Roger Wright

            I guess the first decent code I ever wrote was an operating system and assembler for the Intel 8080 in an Altair 8800 microcomputer. I wasn't all that surprised that it worked, though, as I'd carefully hand crafted all the bytes myself, entering them with 8 toggle switches, so errors were more "expensive" back then.

            Will Rogers never met me.

            G Offline
            G Offline
            Gary Wheeler
            wrote on last edited by
            #10

            Roger Wright wrote:

            entering them with 8 toggle switches

            Ah, the good old days. I did that on a PDP-11/05 at school. The machine had an 80 word bootstrap that was fairly easy to wipe by accident, so you had to fat-finger it in via the front panel switches. I only had to do it once or twice, and it took me a few minutes. Reportedly there was a guy who could do it in less than 60 seconds. If you think about it, though, that implied if you became practiced at it, your code was pretty bad to create the problem in the first place.

            Software Zen: delete this;

            R 1 Reply Last reply
            0
            • V Vasily Tserekh

              This is the first descent code I wrote when I began to make programs, it was in C++ Is about an algorithm to arrangle ships in a classic battleship game, first funcion is to check if a ship can be placed at x;y second is for placing the ship and 3th was for placing all the ships. I was so amazed that the code worked that I felt a joy I only felt few times since then. How about you

              //------Check if in square X;Y a ship can be placed------------------

              /* x,y: Board coordinates
              Size: Ship size
              Pos: posicion (1 horizontal vertical)
              board: board number
              */
              bool TGame::Free(int x,int y,int size,int pos,int board )
              {
              int ship[7];
              int side1[7];
              int side2[7];
              bool answer=true;
              if(aBoard[board]->GetInfo(x,y)!=0)
              {
              return answer=false;
              }
              if (pos==1)
              {
              for(int i=0;i<size+2;i++)
              {ship[i]=aBoard[board]->GetInfo(x-1+i,y);
              side1[i]=aBoard[board]->GetInfo(x-1+i,y-1);
              side2[i]=aBoard[board]->GetInfo(x-1+i,y+1);
              }
              }

              else
              { for(int i=0;i<size+2;i++)
              {ship[i]=aBoard[board]->GetInfo(x,y-1+i);
              side1[i]=aBoard[board]->GetInfo(x-1,y-1+i);
              side2[i]=aBoard[board]->GetInfo(x+1,y-1+i);
              }
              }
              if (x==0 && pos==1)
              {ship[0]=0;
              side1[0]=0;
              side2[0]=0;

              }
              for (int i=0;i<size+2;i++)
              {if (ship[i]!= 0)answer=false;
              if (side1[i]!= 0)answer=false;
              if (side2[i]!= 0)answer=false;
              }
              return answer;
              }
              //------Place a ship in the board-------------------------------------

              /* Size: Ship size
              board: Board
              Ship: Place of the ship in the ship array
              Type: ship type
              */
              void TGame:: RandomShip(int size,int type,int board,int ship)
              {
              int pos=random(2);
              randomize();
              int x,y;
              bool answer=false; ;
              if (pos==1)
              {
              while (answer==false)
              {
              x=random(11)-size;
              while(x<0)
              {x=random(11)-size;
              }
              y=random(15);
              answer=Free(x,y,size,pos,board);
              }
              for(int i=0;i<size;i++)
              {aBoard[board]->SetInfo(x+i,y,type);
              }
              }
              else
              {
              while (answer==false)
              {
              x=random(10);
              y=random(16)-size;
              while(y<0)
              {y=random(16)-size;
              }
              answer=Free(x,y,size,pos,board);
              }
              for(int i=0;i<size;i++)
              {aBoard[board]->SetInfo(x,y+i,type);
              }
              }
              if(board==0)
              {
              int ax=Transfo

              E Offline
              E Offline
              Ennis Ray Lynch Jr
              wrote on last edited by
              #11

              You know you can do this with intersecting vectors right? : )

              Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost "All users always want Excel" --Ennis Lynch

              V 1 Reply Last reply
              0
              • G Gary Wheeler

                Roger Wright wrote:

                entering them with 8 toggle switches

                Ah, the good old days. I did that on a PDP-11/05 at school. The machine had an 80 word bootstrap that was fairly easy to wipe by accident, so you had to fat-finger it in via the front panel switches. I only had to do it once or twice, and it took me a few minutes. Reportedly there was a guy who could do it in less than 60 seconds. If you think about it, though, that implied if you became practiced at it, your code was pretty bad to create the problem in the first place.

                Software Zen: delete this;

                R Offline
                R Offline
                Roger Wright
                wrote on last edited by
                #12

                Hehehe... My bootstrap loader, once I got the whole package punched to paper tape, was only 16 bytes, so it wasn't too laborious to enter. The real challenge was the first time. Power wasn't all that reliable, and it seemed that every time I got close to loading the entire system, the power blinked. Then it would fail while I was trying to punch the image to tape. All in all, it took about 6 days to finally get the first version entered into RAM and successfully punched to tape. After that it was smooth sailing, since I could do maintenance and extensions to the system using my assembler. That was a great feeling! :-D

                Will Rogers never met me.

                G 1 Reply Last reply
                0
                • R Roger Wright

                  Hehehe... My bootstrap loader, once I got the whole package punched to paper tape, was only 16 bytes, so it wasn't too laborious to enter. The real challenge was the first time. Power wasn't all that reliable, and it seemed that every time I got close to loading the entire system, the power blinked. Then it would fail while I was trying to punch the image to tape. All in all, it took about 6 days to finally get the first version entered into RAM and successfully punched to tape. After that it was smooth sailing, since I could do maintenance and extensions to the system using my assembler. That was a great feeling! :-D

                  Will Rogers never met me.

                  G Offline
                  G Offline
                  Gary Wheeler
                  wrote on last edited by
                  #13

                  My step-dad had a COSMAC ELF[^] single-board computer with 2K RAM on board. We fat-fingered in a 1.5K Tiny Basic interpreter (which took forever). He rigged up a battery backup using an old car battery, which kept the thing alive for months at a time.

                  Software Zen: delete this;

                  R 1 Reply Last reply
                  0
                  • E Ennis Ray Lynch Jr

                    You know you can do this with intersecting vectors right? : )

                    Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost "All users always want Excel" --Ennis Lynch

                    V Offline
                    V Offline
                    Vasily Tserekh
                    wrote on last edited by
                    #14

                    yes but that is the same than killing an ant with a bazooka

                    P 1 Reply Last reply
                    0
                    • V Vasily Tserekh

                      yes but that is the same than killing an ant with a bazooka

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #15

                      Splatting an ant can be great fun.

                      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                      A 1 Reply Last reply
                      0
                      • P Pete OHanlon

                        Splatting an ant can be great fun.

                        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                        A Offline
                        A Offline
                        Andrew Rissing
                        wrote on last edited by
                        #16

                        What about the ole' magnifying glass approach? The only downside is that you typically had to look away when it just started to get good.

                        1 Reply Last reply
                        0
                        • L Lost User

                          I was able to boil that down to 3 lines. I'd show you the code, but then I'd have to charge you for it. :-D

                          C Offline
                          C Offline
                          Chris Maunder
                          wrote on last edited by
                          #17

                          The correct comeback should be "I can get it down to 3 lines but there's not enough room in the margin to write it".

                          cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                          L V 2 Replies Last reply
                          0
                          • G Gary Wheeler

                            My step-dad had a COSMAC ELF[^] single-board computer with 2K RAM on board. We fat-fingered in a 1.5K Tiny Basic interpreter (which took forever). He rigged up a battery backup using an old car battery, which kept the thing alive for months at a time.

                            Software Zen: delete this;

                            R Offline
                            R Offline
                            Roger Wright
                            wrote on last edited by
                            #18

                            Gary Wheeler wrote:

                            rigged up a battery backup using an old car battery

                            Great thought! I considered something like that, but the 8080 needed +5, +12, and -12 Vdc, and it just didn't seem practical at the time. In retrospect, it would have been a heck of a lot cheaper than wasting my time for so many days. :doh:

                            Will Rogers never met me.

                            G 1 Reply Last reply
                            0
                            • P PIEBALDconsult

                              Mine took 7.5 million years to execute and simply said "42". :-\

                              K Offline
                              K Offline
                              Keith Barrow
                              wrote on last edited by
                              #19

                              Cap duly doffed!

                              Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                              -Or-
                              A Dead ringer for Kate Winslett[^]

                              1 Reply Last reply
                              0
                              • C Chris Maunder

                                The correct comeback should be "I can get it down to 3 lines but there's not enough room in the margin to write it".

                                cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #20

                                Please give yourself 5points for that ;)

                                MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                                1 Reply Last reply
                                0
                                • C Chris Maunder

                                  The correct comeback should be "I can get it down to 3 lines but there's not enough room in the margin to write it".

                                  cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                                  V Offline
                                  V Offline
                                  Vasily Tserekh
                                  wrote on last edited by
                                  #21

                                  hahaahhah, perfect I wish I could vote you up. That remember me when I was in high school back in my home country, in a very selected high school I pissed a friend of mine because I could write an algorithm finding the pithagoric numbers in less lines than him and he was so frustrated that he began making the hole program in one line, but the line was verrrryyy long,

                                  1 Reply Last reply
                                  0
                                  • R Roger Wright

                                    Gary Wheeler wrote:

                                    rigged up a battery backup using an old car battery

                                    Great thought! I considered something like that, but the 8080 needed +5, +12, and -12 Vdc, and it just didn't seem practical at the time. In retrospect, it would have been a heck of a lot cheaper than wasting my time for so many days. :doh:

                                    Will Rogers never met me.

                                    G Offline
                                    G Offline
                                    Gary Wheeler
                                    wrote on last edited by
                                    #22

                                    My step-dad's a E.E. and a genuine Renaissance man in terms of being handy at these sorts of things.

                                    Software Zen: delete this;

                                    R 1 Reply Last reply
                                    0
                                    • G Gary Wheeler

                                      My step-dad's a E.E. and a genuine Renaissance man in terms of being handy at these sorts of things.

                                      Software Zen: delete this;

                                      R Offline
                                      R Offline
                                      Roger Wright
                                      wrote on last edited by
                                      #23

                                      Yeah, we E.E.s are handy that way. :-D

                                      Will Rogers never met me.

                                      1 Reply Last reply
                                      0
                                      • P PIEBALDconsult

                                        Mine took 7.5 million years to execute and simply said "42". :-\

                                        R Offline
                                        R Offline
                                        Roger Wright
                                        wrote on last edited by
                                        #24

                                        After waiting that long, I bet you can't even remember the question correctly... ;P

                                        Will Rogers never met me.

                                        P 1 Reply Last reply
                                        0
                                        • R Roger Wright

                                          After waiting that long, I bet you can't even remember the question correctly... ;P

                                          Will Rogers never met me.

                                          P Offline
                                          P Offline
                                          PIEBALDconsult
                                          wrote on last edited by
                                          #25

                                          How many whores must a man...? No. How many toads...? Hmmm... I shoulda put it in a comment. :sigh:

                                          R 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups