#include #include #include /* risk.c * author: greg hamerly (greg_hamerly at baylor dot edu) * * This program was inspired by a long night of playing the board game "Risk". * The mechanics of this game can become very tiresome after a while, since it * requires a lot of dice-rolling by players. This program expedites the rolling * process by assuming that the attacker will attack an opponent until either * side loses (either the attacker has only one army left, or the defender loses * all armies). It also assumes that the attacker will always attack with all * his armies, and likewise the defender will always defend with all his armies * (thus giving each the best chance). These are reasonable assumptions. * * Copyright (C) 2002 Greg Hamerly (greg_hamerly at baylor dot edu) * * 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 2 * 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, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * or see http://www.gnu.org/copyleft/gpl.html */ int min(int a, int b) { return a < b ? a : b; } int max(int a, int b) { return a > b ? a : b; } int cmpInt(const void *a, const void *b) { const int *x = (const int *)a; const int *y = (const int *)b; return *y - *x; } int main(int argc, char **argv) { int attackers, defenders, r, i, roll; int attackDice[3], defendDice[2]; int numAttackDice, numDefendDice; srand(time(NULL)); if (argc < 3) { printf("usage: %s numattackers numdefenders\n", argv[0]); return 0; } attackers = atoi(argv[1]); defenders = atoi(argv[2]); printf("number of attackers: %d\n", attackers); printf("number of defenders: %d\n", defenders); roll = 0; while ((attackers > 1) && (defenders > 0)) { roll++; numAttackDice = min(3, attackers - 1); numDefendDice = min(2, defenders); for (i = 0; i < numAttackDice; i++) { attackDice[i] = (rand() % 6) + 1; } for (i = 0; i < numDefendDice; i++) { defendDice[i] = (rand() % 6) + 1; } qsort(attackDice, numAttackDice, sizeof(int), cmpInt); qsort(defendDice, numDefendDice, sizeof(int), cmpInt); printf("------------------------------------\n"); printf("roll # %d [attacking: %d, defending: %d]\n", roll, attackers, defenders); printf("attacks: "); for (i = 0; i < numAttackDice; i++) { printf("%d ", attackDice[i]); } printf("\n"); printf("defends: "); for (i = 0; i < numDefendDice; i++) { printf("%d ", defendDice[i]); } printf("\n"); for (i = 0; i < min(numAttackDice, numDefendDice); i++) { printf("dice %d: %d versus %d: ", i + 1, attackDice[i], defendDice[i]); if (attackDice[i] > defendDice[i]) { printf("attack won\n"); defenders--; } else { printf("defender won\n"); attackers--; } } } printf("====================================\n"); printf("Results:\n"); printf("attackers: %d men left\n", attackers); printf("defenders: %d men left\n", defenders); return 0; }