文档库 最新最全的文档下载
当前位置:文档库 › 大富翁

大富翁

前一段时间买了一套桌游,强手大富翁,周末与同学玩了几次,结果是一次都没玩完过,太让人郁闷了,花了几个小时的时间换来的却是一个没有赢家的结果,虽说游戏的本身是让人娱乐的,但是总分不出胜负也太不靠普了,于是呼就写个程序模拟一下整个过程,由于很长时间没有写代码了,所以编译DEBUG过程异常艰苦。。。程序测试结果不出所料:如果按照游戏原来规则,80%的概率是玩不完的,真不知道为什么规则不能改一改。银行每次发钱改成100就可以了,基本可以保证在100轮之内玩完,当玩家超过6个时,这个游戏基本上就是去了意义。所以说这种游戏娱乐娱乐就好,莫要当真,故语云:“认真你就输了”。
大富翁模拟程序源码下载地址: 下载地址
https://www.wendangku.net/doc/7815872748.html,/files/3af92328-0b78-11e0-84ff-0015c55db73d/
========================================================================
环境:linux Centos5.5 , gcc4.1.2 , GNU Make 3.81
游戏规则:由于是测试,所以可以选择不同的游戏规则,每个玩家经过起点得到200,玩家比需要在他所拥有的城市中平均建造房子(这个规则可以在ini.in文件中去除掉),有机会卡,基金卡,具体规则见网上。
思路:由于这个游戏的规则不难,所以一步一步来模拟就可以了,dice()表示player投色子,update()表示一个城市房子的升级,go(step)表示玩家可以向前走几步。然后总得是while循环,其中机会卡和基金卡设计的比较麻烦,由于要保证程序的效率,事先作一些预处理,包括计算每个城市到最近的地铁站或者公共事业等,详细的见代码注释(写的不是很清楚)。
具体:
class Player; 玩家,own[]表示拥有的城市,rec_pos[]表示玩家经过的地方
go(int) 向前走step步
dice() 投色子,一共有两个
get_card(Card*) 得到一张卡片
is_failed() 测试玩家是否失败
class Card;包括基金卡,机会卡,type标志着卡片的类型,value标志这具体内容
class City;城市其中rec_player[]记录此城市被占用(经过)情况
update() 城市升级
Card fund[],oppt[]; 分别代表基金卡和机会卡
City city[]; 代表城市
README:
其中所有游戏数据保存在data.in文件中,包括各种城市、机会卡、基金卡等。(城市的名称是用拼音来写的。。。)
ini.in文件中保存的是程序配置文件,第一行表示经过起点时银行发放钱数,第二行表示玩家初始钱数,第三行表示是否输出调式信息,第四行表示是否启用规则。
部分代码:

class Card;
class City;

class Player{
public:
int id;
int irec; //the

index in the rec_pos[]
int sel; //the card he select ,should be 0 or 1
int n_own; //the number of city i have
City* own[MAXN_CITY]; // the city he have
int money;
int in_jail;
int cur_pos;
int pre_pos;
Card* card; //the current card he select
int rec_pos[MAXN]; // record the pos he go
int rec_card;
int rec_failed;

int a,b; // the two number he throws;
int dice();
int get_money(int);
int is_failed();
int lose_money(int);
int go(int);
int go_to(int);
int pay_rent(City*);
int get_card(int);
};
class City{
public:
char name[MAXN_LEN];
int id;
/*
* 0: none
* 1: normal building
* 2: public building
* 3: train station
* 4: fund
* 5: oppt
* 7: shui
*/
int type;
int value; // the money you should pay to bank
int cost; // the cost per house
int level;
int rent[MAXN_LEVEL]; // the rent you get
int rec_player[MAXN]; //record the people who arrived here
int irec; //the index in the rec_player;
Player* host;

int update(); // update the city;
};
class Card{
public:
/*
* 0: none
* 1: get money
* 2: lose money
* 3: go to the city num;
* 4: the number player should go
* 5: go the next train station
* ( the times )
* 6: go the next public building
* ( the times )
* 7: go to the jail !
* 8: get money from every person
* 9: send money to every person
* 10: recover the house
* 11: get a house from the other person
*/
int type;
int value;
int extra;
};

City city[MAXN_CITY]; // total city in the map
int next_train[MAXN_CITY]; // the index of the road to next train station
int next_public[MAXN_CITY]; // the index of the road to next public buildingint n_player;
int n_city;
int bank_give;
int start_money;
int n_fund; //the number of the fund card
int n_oppt; //the number of the oppt card
int ifund,ioppt; //the index of the card
int failed[MAXN_PLAYER];
int n_player; //the players in the game
int cur_player; //current player in the game
int INI_PRINT;
int n_round; // record the round of the game
Player player[MAXN_PLAYER];
Card fund[MAXN_CARD];
Card oppt[MAXN_CARD];

相关文档
相关文档 最新文档