void ribi::Chess::SquareSelector::DoSelect()
{
  assert(m_cursor);

  //No square selected
  if (!m_selected)
  {
    m_selected.reset(new Square(*m_cursor));
    m_signal_changed();
  }
  else
  {
    //A square is already selected

    //The selected piece is unselected
    if (*m_cursor == *m_selected)
    {
      const boost::shared_ptr<const Square> no_selection;
      m_selected = no_selection;
      m_signal_changed();
    }
    else
    {
      //Another square is selected
      m_selected.reset(new Square(*m_cursor));
    }
  }
}
예제 #2
0
void ribi::DrawCanvas::DrawEllipse(const double left, const double top, const double right, const double bottom) noexcept
{
  assert(left < right);
  assert(top < bottom);
  const double midx = (left + right) / 2.0;
  const double midy = (top + bottom) / 2.0;
  assert(midx > 0.0);
  const double pi = boost::math::constants::pi<double>();
  const double ray_horizontal = (right  - left) / 2.0;
  const double ray_vertical   = (bottom - top ) / 2.0;
  assert(ray_horizontal > 0.0);
  assert(ray_vertical > 0.0);
  const double average_ray    = (ray_horizontal + ray_vertical) / 2.0;
  assert(average_ray > 0.0);
  const double circumference = average_ray * pi * 2.0;
  const int n_steps = static_cast<int>(circumference + 0.5);
  assert(n_steps > 0);
  const double d_angle = 2.0 * pi / static_cast<double>(n_steps);
  assert(d_angle > 0.0);
  double angle = 0.0;
  for (int i=0; i!=n_steps; ++i)
  {
    const double x = midx + (std::sin(angle) * ray_horizontal);
    const double y = midy - (std::cos(angle) * ray_vertical);
    DrawDot(x,y);
    angle += d_angle;
  }
  m_signal_changed(this);
}
void ribi::tictactoe::Board::SetSummarizedState(const int original_state) noexcept
{
  if (GetSummarizedState() == original_state) return;

  assert(original_state >= 0);
  #ifndef NDEBUG
  if (original_state >= Helper().IntPower(3,9))
  {
    TRACE("ERROR");
  }
  #endif
  assert(original_state < Helper().IntPower(3,9));

  int s = original_state;
  for (int i=0; i!=9; ++i)
  {
    m_board[i/3][i%3] = StateToSquare(s % 3);
    s/=3;
  }

  //Internal check
  assert(GetSummarizedState()==original_state);

  m_signal_changed(this);
}
예제 #4
0
void ribi::DrawCanvas::DrawArc(
  const double left, const double top, const double right, const double bottom,
    const boost::units::quantity<boost::units::si::plane_angle> startAngle,
    const boost::units::quantity<boost::units::si::plane_angle> spanAngle) noexcept
{
  assert(left < right);
  assert(top < bottom);
  const double midx = (left + right) / 2.0;
  const double midy = (top + bottom) / 2.0;
  const double pi = boost::math::constants::pi<double>();
  const double ray_horizontal = (right  - left) / 2.0;
  const double ray_vertical   = (bottom - top ) / 2.0;
  const double average_ray    = (ray_horizontal + ray_vertical) / 2.0;
  const double arclength = average_ray * pi * 2.0 * (spanAngle.value() / (2.0 * pi));
  const int n_steps = std::abs(static_cast<int>(arclength + 0.5));
  if (n_steps == 0) return;
  assert(n_steps > 0);
  double angle { startAngle.value() };
  const double dAngle = spanAngle.value() / static_cast<double>(n_steps);
  for (int i=0; i!=n_steps; ++i)
  {
    double x = midx + (std::sin(angle) * ray_horizontal);
    double y = midy - (std::cos(angle) * ray_vertical);
    DrawDot(x,y);
    angle += dAngle;
  }
  m_signal_changed(this);
}
void ribi::Chess::SquareSelector::Click(
  const boost::shared_ptr<const Square> square,
  const bool can_select_square)
{
  assert(m_cursor);

  //No square selected: set cursor and selector on selected piece
  if (!m_selected)
  {
    m_cursor = square;
    if (can_select_square)
    {
      m_selected = square;
    }
    else
    {
      const boost::shared_ptr<const Square> no_selection;
      m_selected = no_selection;
    }
    m_signal_changed();
  }
  else
  {
    //A square is already selected

    //The selected piece is unselected: set cursor on unselected square, set selector to null
    if (*m_cursor == *m_selected)
    {
      m_cursor = square;
      const boost::shared_ptr<const Square> no_selection;
      m_selected = no_selection;
      m_signal_changed();
    }
    else
    {
      //Another square is selected: keep selector on selected piece
      //Don't care if move is valid:
      //- if move is valid,   piece   is moved and selector is removed
      //- if move is invalid, nothing is moved and selector is removed
      m_cursor = square;
      const boost::shared_ptr<const Square> no_selection;
      m_selected = no_selection;
    }
  }
  assert(m_cursor);
}
예제 #6
0
void ribi::Shape::SetRotation(const double rotation) noexcept
{
  if (m_rotation != rotation)
  {
    m_rotation = rotation;
    m_signal_changed();
  }
}
void ribi::tictactoe::Board::DoMove(const int x, const int y, const Player player) noexcept
{
  assert(CanDoMove(x,y));
  //std::clog << "Player " << m_current_player
  //  << ": (" << x << "," << y << ")\n";
  m_board[x][y] = Helper().PlayerToSquare(player);
  m_signal_changed(this);
}
예제 #8
0
void E42mcStateGeneratorWidget::newState(bool emit){
	delete state;
	state = new E42mcState(config);

	const pb::E42mcStateGeneratorConfig* sgc = dynamic_cast<const pb::E42mcStateGeneratorConfig*>(cfg_widget.getData());

	double e_p = sgc->e_p0();
	double phi_p = sgc->phi_p0();
	double e_m = sgc->e_m0();
	double phi_m = sgc->phi_m0();
	double a0 = sgc->a0();

//	double right = 2*M_PI;
//	double left = 0.0;

	int N = config->n();
	int sqN = sqrt(N);

//	double* psis = new double[N];
//	int i = 0;
//	for_each(psis, psis+N, [&i,N](double& d)->void{d = (double)i++ / N * (2*M_PI);});
//	random_shuffle(psis, psis+N);

	for(int i=0; i<sqN; i++){
		for(int j=0; j<sqN; j++){
			int k = i*sqN+j;

			//double psi = rand() / (double)RAND_MAX * (2*M_PI) + 0;
	//		double psi = psis[i];
			double z = i / (double)sqN;// * 5 + 0;
			double psi = j / (double)sqN * 2*M_PI;// * 5 + 0;
	//		double delta = rand() / (double)RAND_MAX * (2*config->delta_0()) - config->delta_0();

			pb::E42mcState::Particles p;
			p.set_x(0);
			p.set_y(0);
			p.set_z(z);
			p.set_xn(a0*cos(psi));//+0.1*sin(psi-2*M_PI*z));
			p.set_yn(a0*sin(psi));
			//p.set_a(a0*(1.0+0.2*sin(int(psi-z+phi))));

			state->mutable_particles(k)->CopyFrom(p);
		}
	}
//	delete psis;

	state->set_x_p(e_p*cos(phi_p));
	state->set_y_p(e_p*sin(phi_p));
	state->set_x_m(e_m*cos(phi_m));
	state->set_y_m(e_m*sin(phi_m));

//	center_masses();

	if(emit)
		m_signal_changed();
}
void ribi::tictactoe::Board::Restart() noexcept
{
  if (GetSummarizedState() == 0) return;

  for (int i=0; i!=9; ++i)
  {
    m_board[i/3][i%3] = Square::empty;
  }
  m_signal_changed(this);
}
예제 #10
0
void ribi::tictactoe::Game::Restart() noexcept
{
  m_board->Restart();

  if (m_current_player != Player::player1)
  {
    m_current_player = Player::player1;
    m_signal_changed(this);
  }
}
void ribi::TicTacToeWidget::PressKey(const TicTacToeWidget::Key key)
{
  switch (key)
  {
    case Key::up    : if (m_y > 0) --m_y; m_signal_changed(); break;
    case Key::right : if (m_x < 2) ++m_x; m_signal_changed(); break;
    case Key::down  : if (m_y < 2) ++m_y; m_signal_changed(); break;
    case Key::left  : if (m_x > 0) --m_x; m_signal_changed(); break;
    case Key::select:
    {
      if (m_tictactoe->CanDoMove(m_x,m_y))
      {
        m_tictactoe->DoMove(m_x,m_y);
        m_signal_changed();
      }
    }
    break;
  }
}
void ribi::tictactoe::Board::SetSquare(
  const int x, const int y, const Square square_state) noexcept
{
  if (m_board[x][y] == square_state) return;

  m_board[x][y] = square_state;

  //Internal test
  assert(GetSquare(x,y)==square_state);

  m_signal_changed(this);
}
예제 #13
0
void ribi::DrawCanvas::Clear() noexcept
{
  for (auto& row: m_canvas)
  {
    for (auto& cell:row)
    {
      cell = 0.0;
    }
  }

  #ifndef NDEBUG
  for (auto row: m_canvas)
  {
    assert(std::accumulate(row.begin(),row.end(),0.0) == 0.0);
  }
  #endif
  m_signal_changed(this);
}
예제 #14
0
void ribi::DrawCanvas::DrawCircle(const double xMid, const double yMid, const double ray) noexcept
{
  const double pi = boost::math::constants::pi<double>();
  const double circumference = ray * pi * 2.0;
  const int n_steps = static_cast<int>(circumference + 0.5);
  if (n_steps == 0) return;
  assert(n_steps > 0);
  const double dAngle = 2.0 * pi / static_cast<double>(n_steps);
  double angle = 0.0;
  for (int i=0; i!=n_steps; ++i)
  {
    double x = xMid + (std::sin(angle) * ray);
    double y = yMid - (std::cos(angle) * ray);
    DrawDot(x,y);
    angle += dAngle;
  }
  m_signal_changed(this);
}
예제 #15
0
void ribi::DrawCanvas::DrawLine(const double x1, const double y1, const double x2, const double y2) noexcept
{

  const double dX = x2 - x1;
  const double dY = y2 - y1;
  const double dist = std::sqrt( (dX * dX) + (dY * dY) );
  const double stepX = dX / dist;
  const double stepY = dY / dist;
  const int nSteps = static_cast<int>(dist + 0.5);
  double x = x1;
  double y = y1;
  for (int i=0; i!=nSteps; ++i)
  {
    DrawDot(x,y);
    x+=stepX;
    y+=stepY;
  }
  m_signal_changed(this);
}
예제 #16
0
void ribi::tictactoe::Game::SetSummarizedState(const int original_state) noexcept
{
  if (GetSummarizedState() == original_state) return;

  //MSB: player
  //LSB: board

  //9 for the board, 1 for the current player
  assert(original_state < Helper().IntPower(3,9 + 1));

  int s = original_state;
  m_board->SetSummarizedState(s % Helper().IntPower(3,9));
  s /= Helper().IntPower(3,9);
  m_current_player = StateToPlayer(s);

  //Internal check
  assert(GetSummarizedState()==original_state);

  m_signal_changed(this);
}
예제 #17
0
void ribi::DrawCanvas::DrawDot(const double x, const double y) noexcept
{
  //Assume a dot has dimensions 1.0 x 1.0
  //and x and y are exactly in the middle of this dot
  const double xBegin = x - 0.5;
  const double yBegin = y - 0.5;
  const double fracLeft = std::ceil(xBegin) - xBegin;
  const double fracTop  = std::ceil(yBegin) - yBegin;
  const int indexLeft = std::floor(xBegin);
  const int indexTop  = std::floor(yBegin);
  if (IsInRange(indexLeft  ,indexTop  ))
    m_canvas[indexTop  ][indexLeft  ] += (fracLeft * fracTop);
  if (IsInRange(indexLeft+1,indexTop  ))
    m_canvas[indexTop  ][indexLeft+1] += ((1.0-fracLeft) * fracTop);
  if (IsInRange(indexLeft  ,indexTop+1))
    m_canvas[indexTop+1][indexLeft  ] += (fracLeft * (1.0-fracTop));
  if (IsInRange(indexLeft+1,indexTop+1))
    m_canvas[indexTop+1][indexLeft+1] += ((1.0-fracLeft) * (1.0-fracTop));
  m_signal_changed(this);
}
void ribi::QtTicTacToeWidget::OnChanged(const TicTacToe * const)
{
  repaint();
  m_signal_changed(this);
}
void ribi::tictactoe::Board::SetBoard(const boost::multi_array<Square,2>& board) noexcept
{
  if (m_board == board) return;
  m_board = board;
  m_signal_changed(this);
}