start on State class

This commit is contained in:
Josh Holtrop 2020-02-09 22:18:15 -05:00
parent 776bed33c7
commit d53cd1d0ad
3 changed files with 39 additions and 0 deletions

View File

@ -4,6 +4,7 @@ end
build do
Environment.new do |env|
env["D_IMPORT_PATH"] << "src"
env.Program("peg-puzzle", glob("src/**/*.d"))
end
end

View File

@ -1,7 +1,9 @@
import std.stdio;
import state;
int main(string[] args)
{
State s = new State(3);
writeln("Peg Puzzle");
return 0;
}

36
src/state.d Normal file
View File

@ -0,0 +1,36 @@
class State
{
private int m_size;
private bool[] m_pegs;
this(int size)
{
assert(size >= 3);
m_size = size;
const int n_pegs = (size * (size + 1)) / 2;
m_pegs = new bool[n_pegs];
m_pegs[] = true;
m_pegs[0] = false;
}
this(State s)
{
m_size = s.m_size;
m_pegs = s.m_pegs.dup;
}
private int peg_index(int row, int col)
{
return (row * (row + 1)) / 2 + col;
}
bool valid_position(int row, int col)
{
return (row >= 0) && (row < m_size) && (col >= 0) && (col <= row);
}
bool peg_present(int row, int col)
{
return m_pegs[peg_index(row, col)];
}
}