/* * WarGames - a WOPR emulator written in C * Copyright (C) 2014 Franklin Wei * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Contact the author at contact@fwei.tk */ #include "gtnw.h" #include "location.h" #include "map.h" #include "util.h" #include #include #include #include #include static bool surrender=false; static int winner=0; /* on surrender */ static unsigned int max(long long a, long long b) { return a>b?a:b; } /* simulate a missile launch */ static void fire_missile(struct location_t* city) { int random=rand()%100; /* leave this at 100 for future adjustments */ int x=city->x, y=city->y; if(random>=90) /* crit */ { map[y][x]='!'; city->population=0; } else if(random>=60) /* major */ { map[y][x]='X'; city->population=max((double)city->population*(double).4-5000, 0); } else if(random>=30) /* minor */ { map[y][x]='*'; city->population=max((double)city->population*(double).6-2500, 0); } else if(random>=10) /* marginal */ { map[y][x]='x'; city->population=max((double)city->population*(double).8-1000, 0); } else /* miss */ { map[y][x]='O'; } } /* calculate populations of US+USSR by totaling the populations of each of their cities */ static void calc_pops(long long* us_pop, long long* ussr_pop) { *us_pop=0; *ussr_pop=0; /* calculate populations */ for(int i=0;iussr_back) { while(us_back!=ussr_back) { ussr_cities[ussr_back].print=false; ++ussr_back; } } us_cities[us_back].print=true; us_cities[us_back].print_name="Total"; us_cities[us_back].population=us_pop; ussr_cities[ussr_back].print=true; ussr_cities[ussr_back].print_name="Total"; ussr_cities[ussr_back].population=ussr_pop; ++us_back; ++ussr_back; print_string("\n\n"); char buf[512]; for(int i=0;i0 && move<5) good=true; } switch(move) { case 1: do_missile_launch(side); break; case 2: do_peace_talks(side); break; case 3: surrender=true; winner=side==1?2:1; break; case 4: break; } } /* play a game of Global Thermonuclear War! */ void global_thermonuclear_war(void) { srand(time(0)); // might want to move to main()... surrender=false; clear(); for(int i=0;i