AZHenley / HarvestMemory

Program a bot using assembly-like code in this competitive game!
MIT License
58 stars 6 forks source link

HarvestMemory

A programming game written by Austin Henley for his software engineering courses at the University of Tennessee. Currently it is a graphical Python app that requires all the players' programs to be local in order to execute, and currently supports up to 30 players.

The object of the game is to harvest more fruit than any other player. Each player writes a program in an assembly-like language. Each program has access to shared memory where they can plant and harvest the fruit.

Harvest Memory's GUI.

Fruit

Fruits are represented in the memory by -100. Harvesting a fruit will yield +5 to your current fruit score. Attempting to harvest a location that does not contain fruit will result in the CPU scheduler penalizing your program.

You may also plant fruit. This will set the memory value to -1 and will automatically decrement 1 each CPU cycle. Once the value reaches -100, it becomes a harvestable fruit.

Fruit in memory can be destroyed by setting the value to any positive value.

All players start with 3 fruit.

The game begins with fruit randomly placed in the memory in groups. The groups are calculated based on an exponential decay function (e.g., 0.75^x where x is the initial memory location plus/minus an offset). This means that multiple fruit will likely be within 1-5 memory locations of each other, though they may be as far apart as 15.

CPU Scheduler

The CPU uses a round-robin scheduling algorithm with a fairness constraint. At the start of the game, it shuffles all players randomly. It will proceed to execute 1 instruction per player. If the instruction takes more than 1 cycle (as most of them do), then that player's program will not execute again until all other players' programs have been provided at least the same number of cycles.

For example: if Player A takes 1 cycle on their turn and Player B takes 10 cycles on their turn, then Player B will be skipped until Player A has taken at least 9 more cycles (the delta between the cycles consumed on their respective turns).

CPU Architecture

Registers, immediates, and memory values are unsigned 32-bit values. Memory addresses are 12-bit. You can not set a memory value to a negative value. Registers and immediates will wrap as expected. Memory values will wrap to a positive value. Memory addresses do not wrap.

The registers are:

Instruction set

The language allows 1 instruction or 1 label per line. A label takes the form: main:

Operands for instructions:

The instructions:

Op & operands Description CPU cycles
harvest a Harvests fruit at a. 5 (20 if it fails)
plant a Plants 1 of your fruits at a. 4
peek r, a Copies value at a to r. 4
poke a, v Sets a to v. 3
goto l Jumps to l. 1
ifequal v, v, l If v1 equals v2 then jump to l. 2
ifless v, v, l If v1 is less than v2 then jump to l. 2
ifmore v, v, l If v1 is greater than v2 then jump to l. 2
add r, v, v Sets r to v1 plus v2. 3
sub r, v, v Sets r to v1 minus v2. 3
mult r, v, v Sets r to v1 multiplied by v2. 5
div r, v, v Sets r to v1 divided by v2. 8
mod r, v, v Sets r to the remainder of v1 divided by v2. 7
random r, v, v Sets r to a random value between v1 and v2, inclusive. 6

Example program

This program will check random locations for fruit. If it finds one, it harvests it.

main:
    random r0, 0, 4095
    ifequal $r0, -100, found
    goto main
found:
    harvest $r0
    goto main