【内测版持续更新中】海战游戏
junhaowang · · 个人记录
【1.0 正在内测,火热更新中】海战游戏,有 438 行!!!
扫码栏(为了方便统计,做成了短链接,可能会有警告哦)
注:真是造镍,二维码有时候会炸。
玩法栏
“
我是一个兵
来自老百姓
打败了日本狗强盗
消灭了蒋匪军……
”
最近我的同学发明了一个游戏,叫做海战。于是本小鼠想要用 C++ 来实现这个游戏。
于是就有了这个程序。
这个游戏的规则是这个样子的:
- 每方有一个军舰,一共三滴血,用完三滴血就
\textrm{GAME}\space\textrm{OVER} 了。 - 每方除了军舰还有三个潜艇,攻击所有右上、右下、左上、左下角两步内的对手。
- 也有三个战船,攻击上下左右两步内的所有对手。
- 有放大招机会,击中该点所在行和列的所有对手。
- 潜艇和军舰击中则消除。
- 击中几个目标要告诉对方,击中军舰不告诉对方,扣一滴血(否则就暴露了)。
我把这个东西做得没有很小白化,就很类似于命令提示符的一个东西。
- 一开始布局
\textrm{center} 表示军舰(我喜欢美式的拼写),然后输入行 (小写a 到小写h ) 和 列(1 到8 ),空格隔开。 - 同理还有
\textrm{dive} 潜艇和\textrm{ship} 战船。 - 输入
\textrm{done}\space0\space0 结束初始化。 - 然后开启攻击模式,输入需要启动攻击的战船或者潜艇,输入行号和列号,同上。
-
- 攻击模式输入
\textrm{z}\space0 可以结束,看电脑的棋局。
- 攻击模式输入
-
- 放大招方法:攻击模式输入
\textrm{s}\space0 ,然后再输入要放大招的行号和列号,同上。
- 放大招方法:攻击模式输入
- 布局和攻击过程中会实时显示用户的棋局,蓝色战舰,绿色潜艇,红色战船。
更新栏
-
-
-
-
- 内测发现
\textrm{Bug} :放大招之后电脑不会攻击,而且也没有清屏,原因:直接使用了continue语句,直接进入下一轮。解决办法:使用goto语句直接进入电脑攻击的片段。
- 内测发现
-
- 内测发现
\textrm{Bug} :结果一闪而过,原因:太早执行cls清屏语句。解决办法:去掉清屏语句。
- 内测发现
-
-
- 内测发现
\textrm{Bug} :电脑会攻击两次,原因:如果电脑放大招,还会继续执行普通攻击语句。解决办法:增加continue语句。
- 内测发现
-
- 内测发现
\textrm{Bug} :放大招击中计数只增不减,原因:没有归零。解决办法:增加归零语句。
- 内测发现
-
- 内测发现
\textrm{Bug} :放大招攻击无效,原因:放大招代码中击中后没有设置为0 的代码。解决办法:增加击中设置为0 的代码。
- 内测发现
-
-
- 内测发现
\textrm{Bug} :放大招之后有时不会清屏,有一次玩的时候竟然显示击中两个敌人,一看电脑棋局竟然连战舰都消失了(战舰应该不会打掉,只扣血),只剩下一个战船。
- 内测发现
-
代码栏
请各位大佬发现漏洞及时指正!
#include <bits/stdc++.h>
#include <windows.h>
#pragma GCC optimize (2)
using namespace std;
int
computer [13][13], //电脑的棋盘
user [13][13], //用户的棋盘
cHeart
= 3, //电脑血量
uHeart
= 3, //用户血量
b, //操作列号
cVis [13][13], //是否覆盖
shipgo [8][2] //船攻击区域
= {-1, 0, -2, 0, 1, 0, 2, 0, 0, -1, 0, -2, 0, 1, 0, 2},
divego [8][2] //潜艇攻击区域
= {-1, -1, -2, -2, -1, 1, -2, 2, 1, 1, 2, 2, 1, -1, 1, -2}
;
string
command
;
char
a //操作行号
;
bool
flag //电脑放大招机会
;
void Color (int a)
{
if (a == 0)
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
if (a == 1)
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
if (a == 2)
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
if (a == 3)
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
if (a == 4)
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
if (a == 5)
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
if (a == 6)
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
} //字颜色,白蓝绿紫红靛黄
int main ()
{
//窗口置顶
HWND hWnd = ::GetForegroundWindow ();
::SetWindowPos (hWnd, HWND_TOPMOST, 0, 0, 100, 100, SWP_NOMOVE | SWP_NOSIZE);
Color (0); //颜色初始化
cout << "欢迎来到海战游戏。不规范输入会导致运行出错。现在请初始化你的布局!\n====================\n";
while (true) //用户布局
{
//输出用户棋局
cout << " 1 2 3 4 5 6 7 8\n";
for (int i = 1; i <= 8; i++)
{
cout << (char) ('a' + i - 1) << ' ';
for (int j = 1; j <= 8; j++)
{
switch (user [i][j])
{
case 1:
Color (5);
break;
case 2:
Color (2);
break;
case 3:
Color (3);
break;
case 0:
Color (0);
}
cout << "●";
}
Color (0);
cout << endl;
}
Color (0); //颜色恢复
cout << "init>>> ";
cin >> command >> a >> b;
if (command == "center")
{
user [a - 'a' + 1][b] = 1;
}
else if (command == "dive")
{
user [a - 'a' + 1][b] = 2;
}
else if (command == "ship")
{
user [a - 'a' + 1][b] = 3;
}
else if (command == "done")
{
break;
}
system ("cls");
}
cout << "初始化成功!电脑正在初始化……";
srand ((unsigned) time (NULL)); //随即种子
//电脑随即初始化
int //军舰位置
ax
= rand () % 9,
ay
= rand () % 9
;
computer [ax][ay] = 1;
cVis [ax][ay] = true;
for (int i = 1; i <= 3; i++)
{
int
xx
= rand () % 9,
yy
= rand () % 9
;
while (cVis [xx][yy]) //避免重复
{
xx = rand () % 9;
yy = rand () % 9;
}
cVis [xx][yy] = true;
computer [xx][yy] = 2;
}
for (int i = 1; i <= 3; i++)
{
int
xx
= rand () % 9,
yy
= rand () % 9
;
while (cVis [xx][yy])
{
xx = rand () % 9;
yy = rand () % 9;
}
cVis [xx][yy] = true;
computer [xx][yy] = 3;
}
Sleep (1000); //制造等待效果
system ("cls");
cout << "\n初始化成功!接下来请进行攻击:\n";
while (cHeart > 0 && uHeart > 0) //没有出就局循环
{
//输出用户棋局
cout << " 1 2 3 4 5 6 7 8\n";
for (int i = 1; i <= 8; i++)
{
cout << (char) ('a' + i - 1) << ' ';
for (int j = 1; j <= 8; j++)
{
switch (user [i][j])
{
case 1:
Color (5);
break;
case 2:
Color (2);
break;
case 3:
Color (3);
break;
case 0:
Color (0);
}
cout << "●";
}
Color (0);
cout << endl;
}
Color (0); //颜色恢复
cout << "attack>>> ";
int
cnt
= 0
;
cin >> a >> b;
if (a == 'z')
{
break;
}
else if (a == 's')
{
cout << "super attack>>> ";
cin >> a >> b;
for (int i = 1; i <= 8; i++)
{
switch (computer [i][b])
{
case 2: case 3:
cnt++;
computer [i][b] = 0;
break;
case 1:
cHeart--;
break;
}
}
for (int j = 1; j <= 8; j++)
{
switch (computer [a - 'a' + 1][j])
{
case 2: case 3:
cnt++;
computer [a - 'a' + 1][j] = 0;
break;
case 1:
cHeart--;
break;
}
}
if (cnt)
{
cout << "你打中了" << cnt << "个!\n";
}
else
{
cout << "你没打中!\n";
}
goto computerAttack;
}
switch (user [a - 'a' + 1][b]) //判断类型
{
case 2:
for (int i = 0; i < 8; i++)
{
int
xx
= a - 'a' + 1 + divego [i][0],
yy
= b + divego [i][1]
; //攻击范围定位
switch (computer [xx][yy])
{
case 1:
cHeart--;
break; //打中战舰扣血,不算打中
case 2: case 3:
cnt++;
computer [xx][yy] = 0;
break;
}
}
break;
case 3:
for (int i = 0; i < 8; i++)
{
int
xx
= a - 'a' + 1 + shipgo [i][0],
yy
= b + shipgo [i][1]
; //攻击范围定位
switch (computer [xx][yy])
{
case 1:
cHeart--;
break; //打中战舰扣血,不算打中
case 2: case 3:
cnt++;
computer [xx][yy] = 0;
break;
}
}
break;
}
if (cnt)
{
cout << "你打中了" << cnt << "个!\n";
}
else
{
cout << "你没打中!\n";
}
computerAttack:
if (rand () % 2 && !flag) //随机大招
{
cnt = 0;
flag = true;
int
cxx
= rand () % 9,
cyy
= rand () % 9
;
for (int i = 1; i <= 8; i++)
{
switch (user [i][cyy])
{
case 2: case 3:
cnt++;
user [i][cyy] = 0;
break;
case 1:
uHeart--;
break;
}
}
for (int j = 1; j <= 8; j++)
{
switch (user [cxx][j])
{
case 2: case 3:
cnt++;
user [cxx][j] = 0;
break;
case 1:
uHeart--;
break;
}
}
if (cnt)
{
cout << "电脑打中了" << cnt << "个!\n";
}
else
{
cout << "电脑没打中!\n";
}
continue;
}
int
cxx
= rand () % 9,
cyy
= rand () % 9
;
while (!computer [cxx][cyy] || computer [cxx][cyy] == 1)
{
cxx = rand () % 9;
cyy = rand () % 9;
}
cnt = 0;
//电脑攻击,原理同上
switch (computer [cxx][cyy])
{
case 2:
for (int i = 0; i < 8; i++)
{
int
xx
= cxx + divego [i][0],
yy
= cyy + divego [i][1]
;
switch (user [xx][yy])
{
case 1:
uHeart--;
break;
case 2: case 3:
user [xx][yy] = 0;
cnt++;
break;
}
}
break;
case 3:
for (int i = 0; i < 8; i++)
{
int
xx
= cxx + shipgo [i][0],
yy
= cyy + shipgo [i][1]
;
switch (user [xx][yy])
{
case 1:
uHeart--;
break;
case 2: case 3:
user [xx][yy] = 0;
cnt++;
break;
}
}
break;
}
if (!cnt)
{
cout << "电脑没打中!";
}
else
{
cout << "电脑打中了" << cnt << "个!";
}
Sleep (3000); //等一会儿再清屏
system ("cls"); //清屏
}
cout << "GAME OVER!";
if (!cHeart)
{
cout << "恭喜玩家胜利!";
}
else if (!uHeart)
{
cout << "电脑胜利!";
}
else
{
cout << "现在您可以查看电脑棋局!\n";
cout << " 1 2 3 4 5 6 7 8\n";
for (int i = 1; i <= 8; i++)
{
cout << (char) ('a' + i - 1) << ' ';
for (int j = 1; j <= 8; j++)
{
switch (computer [i][j])
{
case 1:
Color (5);
break;
case 2:
Color (2);
break;
case 3:
Color (3);
break;
case 0:
Color (0);
}
cout << "●";
}
Color (0);
cout << endl;
}
}
system ("pause"); //按任意键继续
return 0; //好习惯
}