commands/snake/snake.cpp
changeset 95 b3ffff030d5c
parent 83 2a78c4ff2eab
equal deleted inserted replaced
94:8df58d8c99e8 95:b3ffff030d5c
   437 	}
   437 	}
   438 
   438 
   439 void CCmdSnake::PlaceBait()
   439 void CCmdSnake::PlaceBait()
   440 	{
   440 	{
   441 	TBool ok;
   441 	TBool ok;
       
   442 	TInt attempts = 0;
   442 	do
   443 	do
   443 		{
   444 		{
       
   445 		attempts++;
   444 		ok = ETrue;
   446 		ok = ETrue;
   445 		iBait.iX = Math::Rand(iRandomSeed) % iBoardSize.iWidth;
   447 		iBait.iX = Math::Rand(iRandomSeed) % iBoardSize.iWidth;
   446 		iBait.iY = Math::Rand(iRandomSeed) % iBoardSize.iHeight;
   448 		iBait.iY = Math::Rand(iRandomSeed) % iBoardSize.iHeight;
   447 		
   449 		
   448 		if (GetBoard(iBait.iX, iBait.iY)) ok = EFalse;
   450 		if (GetBoard(iBait.iX, iBait.iY)) ok = EFalse;
   451 			for (TInt i=0; i<iSnake.Count(); ++i)
   453 			for (TInt i=0; i<iSnake.Count(); ++i)
   452 				{
   454 				{
   453 				if (iSnake[i] == iBait) ok = EFalse;
   455 				if (iSnake[i] == iBait) ok = EFalse;
   454 				}
   456 				}
   455 			}
   457 			}
       
   458 		if (attempts >= iBoardSize.iWidth * iBoardSize.iHeight) return; // Snake has filled the entire board! Time to bail (the snake will run into itself right after we return here)
   456 		} while (!ok);
   459 		} while (!ok);
   457 	iCons.SetCursorPosAbs(iBait);
   460 	iCons.SetCursorPosAbs(iBait);
   458 	iCons.Write(iUnicode ? KUnicodeBait : KBait);
   461 	iCons.Write(iUnicode ? KUnicodeBait : KBait);
   459 	}
   462 	}
   460 
   463