话不多说,新增人机
玩家 | 1号 | 2号 |
---|---|---|
移动 | w跳ad左右移动 | ↑跳←→左右移动 |
攻击 | s | ↓ |
掷药 | 空格 | 0 |
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <conio.h>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <windows.h>
using namespace std;
const int M = 15, N = 105;
int mapp[N][M];
string s[M] = { "" , "冰冻状态" , "混淆状态" , "黑暗状态" , "漂浮状态" };
int num;
bool rule_attact , rule_poison , rule_lava , rule_AI;
int lava_position , lava_down_time;
int AI_move_time;
struct player {
bool on;
int x , y;
int if_jump;
int effect[M];
int numm;
int direction;
int affect;
int st , timee;
bool survive;
}a[M];
void map_reset ( ) {
lava_position = -5;
lava_down_time = 0;
num = 1;
for ( int i = 1 ; i <= 100 ; i += 3 ) {
for ( int j = 1 ; j <= 10 ; j ++ ) mapp[i][j] = 1;
mapp[i + 1][1] = mapp[i + 1][10] = mapp[i + 2][1] = mapp[i + 2][10] = 1;
mapp[i][rand () % 8 + 2] = 0;
}
for ( int j = 1 ; j <= 10 ; j ++ ) {
mapp[100][j] = 1;
}
}
void player_reset ( ) {
a[2].on = 0;
a[1].x = a[2].x = a[3].x = 3;
a[1].y = a[2].y = a[3].y = 2;
a[1].if_jump = a[2].if_jump = 0;
a[1].direction = a[2].direction = 1;
a[1].affect = a[2].affect = a[3].affect = 0;
a[1].numm = a[2].numm = a[3].numm = 0;
a[1].st = a[2].st = 0;
a[2].on = false;
a[1].survive = a[2].survive = a[3].survive = true;
for ( int i = 1 ; i <= 10 ; i ++) a[1].effect[i] = a[2].effect[i] = a[3].effect[i] = 0;
}
void put_map ( ) {
for ( int i = -3 ; i <= 3 ; i ++ ) {
for ( int j = 1 ; j <= 10 ; j ++ ) {
if ( i == 0 && j == a[1].y ) printf ( "○" );
else if ( a[1].x + i <= lava_position ) printf ( "▓" );
else if ( a[1].effect[3] > clock () ) printf("**");
else if ( a[1].x + i == a[2].x && j == a[2].y && a[2].on ) printf ( "●" );
else if ( a[1].x + i == a[3].x && j == a[3].y && rule_AI ) printf ( "▲" );
else if ( mapp[a[1].x + i][j] == 1 ) printf ( "█" );
else if ( mapp[a[1].x + i][j] == 2 ) printf ( "☆" );
else printf( " " );
}
printf( " " );
if ( a[2].on ) {
for ( int j = 1 ; j <= 10 ; j ++ ) {
if ( i == 0 && j == a[2].y ) printf ( "●" );
else if ( a[2].x + i <= lava_position ) printf ( "▓" );
else if ( a[2].effect[3] > clock () ) printf("**");
else if ( a[2].x + i == a[1].x && j == a[1].y ) printf ( "○" );
else if ( a[2].x + i == a[3].x && j == a[3].y && rule_AI ) printf ( "▲" );
else if ( mapp[a[2].x + i][j] == 1 ) printf ( "█" );
else if ( mapp[a[2].x + i][j] == 2 ) printf ( "☆" );
else printf ( " " );
}
}
printf ( "\n" );
}
for ( int i = 1 ; i <= 10 ; i ++ ) {
if ( a[1].effect[i] > clock () ) {
cout << s[i] << " ";
}
else cout << " ";
if ( a[2].effect[i] > clock () && a[2].on ) {
cout << s[i];
}
cout << endl;
}
if ( rule_poison ) {
printf ( "药水:%2d" , a[1].affect);
if ( a[2].on ) printf (" 药水:%2d" , a[2].affect );
printf ( "\n" );
}
if ( num != 1) {
printf ( "圈数:%2d" , a[1].numm);
if ( a[2].on ) printf (" 圈数:%2d" , a[2].numm );
printf ( "\n" );
}
printf ( "层数:%2d" , a[1].x / 3 );
if ( a[2].on ) printf (" 层数:%2d" , a[2].x / 3 );
printf ( "\n" );
printf ( "用时:%2dmin%2ds%dms" , ( a[1].st == 0 ? 0 : (a[1].numm == num ? a[1].timee : clock () - a[1].st ) ) / 60000 , ( a[1].st == 0 ? 0 : (a[1].numm == num ? a[1].timee : clock () - a[1].st ) ) % 60000 / 1000 , ( a[1].st == 0 ? 0 : (a[1].numm == num ? a[1].timee : clock () - a[1].st ) ) % 1000);
if ( a[2].on ) printf (" 用时:%2dmin%2ds%dms" , (a[2].st == 0 ? 0 : (a[2].numm == num ? a[2].timee : clock () - a[2].st ) ) / 60000 , ( a[2].st == 0 ? 0 : (a[2].numm == num ? a[2].timee : clock () - a[2].st ) ) % 60000 / 1000 , ( a[2].st == 0 ? 0 : (a[2].numm == num ? a[2].timee : clock () - a[2].st ) ) % 1000 );
if ( rule_AI ) {
printf ( "\n" );
printf ( "人机:\n" );
for ( int i = 1 ; i <= 10 ; i ++ ) {
if ( a[3].effect[i] > clock () ) {
cout << s[i] << endl;
}
}
if ( rule_poison )
printf ( "药水:%d\n" , a[3].affect );
if ( num != 1 )
printf ( "圈数:%d\n" , a[3].numm );
printf ( "层数:%d\n" , a[3].x / 3 );
printf ( "用时:%d分%d秒%d毫秒\n" , ( a[3].numm == num ? a[3].timee : ( clock ( ) - a[3].st ) ) / 60000 , ( a[3].numm == num ? a[3].timee : ( clock ( ) - a[3].st ) ) % 60000 / 1000 , ( a[3].numm == num ? a[3].timee : ( clock ( ) - a[3].st ) ) % 1000 );
}
}
void move ( ) {
if ( kbhit() ) {
char c = getch ();
if ( a[1].effect[1] <= clock () ) {
if ( a[1].effect[2] > clock () && ( c == 'a' || c == 'd' ) ) c = time (0) % 2 == 1 ? 'd' : 'a';
if ( c == 'a' && a[1].y != 2 ) a[1].y -- , a[1].direction = -1;
if ( c == 'd' && a[1].y != 9 ) a[1].y ++ , a[1].direction = 1;
if ( c == 'w' && a[1].x % 3 == 0 ) {
a[1].if_jump = clock() + 100;
}
if ( c == 's' && rule_attact && a[1].y + a[1].direction == a[2].y && a[2].y + a[1].direction != 1 && a[2].y + a[1].direction != 10 ) a[2].y += a[1].direction;
}
if ( c == -32 && a[2].effect[1] <= clock () ) {
c = getch();
if ( a[2].effect[2] > clock () && ( c == 75 || c == 77 ) ) c = time (0) % 2 == 1 ? 77 : 75;
if ( c == 75 && a[2].y != 2 ) a[2].y -- , a[2].direction = -1;
if ( c == 77 && a[2].y != 9 ) a[2].y ++ , a[2].direction = 1;
if ( c == 72 && a[2].x % 3 == 0 ) {
a[2].if_jump = clock() + 100;
}
if ( c == 80 && rule_attact && a[2].y + a[2].direction == a[1].y && a[1].y + a[2].direction != 1 && a[1].y + a[2].direction != 10 ) a[1].y += a[2].direction;
}
if ( c == ' ' && a[ 1 ].affect > 0) {
a[ 1 ].affect --;
if ( rule_AI ) {
if ( a[ 2 ].on )
a[ rand ( ) % 2 + 2 ].effect[ rand () % 4 + 1 ] = clock ( ) + 3000;
else
a[ 3 ].effect[ rand () % 4 + 1 ] = clock ( ) + 3000;
}
else {
a[ 2 ].effect[ rand () % 4 + 1 ] = clock ( ) + 3000;
}
}
if ( c == '0' && a[ 2 ].affect > 0) {
a[2].affect --;
if ( rule_AI )
a[ rand () % 2 * 2 + 1 ].effect[rand () % 4 + 1] = clock() + 3000;
else
a[ 1 ].effect[rand () % 4 + 1] = clock() + 3000;
}
}
if ( a[1].if_jump > clock() || a[1].effect[4] > clock() ) {
if ( mapp[a[1].x - 1][a[1].y] != 1 && a[1].x > -1 )
a[1].x --;
}
else if ( mapp[a[1].x + 1][a[1].y] != 1 )
a[1].x ++;
if ( a[2].if_jump > clock() || a[2].effect[4] > clock() ) {
if ( mapp[a[2].x - 1][a[2].y] != 1 && a[2].x > -1 )
a[2].x --;
}
else if ( mapp[a[2].x + 1][a[2].y] != 1 )
a[2].x ++;
}
int found ( ) {
for ( int i = 2 ; i <= 9 ; i ++ ) {
if ( mapp[a[ 3 ].x + ( a[ 3 ].x % 3 == 1 ? 3 : ( a[3].x % 3 == 2 ? 2 : 1 ) ) ][ i ] == 0 )
return i < a[ 3 ].y ? -1 : ( i == a[ 3 ].y ? 0 : 1 );;
}
}
void AI_move ( ) {
if ( !a[ 3 ].survive ) return ;
if ( AI_move_time <= clock ( ) && a[3].effect[1] < clock ( ) ) {
if ( a[3].effect[2] > clock ( ) || a[3].effect[3] > clock ( ) ) {
a[3].y += rand ( ) % 3 - 1;
a[3].y = a[3].y == 0 ? 1 : a[3].y;
a[3].y = a[3].y == 10 ? 9 : a[3].y;
}
else
a[3].y += found ( );
}
if ( a[3].if_jump > clock() || a[3].effect[4] > clock() ) {
if ( mapp[a[3].x - 1][a[3].y] != 1 && a[3].x > -1 )
a[3].x --;
}
else if ( mapp[a[ 3 ].x + 1][a[ 3 ].y] != 1 )
a[ 3 ].x ++;
if ( a[ 3 ].affect > 0 ) {
a[ 3 ].affect --;
if ( a[ 2 ].on )
a[ rand ( ) % 2 + 1 ].effect[ rand ( ) % 4 + 1 ] = clock ( ) + 3000;
else
a[ 1 ].effect[ rand ( ) % 4 + 1 ] = clock ( ) + 3000;
}
}
void run ( ) {
int t = clock () + 1000;
bool flag_ = true , flag__ = true;
if ( rule_lava ) lava_down_time = clock () + 300;
a[3].st = clock ( );
while ( ( a[1].numm < num || ( a[2].numm < num && a[2].on ) || (a[3].numm < num && rule_AI ) ) && ( a[1].survive || a[2].survive || a[3].survive ) ) {
if ( clock () > lava_down_time && lava_down_time != 0) lava_position ++ , lava_down_time = clock () + rand () % 50 * 10;
if ( t <= clock () && rule_poison ) {
mapp[rand () % 34 * 3][rand () % 8 + 2] = 2;
t = clock () + 5000;
}
if ( flag_ && ( a[1].x != 3 || a[1].y != 2 ) ) a[1].st = clock () , flag_ = false;
if ( flag__ && ( a[2].x != 3 || a[2].y != 2 ) ) a[2].st = clock () , flag__ = false;
put_map ();
move ();
if ( mapp[a[1].x][a[1].y] == 2 ) a[1].affect ++ , mapp[a[1].x][a[1].y] = 0;
if ( mapp[a[2].x][a[2].y] == 2 ) a[2].affect ++ , mapp[a[2].x][a[2].y] = 0;
if ( mapp[a[3].x][a[3].y] == 2 ) a[3].affect ++ , mapp[a[3].x][a[3].y] = 0;
Sleep (100);
system ( "cls" );
if ( a[1].x <= lava_position ) a[1].effect[1] = 2147483647 , a[1].survive = false;
if ( a[2].x <= lava_position ) a[2].effect[1] = 2147483647 , a[2].survive = false;
if ( a[3].x <= lava_position ) a[3].effect[1] = 2147483647 , a[3].survive = false;
if ( a[ 1 ].x == 99 ) {
a[ 1 ].numm ++;
a[ 1 ].x = 0;
if ( a[ 1 ].numm == num ) a[ 1 ].timee = clock () - a[ 1 ].st;
}
if ( a[ 2 ].x == 99 ) {
a[ 2 ].numm ++;
a[ 2 ].x = 0;
if ( a[ 2 ].numm == num ) a[ 2 ].timee = clock () - a[ 2 ].st;
}
if ( a[ 3 ].x == 99 ) {
a[ 3 ].numm ++;
a[ 3 ].x = 0;
if ( a[ 3 ].numm == num ) a[ 3 ].timee = clock () - a[ 3 ].st;
}
if ( AI_move_time < clock ( ) && a[ 3 ].numm < num ) {
AI_move ( );
AI_move_time = clock ( ) + 140;
}
}
if (rule_lava) {
if ( a[ 1 ].numm == 1 ) printf ( "1号成功逃脱 " );
if ( a[ 2 ].numm == 1 && a[ 2 ].on ) printf ( "2号成功逃脱" );
if ( a[ 3 ].numm == 1 && rule_AI ) printf ( "3号成功逃脱" );
}
else if ( rule_AI || a[2].on ) {
int index;
int time_min;
index = ( a[1].timee < a[3].timee && rule_AI ) ? ( ( a[1].timee < a[2].timee && a[2].on ) ? 1 : 2) : 3;
printf ( "玩家%d赢了,用时%d分%d秒%d毫秒" , index , a[ index ].timee / 60000 , a[ index ].timee % 60000 / 1000 , a[ index ].timee % 1000 );
}
else
printf ( "恭喜您,成功逃脱" );
Sleep ( 1500 );
return ;
}
void set_game () {
printf ( "欢迎游玩" );
Sleep (3000);
system ( "cls" );
printf ( "请确定规则\n" );
printf ( "方向键上下移动,空格更改选项,回车确认\n");
printf ( "圈数:%d\n" , num );
printf ( "药水:%s\n" , rule_poison ? "是" : "否" );
printf ( "攻击:%s\n" , rule_attact ? "是" : "否" );
printf ( "多人:%s\n" , a[2].on ? "是" : "否" );
printf ( "岩浆:%s\n" , rule_lava ? "是" : "否" );
printf ( "人机:%s\n" , rule_AI ? "是" : "否" );
char r;
HANDLE hout;
COORD coord;
hout = GetStdHandle(STD_OUTPUT_HANDLE);
coord.X = 6;
coord.Y = 2;
SetConsoleCursorPosition(hout, coord);
while ( r != 13 ) {
r = getch ();
coord.X = 3;
if ( r == -32 ) {
r = getch ();
if ( r == 72 ) {
coord.Y -= 1;
if ( coord.Y == 1 ) coord.Y = 7;
coord.X = 6;
SetConsoleCursorPosition(hout, coord);
}
if ( r == 80 ) {
coord.Y += 1;
if ( coord.Y == 8 ) coord.Y = 2;
coord.X = 6;
SetConsoleCursorPosition(hout, coord);
}
}
if ( r == ' ' && coord.Y == 2 && rule_lava == false) {
num = num % 10 + 1;
printf ( "%d " , num );
coord.X = 6;
SetConsoleCursorPosition(hout, coord);
}
if ( r == ' ' && coord.Y == 3 ) {
rule_poison = !rule_poison;
printf( "%s" , rule_poison ? "是" : "否" );
coord.X = 6;
SetConsoleCursorPosition(hout, coord);
}
if ( r == ' ' && coord.Y == 4 ) {
rule_attact = !rule_attact;
printf( "%s" , rule_attact ? "是" : "否" );
coord.X = 6;
SetConsoleCursorPosition(hout, coord);
}
if ( r == ' ' && coord.Y == 5 ) {
a[2].on = !a[2].on;
printf( "%s" , a[2].on ? "是" : "否" );
coord.X = 6;
SetConsoleCursorPosition(hout, coord);
}
if ( r == ' ' && coord.Y == 6 ) {
rule_lava = !rule_lava;
printf( "%s" , rule_lava ? "是" : "否" );
num = 1;
coord.X = 6;
coord.Y = 2;
SetConsoleCursorPosition(hout, coord);
printf ( "1" );
coord.Y = 6;
SetConsoleCursorPosition(hout, coord);
}
if ( r == ' ' && coord.Y == 7 ) {
rule_AI = !rule_AI;
printf( "%s" , rule_AI ? "是" : "否" );
coord.X = 6;
SetConsoleCursorPosition(hout, coord);
}
Sleep (200);
}
}
int main () {
srand ( unsigned ( time ( 0 ) ) );
while ( true ) {
system ( "cls" );
player_reset ( );
map_reset ( );
set_game ( );
run ( );
}
return 0;
}