昨天没有更新文章,因为这两天都在忙着开发一个德州扑克的游戏。项目是公开的,挂在github上:https://github.com/DingXuefeng/Nayav 本来是想用object C来写的,后来还是决定用我最熟悉的C++来写了。
这个项目的终极目的是编写一个可以和人类抗衡的扑克AI,有自己的个性,会使诈,bluff the bluffer等等,并且极度冷静。如果能有别人一起开发就最好啦。
目前我有如下几个体验:
- 实现一个看上去简单(但往往超级复杂)的功能,一定要一步一步,先写一些dummy method。
- 如果一个class太大了,应该refraction把它分成两个或者更多的小类。重构的技巧是把所有的m_xx的类变量变成Get_xx()的方法,然后把Get_xx()内容从 return m_xx换成return Get_secondClass()->Get_xx();,然后就可以把m_xx变量直接搬到secondClass中去了。可以写两个get,分别对应Gettter和Setter:
- const X& Get_xx() const { return xx; }
- X& Get_xx() { return xx; }
- 如果一个method里面不包含本类的任何属性,这个method可以安全的移动到secondClass。
一个例子
class A {
public:
A : m_a(0) {};
void initialize_a() { m_a= 99; };
void doSomething() { m_a += 1; return m_a; };
private:
int m_a;
};
首先变成这样:
class A {
public:
A : m_a(0) {};
void initialize_a() { Get_a() = 99; };
void doSomething() { Get_a() += 1; };
private:
const int &Get_a() const { return m_a; }
int &Get_a() { return m_a; }
int m_a;
};
然后
class A {
public:
A : m_a(0) {};
void initialize_a() { Get_a() = 99; };
void doSomething() { Get_a() += 1; };
private:
const int &Get_a() const { return Get_b()->Get_a(); }
int &Get_a() { return Get_b()->Get_a(); }
B m_b;
};
class B {
private:
const int &Get_a() const { return m_a; }
int &Get_a() { return m_a; }
int m_a;
friend class A;
};
最后把method也搬过来
class A {
public:
void initialize_a() { Get_b()->inialize_a(); };
void doSomething() { Get_b()->doSomething(); };
private:
B m_b;
};
class B {
public:
B() : m_a(0) {};
void initialize_a() { Get_a() = 99; };
void doSomething() { Get_a() += 1; };
private:
const int &Get_a() const { return m_a; }
int &Get_a() { return m_a; }
int m_a;
friend class A;
};