C++游戏

1.贪吃蛇

AC代码

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#include <string.h>
// 定义标记上下左右的明示常量 
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
#define ESC 5 
#define FOOD 10
// 定义表示位置的结构体类型
typedef struct snake{
int x;
int y;
struct snake *next;
}snake;
// 定义全局变量 
int score = 0; // 当前得分
int speed = 200; // 存储当前速度
int status;
snake *tail, *head; // 存储蛇头蛇尾 
snake *food, *q;// q用于遍历链表 
HANDLE hOUT;
void gotoxy(int x, int y); // 设置光标位置 
int choice(void); // 载入游戏界面
int color(int c); // 设置字体颜色 
void printGame(void); // 打印游戏界面 
void printSnake(void); // 打印蛇身
void printFood(void); // 打印食物
void printTips(void); // 打印提示
void snakeMove(void); // 主操作函数 
int biteSelf(void); // 判断是否咬到了自己
int encounterWall(void); // 判断是否撞墙 
void keyboardControl(void); // 获取击键 
void speedUp(void); // 加速
void speedDown(void); // 减速
int endGame(void); // 结束函数; 
char *s_gets(char *st, int n); // 读取字符
void frees(snake *); // 释放内存 
int main(int argc, char *argv[]){
while (1)
{
if (choice() == 1)
keyboardControl();
else
{
gotoxy(5, 15);
printf("按任意键返回");
getchar(); // 去除前一个前导换行 
while (1)
{
if (getchar())
{
system("cls");
break;
}
}
}
}
frees(head);
return 0;
}
void gotoxy(int x, int y)
{
COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
int choice(void)
{
int yourchoice;
// 画出界面 
gotoxy(35, 5);
color(11);
printf("\t贪吃蛇大作战\n");
printf("\n\n");
color(13);
printf("\t\t★★★★★★★★  Snake!");
printf("\t\t★★★★★★★★  Snake!");
gotoxy(25, 15);
color(12);
printf("1.进入游戏\t2.查看说明\t3.退出游戏\n");
color(11);
printf("请选择:");
scanf("%d", &yourchoice);
switch (yourchoice)
{
case 1:
system("cls");
// 初始化 
printGame();
printSnake();
printFood();
break;
case 2:
system("cls");
printTips();
break;
case 3:
system("cls");
gotoxy(30, 10);
color(11);
printf("Bye!");
exit(0);
default:
system("cls");
printf("没有此序号,请输入1,2或3\n");
Sleep(2000);
system("cls");
}
return yourchoice;
}
int color(int c)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);        //更改文字颜色
return 0;
}
void printGame()
{
int i, j;
gotoxy(5, 5);
printf("游戏载入中...请稍后");
Sleep(2000);
system("cls");
// 打印上下界面
for (i = 0; i <= 50; i += 2)
{
gotoxy(i, 0);
printf("□");
gotoxy(i, 25);
printf("□");
}
// 打印左右界面
for (i = 0; i <= 25; i += 1)
{
gotoxy(0, i);
printf("□");
gotoxy(50, i);
printf("□");
}
// 打印中间网格
for (i = 1; i <= 24; i += 1)
{
for (j = 2; j <= 48; j += 2)
{
gotoxy(j, i);
color(11);
printf("■");
}
}
// 打印右侧的规则和计分栏
gotoxy(60, 13);
printf("当前分数:%d分,当前速度%d", score, speed);
gotoxy(60, 15);
printf("用↑ ↓ ← →分别控制蛇的移动\n");
gotoxy(60, 18);
printf("每次获取食物加10分  按下F1加速,F2减速,空格暂停\n");
gotoxy(60, 20);
printf("不能撞墙和咬到自己!");
gotoxy(60, 22);
printf("速度不低于100,不高于300");
}
void printSnake(void)
{
int i;
// 设定蛇尾(16,13),头插入,初始向右 
tail = (snake*)malloc(sizeof(snake));
tail->x = 16;
tail->y = 13;
tail->next = NULL;
// 设定初始蛇长是4
for (i = 1; i <= 4; i++)
{
head = (snake*)malloc(sizeof(snake));
head->next = tail;
head->x = 16 + 2 * i;
head->y = 13;
tail = head; // 头成为尾
}
// 输出蛇身
while (tail->next)
{
gotoxy(tail->x, tail->y);
color(14);
printf("★");
tail = tail->next;
}
}
void printFood(void)
{
srand((unsigned)time(NULL)); // 利用时钟修改种子 
food = (snake*)malloc(sizeof(snake));
food->x = 1; // 初始化x坐标 
while (food->x % 2 && food->x)
{
food->x = rand() % 46 + 2;// 2-48 
}
food->y = rand() % 23 + 1; // 1-24 
q = head; // 不改变头遍历链表 
while (q->next)
{
if (q->x == food->x && q->y == food->y)
{
free(food);
printFood();
}
else
{
gotoxy(food->x, food->y);
color(12);
printf("●");
break;
}
}
}
void printTips(void)
{
color(11);
printf("***********Tips************\n");
printf("1.采用合理的速度可以获得更高的分数哦!\n");
printf("2.一定不要撞到自己或者两边的墙!\n");
printf("3.游戏过程中按ESC退出游戏!\n");
}
void snakeMove(void)
{
snake *snakenext;
snakenext = (snake*)malloc(sizeof(snake));
if (biteSelf())
{
gotoxy(60, 11);
printf("咬到自己啦!");
free(snakenext);
Sleep(1500);
system("cls");
exit(0);
}
else if (encounterWall())
{
gotoxy(60, 11);
printf("撞到墙啦!");
free(snakenext);
Sleep(1500);
system("cls");
exit(0);
}
else
{
// 前两个条件判断完成才开始移动 
Sleep(350 - speed);
if (status == UP)
{
snakenext->x = head->x;
snakenext->y = head->y - 1;
snakenext->next = head;
head = snakenext;
q = head;
if (snakenext->x == food->x && snakenext->y == food->y)
{
while (q)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q = q->next;
}
score += FOOD;
gotoxy(60, 13);
printf("当前分数:%d分,当前速度%d", score, speed);
printFood();
}
else
{
while (q->next->next)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q = q->next;
}
gotoxy(q->next->x, q->next->y);
color(11);
printf("■");
free(q->next);
q->next = NULL;
}
}
else if (status == DOWN)
{
snakenext->x = head->x;
snakenext->y = head->y + 1;
snakenext->next = head;
head = snakenext;
q = head;
if (snakenext->x == food->x && snakenext->y == food->y)
{
while (q)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q = q->next;
}
score += FOOD;
gotoxy(60, 13);
printf("当前分数:%d分,当前速度%d", score, speed);
printFood();
}
else
{
while (q->next->next)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q = q->next;
}
gotoxy(q->next->x, q->next->y);
color(11);
printf("■");
free(q->next);
q->next = NULL;
}
}
else if (status == LEFT)
{
snakenext->x = head->x - 2;
snakenext->y = head->y;
snakenext->next = head;
head = snakenext;
q = head;
if (snakenext->x == food->x && snakenext->y == food->y)
{
while (q)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q = q->next;
}
score += FOOD;
gotoxy(60, 13);
printf("当前分数:%d分,当前速度%d", score, speed);
printFood();
}
else
{
while (q->next->next)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q = q->next;
}
gotoxy(q->next->x, q->next->y);
color(11);
printf("■");
free(q->next);
q->next = NULL;
}
}
else if (status == RIGHT)
{
snakenext->x = head->x + 2;
snakenext->y = head->y;
snakenext->next = head;
head = snakenext;
q = head;
if (snakenext->x == food->x && snakenext->y == food->y)
{
while (q)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q = q->next;
}
score += FOOD;
gotoxy(60, 13);
printf("当前分数:%d分,当前速度%d", score, speed);
printFood();
}
else
{
while (q->next->next)
{
gotoxy(q->x, q->y);
color(14);
printf("★");
q = q->next;
}
gotoxy(q->next->x, q->next->y);
color(11);
printf("■");
free(q->next);
q->next = NULL;
}
}
}
}
int biteSelf(void)
{
int x = 0; // 默认未咬到自己
q = head->next;
// 遍历蛇身 
while (q->next)
{
if (q->x == head->x && q->y == head->y)
{
x = 1;
}
q = q->next;
}
return x;
}
int encounterWall(void)
{
int x = 0; // 默认未撞到墙
if (head->x == 0 || head->x == 50 || head->y == 0 || head->y == 25)
x = 1;
return x;
}
void keyboardControl(void)
{
status = RIGHT; // 初始蛇向右移动
while (1)
{
if (GetAsyncKeyState(VK_UP) && status != DOWN) // GetAsyncKeyState函数用来判断函数调用时指定虚拟键的状态
{
status = UP;           //如果蛇不是向下前进的时候,按上键,执行向上前进操作
}
else if (GetAsyncKeyState(VK_DOWN) && status != UP) // 如果蛇不是向上前进的时候,按下键,执行向下前进操作
{
status = DOWN;
}
else if (GetAsyncKeyState(VK_LEFT) && status != RIGHT) // 如果蛇不是向右前进的时候,按左键,执行向左前进
{
status = LEFT;
}
else if (GetAsyncKeyState(VK_RIGHT) && status != LEFT) // 如果蛇不是向左前进的时候,按右键,执行向右前进
{
status = RIGHT;
}
if (GetAsyncKeyState(VK_SPACE))// 空格暂停 
{
while (1)
{
Sleep(300);
if (GetAsyncKeyState(VK_SPACE)) // 再次按空格改变状态 
{
break;
}
}
}
else if (GetAsyncKeyState(VK_ESCAPE))
{
status = ESC; // 按esc键,直接到结束界面
if (endGame())
{
Sleep(500);
system("cls");
break;
}
}
else if (GetAsyncKeyState(VK_F1)) // 按F1键,加速
{
speedUp();
gotoxy(60, 13);
printf("当前分数:%d分,当前速度%d", score, speed);
}
else if (GetAsyncKeyState(VK_F2)) // 按F2键,减速
{
speedDown();
gotoxy(60, 13);
printf("当前分数:%d分,当前速度%d", score, speed);
}
snakeMove();
}
}
void speedUp(void)
{
if (speed <= 280)
speed += 20;
}
void speedDown(void)
{
if (speed >= 120)
speed -= 20;
}
int endGame(void)
{
char x = 0;
char judge[5];
getchar();
gotoxy(60, 9);
printf("确定退出吗?(Yes/No)");
gotoxy(60, 11);
s_gets(judge, 5);
if (strcmp(judge, "Yes") == 0)
{
Sleep(250);
system("cls");
gotoxy(40, 11);
printf("\tBye!");
x = 1;
}
else
x = 0;
return x;
}
char *s_gets(char *st, int n)
{
char *ret_val;
char *find;
gotoxy(60, 11);
ret_val = fgets(st, n, stdin);
if (ret_val)
{
find = strchr(st, '\n');
if (find)
*find = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
void frees(snake *s)
{
snake *current = s;
while (current)
{
current = s;
s = current->next;
free(current);
}
}

2.俄罗斯方块

AC代码

#include<iostream>
#include<math.h>
#include<Windows.h>
#include<conio.h>
#include<ctime>
using namespace std;
enum DIR
{
UP,
RIGHT,
DOWN,
LEFT
};
time_t start = 0, finish = 0;
int _x = 6, _y = 1;//图形生成位置
int map[30][16] = { 0 };
int sharp[20][8] = {
{0,0,0,0,0,0,0,0},
//I形
{0,0,0,1,0,2,0,3},
{0,0,1,0,2,0,3,0},
//■形
{0,0,1,0,0,1,1,1},
//L形
{0,0,0,1,0,2,1,2},
{0,0,0,1,1,0,2,0},
{0,0,1,0,1,1,1,2},
{0,1,1,1,2,0,2,1},
//J形
{0,2,1,0,1,1,1,2},
{0,0,0,1,1,1,2,1},
{0,0,0,1,0,2,1,0},
{0,0,1,0,2,0,2,1},
//Z形
{0,0,1,0,1,1,2,1},
{0,1,0,2,1,0,1,1},
//S形
{0,1,1,0,1,1,2,0},
{0,0,0,1,1,1,1,2},
//T形
{0,1,1,0,1,1,2,1},
{0,0,0,1,0,2,1,1},
{0,0,1,0,1,1,2,0},
{0,1,1,0,1,1,1,2}
};
class Game
{
public:
int score;//游戏分数
int _id;//图形编号
int top;//最高点高度
int speed;//下落速度
Game();
void showMenu();//显示菜单
void showGround();//显示游戏界面
void gameOver();//游戏结束界面
void Run();//运行游戏
void sharpDraw(int id, bool show = false);//绘制图形
void keyControl();//键盘控制
bool move(int dir, int id);//移动判断
bool downSet(int id);//下落
void Turn(int id);//旋转
void clean();//消行
};
void SetPos(int i, int j)//控制光标位置, 列, 行
{
COORD pos = { i,j };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
int main()
{
CONSOLE_CURSOR_INFO cursor;
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
cursor.bVisible = 0;	//这四行用来设置光标不显示
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
srand((unsigned)time(NULL));
Game game;
game.showMenu();
return 0;
}
Game::Game()
{
score = 0;
_id = 0;
top = 58;
speed = 1000;
}
void Game::showMenu()
{
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 26; j++)
{
if ((i == 0 || i == 29) || (j == 0 || j == 25))
{
cout << "■";
}
else
{
cout << "  ";
}
}
cout << endl;
}
SetPos(17, 8);
cout << "俄 罗 斯 方 块" << endl;
SetPos(13, 12);
cout << "↑旋转方块  ↓加速下滑" << endl;
SetPos(12, 14);
cout << "← →左右移动  空格  暂停" << endl;
SetPos(15, 20);
cout << "0 退出  Enter 开始" << endl;
while (1)
{
int select = _getch();
if (select == 13)
{
system("cls");
this->Run();
}
else if (select = 48)
{
system("cls");
exit(0);
}
}
}
void Game::showGround()
{
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 26; j++)
{
if ((i == 0 || i == 29) || (j == 0 || j == 25 || j == 15))
{
cout << "■";
}
else if (i == 15 && j > 15)
{
cout << "■";
}
else
{
cout << "  ";
}
}
cout << endl;
}
SetPos(31, 2);
cout << "下 个图形" << endl;
SetPos(31, 17);
cout << "当 前得分" << endl;
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 16; j++)
{
if ((i == 0 || i == 29) || (j == 0 || j == 15))
{
map[i][j] = 1;
}
else
{
map[i][j] = 0;
}
}
}
}
void Game::gameOver()
{
for (int i = 5; i < 15; i++)
{
SetPos(1, i);
cout << "                            " << endl;
}
SetPos(8, 7);
cout << "G a m e   O v e r" << endl;
SetPos(3, 10);
cout << "0 退出   Enter 重新开始" << endl;
while (1)
{
int select = _getch();
if (select == 13)
{
system("cls");
this->Run();
}
else if (select == 48)
{
system("cls");
exit(0);
}
}
}
void Game::Run()
{
score = 0;
_id = 0;
top = 58;
_x = 6;
_y = 1;
showGround();
start = clock();
int new_id = rand() % 19 + 1;
while (1)
{
sharpDraw(_id);
keyControl();
if (downSet(_id))
{
sharpDraw(-new_id, 1);
_id = new_id;
new_id = rand() % 19 + 1;
sharpDraw(new_id, 1);
clean();
}
SetPos(34, 20);
cout << score << endl;
}
}
void Game::sharpDraw(int id, bool show)
{
int x, y;
if (show == true)
{
if (id > 0)
{
for (int i = 0; i < 4; i++)
{
x = 19 + sharp[id][2 * i];
y = 6 + sharp[id][2 * i + 1];
SetPos(2 * x, y);
cout << "■";
}
}
else
{
for (int i = 0; i < 4; i++)
{
x = 19 + sharp[-id][2 * i];
y = 6 + sharp[-id][2 * i + 1];
SetPos(2 * x, y);
cout << "  ";
}
}
return;
}
if (id > 0)
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
SetPos(2 * x, y);
cout << "■";
}
}
else
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[-id][2 * i];
y = _y + sharp[-id][2 * i + 1];
SetPos(2 * x, y);
cout << "  ";
}
}
return;
}
bool Game::downSet(int id)
{
if (id == 0)
return true;
finish = clock();
if (finish - start < speed)
{
return false;
}
start = clock();
if (!move(DOWN, _id))
{
int x, y;
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
map[y][x] = 1;
if (y < top)
{
top = y;
}
if (top <= 1)
{
gameOver();
}
}
_x = 6;
_y = 1;
return true;
}
sharpDraw(-id);
_y++;
sharpDraw(id);
return false;
}
bool Game::move(int dir, int id)
{
int x, y;
switch (dir)
{
case UP:
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y][x] == 1)
{
return false;
}
}
break;
case DOWN:
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y + 1][x] == 1)
{
return false;
}
}
}
break;
case RIGHT:
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y][x + 1] == 1)
{
return false;
}
}
}
break;
case LEFT:
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y][x - 1] == 1)
{
return false;
}
}
}
break;
default:
break;
}
return true;
}
void Game::Turn(int id)
{
switch (id)
{
case 1:id++; break;
case 2:id--; break;
case 3: break;
case 4:id++; break;
case 5:id++; break;
case 6:id++; break;
case 7:id -= 3; break;
case 8:id++; break;
case 9:id++; break;
case 10:id++; break;
case 11:id -= 3; break;
case 12:id++; break;
case 13:id--; break;
case 14:id++; break;
case 15:id--; break;
case 16:id++; break;
case 17:id++; break;
case 18:id++; break;
case 19:id -= 3; break;
default:
break;
}
if (!move(UP, id))
{
return;
}
sharpDraw(-_id);
_id = id;
}
void Game::keyControl()
{
if (!_kbhit())
return;
int key = _getch();
switch (key)
{
case 72:
Turn(_id);
break;
case 80:
if (move(DOWN, _id))
{
sharpDraw(-_id);
_y++;
}
break;
case 75:
if (move(LEFT, _id))
{
sharpDraw(-_id);
_x--;
}
break;
case 77:
if (move(RIGHT, _id))
{
sharpDraw(-_id);
_x++;
}
break;
case 32:
{
for (int i = 5; i < 15; i++)
{
SetPos(1, i);
cout << "                            " << endl;
}
SetPos(10, 7);
cout << "游 戏 暂 停" << endl;
SetPos(3, 10);
cout << "0 返回菜单  回车 继续游戏" << endl;
while (1)
{
int select = _getch();
if (select == 13)
{
for (int i = 5; i < 15; i++)
{
SetPos(1, i);
cout << "                            " << endl;
}
break;
}
else if (select == 48)
{
system("cls");
showMenu();
}
}
}
default:
break;
}
}
void Game::clean()
{
int n = -1;
int line = -1;
while (1)
{
for (int i = 28; i > 0; i--)
{
for (int j = 1; j < 15; j++)
{
line = i;
if (map[i][j] == 0)
{
line = -1;
break;
}
}
if (line != -1)
break;
}
if (line == -1)
break;
for (int i = line; i > 0; i--)
{
for (int j = 1; j < 15; j++)
{
if (i == 1)
map[i][j] = 0;
else
{
map[i][j] = map[i - 1][j];
SetPos(2 * j, i);
if (map[i][j] == 1)
cout << "■";
else
cout << "  ";
}
}
}
top++;
n++;
}
if (n >= 0)
{
score += n * n * 100 + 100;
if (speed > 100)
speed = 1000 - score / 10;
}
}

3.狼人杀

AC代码

#include <iostream>//C++输入输出流库
#include <cstdlib>//使用srand函数要用到这个库
#include <ctime>//使用time函数要用到这个库
#include <Windows.h>
#include <conio.h>
long long sr=0;
using namespace std;
void brc()
{
	system("cls");
	long long jy=1,wd=3;
	long long sy=3,wj=3;
	srand((int)time(0));
	long long n=rand()%15+1;
	cout<<"                                你是"<<n<<"号"<<endl;
	cout<<"                               正在分配身份"<<endl;
	Sleep(1500);
	long long m[20];
	for(long long i=1;i<=15;i++)
		m[i]=1;
	long long s[20]={0};
	long long yy[20]={0};
	long long l=0,w=0,j=0,p=0;
	for(long long i=1;i<=15;i++)
	{
		do
		{
			long long a=rand()%4+1;
			if(a==1&&l<1)
			{
				l++;
				s[i]=1;
				sr=i;
			}
			else if(a==2&&w<2)
			{
				w++;
				s[i]=2;
			}
			else if(a==4&&p<3)
			{
				p++;
				s[i]=4;
			}
			else if(a==3&&j<9
			)
			{
				j++;
				s[i]=3;
			}
		}
		while(s[i]==0);
	}
	if(s[n]==1)
		cout<<"                               你是>>杀手<<"<<endl;
	else if(s[n]==2)
		cout<<"                               你是>>预言家<<"<<endl;
	else if(s[n]==3)
		cout<<"                               你是>>平民<<"<<endl;
	else
		cout<<"                               你是>>法师<<"<<endl;
	cout<<"                             游戏将在5秒后开始"<<endl; 
	Sleep(5000);
	long long c=15;
	long long f=0,flag=0;
	long long day=1;
	long long lr;
	long long tp[20]={0};
	do
	{
		lr=0;
		system("cls");
		cout<<"                                 第"<<day<<"晚开始"<<endl;
		cout<<"你是"<<n<<"号"<<endl;
		long long x;
		for(long long i=1;i<=15;i++) 
		{
			if(i==n&&m[n]==1)
			{
				if(s[n]==1)
				{
					cout<<"                                 你是杀手"<<endl;
					for(long long j=1;j<=15;j++)
						if(m[j]==1&&s[j]!=1)
							cout<<j<<" ";
					cout<<endl;
					cout<<"                                 你选择杀掉"<<endl; 
					long long a;
					cin>>a;
					m[a]=0;
					cout<<endl;
					cout<<"                                 杀手杀了"<<a<<"号"; 
					if(s[a]==1)
						cout<<"(杀手)"<<endl;
					else if(s[a]==2)
						cout<<"(预言家)"<<endl;
					else if(s[a]==3)
						cout<<"(平民)"<<endl;
					else
						cout<<"(法师)"<<endl;
					c--;
				}	
				else
				if(s[n]==2)
				{
					cout<<"                                 你是预言家"<<endl;
					cout<<"                               ";
					for(long long j=1;j<=15;j++)
						if(m[j]==1)
							cout<<j<<" ";
					cout<<endl<<"                              你选择预言谁的身份"<<endl;
					long long a;
					cin>>a;
					if(s[a]==1)
					{
						cout<<"                        "<<a<<"号是杀手,请注意"<<endl;
						Sleep(1000);
					}
					else
						cout<<"                            "<<a<<"号是好人"<<endl; 
				}
			}
			else if(s[i]==1&&m[i]==1)
			{
				long long a;
				do
				{
					a=rand()%15+1;
				}
				while(a==i||m[a]==0||s[a]==1);
				cout<<"                           杀手杀掉了"<<a<<"号"; 
				if(s[a]==1)
					cout<<"(杀手)"<<endl;
				else if(s[a]==2)
					cout<<"(预言家)"<<endl;
				else if(s[a]==3)
					cout<<"(平民)"<<endl;
				else
					cout<<"(法师)"<<endl;
				m[a]=0;
				c--;
				break;//
			}
			else if(s[i]==2)
			{
				if(flag==0||m[flag]==0)
				{
					long long a;
					do
					{
						a=rand()%15+1;
					}
					while(a==i||m[a]==0);
					if(s[a]==1)
						yy[a]=1;	
					else if(s[a]==3)
						yy[a]=3;
				}	
			}
			else if(s[i]==4)
			{
				f=0;
				while(f==0)
				{
					long long a;
					a=rand()%3+1;
					if(a==3)break;
					if(a==1)
					{
						if(sy>0)
						{
							sy--;
							f=1; 
							long long b;
							do
							{
								b=rand()%15+1;
								b=rand()%15+1;
							}
							while(b==i||m[b]==0);
							m[b]=0;
							cout<<"                           法师毒死了"<<b<<"号"; 
							if(s[b]==1)
								cout<<"(杀手)"<<endl;
							else if(s[b]==2)
								cout<<"(预言家)"<<endl;
							else if(s[b]==3)
								cout<<"(平民)"<<endl;
							c--;
							if(s[b]==1&&n!=sr)
							{
								cout<<endl<<"							   "<<sr<<"号是杀手!"; 
								cout<<endl<<"								杀手已死亡!"<<endl;
								cout<<"                          	游戏结束"<<endl;
								return ;
							}
							break;//
						}
					}
					else if(a==2&&day>1)
					{
						if(jy>0)
						{
							jy--;
							f=1; 
							long long b;
							do
							{
								b=rand()%15+1;
							}
							while(b==i||m[b]==1);
							c++;
							cout<<"                           法师复活了"<<b<<"号"<<endl;
							m[b]=1;
							if(m[sr]==0&&n!=sr)
							{
								cout<<endl<<"							   "<<sr<<"号是杀手!"; 
								cout<<endl<<"						杀手已死亡!"<<endl;
								cout<<"                          	游戏结束"<<endl;
								return ;
							}
							break;//
						}
					}
					
				}
			}
		}
		if(s[n]==4)
		{
			cout<<"                                 你是法师"<<endl;
			cout<<"存活状况:"<<endl; 
			for(long long j=1;j<=15;j++)
				if(m[j]==1)
					cout<<j<<" ";
			cout<<endl;
			cout<<"                        你选择 1.* 杀 *还是 2.* 救 * 3.* 啥都不做 *"<<endl;
			long long a,f=0;
			while(f==0)
			{
				cin>>a;
				if(a==3) break;
				if(a==1)
				{
					if(wd>0)
					{
						wd--;
						f=1; 
						cout<<"                                你选择了杀人"<<endl;
						for(long long i=1;i<=15;i++)
							if(m[i]==1)
								cout<<i<<" ";
						cout<<endl; 
						long long b;
						cin>>b;
						if(s[b]==1)
							cout<<"(杀手)"<<endl;
						else if(s[b]==2)
							cout<<"(预言家)"<<endl;
						else if(s[b]==3)
							cout<<"(平民)"<<endl;
						else
							cout<<"(法师)"<<endl;
						m[b]=0;
						c--; 
						if(m[sr]==0&&n!=sr)
						{
							cout<<endl<<"							   "<<sr<<"号是杀手!"; 
							cout<<endl<<"						杀手已死亡!"<<endl;
							cout<<"                          	游戏结束"<<endl;
							return ;
						}
					}
					else
					{
						cout<<">>毒药<<不足"<<endl; 
					}
				}
				else if(a==2)
				{
					if(wj>0)
					{
						wj--;
						f=1; 
						cout<<"你选择了复活"<<endl;
						cout<<"阵亡名单:"<<endl;
						for(long long i=1;i<=15;i++)
							if(m[i]==0)
								cout<<i<<"号,身份:"<<s[i]<<" "<<endl;;
						long long b;
						cin>>b;
						m[b]=1;
						c++;
					}
					else
					{
						cout<<">>复活药水<<不足"<<endl; 
					}
				}
			}
		}
		Sleep(1000);
		long double p[20]={0};
		cout<<"                                 第"<<day<<"晚结束"<<endl;
		cout<<endl;
		if(m[n]==0)
		{
			cout<<"                             你已经>>死<<了";	
			break;
		}
		
		cout<<"                                    存活:"<<endl;
		cout<<"                       ";
		for(long long i=1;i<=15;i++)
			if(m[i]==1)
				cout<<i<<" ";
		cout<<endl;
		cout<<"                                    请投票...."<<endl;
		for(long long i=1;i<=15;i++)
		{
			if(i==n&&m[n]==1)
			{
				cout<<endl;
				cout<<"                                 你选择投几号"<<endl; 
				long long a=99;
				while(a==99) 
				{
					cin>>a;
					if(a==99)
						for(long long i=1;i<=15;i++)
							if(m[i]==1)
								cout<<"                                    "<<i<<"."<<s[i]<<endl;
				}
				if(s[i]==3&&day>4)
					p[a]+=1.5;
				else
					p[a]++;	
				cout<<endl;
				cout<<"                                    "<<i<<"->"<<a<<endl;
				if(s[a]==3)
				{
					tp[a]=i;
				}
			}
			else if(s[i]==1&&m[i]==1)
			{
				long long a;
				do
				{
					a=rand()%15+1;	
				}
				while(m[a]==0||a==i||s[a]==1);
				p[a]++;
				cout<<"                                    "<<i<<"->"<<a<<endl;
				if(s[a]==3)
				{
					tp[a]=i;
				}
			}
			else if(s[i]==2&&m[i]==1)
			{
				if(flag!=0)
				{
					p[f]++;
					cout<<"                                    "<<i<<"->"<<flag<<endl;
				}
				else
				{
					long long a;
					do
					{
						a=rand()%15+1;	
					}
					while(m[a]==0||a==i||yy[a]==3);
					p[a]++;
					if(s[a]==3)	
					{
						tp[a]=i;
					}
					cout<<"                                    "<<i<<"->"<<a<<endl;
				}
			} 
			else if(s[i]==3&&m[i]==1)
			{
				if(tp[i]==0)
				{
					long long a;
					do
					{
						a=rand()%15+1;	
					}
					while(m[a]==0||a==i);
					p[a]++;
					cout<<"                                    "<<i<<"->"<<a<<endl;
				}
				else
				{
					if(m[tp[i]]==1)
					{
						p[tp[i]]++;
						cout<<"                                    "<<i<<"->"<<tp[i]<<endl;
					}
					else
					{
						long long a;
						do
						{
							a=rand()%15+1;	
						}
						while(m[a]==0||a==i);
						p[a]++;
						cout<<"                                    "<<i<<"->"<<a<<endl;
					}
				}
			}
			else if(s[i]==4&&m[i]==1)
			{
				long long a;
				do
				{
					a=rand()%15+1;	
				}
				while(m[a]==0||a==i);
				p[a]++;
				cout<<"                                    "<<i<<"->"<<a<<endl;
			}
		}
		system("cls");
		cout<<"                                 投票情况:"<<endl;
		for(long long i=1;i<=15;i++)
			if(m[i]==1)
				cout<<"                                 "<<i<<"号"<<"  票数:"<<p[i]<<endl; 
		long long sw,max=-100; 
		for(long long i=1;i<=15;i++)
		{
			if(p[i]>max)
			{
				sw=i;
				max=p[i];
			}
		}
		m[sw]=0;
		cout<<"                                  "<<sw<<"死了"<<endl;
		c--;
		cout<<"                              "<<sw<<"号的身份是"; 
		if(s[sw]==1)
			cout<<"杀手"<<endl;
		else if(s[sw]==2)
			cout<<"预言家"<<endl;
		else if(s[sw]==3)
			cout<<"平民"<<endl;
		else 
			cout<<"法师"<<endl; 
		if(s[sw]==1&&n!=sr)
		{
			cout<<endl<<"							   "<<sr<<"号是杀手!"; 
			cout<<endl<<"						杀手已死亡!"<<endl;
			cout<<"                          	游戏结束"<<endl;
			return ;
		}
		day++;  
		if(s[n]!=1)   
		{
			for(long long i=1;i<=15;i++)
			if(s[i]==1&&m[i]==1)
				lr=1;
		}
		else
		{
			if(s[n]==1&&c==2)
				lr=0;
		}
		system("pause"); 
		cout<<endl;
		if(m[sr]==0&&n!=sr)
		{
			cout<<endl<<"							   "<<sr<<"号是杀手!"<<endl; 
			cout<<endl<<"						杀手已死亡!"<<endl;
			cout<<"                          	游戏结束"<<endl;
			return ;
		}
	}
	while(m[n]==1&&c>1);
	if(sr==n&&m[n]==1)
	{
		cout<<"								你杀掉了所有人!!!"<<endl;
	}
	cout<<"                          	游戏结束"<<endl;
	return ;
}
int main()
{
    srand((int)time(0));  
	char a='1';
	while(1)
	{
		system("cls");
		cout<<"                                   			杀手游戏"<<endl;
		cout<<"------------------------------------------------------------------------------------------------------------------------";
		cout<<"                                 		   1.开始游戏"<<endl;
		cout<<"                                		  2.查看游戏规则"<<endl;
		a=getch();
		if(a=='1')
		{
			cout<<"                                 		   1. 15人场"<<endl;
			cout<<"                          			2. 30人娱乐战(10猎人)<敬请期待>"<<endl;
			a='2';
			while(a=='2')
			{
				a=getch();
				switch(a)
				{
					case '1':
						brc();
					break;
				}
				if(a=='1')
					break;
			}
			if(a=='1')
				break;
		}
		else if(a=='2')
		{
			cout<<"杀手:每当晚上的时候可使用杀人权杀掉一人"<<endl;//1
			cout<<"预言家:每天晚上可以知道一个人的身份"<<endl;//2 
			cout<<"平民:无技能,当玩家是平民时,在第5天拥有1.5票的投票权"<<endl;//3
			cout<<"猎人:此身份仅限于30人娱乐局中,死亡后可带走一人"<<endl; 
			cout<<"投票细则:\n(游戏中除了你以外全是由超级AI人工代替)"<<endl;
			cout<<"预言家如果预言到杀手将一直对他投票,如果预言到好人将永远不会对他投票"<<endl;
			cout<<"平民将投上一个晚上对他投票他的人"<<endl;
			cout<<"********利用好超级AI游戏规则找出杀手**********"<<endl;
			system("pause");
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值