diff --git a/Header.h b/Header.h new file mode 100644 index 0000000..1c42f59 --- /dev/null +++ b/Header.h @@ -0,0 +1,346 @@ +// +// Header.h +// CGame +// +// Created by Eric on 16/7/22. +// Copyright © 2016年 Bakantu Eric. All rights reserved. +// + +#ifndef Header_h +#define Header_h + +#include +#include +#include +#include + +typedef struct science{ + double powdev; + double lndcstdev; + double sldfitdev; + double sldadddev; + double pdtratdev; +}scc; + +typedef struct country { + char *name; + double store; + double sldfight; + unsigned int power; + double lndcoast; + double sldcoast; + double sldadd; + double pdtrat; + int form; + scc *p_scc; + struct country *n_cty; +}cty; + +typedef struct land { + int x; + int y; + double people; + double peoadd; + double product; + int soldiers; + cty *belong; + struct land *n_lnd; +}lnd; + +//CONST +int lnd_num; +char *c_str[] = {"汉", "唐","元","明","清","宋","秦"}; + +/*Function*/ + +//MAJOR +lnd *initlnd(int num); +cty *initcty(int num, int *form); +int givelnd(cty *p_cty, lnd *p_lnd); +int lndpdt(lnd *p_lnd); +int lndpeo(lnd *p_lnd); +int sldadd(cty *p_cty, lnd *p_lnd, double rat); +int ctycst(lnd *p_lnd); +int conquer(lnd *pf_lnd, lnd *pt_lnd, int g_sld); +int sldmove(lnd *pf_lnd, lnd *pt_lnd, int g_sld); +int anlywin(lnd * p_lnd); +lnd *find_lnd(lnd *p_lnd,int x, int y); +int srhpow(cty *p_cty, lnd *p_lnd); +int mryfree(lnd *p_lnd, cty *p_cty); + +//SCC +int fpowdev(cty *p_cty); +int flndcstdev(cty *p_cty); +int fsldfitdev(cty *p_cty); +int fpdtratdev(cty *p_cty); +int fpdtratdev(cty *p_cty); +int fladratdev(cty *p_cty); + + +//DECRIBE +lnd *initlnd(int num){ + srand((unsigned int)time(NULL)); + lnd *h_lnd = NULL, *pl_lnd = NULL; + int x = 0, y = 0; + for (int i = 0; i < num; i++){ + lnd *p_lnd = (lnd *) malloc(sizeof(lnd)); + if (h_lnd == NULL){ + h_lnd = p_lnd; + pl_lnd = h_lnd; + p_lnd->n_lnd = NULL; + } + else{ + pl_lnd->n_lnd = p_lnd; + p_lnd->n_lnd = NULL; + pl_lnd = p_lnd; + } + p_lnd->people = rand()%200+500; + p_lnd->peoadd = (rand()%10+1)*0.001; + p_lnd->belong = NULL; + p_lnd->product = rand()%6+3; + p_lnd->soldiers = 0; + p_lnd->x = x; + p_lnd->y = y++; + if(!(rand()%2)){ + x++; + y = 0; + } + } + return h_lnd; +} + +cty *initcty(int num, int *form){ + cty *h_cty = NULL, *pl_cty = NULL; + for (int i = 0; i < num; i++){ + cty *p_cty = (cty *) malloc(sizeof(cty)); + p_cty->p_scc = (scc *) malloc(sizeof(scc)); + if (h_cty == NULL){ + h_cty = p_cty; + pl_cty = p_cty; + p_cty->n_cty = NULL; + } + else{ + pl_cty->n_cty = p_cty; + p_cty->n_cty = NULL; + pl_cty = p_cty; + } + p_cty->form = form[i]; + p_cty->store = 0.0; + if (!form[i]){ + p_cty->sldadd = 0.2; + p_cty->sldcoast = 2.5; + p_cty->power = 6; + p_cty->lndcoast = 0.8; + p_cty->pdtrat = 0.4; + p_cty->sldfight = 1; + p_cty->p_scc->lndcstdev = 1100; + p_cty->p_scc->sldfitdev = 1150; + p_cty->p_scc->pdtratdev = 1200; + p_cty->p_scc->powdev = 1000; + p_cty->p_scc->sldadddev = 1010; + + } + else{ + p_cty->sldadd = 0.1; + p_cty->sldcoast = 4.5; + p_cty->power = 3; + p_cty->lndcoast = 2.6; + p_cty->pdtrat = 0.7; + p_cty->sldfight = 2; + p_cty->p_scc->lndcstdev = 800; + p_cty->p_scc->sldfitdev = 850; + p_cty->p_scc->pdtratdev = 850; + p_cty->p_scc->powdev = 1200; + p_cty->p_scc->sldadddev = 1010; + + } + + p_cty->name = c_str[i]; + + + } + return h_cty; +} + +int lndpdt(lnd *p_lnd){ + while (p_lnd != NULL){ + if (p_lnd->belong != NULL){ + p_lnd->belong->store += p_lnd->people * p_lnd->product * p_lnd->belong->pdtrat; + } + p_lnd = p_lnd->n_lnd; + } + return 0; +} + + + +int givelnd(cty *h_cty, lnd *h_lnd){ + cty *p_cty = h_cty; + lnd *p_lnd = h_lnd; + //Give lnad + while(p_cty != NULL){ + for(int i = 0; i < (rand()%(lnd_num-1))+1;i++) p_lnd = p_lnd->n_lnd; + p_lnd->belong = p_cty; + p_cty = p_cty->n_cty; + } + return 0; +} + +int lndpeo(lnd *p_lnd){ + while (p_lnd != NULL) { + p_lnd->people += p_lnd->people * p_lnd->peoadd; + p_lnd = p_lnd->n_lnd; + } + return 0; +} + +int sldadd(cty *p_cty, lnd *p_lnd, double rat){ + if (p_lnd->belong == p_cty && rat <= p_cty->sldadd){ + p_lnd->soldiers += p_lnd->people * rat; + p_lnd->people -= p_lnd->people * rat; + } + else return -1; + return 0; +} + +int ctycst(lnd *p_lnd){ + while(p_lnd != NULL){ + if (p_lnd->belong != NULL){ + cty *p_cty = p_lnd->belong; + p_cty->store -= p_cty->lndcoast * p_lnd->people + p_cty->sldcoast * p_lnd->soldiers; + } + p_lnd = p_lnd->n_lnd; + } + return 0; +} + +int conquer(lnd *pf_lnd, lnd *pt_lnd, int g_sld){ + if (pf_lnd ->belong != NULL && g_sld <= pf_lnd->soldiers){ + pf_lnd->soldiers -= g_sld; + if (pt_lnd->belong == NULL){ + pt_lnd->belong = pf_lnd->belong; + pt_lnd->soldiers = g_sld; + } + else{ + if(g_sld * pf_lnd->belong->sldfight > pt_lnd->soldiers * pt_lnd->belong->sldfight){ + pt_lnd->belong = pf_lnd->belong; + pt_lnd->soldiers = (g_sld * pf_lnd->belong->sldfight - pt_lnd->soldiers * pt_lnd-> belong->sldfight) + / pf_lnd->belong->sldfight; + } + else if (g_sld * pf_lnd->belong->sldfight < pt_lnd->soldiers * pt_lnd->belong->sldfight){ + pt_lnd->soldiers = (pt_lnd->soldiers * pt_lnd->belong->sldfight - g_sld * pf_lnd->belong->sldfight) + / pt_lnd->belong->sldfight; + } + else{ + pt_lnd->belong = NULL; + pt_lnd->soldiers = 0; + } + } + + } + else return -1; + return 0; +} + +int sldmove(lnd *pf_lnd, lnd *pt_lnd, int g_sld){ + if (pf_lnd->belong != NULL && pt_lnd ->belong != NULL && g_sld <= pf_lnd->soldiers){ + + pf_lnd->soldiers -= g_sld; + pt_lnd->soldiers += g_sld; + } + else return -1; + return 0; +} + +int fpowdev(cty *p_cty){ + double cost = sqrt( 2* p_cty->p_scc->powdev * p_cty->p_scc->powdev); + if (p_cty->store >= cost){ + p_cty->store -= cost; + p_cty->power = (double) p_cty->power * 1.5; + p_cty->p_scc->powdev = cost; + } + else return -1; + return 0; +} + +int flndcstdev(cty *p_cty){ + double cost = sqrt( 2* p_cty->p_scc->lndcstdev * p_cty->p_scc->lndcstdev); + if (p_cty->store >= cost){ + p_cty->store -= cost; + p_cty->lndcoast = (double) p_cty->lndcoast * 0.85; + p_cty->p_scc->lndcstdev = cost; + } + else return -1; + return 0; +} + +int fsldfitdev(cty *p_cty){ + double cost = sqrt(2 * p_cty->p_scc->sldfitdev * p_cty->p_scc->sldfitdev); + if (p_cty->store >= cost){ + p_cty->store -= cost; + p_cty->sldfight = (double) p_cty->sldfight * 1.25; + p_cty->sldcoast = (double) p_cty->sldcoast * 1.35; + p_cty->p_scc->sldfitdev = cost; + } + else return -1; + return 0; +} + +int fpdtratdev(cty *p_cty){ + double cost = sqrt( 2* p_cty->p_scc->pdtratdev * p_cty->p_scc->pdtratdev); + if (p_cty->store >= cost){ + p_cty->store -= cost; + p_cty->pdtrat = (double) p_cty->pdtrat * 1.15; + p_cty->p_scc->pdtratdev = cost; + } + else return -1; + return 0; +} + +int anlywin(lnd * p_lnd){ + + return 0; +} + +lnd *find_lnd(lnd *p_lnd,int x, int y){ + + while (p_lnd != NULL) { + if (p_lnd->x == x && p_lnd->y == y) return p_lnd; + p_lnd = p_lnd->n_lnd; + } + return NULL; +} + +int fladratdev(cty *p_cty){ + double cost = sqrt( 2 * p_cty->p_scc->sldadddev * p_cty->p_scc->sldadddev); + if (p_cty->store >= cost && p_cty->sldadd < 1){ + p_cty->store -= cost; + p_cty->sldadd = p_cty->sldadd * 1.15; + p_cty->p_scc->sldadddev = cost; + } + else return -1; + return 0; +} + +int srhpow(cty *p_cty, lnd *p_lnd){ + int num = 0; + while (p_lnd != NULL){ + if (p_lnd->belong == p_cty) num++; + p_lnd = p_lnd->n_lnd; + } + return num; +} + +int mryfree(lnd *p_lnd, cty *p_cty){ + while (p_lnd != NULL){ + free(p_lnd); + p_lnd = p_lnd->n_lnd; + } + while (p_cty != NULL){ + free(p_cty); + p_cty = p_cty->n_cty; + } + return 0; +} + +#endif /* Header_h */ diff --git a/Opengl.h b/Opengl.h new file mode 100644 index 0000000..888b03d --- /dev/null +++ b/Opengl.h @@ -0,0 +1,49 @@ +// +// Opengl.h +// CGame +// +// Created by Eric on 16/7/22. +// Copyright © 2016年 Bakantu Eric. All rights reserved. +// + +#ifndef Opengl_h +#define Opengl_h + +#include + +/*Var*/ +lnd *g_lnd = NULL; +cty *g_cty = NULL; +int *form = NULL; + +/*Function*/ + +//MAJOR +void oglinit(int argc, const char * argv[]); +void oglstart(void); +void game_engine(void); +void ctl_engine(int will); +void ogl_menu(void); +void ogl_game(void); +void ogl_gmenu(void); + +//DESCRIBE +void oglinit(int argc, const char * argv[]){ + glutInit(&argc, (char **) argv); + glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); + glutInitWindowSize(500, 300); + glutCreateWindow("CGame"); + glutDisplayFunc(&ogl_menu); + glutMainLoop(); +} + +void ogl_menu(void){ + glBegin(GL_POLYGON); + glVertex2f(0, 0); + glVertex2f(1, 1); + glVertex2f(1, 0); + glVertex2f(0, 1); +} + + +#endif /* Opengl_h */ diff --git a/Order.h b/Order.h new file mode 100644 index 0000000..599cc56 --- /dev/null +++ b/Order.h @@ -0,0 +1,114 @@ +// +// Order.h +// CGame +// +// Created by Eric on 16/7/23. +// Copyright © 2016年 Bakantu Eric. All rights reserved. +// + +#ifndef Order_h +#define Order_h + +int show_map(lnd *p_lnd); +int o_conquer(lnd *p_lnd, cty *p_cty); +int o_sldadd(lnd *p_lnd, cty *p_cty); +int o_movesld(lnd *p_lnd, cty *p_cty); + + +int show_map(lnd *p_lnd){ + int m_x = p_lnd->x; + while (p_lnd != NULL){ + if (m_x == p_lnd->x) { + if (p_lnd->belong != NULL) printf (" $#$ 人口: %.0f 产能t: %.2f 属于:%s $#$ ",p_lnd->people,p_lnd->product,p_lnd->belong->name); + else printf (" $#$ 人口: %.0f 产能t: %.2f 属于: 空 $#$ ",p_lnd->people,p_lnd->product); + + } + else{ + m_x = p_lnd->x; + printf("\n"); + if (p_lnd->belong != NULL) printf (" $#$ 人口: %.0f 产能t: %.2f 属于:%s $#$ ",p_lnd->people,p_lnd->product,p_lnd->belong->name); + else printf (" $#$ 人口: %.0f 产能t: %.2f 属于: 空 $#$ ",p_lnd->people,p_lnd->product); } + p_lnd = p_lnd->n_lnd; + } + printf("\n"); + return 0; +} + +int o_conquer(lnd *p_lnd, cty *p_cty){ + int b_x,b_y,d_x,d_y,snum; + printf("clear"); + printf("-----------------------------------------------------------------\n"); + printf("请输入本土坐标:(x)"); + scanf("%d",&b_x); + printf("请输入本土坐标:(y)"); + scanf("%d",&b_y); + printf("请输入敌坐标:(x)"); + scanf("%d",&d_x); + printf("请输入敌坐标:(y)"); + scanf("%d",&d_y); + printf("请输入投入兵力:"); + scanf("%d",&snum); + printf("-----------------------------------------------------------------\n"); + fflush(stdin); + lnd *b_lnd, *d_lnd; + b_lnd = find_lnd(p_lnd, b_x, b_y); + d_lnd = find_lnd(p_lnd, d_x, d_y); + if (srhpow(p_cty,p_lnd) < p_cty->power) + if (d_x == b_x+1 || d_x == b_x-1 || d_x == b_x) + if (d_y == b_y +1 || d_y == b_y-1 || d_y == b_y) + if (b_lnd != NULL && p_lnd != NULL && snum >= 75){ + if (!conquer(b_lnd, d_lnd, snum)) printf("占领目标成功\n"); + getchar(); + } + + return 0; +} + +int o_sldadd(lnd *p_lnd, cty *p_cty){ + int x, y; + lnd *ps_lnd; + double rat = 0; + printf("------------------------------------------------------------------\n"); + printf("输入征兵土地坐标:(x)"); + scanf("%d",&x); + printf("输入征兵土地坐标:(y)"); + scanf("%d",&y); + printf("输入征兵率:"); + scanf("%lf",&rat); + printf("------------------------------------------------------------------\n"); + fflush(stdin); + ps_lnd = find_lnd(p_lnd, x, y); + if (p_cty == ps_lnd->belong) { + if(!sldadd(ps_lnd->belong, ps_lnd, rat)) printf("征兵成功!\n"); + getchar(); + } + return 0; +} + +int o_movesld(lnd *p_lnd, cty *p_cty){ + int f_x, f_y, d_x, d_y, snum; + lnd *pf_lnd, *pt_lnd; + printf("------------------------------------------------------------------\n"); + printf("输入出发土地坐标:(x)"); + scanf("%d",&f_x); + printf("输入出发土地坐标:(y)"); + scanf("%d",&f_y); + printf("输入目的土地坐标:(x)"); + scanf("%d",&d_x); + printf("输入目的土地坐标:(y)"); + scanf("%d",&d_y); + printf("请输入兵力:"); + scanf("%d",&snum); + printf("------------------------------------------------------------------\n"); + fflush(stdin); + pf_lnd = find_lnd(p_lnd, f_x, f_y); + pt_lnd = find_lnd(p_lnd, d_x, d_y); + if (p_cty == pf_lnd->belong && p_cty == pt_lnd->belong){ + if (!sldmove(pf_lnd, pt_lnd, snum)) printf("移动兵力成功!\n"); + getchar(); + } + return 0; +} + + +#endif /* Order_h */ diff --git a/main.c b/main.c new file mode 100644 index 0000000..6b8e34b --- /dev/null +++ b/main.c @@ -0,0 +1,116 @@ +// +// main.c +// CGame +// +// Created by Eric on 16/7/22. +// Copyright © 2016年 Bakantu Eric. All rights reserved. +// + +#include "Header.h" +//#include "Opengl.h" +#include "Order.h" +#include "Keyboard.h" + + + +int main(int argc, const char * argv[]) { + int *form = (int *) malloc(sizeof(int) * 3); + lnd *p_lnd = initlnd(10); + form[0] = 0; + form[1] = 0; + form[2] = 1; + cty *p_cty = initcty(3, form); + char ch; + givelnd(p_cty, p_lnd); + init_keyboard(); + while (ch != 'q') { + ch = ' '; + system("clear"); + printf("MAP:\n"); + show_map(p_lnd); + printf("----------------------------------------------\n"); + printf("国家信息:\n"); + printf("国号:%s\n",p_cty->name); + printf("国库:%lf\n",p_cty->store); + if (!p_cty->form) printf("国家体制:社会主义\n"); + else printf("国家体制:资本主义\n"); + printf("土地花费:%.2lf\n",p_cty->lndcoast); + printf("土地升级花费:%.2lf\n",sqrt(2*p_cty->p_scc->lndcstdev*p_cty->p_scc->lndcstdev)); + printf("生产效率:%.2lf\n",p_cty->pdtrat); + printf("生产效率升级花费:%.2lf\n",sqrt(2*p_cty->p_scc->pdtratdev*p_cty->p_scc->pdtratdev)); + printf("士兵花费:%.2lf\n",p_cty->sldcoast); + printf("士兵战斗力:%.2lf\n",p_cty->sldfight); + printf("士兵升级花费:%.2lf\n",sqrt(2*p_cty->p_scc->sldfitdev*p_cty->p_scc->sldfitdev)); + printf("最大征兵百分比:%.2lf\n",p_cty->sldadd); + printf("最大征兵百分比升级花费:%.2lf\n",sqrt(2*p_cty->p_scc->sldadddev*p_cty->p_scc->sldadddev)); + printf("权力值:%d\n",p_cty->power); + printf("----------------------------------------------\n"); + printf("领土信息:\n"); + lnd *ps_lnd = p_lnd; + while(ps_lnd != NULL){ + if(ps_lnd->belong == p_cty){ + printf("----------------------------\n"); + printf("坐标:(%d,%d)\n",ps_lnd->x,ps_lnd->y); + printf("人口:%.0lf",ps_lnd->people); + printf("人口增长率: %.3lf ",ps_lnd->peoadd); + printf("生产力: %.2lf",ps_lnd->product); + printf("兵力:%d\n",ps_lnd->soldiers); + printf("\n"); + } + ps_lnd = ps_lnd->n_lnd; + } + if(kbhit()){ + ch = readch(); + switch (ch) { + case 'c': + close_keyboard(); + fflush(stdin); + o_conquer(p_lnd,p_cty); + init_keyboard(); + break; + case 'm': + close_keyboard(); + fflush(stdin); + o_movesld(p_lnd, p_cty); + init_keyboard(); + break; + case 'a': + close_keyboard(); + fflush(stdin); + o_sldadd(p_lnd, p_cty); + init_keyboard(); + break; + case 'p': + fpdtratdev(p_cty); + printf("PDT\n"); + break; + case 'f': + fsldfitdev(p_cty); + printf("SLDCST\n"); + break; + case 'l': + flndcstdev(p_cty); + printf("LNDCST\n"); + break; + case 'o': + fpowdev(p_cty); + printf("POWDEV\n"); + break; + case 'r': + fladratdev(p_cty); + printf("SLDADD\n"); + break; + default: + break; + } + } + lndpdt(p_lnd); + lndpeo(p_lnd); + ctycst(p_lnd); + usleep(1500000); + } + free(form); + mryfree(p_lnd, p_cty); + return 0; +} +