看《未来简史》讲到数据主义,其信奉算法是一切,这里,姑且做一个小尝试,用c++模拟一只十分生硬的草履虫吧...
#include <iostream>
#include <string>
#include <windows.h>
#include <cstdlib>
#include <ctime>
using namespace std;
int getRand(int min, int max);
void gotoxy(short x, short y);
void cls();
bool Radar(int x,int y);
int eat(int len,int mydir1,int mydir2,int x,int y,double energy,bool food);
int main(void) {
system("title paramecium caudatum");
int len=200,mydir[2]= {0,0},x=40,y=10,fx,num;
double energy=80.00,bf=80.00;
bool food=false,flag=false,flag1=false;
char start;
string str="/";
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout<<"\t--<<草履虫模拟器>>--\n";
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY);
cout<<"形象:";
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout<<"O";
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY);
cout<<"草履虫\n";
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout<<" .";
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY);
cout<<"食物\n";
cout<<"*:若要给草履虫喂食,请按f键,f键可能不灵,请按多次\n 因为可能使程序过于复杂,已经关闭分裂功能\n\n";
cout<<"即将开始...\n\n\n这之后可能有一段失灵期(3s左右),请耐心等待,若已知悉,请输入任意键:";
cin>>start;
cls();
while(true) {
flag=true;
while(flag==true) {
if(getRand(1,3)==3) {
if (energy<80.00) {
energy=10000*eat(len,mydir[0],mydir[1],x,y,energy,food);
Sleep(100);
}
gotoxy(0,0);
str="进食结束,回到(0,0)";
str="/";
continue;
}
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY);
gotoxy(0,1);
cout<<"长度:"<<len<<"nm\t";
if (energy < 0) {
energy=100.00-energy;
} else if (energy == 0) {
energy=80.00;
}
cout<<"能量:"<<energy<<"%\t";
cout<<"方向:"<<mydir[0]<<","<<mydir[1]<<"\n";
gotoxy(55,1);
cout<<"|";
gotoxy(0,2);
cout<<"食物:"<<food<<endl;
gotoxy(55,2);
cout<<"|";
gotoxy(0,3);
cout<<"[消息窗]:"<<str;
gotoxy(55,3);
cout<<"|";
cout<<"\n-------------------------------------------------------";
gotoxy(60,21);
cout<<"X:"<<x<<" "<<"Y:"<<y;
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
srand(time(0));
num=rand()%2;
srand(time(0));
if(rand()%2)num+=1;
else num-=1;
mydir[0]=num;
srand(time(0));
num=rand()%2;
srand(time(0));
if(rand()%3)num+=1;
else num-=1;
if(x==1)num=1;
mydir[1]=num;
gotoxy(x,y);
cout<<"O";
Sleep(100);
cls();
if(mydir[0]==2&&x<70) {
x++;
energy=energy-0.10;
} else if(mydir[0]==-1&&x>2) {
x--;
energy=energy-0.10;
}
if(mydir[1]==1&&y<20) {
y++;
energy=energy-0.10;
} else if(mydir[1]==-1&&y>5) {
y--;
energy=energy-0.10;
}
}
gotoxy(0,0);
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY);
gotoxy(0,1);
cout<<"长度:"<<len<<"nm\t";
if (energy < 0) {
energy=100.00-energy;
} else if (energy == 0) {
energy=80.00;
}
cout<<"能量:"<<energy<<"%\t";
cout<<"方向:"<<mydir[0]<<","<<mydir[1]<<"\n";
gotoxy(55,1);
cout<<"|";
gotoxy(0,2);
cout<<"食物:"<<food<<endl;
gotoxy(55,2);
cout<<"|";
gotoxy(0,3);
cout<<"[消息窗]:"<<str;
gotoxy(55,3);
cout<<"|";
cout<<"\n-------------------------------------------------------";
gotoxy(60,21);
cout<<"X:"<<x<<" "<<"Y:"<<y;
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
gotoxy(x,y);
cout<<"O";
if(energy<=80.00)bf=energy;
else{
energy=bf;
str="能量溢出,已启备份";
}
}
system("pause");
return 0;
}
int getRand(int min, int max) {
return(rand()%(max-min+1))+min;
}
void gotoxy(short x, short y) {
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void cls() {
//双精度清屏,可以做到不闪屏的效果
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);
int bufferSize = csbi.dwSize.X * csbi.dwSize.Y;
CHAR_INFO* buffer1 = new CHAR_INFO[bufferSize];
CHAR_INFO* buffer2 = new CHAR_INFO[bufferSize];
for (int i = 0; i < bufferSize; i++) {
buffer1[i].Char.AsciiChar = ' ';
buffer1[i].Attributes = csbi.wAttributes;
buffer2[i].Char.AsciiChar = ' ';
buffer2[i].Attributes = csbi.wAttributes;
}
SMALL_RECT srctReadRect = {0,0,csbi.dwSize.X-1,csbi.dwSize.Y-1};
COORD coordBufSize = {csbi.dwSize.X, csbi.dwSize.Y};
COORD coordBufCoord = { 0, 0 };
WriteConsoleOutput(hConsole, buffer1, coordBufSize, coordBufCoord, &srctReadRect);
CHAR_INFO* currentBuffer = buffer1;
CHAR_INFO* nextBuffer = buffer2;
}
int eat(int len,int mydir1,int mydir2,int m,int n,double energy,bool food) {
int x=m,y=n;
food=true;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY);
gotoxy(0,1);
cout<<"长度:"<<len<<"nm\t";
if (energy < 0) {
energy=100.00-energy;
} else if (energy == 0) {
energy=80.00;
}
cout<<"能量:"<<energy<<"%\t";
cout<<"方向:"<<mydir1<<","<<mydir2<<"\n";
gotoxy(55,1);
cout<<"|";
gotoxy(0,2);
cout<<"食物:"<<food<<endl;
gotoxy(55,2);
cout<<"|";
gotoxy(0,3);
cout<<"[消息窗]:草履虫将会进食...";
gotoxy(55,3);
cout<<"|";
cout<<"\n-------------------------------------------------------";
gotoxy(60,21);
cout<<"X:"<<x<<" "<<"Y:"<<y;
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
if(food==true) {
gotoxy(50,20);
cout<<".";
}
if(x<50)mydir1=1;
else if(x==50)mydir1=0;
else mydir1=-1;
if(y<20)mydir1=1;
else if(y==20)mydir1=0;
else mydir1=-1;
cout<<"O";
if(food==true) {
switch(mydir1) {
case 1:
x++;
energy=energy-0.10;
break;
case 0:
energy=energy-0.01;
break;
case -1:
x--;
energy=energy-0.10;
break;
}
switch(mydir2) {
case 1:
y++;
energy=energy-0.10;
break;
case 0:
energy=energy-0.01;
break;
case -1:
y--;
energy=energy-0.10;
break;
}
}
if(Radar(x,y)==true) {
food=false;
len+=100;
return energy;
}
}
bool Radar(int x,int y) {
int spotx[3]= {x+1,x,x-1},spoty[3]= {y+1,y,y-1};
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(spotx[i]==50&&spoty[j]==20)return true;
}
}
return false;
}