示例#1
0
文件: functions.cpp 项目: thomas7/hw5
void promptBattleship(QString grid[10][10]) {
    cout << "Enter the coordinate for your Battleship" << endl;
    QString coord;
    do {
        cin >> coord;
    } while (!checkValidCoord(coord) | !placeShip(grid, coord));
    return;
}
示例#2
0
文件: functions.cpp 项目: thomas7/hw5
void promptAircraftCarrier(QString grid[10][10]) {
    cout << "Enter the coordinate for your Aircraft Carrier" << endl;
    QString coord;
    do {
        cin >> coord;
    } while (!checkValidCoord(coord) | !placeShip(grid, coord));
    return;
}
示例#3
0
/*************************************************************************
* Function name: placeShips
* The Input: -
* The output: -
* The Function operation: handles placing all the ships on player's board
*************************************************************************/
void Player::placeShips() {
	const int SHIP_NUM = 6;

	// Go over the ships and choose place for them
	for (int i = 1; i <= SHIP_NUM; ++i) {
		placeShip(i);
	}
}
示例#4
0
void interactivePlaceShip(PLAYER *plr, char* shipName, int size)
{
	COORDINATE target;
	char direction;
	bool placed=false;

	printGrid(plr->board);

	while(!placed)
	{
		printf("Where is the front of your %s? ", shipName);
		inputCoord(&target);
		printf("Which direction is the rest of it? (n,e,w,s):");
		scanf(" %c", &direction);
		placed = placeShip(size, &target, direction, plr);
	}
}
void promptDestroyer(QString grid[10][10]) {
    cout << "Enter the coordinate for your Destroyer" << endl;
    QString coord;
    do {
        cin >> coord;
    } while (!checkValidCoord(coord) | !placeShip(grid, coord));


    cout << "Enter the second coordinate for your Destroyer" << endl;
    
    QString coord1;
	coord1 = coord;
    do {
        cin >> coord;
    } while (!checkValidCoordDestroyer(coord, coord1) | !placeShip2(grid, coord,coord1));   
    
    
    return;
}
void promptAircraftCarrier(QString grid[10][10]) {
    cout << "Enter the coordinate for your Aircraft Carrier" << endl;
    QString coord;
    do {
        cin >> coord;
    } while (!checkValidCoord(coord) | !placeShip(grid, coord)); //check already existing point in placeShip


    cout << "Enter the second coordinate for your Aircraft Carrier" << endl;
    
    QString coord1;
	coord1 = coord;
    do {
        cin >> coord;
    } while (!checkValidCoordAircraftCarrier(coord, coord1) | !placeShip2(grid, coord,coord1));
    
    
    return;
}
示例#7
0
void MapWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if (state == Edit) {
        if (event->button() == Qt::RightButton ||
            event->button() == Qt::MidButton
        ) {
            rotation = rotation == Qt::Horizontal
                                   ? Qt::Vertical : Qt::Horizontal;
        } else if (event->button() == Qt::LeftButton) {
            if (possiblePlaceShip(event->x(), event->y()))
                placeShip(event->x(), event->y());
        }
    } else if (state == Turn) {
        if (event->button() == Qt::LeftButton) {
            int i = event->y() / CELL_WIDTH;
            int j = event->x() / CELL_WIDTH;
            if (i >= 0 && j >= 0 && i < MAX_CELLS && j < MAX_CELLS)
                emit attack(i, j);
        }
    }
}
示例#8
0
void Game::placeLoop(bool p)
{
	printf("\nInput string should be formatted like \"v,y,x\" where:");
	printf("\nv is whether the ship should be vertical: y for yes, n for no");
	printf("\ny is a character between a-j");
	printf("\nx is an integer between 1-10");
	printf("\nAll input should be lowercase with commas for seperating\n");

	bool valid;

	//loop for all ships
	//placeShip(2, 'd', false, 0, p);

	char input[7] = "";
	//input[0] = y/n verticalness
	//input[2] = y-value
	//input[4] = x-value (subtract 48)
	//input[6] = '\0'

	for (int i(0); i < 5; i++)
	{
		//Start validation do-while
		do{
			//Ask the user
			printf("\nPlease input placement for your ");
			printf((p ? p1 : p2).getPieces()[i].getShipName());
			printf(": ");

			//(re)set valid
			valid = true;

			//Input from user
			scanf_s("%6s", input, 7);

			//Validate user input
			//Example input string: "y,d,10"
			if (!(input[0] == 'y' || input[0] == 'n'))
			{
				printf("\nERROR: Input for v must be either y or n");
				valid = false;
			}

			else if (!(input[2] >= 97 && input[2] <= 106))
			{
				printf("\nERROR: Input for y must be between a-j");
				valid = false;
			}

			else if (!(input[4] >= 48 && input[4] <= 57))
			{
				printf("\nERROR: Input for x must be between 1-10");
				valid = false;
			}

			else if (input[4] >= 49 && !(input[5] == '0' || input[5] == '\0'))
			{
				printf("\nERROR: Input for x must be between 1-10");
				valid = false;
			}

			//Validate ship placement
			if (valid)
				valid = (p ? p1 : p2).checkPlacement((input[5] == '0' ? 10 : input[4] - 48), input[2], input[0] == 'y', (p ? p1 : p2).getPieces()[i]);

			if (!valid)
				printf("\nERROR: Invalid placement");

			//End validation do-while
		} while (!valid);

		//Place ship
		placeShip((input[5] == '0' ? 10 : input[4] - 48), input[2], input[0] == 'y', i, p);
	}
}
示例#9
0
GameView::GameView(QWidget *parent){
    // Сглаживание
    this -> setRenderHint(QPainter::Antialiasing);

    // Количество игроков
    count_players = 2;

    // Количество действий
    // X A W S D Tab Q E Space
    count_actions = 9;

    // Забиваем вектор из действий
    if ( count_players > 0 ){
        QVector<int> temp = {
            Qt::Key_X, Qt::Key_A, Qt::Key_W, Qt::Key_S, Qt::Key_D,
            Qt::Key_Tab, Qt::Key_Q, Qt::Key_E, Qt::Key_Space
        };
        controls.push_back(temp);
    }

    if ( count_players > 1 ){
        QVector<int> temp = {
            Qt::Key_2, Qt::Key_4, Qt::Key_8, Qt::Key_5, Qt::Key_6,
            Qt::Key_Plus, Qt::Key_7, Qt::Key_9, Qt::Key_0
        };
        controls.push_back(temp);
    }


    // Инициализируем переменную fps
    fps = 120;

    // Создаем графическую сцену
    scene = new QGraphicsScene(this);
    scene -> setSceneRect(-1, -1, screen_size.x()+1, screen_size.y()+1);

    // Ставим фон сцены
    QImage * img = new QImage(":/images/bg_1_1.png");
    resizeImage(img, QSize(1920.0*resize_factor, 1080.0*resize_factor));
    QBrush bg_brush(*img);
    scene -> setBackgroundBrush(bg_brush);

    // Добавляем границы сцены
    addBorder();

    // Ставим нашу сцену
    setScene(scene);

    // Отключение полос прокрутки
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    // Ставим заголовок окна
    setWindowTitle("Space Wars");

    // Добавление игрока 1
    player1 = new Ship(1, 1, 3, 1);
    placeShip(player1,
        scene -> width() / 2.0 - player1 -> boundingRect().width(),
        scene -> height() / 2.0 - player1 -> boundingRect().height() / 2.0
    );

    // Добавление игрока 2
    player2 = new Ship(1, 2, 4, 1);
    placeShip(player2,
        scene -> width() / 2.0,
        scene -> height() / 2.0 - player1 -> boundingRect().height() / 2.0
    );

    // Добавление мобов
    /*
    for (int j = 2; j <= 2; ++j)
        for (int i = 1; i <= 4; ++i){
            Ship * ship = new Ship(1, j, i, qrand()%4+1);
            npcs.insert(ship);
            placeShip(ship,
                qrand()%qRound(scene -> width()- ship -> boundingRect().width() ),
                qrand()%qRound(scene -> height()- ship -> boundingRect().height() )
            );
        }
    */

    // Главный таймер
    QTimer * timer = new QTimer();
    connect(timer, SIGNAL(timeout()), this, SLOT(mainTimer()));
    timer -> start(1000.0 / fps);
}