Click here to Skip to main content

Welcome to the Lounge

   

For lazing about and discussing anything in a software developer's life that takes your fancy.
The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. No flame wars, no abusive conduct and please don't post ads.

Technical discussions are welcome, but if you need specific help please use the programming forums.


 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page  Show 
GeneralRe: Chinese hackers!protectorAspDotNetDev10 Jan '13 - 11:58 
GeneralRe: Chinese hackers!memberRoger Wright10 Jan '13 - 18:01 
GeneralRe: Chinese hackers!memberRoger Wright10 Jan '13 - 17:59 
GeneralRe: Chinese hackers!memberMark_Wallace10 Jan '13 - 20:31 
GeneralRe: Chinese hackers!memberMark_Wallace10 Jan '13 - 20:50 
GeneralRe: Chinese hackers!memberMarco Bertschi11 Jan '13 - 1:17 
GeneralThe first descent code I wrote in my lifememberVasily Tserekh10 Jan '13 - 6:57 
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=Transform(x);
int ay=Transform(y);
aPosXY[ship].x=ax;
aPosXY[ship].y=ay;
aPosXY[ship].pos=pos;
}
 
}
//----Posiciona todos los barcos en el tablero------------------------------

 void TGame::RandomAllocation(int board)
 {
   RandomShip(5,6,board,0);
   RandomShip(4,5,board,1);
   RandomShip(3,4,board,2);
   RandomShip(3,3,board,3);
   RandomShip(2,2,board,4);
   RandomShip(2,1,board,5);
 }
//---------------------------------------------------------------------------

GeneralRe: The first descent code I wrote in my lifememberRichard Deeming10 Jan '13 - 7:11 
GeneralRe: The first descent code I wrote in my lifemembermla15410 Jan '13 - 7:24 
GeneralRe: The first descent code I wrote in my lifememberMember 460889812 Jan '13 - 22:52 
GeneralRe: The first descent code I wrote in my lifememberRoger Wright10 Jan '13 - 7:12 
GeneralRe: The first descent code I wrote in my lifememberGary Wheeler10 Jan '13 - 8:11 
GeneralRe: The first descent code I wrote in my lifememberRoger Wright10 Jan '13 - 8:26 
GeneralRe: The first descent code I wrote in my lifememberGary Wheeler10 Jan '13 - 8:35 
GeneralRe: The first descent code I wrote in my lifememberRoger Wright10 Jan '13 - 9:49 
GeneralRe: The first descent code I wrote in my lifememberGary Wheeler10 Jan '13 - 10:16 
GeneralRe: The first descent code I wrote in my lifememberRoger Wright10 Jan '13 - 10:35 
GeneralRe: The first descent code I wrote in my lifememberMathlab10 Jan '13 - 7:15 
GeneralRe: The first descent code I wrote in my lifememberRavi Bhavnani10 Jan '13 - 7:16 
GeneralRe: The first descent code I wrote in my lifememberMehGerbil10 Jan '13 - 7:24 
GeneralRe: The first descent code I wrote in my lifeadminChris Maunder10 Jan '13 - 9:44 
GeneralRe: The first descent code I wrote in my lifemember_Maxxx_10 Jan '13 - 10:09 
GeneralRe: The first descent code I wrote in my lifememberVasily Tserekh10 Jan '13 - 10:13 
GeneralRe: The first descent code I wrote in my lifememberPIEBALDconsult10 Jan '13 - 7:56 
GeneralRe: The first descent code I wrote in my lifementorKeith Barrow10 Jan '13 - 10:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 24 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid