Skip to content

Instantly share code, notes, and snippets.

@meric
Created March 19, 2011 10:58
Show Gist options
  • Save meric/877388 to your computer and use it in GitHub Desktop.
Save meric/877388 to your computer and use it in GitHub Desktop.
#include "minefield.h"
#include "util.h"
#include "size.h"
#include "vector.h"
#include "spot.h"
/* create a new minefield */
minefield minefield_new(size s)
{
minefield m;
m.size = s;
int i = 0;
for(;i < s.width*s.height; i++) {
m.spots[i] = spot_new(index_to_vector(s, i));
}
return m;
}
/* get the spot of m with location loc */
spot minefield_spot(minefield m, vector loc)
{
return m.spots[vector_to_index(loc, m.size)];
}
/* create a copy of m with spot at loc set */
minefield minefield_spot_set(minefield m, vector loc, spot s)
{
m.spots[vector_to_index(loc, m.size)] = s;
return m;
}
/* transform a spot at loc, using a function that takes and returns a spot. */
/* this is a macro that saves m, loc from being repeated twice. */
/* use macroes *very* carefully */
#define minefield_spot_transform(m, loc, fn, ...) \
minefield_spot_set(m, loc, fn(minefield_spot(m, loc), __VA_ARGS__));
/* get the flagged of spot at loc */
BOOL minefield_flagged_at(minefield m, vector loc)
{
return minefield_spot(m, loc).flagged;
}
/* create a copy of m with flagged of spot at loc set */
minefield minefield_flagged_at_set(minefield m, vector loc, BOOL flagged)
{
return minefield_spot_transform(m, loc, spot_flagged_set, flagged);
}
/* get the flagged of spot at loc */
BOOL minefield_revealed_at(minefield m, vector loc)
{
return minefield_spot(m, loc).revealed;
}
/* create a copy of m with revealed of spot at loc set */
minefield minefield_revealed_at_set(minefield m, vector loc, BOOL revealed)
{
return minefield_spot_transform(m, loc, spot_revealed_set, revealed);
}
/* get the has_mine of spot at loc */
BOOL minefield_has_mine_at(minefield m, vector loc)
{
return minefield_spot(m, loc).has_mine;
}
/* create a copy of m with has_mine of spot at loc set */
minefield minefield_has_mine_at_set(minefield m, vector loc, BOOL has_mine)
{
return minefield_spot_transform(m, loc, spot_has_mine_set, has_mine);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment