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.


 
GeneralRe: Chinese hackers! PinmemberMehGerbil11-Jan-13 3:22 
GeneralRe: Chinese hackers! PinmemberMark_Wallace11-Jan-13 3:36 
GeneralRe: Chinese hackers! PinmemberChris Losinger10-Jan-13 11:18 
GeneralRe: Chinese hackers! PinmemberRoger Wright10-Jan-13 12:24 
GeneralRe: Chinese hackers! PinmemberPaul Conrad10-Jan-13 12:54 
GeneralRe: Chinese hackers! PinprotectorAspDotNetDev10-Jan-13 12:33 
GeneralRe: Chinese hackers! PinmemberChris Losinger10-Jan-13 12:52 
GeneralRe: Chinese hackers! PinprotectorAspDotNetDev10-Jan-13 12:58 
GeneralRe: Chinese hackers! PinmemberRoger Wright10-Jan-13 19:01 
GeneralRe: Chinese hackers! PinmemberRoger Wright10-Jan-13 18:59 
GeneralRe: Chinese hackers! PinmemberMark_Wallace10-Jan-13 21:31 
GeneralRe: Chinese hackers! PinmemberMark_Wallace10-Jan-13 21:50 
GeneralRe: Chinese hackers! PinmemberMarco Bertschi11-Jan-13 2:17 
GeneralThe first descent code I wrote in my life PinmemberVasily Tserekh10-Jan-13 7: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 life PinmemberRichard Deeming10-Jan-13 8:11 
GeneralRe: The first descent code I wrote in my life Pinmembermla15410-Jan-13 8:24 
GeneralRe: The first descent code I wrote in my life PinmemberMember 460889812-Jan-13 23:52 
GeneralRe: The first descent code I wrote in my life PinmemberRoger Wright10-Jan-13 8:12 
GeneralRe: The first descent code I wrote in my life PinmemberGary Wheeler10-Jan-13 9:11 
GeneralRe: The first descent code I wrote in my life PinmemberRoger Wright10-Jan-13 9:26 
GeneralRe: The first descent code I wrote in my life PinmemberGary Wheeler10-Jan-13 9:35 
GeneralRe: The first descent code I wrote in my life PinmemberRoger Wright10-Jan-13 10:49 
GeneralRe: The first descent code I wrote in my life PinmemberGary Wheeler10-Jan-13 11:16 
GeneralRe: The first descent code I wrote in my life PinmemberRoger Wright10-Jan-13 11:35 
GeneralRe: The first descent code I wrote in my life PinmemberMathlab10-Jan-13 8:15 
GeneralRe: The first descent code I wrote in my life PinmemberRavi Bhavnani10-Jan-13 8:16 
GeneralRe: The first descent code I wrote in my life PinmemberMehGerbil10-Jan-13 8:24 
GeneralRe: The first descent code I wrote in my life PinadminChris Maunder10-Jan-13 10:44 
GeneralRe: The first descent code I wrote in my life Pinmember_Maxxx_10-Jan-13 11:09 
GeneralRe: The first descent code I wrote in my life PinmemberVasily Tserekh10-Jan-13 11:13 
GeneralRe: The first descent code I wrote in my life PinmemberPIEBALDconsult10-Jan-13 8:56 
GeneralRe: The first descent code I wrote in my life PinmentorKeith Barrow10-Jan-13 11:00 
GeneralRe: The first descent code I wrote in my life PinmemberRoger Wright10-Jan-13 12:29 
GeneralRe: The first descent code I wrote in my life PinmemberPIEBALDconsult10-Jan-13 14:54 
GeneralRe: The first descent code I wrote in my life PinmemberRoger Wright10-Jan-13 18:12 
GeneralRe: The first descent code I wrote in my life PinmemberRanjan.D11-Jan-13 8:53 
GeneralRe: The first descent code I wrote in my life PinmemberJudah Himango10-Jan-13 8:59 
GeneralRe: The first descent code I wrote in my life PinmemberEnnis Ray Lynch, Jr.10-Jan-13 9:19 
GeneralRe: The first descent code I wrote in my life PinmemberVasily Tserekh10-Jan-13 9:51 
GeneralRe: The first descent code I wrote in my life PinprotectorPete O'Hanlon10-Jan-13 10:21 
GeneralRe: The first descent code I wrote in my life PinmemberAndrew Rissing10-Jan-13 10:29 
GeneralRe: The first descent code I wrote in my life PinmemberMark_Wallace10-Jan-13 21:34 
GeneralRe: The first descent code I wrote in my life PinmemberClumpco10-Jan-13 23:24 
AnswerRe: The first descent code I wrote in my life PinmemberJecc11-Jan-13 0:15 
GeneralRe: The first descent code I wrote in my life PinmemberFran Porretto11-Jan-13 1:59 
GeneralRe: The first descent code I wrote in my life Pinmemberpontellen11-Jan-13 4:17 
GeneralRe: The first descent code I wrote in my life PinmemberReese Currie11-Jan-13 4:45 
GeneralRe: The first descent code I wrote in my life PinmemberPIEBALDconsult11-Jan-13 4:58 
GeneralRe: The first descent code I wrote in my life PinmemberReese Currie11-Jan-13 8:32 
GeneralRe: The first descent code I wrote in my life [modified] PinmemberBrainiacV11-Jan-13 4:47 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 23 Nov 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid