Assignment 4: Blotto Competition

Objectives

Introduction

Blotto is a simultaneous game for two players in which the players distribute military units over a number of battlefields each worth some number of points that represent the battlefields' strategic value (Note that the linked article discusses a limited version in which each battlefield is worth the same amount; we allow for a different value for each battlefield, and if you prefer a more peaceful context, Blotto can be seen a a model of allocating campaign resources over contested areas, or spreading advertising resources across various media outlets.) Each player has a set number of units to distribute, each unit is equivalent to each other unit, and a unit must be allocated entirely to a single battlefield. For each battlefield, the player who allocates more units to that battlefield wins the points for it (with the points split evenly in case of a tie, including a 0-0 tie), and the winner is the player with the most total points.

For example, suppose there are four battlefields worth 1, 2, 3, and 4 points respectively and each player has 10 units to distribute. If player 1's distribution is $(2, 2, 2, 4)$ and player 2's distribution is $(3, 4, 2, 1)$ then player 1 wins battlefield 4, player 2 wins battlefields 1 and 2, and the players tie on battlefield 3. Player 1's score is then $1.5 + 4 = 5.5$ and player 2's score is $1 + 2 + 1.5 = 4.5$. Player 1 wins.

Overview

Write a program called Blotto that reads players' distributions from standard input, along with a list of head-to-head matchups between pairs of those players. Output gives the result of each matchup, where, for each matchup, points are awarded to each player based on who placed more units in each battlefield, and the value of the battlefields given on the command line. For example, if the file blotto.in contains
P1,2,2,2,3
P2,3,3,2,1
P3,2,2,5,0

P1 P2
P1 P3
P2 P3
then running ./Blotto 1 2 3 4 < blotto.in should result in output
P1 5.5 - P2 4.5
P1 5.5 - P3 4.5
P2 7.0 - P3 3.0

Implementing the gmap ADT

You must implement the gmap ADT according to the specification in /c/cs223/hw4/Required/gmap.h, which can then be used to store the players' distributions as you read them from standard input for retrieval later when you read the matchups. The gmap ADT defines a map from generic keys to generic values, with the functions used for hashing, copying, comparing, and freeing the keys passed to the map upon creation.

The gmap functions must run in the following time bounds, where $n$ is the number of key/value pairs in the map:

These bounds assume that the key operations (copy, compare, hash, and free), and malloc and free run in $O(1)$ time. Expected running times are under the assumption that the distribution of keys and the hash function are such that the hash values of the keys are distributed randomly and uniformly across the range of the size_t type.

Your ADT will be tested with a series of unit tests that test individual functions. An incomplete suite of unit tests is implemented in /c/cs223/hw4/Required/gmap_unit.c (with supporting modules gmap_test_functions and string_key in the same directory) which will be replaced with a complete test suite for the private test script. Your makefile must include a target GmapUnit that is built from those files and your implementation of the gmap module. All unit tests must pass with no errors reported from Valgrind, and any use of your gmap module that doesn't violate the functions' preconditions must result in no errors from Valgrind. No public or private tests will violate the preconditions, and we will not test out-of-memory conditions for this part or the Blotto executable described below.

Note that although you may not use all of the functions provided by the gmap module when writing the Blotto program, you must implement all of them for this part of the assignment.

Your Blotto program must run in expected $O(n + m)$ time for a fixed number of battlefields and total number of units, where $n$ is the number of entries and $m$ is the number of matchups, under the assumption that malloc and free run in $O(1)$ time.

Details

Command-line Arguments

Input

Output

If any of the input to the Blotto program (standard input or the command-line arguments) is invalid for any reason, then the output can be anything and your program must not crash or hang.

Hints and Other Considerations

Submissions

Submit your makefile, log file, and source code necessary to build the executables Blotto and GmapUnit using your makefile. Your makefile should assume that the files from /c/cs223/hw4/Required have been copied into the current directory but not that they are still in their original locations, and you should not submit those files.