#include "Gate.h"
#include <iostream.h>
#include "Dice.h"

// ************************** Gate ****************8

Gate::Gate(Boolean output)
:	_level(output)
{
}

Boolean Gate::output()
{	return _level;
}

void Gate::connect(Gate *)
{	// nothing
}



// ************************** UnaryGate ****************8

UnaryGate::UnaryGate()
:	Gate(),
	_input(NULL)
{
}

Boolean UnaryGate::output()
{	if(_input) return _input->output();
	Die roller(2);
	return Boolean(roller.roll() - 1);
}

void UnaryGate::connect(Gate * g)
{	_input = g;
}



// ************************** BinaryGate ****************8

BinaryGate::BinaryGate()
:	Gate(),
	_leftInput(NULL),
	_rightInput(NULL)
{
}

Boolean BinaryGate::output()
{	return Gate::output();
}

void BinaryGate::connect(Gate * g)
{	if(!_leftInput)_leftInput = g;
	else _rightInput = g;
}


// ************************** AndGate ****************8

AndGate::AndGate()
:	BinaryGate()
{
}

Boolean AndGate::output()
{  if(_leftInput && _rightInput)
		return Boolean(_leftInput->output() && _rightInput->output());
	return false;
}



// ************************** OrGate ****************8

OrGate::OrGate()
:	BinaryGate()
{
}

Boolean OrGate::output()
{  if(_leftInput && _rightInput)
		return Boolean(_leftInput->output() || _rightInput->output());
	return false;
}



// ************************** NotGate ****************8

NotGate::NotGate()
:	UnaryGate()
{
}

Boolean NotGate::output()
{	if(_input)return Boolean(!_input->output());
	return false;
}

// ************************** Switch ****************8

Switch::Switch(Boolean state) // false = off, true = on;
:	Gate(state)
{
}

void Switch::flip()
{	_level = Boolean(!_level);
}

void Switch::on()
{	_level = true;
}

void Switch::off()
{	_level = false;
}



