Пример #1
0
void CGamePolePlayer::setShips()    /*************************/
{
   short x, y;
   bool vertical;

   for( size_t size = BIGGEST_SHIP_SIZE; size > 0; size-- )
   {
      for( size_t j = 0; j <= BIGGEST_SHIP_SIZE-size; j++ )
      {
         do
         {
            vertical = rand()%2;
            if( vertical )
            {
               x = rand() % (GAME_POLE_SIZE+1-size);
               y = rand() % 10;
            }
            else
            {
               x = rand() % 10;
               y = rand() % (GAME_POLE_SIZE+1-size);
            }
         }
         while( !canSetShip( x, y, vertical, size ) );

         if( vertical )
         {
            for( size_t i = 0; i < size; i++ )
            {
               setValue( x+i, y, SHIP_NON_SHOOTED );
            }
         }
         else
         {
            for( size_t i = 0; i < size; i++ )
            {
               setValue( x, y+i, SHIP_NON_SHOOTED );
            }
         }
      }
   }
}
void Game_functions::ship_menu(string type, string IP_ADDR){
	string input;
	cout << "Hi, What's your name?:";
	cin >> player_name;
	showBoard();
	cout << "Nice to meet you " << player_name << endl;
	cout << "Say me something to start" << endl;
	cin >> input;
	cout << "Choose the position for your ships" << endl;

	do{
		showBoard();
		cout << "#1 Aircraft" << endl;

		do{
			cout << "Letter: ";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> aircraft_letter;
		} while(getLetter(aircraft_letter)==666);

		do{
			cout << "Number: ";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> aircraft_number;
		} while(!cin || aircraft_number>24 || aircraft_number<0);

		do{
			cout << "Pos (v for vertical, h for horizontal):";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> aircraft_pos;
		} while(!cin || verPos(aircraft_pos)==false);
	} while(canSetShip(getLetter(aircraft_letter), aircraft_number, Aircraft, aircraft_pos)==false);

	setShip(Aircraft, aircraft_pos, aircraft_letter, aircraft_number);
	
	do{
		showBoard();
		cout << "#1 Battleship" << endl;
		do{
			cout << "Letter: ";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> battleship1_letter;
		} while(getLetter(battleship1_letter)==666);

		do{
			cout << "Number: ";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> battleship1_number;
		} while(!cin || battleship1_number>24 || battleship1_number<0);

		do{
			cout << "Pos (v for vertical, h for horizontal):";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> battleship1_pos;
		} while(!cin || verPos(battleship1_pos)==false);
	} while(canSetShip(getLetter(battleship1_letter), battleship1_number, Battleship, battleship1_pos)==false);

	setShip(Battleship, battleship1_pos, battleship1_letter, battleship1_number);

	do{
		showBoard();
		cout << "#2 Battleship" << endl;
		do{
			cout << "Letter: ";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> battleship2_letter;
		} while(getLetter(battleship2_letter)==666);

		do{
			cout << "Number: ";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> battleship2_number;
		} while(!cin || battleship2_number>24 || battleship2_number<0);

		do{
			cout << "Pos (v for vertical, h for horizontal):";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> battleship2_pos;
		} while(!cin || verPos(battleship2_pos)==false);
	} while(canSetShip(getLetter(battleship2_letter), battleship2_number, Battleship, battleship2_pos)==false);

	setShip(Battleship, battleship2_pos, battleship2_letter, battleship2_number);

	do{
		showBoard();
		cout << "#1 Cruiser" << endl;

		do{
			cout << "Letter: ";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> cruiser_letter;
		} while(getLetter(cruiser_letter)==666);

		do{
			cout << "Number: ";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> cruiser_number;
		} while(!cin || cruiser_number>24 || cruiser_number<0);

		do{
			cout << "Pos (v for vertical, h for horizontal):";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> cruiser_pos;
		} while(!cin || verPos(cruiser_pos)==false);
	} while(canSetShip(getLetter(cruiser_letter), cruiser_number, Cruiser, cruiser_pos)==false);

	setShip(Cruiser, cruiser_pos, cruiser_letter, cruiser_number);

	do{
		showBoard();
		cout << "#1 Frigate" << endl;

		do{
			cout << "Letter: ";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> frigate1_letter;
		} while(getLetter(frigate1_letter)==666);

		do{
			cout << "Number: ";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> frigate1_number;
		} while(!cin || frigate1_number>24 || frigate1_number<0);

		do{
			cout << "Pos (v for vertical, h for horizontal):";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> frigate1_pos;
		} while(!cin || verPos(frigate1_pos)==false);
	} while(canSetShip(getLetter(frigate1_letter), frigate1_number, Frigate, frigate1_pos)==false);

	setShip(Frigate, frigate1_pos, frigate1_letter, frigate1_number);

	do{
		showBoard();
		cout << "#1 Submarine" << endl;

		do{
			cout << "Letter: ";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> submarine_letter;
		} while(getLetter(submarine_letter)==666);

		do{
			cout << "Number: ";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> submarine_number;
		} while(!cin || submarine_number>24 || submarine_number<0);

		do{
			cout << "Pos (v for vertical, h for horizontal):";
			cin.clear();
			cin.ignore(256, '\n');
			cin >> submarine_pos;
		} while(!cin || verPos(submarine_pos)==false);
	} while(canSetShip(getLetter(submarine_letter), submarine_number, Submarine, submarine_pos)==false);

	setShip(Submarine, submarine_pos, submarine_letter, submarine_number);
	clear_screen();
	cout << "Let's start the grid..." << endl;
	
	if (type=="server"){
		nett.make_server();
	} else if(type=="client"){
		nett.connect_to_server(IP_ADDR.c_str());
	}

}