#include<stdio.h>
#include"game.h"
#include<malloc.h>
#include<memory.h>
#include<stdlib.h>
void put_Block(st_game *,int);
void meet_Block(st_game *,int);
st_tool tool_tb[MAX_TOOL_NUM]={{'G',"Godwealth",0,500,Green,NULL,NULL}, //财神
{'B',"Block",1,200,Red,put_Block,meet_Block},//路障
{'C',"Control dice",2,200,Green,NULL,NULL}, //遥控骰子
{'\0',"",0,0,0,NULL,NULL},
};
st_land_info init_land_st={
x:0,
y:0,
price:0,
p_player:NULL,
level:Level_0,
color:None,
owner_color:None,
tool_n:0,
symbol:0,
};
//初始化游戏结构体 包括玩家,地图及地块信息
st_game * init_game(int player_num)
{
st_game *p=malloc(sizeof(st_game));
if(p==NULL) return NULL;
set_game_pnum(p,player_num);
set_game_id(p,0);
int i;
for(i=0;i<player_num;i++)
if(((p->p_player_tab)[i]=init_player(i))==NULL) goto init_player_fail;
if((p->p_map=init_map_info())==NULL) goto init_map_fail;
init_shop(p->p_map);
init_land(&(p->p_land));
return p;
init_map_fail:
init_player_fail:
for(;i>=0;i--)
deinit_player(&(p->p_player_tab)[i]);
free(p);
return NULL;
}
//结束游戏时销毁游戏结构体 释放相关空间
void deinit_game(st_game **p)
{
if(*p==NULL) return;
int i=get_game_pnum(*p);
for(;i>0;i--)
deinit_player(&((*p)->p_player_tab)[i-1]);
deinit_map_info(&((*p)->p_map));
free(*p);
*p=NULL;
}
//打印地图
/*有待优化
*aaaaaaa*
*a a*
*a a*
*aaaaaaa*
*/
void print_map_line(int num,int flag)
{
#ifdef LINUX_CONSOLE
print_line(' ',num);
#else
if(flag==1)
printf("\n");
#endif
}
/*首先判断land中的symbol是否有符号如果有,打印land中的临时symbol,否则打印地图元素:
地图是形状
aaaa
b d
cccc
首先打印a行,然后打印第二行到第height-1行(从b列打到d列),最后打印c行,每行前后加上
空格,让地图在中间显示。land是一维数组,map是二维数组,可看成将land围绕在map的四周
,他们的索引有一定的对应关系,也是分成四部分。
*/
void print_game_map(st_game *p)
{
int i,j,k;
st_land_info *pland=get_game_land(p,0);
st_map_info *pmap=p->p_map;
if(p==NULL) return;
print_map_line(window_col()/2-WIDTH/2,0);
for(i=j=0;j<WIDTH;j++)
if(get_land_symbol(pland+j)!=0) //如果land中有需要显示的符号 则显示
print_land(p,j);
else
print_color(get_map_e(pmap,i,j),get_land_ownclor(pland+j));//打印第一行a
print_map_line(window_col()/2-WIDTH+WIDTH/2,1);
//打印中间有空格的a
for(i=1,j=0;i<HEIGHT-1;i++)
{
k=WIDTH-2;
print_map_line(window_col()/2-WIDTH/2,0);
if(get_land_symbol(get_game_land(p,LAND_NUM-i))!=0)
print_land(p,LAND_NUM-i);
else
print_color(get_map_e(pmap,i,0),get_land_ownclor(pland+LAND_NUM-i));//打印第一列
while((k--)>0)
printf(" ");
if(get_land_symbol(get_game_land(p,WIDTH+i-1))!=0)
print_land(p,WIDTH+i-1);
else
print_color(p->p_map->map[i][WIDTH-1],(p->p_land[WIDTH+i-1]).owner_color);//打印最后一列
print_map_line(window_col()/2-WIDTH+WIDTH/2,1);
}
print_map_line(window_col()/2-WIDTH/2,0);
for(i=HEIGHT-1,j=0;j<WIDTH;j++)
if(get_land_symbol(get_game_land(p,LAND_NUM-i-j))!=0)
print_land(p,LAND_NUM-i-j);
else
print_color(p->p_map->map[i][j],(p->p_land[LAND_NUM-i-j]).owner_color);//打印最后一行a
print_map_line(window_col()/2-WIDTH+WIDTH/2,1);
}
//显示地图上的玩家 当玩家重叠时,显示当前玩家
//切换玩家后使用update_map(game,0),显示当前玩家
int update_map(st_game *p,int n)
{
int i=0,tmp;
int player_num=get_game_pnum(p);
st_land_info *land;
//更新除当前玩家外各玩家的地图显示 因为玩家移动后可能清除其它玩家符号
for(;i<player_num-1;i++)
{
st_player *player=get_playern(p,(get_p_id(p)+i+1)%player_num);
tmp=get_player_step(player);
land=get_game_land(p,tmp);
set_land_symbol(land,get_player_symbol(player));
set_land_color(land,get_player_color(player));
}
//更新当前玩家的显示
int step=get_p_step(p); //现在玩家的步数
land=get_game_land(p,step);
if(get_land_symbol(land)== get_p_symbol(p)) //如果当前位置只有自己 则恢复为无
{
set_land_symbol(land,0); //清除land上的符号
set_land_color(land,None); //清除land的颜色
}
int new_step=(n+step)%LAND_NUM;
int x=0,y=0;
get_n_pos(new_step,&x,&y); //传入x=y=0,传出新坐标
//printf("x=%d,y=%d,pos=%d\n",x,y,new_step);
set_p_pos(p,x,y); //更新玩家的坐标
set_p_step(p,new_step); //更新玩家相对原点走过的步数 以索引land[]
land=get_game_land(p,new_step); //玩家走到新位置
set_land_symbol(land,get_p_symbol(p)); //设置新位置land上显示的符号
set_land_color(land,get_p_color(p)); //设置地块颜色
return 0;
}
int buy_land(st_game *game,st_land_info *land)
{
//计算土地价值
int x=0,y=0;
get_p_pos(game,&x,&y);
//st_land_info *land=get_game_land(game,pos);
int init_vlaue=get_map_value(game->p_map,x,y);
init_vlaue*=100;
if(get_land_price(land)!=0) init_vlaue=get_land_price(land); //购买已退出玩家的土地
//买地 更新land map p->money
int money=get_p_money(game);
if( money < init_vlaue)
{
printf("Sorry,your money %d are not enough to buy land that worth %d",money,init_vlaue);
return 1;
}
printf("land worth is $%d, Do you want to buy land? Y-N\n",init_vlaue);
if(is_yes())
{
set_p_money(game,money-init_vlaue);
set_land_player(land,get_player(game));
set_land_price(land,init_vlaue);
set_land_ownclor(land,get_p_color(game));
print_name(game);
printf(" spend %d on land\n",init_vlaue);
}
return 0;
}
int pay_rent(st_game *game)
{
//计算应付租金
int step=get_p_step(game);
st_land_info *pland=get_game_land(game,step);
st_player *player_pay=get_player(game);
st_player *house_owner=get_land_player(pland);
//p1->money-- p2->money++
int rent=get_land_price(pland)/2;
int player_money=get_player_money(player_pay);
printf("%s pay $%d to %s for rent\n",get_player_name(player_pay),\
rent,get_player_name(house_owner));
if(player_money < rent)
{
rent=player_money;
printf("player %s bankrupt!\n",get_player_name(player_pay));
player_quit(game,get_player_id(player_pay));
}
else
{
set_player_money(player_pay,get_player_money(player_pay)-rent);
}
set_player_money(house_owner,get_player_money(house_owner)+rent);
return 0;
//p1破产? bankrupt(game,id);
}
int game_over(st_game *game)
{
//只剩一人,游戏结束
//int win_col=window_col();
char name[MAX_NAME_LEN+10]={0};
sprintf(name,"%s Win",get_p_name(game));
print_bar(name,'=',get_p_color(game));
//最后的玩家在table的0位置
print_p_info(game,0);
print_bar("Game Over!",'=',Red);
deinit_game(&game);
exit(0);
}
//传入的是st_player中的id
void player_quit(st_game *game,int id)
{
//删除game中的指针 后继的指针前移 玩家数--
int player_num=get_game_pnum(game);
st_player *player;
st_land_info *pland=get_game_land(game,0);
int i,j,player_id;
char msg[MAX_NAME_LEN+64];
//在playertable中查找玩家
for(i=0;i<player_num;i++)
{
player=get_playern(game,i);
player_id=get_player_id(player);
if(player_id==id)
{
sprintf(msg,"player %s quit!\n",get_player_name(player));
print_color_str(msg,get_player_color(player));
pland=get_game_land(game,get_player_step(player));
set_land_symbol(pland,0);
set_land_color(pland,None);
//将土地归还系统
pland=get_game_land(game,0);
for(j=0;j<LAND_NUM;j++)
if(get_land_player(pland+j)!=NULL && get_player_id(get_land_player(pland+j))== player_id)
set_land_player(pland+j,NULL);
deinit_player(&player);
break;
}
}
//table中最后一名玩家不用移动指针,需更新id位置
if(i==(player_num-1))
set_game_id(game,0);
else
{
for(;i<player_num-1;i++)
((game)->p_player_tab)[i]=((game)->p_player_tab)[i+1];
}
set_game_pnum(game,--player_num);
if(player_num == 1)
game_over(game);
}
e_LandOwnerType whose_land(st_game *game,st_land_info *land)
{
int x,y;
st_player *player=get_player(game);
get_player_pos(player,&x,&y);
char e=get_map_e(game->p_ma
linux终端c编程练习 大富翁游戏
5星 · 超过95%的资源 需积分: 32 93 浏览量
2012-12-04
10:42:42
上传
评论
收藏 232KB RAR 举报
open_gg
- 粉丝: 1
- 资源: 9
最新资源
- 数据结构课程设计-全国交通出行咨询模拟系统C语言实现源码.zip
- cef-binary-109.0.1+gcd5e37a+chromium-109.0.5414.8-windows32
- 基于C语言的全国交通咨询系统模拟源码.zip
- 炫酷代码雨,超级炫酷哦!!!
- 基于物联网MQTT协议的智能停车场管理系统
- POETIZE个人博客系统源码 - 最美博客
- 基于深度学习的行人检测系统源码+项目说明(YoloV3+Tensorflow).zip
- 区块链系统:以比特币为基础
- 直接插入排序(Insertion Sort)是一种简单直观的排序算法.txt
- Python实现基于深度学习的预测区域电力负荷模型源码+项目说明(高分项目).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
前往页