这样下去泰迪熊会成为一代编程高手吗?(竞争篇)

tkminigame 2009-07-15 09:06:05
新程序来了——
这一次,我们为teddy bear添加了一个强大的竞争对手:Kermit the Frog! 虽然大家都是在csdn学习,但是Kermit the Frog的表现行为和teddy bear有所不同。比如当Kermit解决一个问题后,犒赏自己的方式是用一堆蚊子(听起来有点恶心),此外,Kermit总是优先进行学习,然后再解答问题,而泰迪熊总是优先解答问题然后再学习。一开始teddy的积分涨得快,kermit的经验涨得快,但是谁能够先成为CSDN的牛人呢?把程序拷贝下来跑跑看吧!


这次的程序和原来有几个改动的地方。
1。为了让角色个性化,设计了一个words模板类,加入到类的构造函数中。
2。增加了本地时间打印功能。
3。把LogonOnCSDN()改为了子线程,原来生成问题的过程变成了主线程。
4。对参数进行了调整,缩短了灌水的时间,加大了问题生成的密度。
5。在多线程情况下出现了访问共享数据不同步导致程序崩溃的错误,这里用了一个本地静态锁来解决这个问题。虽然我个人感觉在大量线程的情况下使用它不太可靠,但是目前来看,它运行得还不错!所以暂时就这样了。
6。这次为我们的角色设计了一个控制“芯片”,用来处理不同的流程,看看我们为两个角色准备的“芯片”是什么样子吧
char* bearChips[] = {"checkOR 3","solve","study","poolingWater","goto 0","end"};
char* frogChips[] = {"checkOR 3","study","solve","poolingWater","goto 0","end"};
相信这是一目了然的。checkOR是流程控制符,用来代替"if....esle if..."的格式。它会逐个的检查后面的各项,如果有一项返回的结果为真,则终止检查返回结果为真。后面的数字n表示要检查的项目。goto 用来实现跳转,后面跟跳转的位置。显然的,这里是个无限循环。最后用end表示芯片程序的终止。有此功能,可以为Coder安插各式各样的芯片。比如:
char* bearChips[] = {"poolingWater","checkOR 3","solve","study","poolingWater","goto 0","end"};//劳逸结合的熊熊
char* bearChips[] = {"poolingWater","poolingWater","solve","poolingWater","poolingWater","goto 0","end"};//贪玩,不爱学习的熊熊。
等等……你也可以修改程序试试看。


#include <process.h>
#include <windows.h>
#include <conio.h>
#include <string.h>
#include <queue>
#include <map>
#include <time.h>
#include <stdarg.h>
#include <math.h>
using namespace std;
const char* qType[] = {"C/C++","JAVA","PHP"};
struct question{
int type;
int score;
};
typedef queue<question> questions;
questions unsolved;
typedef map<const char*,char*> WordsTemplate;

class Coder{
public:
char name[256];

int score;
int exp[3];
questions bookmark;
WordsTemplate lines;
char **chips;

Coder(const char* name,char **chips,WordsTemplate lines):score(0){
this->chips=chips;
this->lines = lines;
strcpy_s(this->name,256,name);
::memset(exp,0,sizeof(exp));
}

void LogOnCSDN();
protected:
bool solve(question& q);
void study(question& q);
void say(char* format,...);
void info(char* format,...);
void update();
void poolingWater(){info("%s is pooling water....\n",name);Sleep(1500);};
void thread();
void parseCommand(int ip,string& output,int index=0);
bool executeCommand(int &ip,void* p);
};
void printSystemTime(){
//print system time;
static bool lock = false;
while(lock){
::Sleep(1);
}
lock = true;
time_t t;
::time(&t);
tm _tm;
::localtime_s(&_tm,&t);
char timechar[256];
::asctime_s(timechar,256,&_tm);
timechar[strlen(timechar)-6] = '\0';
printf("[%s]",timechar);
lock = false;
}
void Coder::say(char* format,...){
if(format==NULL) return;
char s[512];
va_list arg;
int done;
va_start (arg, format);
done = ::vsnprintf_s(s, 512, 512, format, arg);
va_end (arg);

printSystemTime();
printf("%s: %s\n",this->name,s);
};
void Coder::info(char* format,...){
if(format==NULL) return;
char s[512];
va_list arg;
int done;
va_start (arg, format);
done = ::vsnprintf_s(s, 512, 512, format, arg);
va_end (arg);
printSystemTime();
printf("[CSDN]: %s",s);
};
void Coder::update(){
char words[512];
::sprintf_s(words,512,"Now %s\'s score is %d.unsolved/bookmarks:(%d/%d).\n\n",name,score,unsolved.size(),bookmark.size());
info(words);
}
void Coder::study(question& q){
char words[512];
::sprintf_s(words,512,"%s is learning %s.....\n",name,qType[q.type]);
info(words);
::Sleep(5000);
exp[q.type]+=q.score/5;
::sprintf_s(words,512,"%s got %d exp on %s.(%d)\n\n",name,q.score/5,qType[q.type],exp[q.type]);
info(words);
}
bool Coder::solve(question& q){
//spend sometime to check the promblem is soluble
Sleep(500);
//raise one percent per 50 point
double successRate = exp[q.type]/5000.0;
//make it high but not too high
if(successRate>0.95) successRate = 0.95;
if(rand()>(int)(((double)RAND_MAX)*successRate)){
return false;
}
int solveTime = (255-exp[q.type])*10;
if(solveTime<=500) solveTime= 500;
::Sleep(solveTime);
return true;
}
void Coder::parseCommand(int ip,string& output,int index){
output.clear();
int stringIndex = 0;

char* pStart = chips[ip];
while(*pStart!='\0'){
if(stringIndex==index){
output.push_back(*pStart);
}else if(stringIndex>index){
break;
}
pStart++;
if(*pStart==' '){
stringIndex++;
}
}
output.push_back('\0');
}
bool Coder::executeCommand(int &ip,void*p){
string command;
this->parseCommand(ip,command);
if(::strcmp(command.c_str(),"checkOR")==0){
update();
::Sleep(1000);
this->parseCommand(ip,command,1);
int times = ::atoi(command.c_str());
int checkORIp = ip+1;
for(int checkORIp = ip+1;checkORIp<=ip+times;){
bool result = this->executeCommand(checkORIp,NULL);
if(result) {
ip = ip+times+1;
return true;
}
}
return false;
}else if(::strcmp(command.c_str(),"goto")==0){
this->parseCommand(ip,command,1);
ip = ::atoi(command.c_str());
}else if(::strcmp(command.c_str(),"solve")==0){
ip++;
static bool lock = false;
while(lock){
::Sleep(1);
}
lock = true;
if(!unsolved.empty()){
info("%s is trying to solve a problem.\n",name);
question q = ::unsolved.front();
unsolved.pop();
lock = false;
if(solve(q)){
say(lines["solve_success"],qType[q.type]);
this->score+=q.score;
say(lines["celebrate"]);
Sleep(100);
}else{
say(lines["solve_failure"]);
bookmark.push(q);
}
::Sleep(500);
return true;
}
lock = false;
return false;
}else if(::strcmp(command.c_str(),"study")==0){
ip++;
if(!bookmark.empty()){
//learning
question q = bookmark.front();
this->study(q);
bookmark.pop();
return true;
}
return false;
}else if(::strcmp(command.c_str(),"poolingWater")==0){
ip++;
this->poolingWater();
return true;
}
return true;

}
void Coder::thread(){
int ip = 0;
while(::strcmp(chips[ip],"end")!=0){
this->executeCommand(ip,NULL);
}
::_endthread();
}
void Coder::LogOnCSDN(){

say(lines["opening"],name);
union {
void (*thread)(void *);
void (Coder::*Coderthread)();
} proc;
proc.Coderthread = &Coder::thread;
::_beginthread(proc.thread,0,this);

}
void generateQuestion(void* p){

time_t t;
::time(&t);
::srand((UINT)t);
while(true){
question q = {rand()%5,(rand()%10+1)*10};
if(q.type<3){
::unsolved.push(q);
}
Sleep(1500);
}

}

void main()
{
//::_beginthread(generateQuestion,0,0);
WordsTemplate bearWords;
bearWords["opening"] = "Hi!I'm a new fish on CSDN!My name is %s.\n";
bearWords["solve_success"] = "I just resolved one %s problem!I must reward myself a piece of honey!";
bearWords["solve_failure"] = "Oh my bee!I can't solve this problem!I must add it to my bookmark!";
bearWords["celebrate"] = "umm,umm,umm,It's yummy!";

char* bearChips[] = {"checkOR 3","solve","study","poolingWater","goto 0","end"};

Coder teddyBear("<<Teddy Bear>>",bearChips,bearWords);

WordsTemplate frogWords;
frogWords["opening"] = "Hi ho! %s here!\n";
frogWords["solve_success"] = "Greeeeeeat!I just resolved one %s problem.Time to have flies!";
frogWords["solve_failure"] = "hmmmmm,it's not easy to being green.Let's have a study!";

char* frogChips[] = {"checkOR 3","study","solve","poolingWater","goto 0","end"};

Coder kermitFrog("<<Kermit the Frog>>",frogChips,frogWords);


teddyBear.LogOnCSDN();
kermitFrog.LogOnCSDN();

generateQuestion(NULL);

}


...全文
65 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
shen8686 2009-07-16
  • 打赏
  • 举报
回复
路过
定个
huhuazhe 2009-07-16
  • 打赏
  • 举报
回复
不疯魔不成活
嘿嘿
有前途
tkminigame 2009-07-16
  • 打赏
  • 举报
回复
不好意思,我用vc8写的。我在vc6也编译了一下,没有看到你说的那种错误,主要是没有_s方法,string的一些方法不支持。此外thread方法不能用,奇怪。。
本人也是新手,这个只是试探性的,希望前辈高手们点拨啊。
f22fbi 2009-07-15
  • 打赏
  • 举报
回复
啥意思?
  • 打赏
  • 举报
回复
新概念编程?
至善者善之敌 2009-07-15
  • 打赏
  • 举报
回复
接分哈哈。
ForestDB 2009-07-15
  • 打赏
  • 举报
回复
又见LZ散分。
The_facE 2009-07-15
  • 打赏
  • 举报
回复
楼主有点走火入魔的感觉.....
NKLoveRene 2009-07-15
  • 打赏
  • 举报
回复
干嘛呢这是
没敢看。。。
liyang_1949 2009-07-15
  • 打赏
  • 举报
回复
又看到LZ发这种程序了 呵呵~~
hoomey 2009-07-15
  • 打赏
  • 举报
回复
没看明白
fiveyes 2009-07-15
  • 打赏
  • 举报
回复
在VC6里编译了一下,出现一堆语法错误missing '{' before '<',而且居然都跑到VC98\Include\functional里面去了,惭愧,看来VC6是不适合的。
换个C-Free3.5试了下,这次也是出现一堆错误,好在没有跑到include里面,庆幸。
看来以后真的要换个VS2008了。遗憾啊!错过一个成为编程高手的机会
Walf_ghoul 2009-07-15
  • 打赏
  • 举报
回复
路过,顶个。。。
脚印儿 2009-07-15
  • 打赏
  • 举报
回复
顶下
SIJIMO 2009-07-15
  • 打赏
  • 举报
回复
看不明白
K_s_G 2009-07-15
  • 打赏
  • 举报
回复
顶个

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧