- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于一项学校作业,我必须创建一个战舰游戏,其中在 8x8 游戏板上随机生成一艘 4 长战舰(水平或垂直)。玩家有 15 枚鱼雷来尝试击沉这艘船。我使用了 2D vector 并完成了大部分代码:
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
using namespace std;
void generateship(vector<vector<int> >&field);
void fire(vector<vector<int> >&field);
void display(const vector<vector<int> >field);
int main()
{
srand(time(0));
vector<vector<int> >field(8);
for (int x = 0; x < field.size(); x++)
field[x].resize(8);
for (int x = 0; x < field.size(); x++)
for (int y = 0; y < field[y].size(); y++)
field[x][y] = 0;
generateship(field);
fire(field);
system("pause");
return 0;
}
void generateship(vector<vector<int> >&field)
{
int row1 = rand() % 8;
int col1 = rand() % 8;
do
{
int row2 = rand() % 3 + (row1 - 1);
int col2 = rand() % 3 + (col1 - 1);
} while (row2 != row1 && col2)
int col3 = rand()
display(field);
}
void fire(vector<vector<int> >&field)
{
int row, col;
int torps = 15;
int hitcounter = 0;
while (hitcounter != 4 || torps != 0)
{
cout << torps << " torpedoes remain. Fire where? ";
cin >> row >> col;
switch (field[row][col])
{
case 0: cout << "Miss!" << endl << endl;
field[row][col] = 2;
break;
case 1: cout << "Hit!" << endl << endl;
field[row][col] = 3;
hitcounter = hitcounter + 1;
break;
case 2: cout << "Missed again!" << endl << endl;
break;
case 3: cout << "Hit again!" << endl << endl;
break;
}
torps = torps - 1;
display(field);
}
if (hitcounter == 4)
cout << "You win!";
else if (torps == 0)
cout << "You are out of torpedoes! Game over.";
}
void display(const vector<vector<int> >field)
{
for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 8; col++)
{
switch (field[row][col])
{
case 0: cout << ". ";
break;
case 1: cout << ". ";
break;
case 2: cout << "X ";
break;
case 3: cout << "O ";
break;
}
}
cout << endl;
}
}
正如您可能看到的那样,我正在为“generateship”功能而苦苦挣扎。我的目标是随机生成一艘完全位于我的 8x8 2D vector 内的 4x1 船(水平或垂直)。任何建议/帮助/意见表示赞赏!
最佳答案
有 2 个随机选择:方向和位置。
方向决定了哪些位置有效,因此最好先随机选择方向(水平或垂直)。
然后随机选择船的左上角位置。如果方向是水平的,x位置应该在0到7-3之间,y位置应该在0到7之间。如果方向是垂直的,y位置应该在0到7-3之间,x位置应该在0到7之间。
顺便说一句,尽量不要对这些数字中的任何一个进行硬编码。最好使用常量,这样您以后就可以轻松更改棋盘和飞船的大小。
关于c++ - 如何在游戏板上随机放置一艘船,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34581707/
我寻求这种性质的东西 //validation rules in model "User" attributes: { age: { required: true,
sailsjs 0.11, Node 0.12.4。使用护照 0.2.1 和本地护照 1.0 使用 sailsjs 蓝图路线只是为了看看发生了什么。该项目只是一个简单的博客,我尝试在蓝图路径 /use
我的 SailsJS 应用程序中有以下模型,我想在字段“room_name”和“school_id”上添加复合唯一键。 我目前所做的是从 mongo 运行这个命令: db.room.ensureIn
我使用的是 sailsjs 0.12。它支持 models 上的 index 属性,也我正在使用 npm 包 Sails-hooks-mongoat创建反向索引等。 这并不理想,但它奏效了。现在,他们
尝试: sails-migrations --env production migrate 返回: Error: connect ECONNREFUSED 我们在端口 3308 下的所有 IP 地址上
我是一名优秀的程序员,十分优秀!