Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) GamePaperRockScissors

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./GamePaperRockScissors/GamePaperRockScissorsConsole.pro

 

include(../../ConsoleApplication.pri)

include(../../Libraries/Boost.pri)

include(../../Classes/CppAbout/CppAbout.pri)
include(../../Classes/CppFileIo/CppFileIo.pri)
include(../../Classes/CppHelp/CppHelp.pri)
include(../../Classes/CppMenuDialog/CppMenuDialog.pri)
include(../../Classes/CppRichelBilderbeekProgram/CppRichelBilderbeekProgram.pri)
include(../../Classes/CppTrace/CppTrace.pri)

include(GamePaperRockScissorsConsole.pri)

#Won't link: no source present
#SOURCES += main.cpp

 

 

 

 

 

Qt project file: ./GamePaperRockScissors/GamePaperRockScissorsDesktop.pro

 

include(../../DesktopApplication.pri)

include(../../Libraries/Boost.pri)

include(../../Classes/CppAbout/CppAbout.pri)
include(../../Classes/CppFileIo/CppFileIo.pri)
include(../../Classes/CppHelp/CppHelp.pri)
include(../../Classes/CppMenuDialog/CppMenuDialog.pri)
include(../../Classes/CppRichelBilderbeekProgram/CppRichelBilderbeekProgram.pri)
include(../../Classes/CppTrace/CppTrace.pri)

include(../../Classes/CppQtAboutDialog/CppQtAboutDialog.pri)
include(../../Classes/CppQtHideAndShowDialog/CppQtHideAndShowDialog.pri)

include(GamePaperRockScissorsDesktop.pri)

#Won't link: no main present
#SOURCES += qtmain.cpp

 

 

 

 

 

Qt project file: ./GamePaperRockScissors/GamePaperRockScissorsWebsite.pro

 

QT       -= core
QT       -= gui
  
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
INCLUDEPATH += \
  ../../Classes/CppAbout \
  ../../Classes/CppTrace \
  ../../Classes/CppWtAboutDialog

SOURCES += \
  ../../Classes/CppAbout/about.cpp \
  ../../Classes/CppWtAboutDialog/wtaboutdialog.cpp \
  wtmain.cpp \
  wtpaperrockscissorsdialog.cpp \
  wtpaperrockscissorsdialogstate.cpp \
  wtpaperrockscissorsevent.cpp \
  wtpaperrockscissorsserver.cpp

HEADERS += \
  ../../Classes/CppAbout/about.h \
  ../../Classes/CppTrace/trace.h \
  ../../Classes/CppWtAboutDialog/wtaboutdialog.h \
  wtpaperrockscissorsdialog.h \
  wtpaperrockscissorsdialogstate.h \
  wtpaperrockscissorsevent.h \
  wtpaperrockscissorsserver.h

#
#
# Type of compile
#
#

CONFIG(release, debug|release) {
  DEFINES += NDEBUG NTRACE_BILDERBIKKEL
}

QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++

unix {
  QMAKE_CXXFLAGS += -Werror
}

#
#
# Boost
#
#

win32 {
  INCLUDEPATH += \
    ../../Libraries/boost_1_54_0
}

#
#
# Wt
#
#

unix {
  LIBS += -lwt -lwthttp
}

 

 

 

 

 

./GamePaperRockScissors/GamePaperRockScissorsConsole.pri

 

INCLUDEPATH += \
    ../../Games/GamePaperRockScissors

SOURCES += \
    ../../Games/GamePaperRockScissors/paperrockscissorsmenudialog.cpp

HEADERS  += \
    ../../Games/GamePaperRockScissors/paperrockscissorsmenudialog.h

OTHER_FILES += \
    ../../Games/GamePaperRockScissors/Licence.txt

 

 

 

 

 

./GamePaperRockScissors/GamePaperRockScissorsDesktop.pri

 

include(../../Games/GamePaperRockScissors/GamePaperRockScissorsConsole.pri)

 

 

 

 

 

./GamePaperRockScissors/GamePaperRockScissorsWebsite.pri

 

include(../../Games/GamePaperRockScissors/GamePaperRockScissorsConsole.pri)

SOURCES +=

HEADERS +=

 

 

 

 

 

./GamePaperRockScissors/paperrockscissorsmenudialog.h

 

#ifndef PAPERROCKSCISSORSMENUDIALOG_H
#define PAPERROCKSCISSORSMENUDIALOG_H

#include "menudialog.h"

namespace ribi {

struct PaperRockScissorsMenuDialog final : public MenuDialog
{
  About GetAbout() const noexcept override;
  Help GetHelp() const noexcept override;
  boost::shared_ptr<const Program> GetProgram() const noexcept override;
  std::string GetVersion() const noexcept override;
  std::vector<std::string> GetVersionHistory() const noexcept override;

  private:
  int ExecuteSpecific(const std::vector<std::string>& argv) noexcept override;

  #ifndef NDEBUG
  static void Test() noexcept;
  #endif
};

} //~namespace ribi

#endif // PAPERROCKSCISSORSMENUDIALOG_H

 

 

 

 

 

./GamePaperRockScissors/paperrockscissorsmenudialog.cpp

 

#include "paperrockscissorsmenudialog.h"

#include <cassert>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdexcept>

#include "testtimer.h"
#include "richelbilderbeekprogram.h"
#include "trace.h"

int ribi::PaperRockScissorsMenuDialog::ExecuteSpecific(const std::vector<std::string>& argv) noexcept
{
  #ifndef NDEBUG
  Test();
  #endif
  const int argc = static_cast<int>(argv.size());
  if (argc == 1)
  {
    std::cout << GetHelp() << '\n';
    return 1;
  }
  std::cout
    << this->GetAbout().GetFileTitle() << " cannot be run in console mode\n"
    << std::endl;
  return 0;
}

ribi::About ribi::PaperRockScissorsMenuDialog::GetAbout() const noexcept
{
  About a(
    "Richel Bilderbeek",
    "PaperRockScissors",
    "PRS cellular automaton",
    "today",
    "2000x-2014",
    "http://www.richelbilderbeek.nl/GamePaperRockScissors.htm",
    GetVersion(),
    GetVersionHistory());
  //a.AddLibrary("QtDialWidget version: " + QtDialWidget::GetVersion());
  return a;
}

ribi::Help ribi::PaperRockScissorsMenuDialog::GetHelp() const noexcept
{
  return Help(
    this->GetAbout().GetFileTitle(),
    this->GetAbout().GetFileDescription(),
    {

    },
    {

    }
  );
}

boost::shared_ptr<const ribi::Program> ribi::PaperRockScissorsMenuDialog::GetProgram() const noexcept
{
  const boost::shared_ptr<const ribi::Program> p {
    new ProgramPaperRockScissors
  };
  assert(p);
  return p;
}

std::string ribi::PaperRockScissorsMenuDialog::GetVersion() const noexcept
{
  return "0.1";
}

std::vector<std::string> ribi::PaperRockScissorsMenuDialog::GetVersionHistory() const noexcept
{
  return {
    "2013-07-13: version 0.1: conformized to ProjectRichelBilderbeekConsoleS"
  };
}

#ifndef NDEBUG
void ribi::PaperRockScissorsMenuDialog::Test() noexcept
{
  {
    static bool is_tested{false};
    if (is_tested) return;
    is_tested = true;
  }
  const TestTimer test_timer(__func__,__FILE__,1.0);
}
#endif

 

 

 

 

 

./GamePaperRockScissors/wtmain.cpp

 

#include <iostream>

#include <boost/program_options.hpp>

#include <Wt/WApplication>
#include <Wt/WEnvironment>
#include <Wt/WText>

#include "trace.h"
#include "wtpaperrockscissorsdialog.h"

struct WtPaperRockScissorsApplication : public Wt::WApplication
{
  WtPaperRockScissorsApplication(const Wt::WEnvironment& env);
};

WtPaperRockScissorsApplication::WtPaperRockScissorsApplication(
  const Wt::WEnvironment& env)
  : WApplication(env)
{
  setTitle("PaperRockScissors");
  this->useStyleSheet("wt.css");
  root()->addWidget(new ribi::WtPaperRockScissorsDialog);
}

Wt::WApplication *createApplication(const Wt::WEnvironment& env)
{
  return new WtPaperRockScissorsApplication(env);
}

int main(int argc, char **argv)
{
  START_TRACE();
  // Declare the supported options.
  boost::program_options::options_description d(
    "Allowed options for CppWtExample3");
  std::string docroot;
  std::string http_address;
  std::string http_port;
  d.add_options()
      ("help",
        "produce this help message")
      ("docroot",
         boost::program_options::value<std::string>(&docroot)->default_value("."),
         "the docroot")
      ("http-address",
         boost::program_options::value<std::string>(&http_address)->default_value("0.0.0.0"),
         "the server's http address")
      ("http-port",
         boost::program_options::value<std::string>(&http_port)->default_value("8080"),
         "the server's http port")
      ;

  boost::program_options::variables_map m;
  boost::program_options::store(
    boost::program_options::parse_command_line(
      argc, argv, d), m);
  boost::program_options::notify(m);

  if (m.count("help"))
  {
    //Display the options_description
    std::cout << d << "\n";
    return 0;
  }

  //Create the arguments in std::string format
  std::vector<std::string> v(7);
  v[0] = argv[0];
  v[1] = "--docroot";
  v[2] = docroot;
  v[3] = "--http-address";
  v[4] = http_address;
  v[5] = "--http-port";
  v[6] = http_port;

  //Convert the arguments to char* format
  std::vector<char*> w(7);
  for (int i=0; i!=7; ++i) w[i] = &v[i][0];

  //Give Wt the modified parameters
  return WRun(w.size(), &w[0], &createApplication);
}

 

 

 

 

 

./GamePaperRockScissors/wtpaperrockscissorsdialog.h

 

//---------------------------------------------------------------------------
/*
PaperRockScissors, paper-rock-scissors game
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolPaperRockScissors.htm
//---------------------------------------------------------------------------
#ifndef WTPAPERROCKSCISSORSDIALOG_H
#define WTPAPERROCKSCISSORSDIALOG_H

#include <string>
#include <vector>

#include <Wt/WContainerWidget>

#include <boost/signals.hpp>
#include <boost/signals2.hpp>

#include "about.h"

namespace Wt
{
  struct WApplication;
  struct WButtonGroup;
  struct WLabel;
  struct WPushButton;
}

namespace ribi {

struct WtPaperRockScissorsDialogState;
struct WtPaperRockScissorsEvent;
struct WtPaperRockScissorsServer;
struct WtPaperRockScissorsDialogState;

struct WtPaperRockScissorsDialog : public Wt::WContainerWidget
{
  typedef WtPaperRockScissorsDialogState State;
  WtPaperRockScissorsDialog(Wt::WContainerWidget *parent = 0);
  ~WtPaperRockScissorsDialog();

  State * GetStateAbout() const;
  State * GetStateConnect() const;
  State * GetStateGame() const;
  State * GetStateLogin() const;
  void SetState(State * const state);

  void Send();

  ///m_signal_close is emitted when program closes
  mutable boost::signals2::signal<void ()> m_signal_close;

  //Server has something to broadcast, called from another session
  void ServerEvent(const WtPaperRockScissorsEvent& event);

  ///connection to server
  boost::signals::connection m_event_connection;

private:
  Wt::WApplication * const m_application;
  WtPaperRockScissorsServer * const m_server;

  State * m_state;
  const boost::scoped_ptr<State> m_state_about;
  const boost::scoped_ptr<State> m_state_connect;
  const boost::scoped_ptr<State> m_state_login;
  const boost::scoped_ptr<State> m_state_game;

  void OnClose();
  void Show();

  public:
  static const std::vector<std::string> GetVersionHistory();
  static const std::string GetVersion();
  static const About GetAbout();

};

} //~namespace ribi

#endif // WTPAPERROCKSCISSORSDIALOG_H

 

 

 

 

 

./GamePaperRockScissors/wtpaperrockscissorsdialog.cpp

 

//---------------------------------------------------------------------------
/*
PaperRockScissors, paper-rock-scissors game
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolPaperRockScissors.htm
//---------------------------------------------------------------------------
#include <boost/lexical_cast.hpp>
#include <boost/numeric/conversion/cast.hpp>

#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WButtonGroup>
#include <Wt/WContainerWidget>
#include <Wt/WGroupBox>
#include <Wt/WLabel>
#include <Wt/WRadioButton>
#include <Wt/WPushButton>

#include "about.h"
#include "wtaboutdialog.h"
#include "wtpaperrockscissorsdialog.h"
#include "wtpaperrockscissorsserver.h"
#include "wtpaperrockscissorsdialogstate.h"

ribi::WtPaperRockScissorsDialog::WtPaperRockScissorsDialog(Wt::WContainerWidget *parent)
  : WContainerWidget(parent),
    m_application(Wt::WApplication::instance()),
    m_server(WtPaperRockScissorsServer::GetInstance()),
    m_state_about(new WtPaperRockScissorsDialogStateAbout(this,WtPaperRockScissorsServer::GetInstance(),0)),
    m_state_connect(new WtPaperRockScissorsDialogStateConnect(this,WtPaperRockScissorsServer::GetInstance())),
    m_state_login(new WtPaperRockScissorsDialogStateLogin(this,WtPaperRockScissorsServer::GetInstance())),
    m_state_game(new WtPaperRockScissorsDialogStateGame(this,WtPaperRockScissorsServer::GetInstance()))
{
  //Need to have Wt::WApplication::instance() at the
  //constructor of the class, to have it directly
  //accessible in ribi::WtPaperRockScissorsDialog::PollChangedEvent

  this->setContentAlignment(Wt::AlignCenter);
  this->SetState(this->GetStateLogin());
  Show();

  // this widget supports server-side updates its PollChangedEvent()
  // member function is connected to a slot that is triggered from outside this
  // session's event loop when another user selects another option.
  m_application->enableUpdates();
}

ribi::WtPaperRockScissorsDialog::~WtPaperRockScissorsDialog()
{
  m_state->Disconnect();
  m_signal_close();
}

const ribi::About ribi::WtPaperRockScissorsDialog::GetAbout()
{
  About a(
    "Richel Bilderbeek",
    "PaperRockScissors",
    "time poll web application",
    "the 13th of April 2011",
    "2011",
    "http://www.richelbilderbeek.nl/ToolPaperRockScissors.htm",
    GetVersion(),
    GetVersionHistory());
  return a;
}

ribi::WtPaperRockScissorsDialogState * ribi::WtPaperRockScissorsDialog::GetStateAbout() const
{
  assert(m_state_about);
  return this->m_state_about.get();
}

ribi::WtPaperRockScissorsDialogState * ribi::WtPaperRockScissorsDialog::GetStateConnect() const
{
  assert(m_state_connect);
  return this->m_state_connect.get();
}

ribi::WtPaperRockScissorsDialogState * ribi::WtPaperRockScissorsDialog::GetStateGame() const
{
  assert(m_state_game);
  return this->m_state_game.get();
}

ribi::WtPaperRockScissorsDialogState * ribi::WtPaperRockScissorsDialog::GetStateLogin() const
{
  assert(m_state_login);
  return this->m_state_login.get();
}

const std::string ribi::WtPaperRockScissorsDialog::GetVersion()
{
  return "1.0";
}

const std::vector<std::string> ribi::WtPaperRockScissorsDialog::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-04-15: Version 1.0: initial version");
  return v;
}

void ribi::WtPaperRockScissorsDialog::OnClose()
{
  m_state->Disconnect();
  m_signal_close();
}

void ribi::WtPaperRockScissorsDialog::SetState(State * const state)
{
  assert(state);
  m_state = state;
  Show();
}

void ribi::WtPaperRockScissorsDialog::Show()
{
  m_state->Show();
}

///Called by server triggered from other session
void ribi::WtPaperRockScissorsDialog::ServerEvent(const WtPaperRockScissorsEvent& event)
{
  Wt::WApplication::UpdateLock lock = m_application->getUpdateLock();

  m_state->ProcessEvent(event);

  m_application->triggerUpdate();
}

 

 

 

 

 

./GamePaperRockScissors/wtpaperrockscissorsdialogstate.h

 

#ifndef WTPAPERROCKSCISSORSDIALOGSTATE_H
#define WTPAPERROCKSCISSORSDIALOGSTATE_H

#include <boost/checked_delete.hpp>

#include <Wt/WObject>


namespace ribi {

struct WtPaperRockScissorsDialog;
struct WtPaperRockScissorsServer;
struct WtPaperRockScissorsEvent;

struct WtPaperRockScissorsDialogState : public Wt::WObject
{
  WtPaperRockScissorsDialogState(
    WtPaperRockScissorsDialog * const dialog,
    WtPaperRockScissorsServer * const server);

  void Connect();
  void Disconnect();
  ///ProcessEvent processes an event given by the server, when
  ///another user does something we need to be noted of
  virtual void ProcessEvent(const WtPaperRockScissorsEvent& event) = 0;

  void SetStateAbout(WtPaperRockScissorsDialogState * const prev_state);

  virtual void Show() = 0;

  protected:
  virtual ~WtPaperRockScissorsDialogState();
  //Do not forget the template brackets, as stated in
  //Herb Sutter. Exceptional C++ style. 2005. ISBN: 0-201-76042-8. Item 8: 'Befriending templates'.
  friend void boost::checked_delete<>(WtPaperRockScissorsDialogState* x);

  protected:
  WtPaperRockScissorsDialog * const m_dialog;
  WtPaperRockScissorsServer * const m_server;
};

struct WtPaperRockScissorsDialogStateAbout
  : public WtPaperRockScissorsDialogState
{
  WtPaperRockScissorsDialogStateAbout(
    WtPaperRockScissorsDialog * const dialog,
    WtPaperRockScissorsServer * const server,
    WtPaperRockScissorsDialogState * const prev_state);
  void ProcessEvent(const WtPaperRockScissorsEvent& event);

  void SetOnCloseState(WtPaperRockScissorsDialogState * const prev_state);
  void Show();

  private:
  virtual ~WtPaperRockScissorsDialogStateAbout() {}
  //Do not forget the template brackets, as stated in
  //Herb Sutter. Exceptional C++ style. 2005. ISBN: 0-201-76042-8. Item 8: 'Befriending templates'.
  friend void boost::checked_delete<>(WtPaperRockScissorsDialogStateAbout* x);

  WtPaperRockScissorsDialogState * m_prev_state;

  void OnClose();
};

struct WtPaperRockScissorsDialogStateConnect
  : public WtPaperRockScissorsDialogState
{
  WtPaperRockScissorsDialogStateConnect(
    WtPaperRockScissorsDialog * const dialog,
    WtPaperRockScissorsServer * const server);

  void ProcessEvent(const WtPaperRockScissorsEvent& event);
  void Show();

  private:
  virtual ~WtPaperRockScissorsDialogStateConnect() {}
  //Do not forget the template brackets, as stated in
  //Herb Sutter. Exceptional C++ style. 2005. ISBN: 0-201-76042-8. Item 8: 'Befriending templates'.
  friend void boost::checked_delete<>(WtPaperRockScissorsDialogStateConnect* x);
};

struct WtPaperRockScissorsDialogStateGame
  : public WtPaperRockScissorsDialogState
{
  WtPaperRockScissorsDialogStateGame(
    WtPaperRockScissorsDialog * const dialog,
    WtPaperRockScissorsServer * const server);

  void ProcessEvent(const WtPaperRockScissorsEvent& event);
  void Send();
  void Show();

  private:
  virtual ~WtPaperRockScissorsDialogStateGame() {}
  //Do not forget the template brackets, as stated in
  //Herb Sutter. Exceptional C++ style. 2005. ISBN: 0-201-76042-8. Item 8: 'Befriending templates'.
  friend void boost::checked_delete<>(WtPaperRockScissorsDialogStateGame* x);

  Wt::WButtonGroup * m_group;
};

struct WtPaperRockScissorsDialogStateLogin
  : public WtPaperRockScissorsDialogState
{
  WtPaperRockScissorsDialogStateLogin(
    WtPaperRockScissorsDialog * const dialog,
    WtPaperRockScissorsServer * const server);

  void ProcessEvent(const WtPaperRockScissorsEvent& event);
  void Show();

  private:
  virtual ~WtPaperRockScissorsDialogStateLogin() {}
  //Do not forget the template brackets, as stated in
  //Herb Sutter. Exceptional C++ style. 2005. ISBN: 0-201-76042-8. Item 8: 'Befriending templates'.
  friend void boost::checked_delete<>(WtPaperRockScissorsDialogStateLogin* x);

  Wt::WLineEdit * m_edit_username;
  void OnEditChanged();
};

} //~namespace ribi

#endif // WTPAPERROCKSCISSORSDIALOGSTATE_H

 

 

 

 

 

./GamePaperRockScissors/wtpaperrockscissorsdialogstate.cpp

 


#include <cassert>
#include <cstdlib>

#include <boost/lexical_cast.hpp>
#include <boost/cast.hpp>

#include <Wt/WBreak>
#include <Wt/WButtonGroup>
#include <Wt/WGroupBox>
#include <Wt/WLabel>
#include <Wt/WLineEdit>
#include <Wt/WRadioButton>
#include <Wt/WPushButton>

#include "about.h"
#include "trace.h"
#include "wtaboutdialog.h"
#include "wtpaperrockscissorsdialog.h"
#include "wtpaperrockscissorsdialogstate.h"
#include "wtpaperrockscissorsserver.h"

ribi::WtPaperRockScissorsDialogState::WtPaperRockScissorsDialogState(
  WtPaperRockScissorsDialog * const dialog,
  WtPaperRockScissorsServer * const server)
  : m_dialog(dialog),
    m_server(server)
{
  assert(m_dialog);
  assert(m_server);
}

ribi::WtPaperRockScissorsDialogState::~WtPaperRockScissorsDialogState()
{
  Disconnect();
}

void ribi::WtPaperRockScissorsDialogState::Connect()
{
  if (!m_dialog->m_event_connection.connected())
  {
    m_dialog->m_event_connection
      = m_server->ChangeIndexEvent().connect(m_dialog,
        &WtPaperRockScissorsDialog::ServerEvent);
  }
}

void ribi::WtPaperRockScissorsDialogState::Disconnect()
{
  //Stop listing to events
  if (m_dialog->m_event_connection.connected())
  {
    m_dialog->m_event_connection.disconnect();
  }
}

void ribi::WtPaperRockScissorsDialogState::SetStateAbout(WtPaperRockScissorsDialogState * const prev_state)
{
  WtPaperRockScissorsDialogStateAbout * about
    = boost::polymorphic_downcast<
        WtPaperRockScissorsDialogStateAbout*
      >(m_dialog->GetStateAbout());
  assert(about);
  about->SetOnCloseState(prev_state);
  m_dialog->SetState(about);
}

ribi::WtPaperRockScissorsDialogStateAbout::WtPaperRockScissorsDialogStateAbout(
  WtPaperRockScissorsDialog * const dialog,
  WtPaperRockScissorsServer * const server,
  WtPaperRockScissorsDialogState * const prev_state)
  : WtPaperRockScissorsDialogState(dialog,server),
    m_prev_state(prev_state)
{
  assert(m_dialog);
  assert(m_server);
  assert(prev_state);
}

void ribi::WtPaperRockScissorsDialogStateAbout::ProcessEvent(const WtPaperRockScissorsEvent&)
{
  //Do nothing :-)
}

void ribi::WtPaperRockScissorsDialogStateAbout::OnClose()
{
  m_dialog->SetState(m_prev_state);
}

void ribi::WtPaperRockScissorsDialogStateAbout::SetOnCloseState(WtPaperRockScissorsDialogState * const prev_state)
{
  assert(prev_state);
  m_prev_state = prev_state;
}

void ribi::WtPaperRockScissorsDialogStateAbout::Show()
{
  //Temporarily do not listen to events
  Disconnect();

  About a = WtPaperRockScissorsDialog::GetAbout();
  WtAboutDialog * const d = new WtAboutDialog(a);
  TRACE("TODO");
  d->m_signal_close.connect(
    boost::bind(
    &ribi::WtPaperRockScissorsDialogStateAbout::OnClose,
    this));
  m_dialog->clear();
  m_dialog->addWidget(d);
}

ribi::WtPaperRockScissorsDialogStateConnect::WtPaperRockScissorsDialogStateConnect(
  WtPaperRockScissorsDialog * const dialog,
  WtPaperRockScissorsServer * const server)
  : WtPaperRockScissorsDialogState(dialog,server)
{
  assert(m_dialog);
}

void ribi::WtPaperRockScissorsDialogStateConnect::ProcessEvent(const WtPaperRockScissorsEvent& /* event */)
{

}

void ribi::WtPaperRockScissorsDialogStateConnect::Show()
{
  Connect();
}

ribi::WtPaperRockScissorsDialogStateGame::WtPaperRockScissorsDialogStateGame(
  WtPaperRockScissorsDialog * const dialog,
  WtPaperRockScissorsServer * const server)
  : WtPaperRockScissorsDialogState(dialog,server)
{
  assert(m_dialog);
}

void ribi::WtPaperRockScissorsDialogStateGame::ProcessEvent(const WtPaperRockScissorsEvent& /* event */)
{

}

///Send the new selected radio button's index
void ribi::WtPaperRockScissorsDialogStateGame::Send()
{
  m_server->SendMessageToServer(std::rand());
}

void ribi::WtPaperRockScissorsDialogStateGame::Show()
{
  m_dialog->clear();
  //Title
  {
    Wt::WLabel * const title = new Wt::WLabel("PaperRockScissors");
    title->setStyleClass("title");
    m_dialog->addWidget(title);
  }
  //ButtonGroup
  {
    Wt::WGroupBox * const container = new Wt::WGroupBox("Which option?",m_dialog);
    m_group = new Wt::WButtonGroup(m_dialog);

    {
      Wt::WRadioButton * const button = new Wt::WRadioButton("A", container);
      new Wt::WBreak(container);
      m_group->addButton(button,0);
    }
    {
      Wt::WRadioButton * const button = new Wt::WRadioButton("B", container);
      new Wt::WBreak(container);
      m_group->addButton(button,1);
    }
    {
      Wt::WRadioButton * const button = new Wt::WRadioButton("C", container);
      new Wt::WBreak(container);
      m_group->addButton(button,2);
    }

    m_group->setSelectedButtonIndex(0);
      //m_server->GetCurrentIndex());
    m_group->checkedChanged().connect(
      this,&ribi::WtPaperRockScissorsDialogStateGame::Send);
  }
  //About
  {
    Wt::WPushButton * button = new Wt::WPushButton("About");
    m_dialog->addWidget(button);
    assert(!"TODO");
    //button->clicked().connect(this, &ribi::WtPaperRockScissorsDialogState::SetStateAbout,boost::lambda::_1);
  }
}

ribi::WtPaperRockScissorsDialogStateLogin::WtPaperRockScissorsDialogStateLogin(
  WtPaperRockScissorsDialog * const dialog,
  WtPaperRockScissorsServer * const server)
  : WtPaperRockScissorsDialogState(dialog,server)
{
  assert(m_dialog);
}

void ribi::WtPaperRockScissorsDialogStateLogin::ProcessEvent(const WtPaperRockScissorsEvent& /* event */)
{

}

void ribi::WtPaperRockScissorsDialogStateLogin::OnEditChanged()
{

}

void ribi::WtPaperRockScissorsDialogStateLogin::Show()
{
  m_dialog->clear();
  //Title
  {
    Wt::WLabel * const title = new Wt::WLabel("PaperRockScissors login");
    title->setStyleClass("title");
    m_dialog->addWidget(title);
  }
  m_dialog->addWidget(new Wt::WBreak);
  //Label
  {
    Wt::WLabel * const label_login = new Wt::WLabel("Login name:");
    m_dialog->addWidget(label_login);
  }
  //m_edit_username
  {
    m_edit_username = new Wt::WLineEdit(
      boost::lexical_cast<std::string>(
        std::rand()));
    m_edit_username->changed().connect(
      this,&ribi::WtPaperRockScissorsDialogStateLogin::OnEditChanged);
    m_dialog->addWidget(m_edit_username);
  }
  m_dialog->addWidget(new Wt::WBreak);
  //About
  {
    Wt::WPushButton * button = new Wt::WPushButton("About");
    m_dialog->addWidget(button);
    assert(!"TODO");
    //button->clicked().connect(this, &ribi::WtPaperRockScissorsDialogState::SetStateAbout);
  }
}

 

 

 

 

 

./GamePaperRockScissors/wtpaperrockscissorsevent.h

 

//---------------------------------------------------------------------------
/*
PaperRockScissors, paper-rock-scissors game
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolPaperRockScissors.htm
//---------------------------------------------------------------------------
#ifndef WTPAPERROCKSCISSORSEVENT_H
#define WTPAPERROCKSCISSORSEVENT_H

#include <vector>

namespace ribi {

struct WtPaperRockScissorsEvent
{
  WtPaperRockScissorsEvent(
    const int index,
    const std::vector<double>& fractions);
  int GetIndex() const { return m_index; }
  const std::vector<double>& GetFractions() const { return m_fractions; }


private:
  const int m_index;
  const std::vector<double> m_fractions;
};

} //~namespace ribi

#endif // WTPAPERROCKSCISSORSEVENT_H

 

 

 

 

 

./GamePaperRockScissors/wtpaperrockscissorsevent.cpp

 

//---------------------------------------------------------------------------
/*
PaperRockScissors, paper-rock-scissors game
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolPaperRockScissors.htm
//---------------------------------------------------------------------------
#include "wtpaperrockscissorsevent.h"

ribi::WtPaperRockScissorsEvent::WtPaperRockScissorsEvent(
  const int index,
  const std::vector<double>& fractions)
  : m_index(index),
    m_fractions(fractions)
{

}

 

 

 

 

 

./GamePaperRockScissors/wtpaperrockscissorsserver.h

 

//---------------------------------------------------------------------------
/*
PaperRockScissors, paper-rock-scissors game
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolPaperRockScissors.htm
//---------------------------------------------------------------------------
#ifndef WTPAPERROCKSCISSORSSERVER_H
#define WTPAPERROCKSCISSORSSERVER_H

#include <boost/thread.hpp>
//#include <boost/timer.hpp>

#include <Wt/WObject>
#include <Wt/WSignal>
#include <Wt/WTimer>

#include "wtpaperrockscissorsevent.h"

namespace ribi {

struct WtPaperRockScissorsServer : public Wt::WObject
{
  static WtPaperRockScissorsServer * GetInstance();

  void SendMessageToServer(const int message);
  Wt::Signal<WtPaperRockScissorsEvent>& ChangeIndexEvent() { return m_change_index_event; }

private:
  WtPaperRockScissorsServer();
  static boost::scoped_ptr<WtPaperRockScissorsServer> m_instance;

  Wt::Signal<WtPaperRockScissorsEvent> m_change_index_event;
  boost::mutex m_mutex;
};

} //~namespace ribi

#endif // WTPAPERROCKSCISSORSSERVER_H

 

 

 

 

 

./GamePaperRockScissors/wtpaperrockscissorsserver.cpp

 

//---------------------------------------------------------------------------
/*
PaperRockScissors, paper-rock-scissors game
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolPaperRockScissors.htm
//---------------------------------------------------------------------------
#include "wtpaperrockscissorsserver.h"

//#include <algorithm>
#include <numeric>
#include <cassert>

#include <boost/numeric/conversion/cast.hpp>

boost::scoped_ptr<ribi::WtPaperRockScissorsServer> ribi::WtPaperRockScissorsServer::m_instance;

ribi::WtPaperRockScissorsServer::WtPaperRockScissorsServer()
{

}

ribi::WtPaperRockScissorsServer * ribi::WtPaperRockScissorsServer::GetInstance()
{
  if (!m_instance) m_instance.reset(new WtPaperRockScissorsServer);
  return m_instance.get();
}

///A client sends a message to the server
void ribi::WtPaperRockScissorsServer::SendMessageToServer(const int /* index */)
{
  boost::mutex::scoped_lock lock(m_mutex);

  //const std::vector<double> fractions = GetCurrentFractions();
  //Update the current index
  //m_current_index = index;

  //The server emits the message to the clients
  m_change_index_event.emit(
    WtPaperRockScissorsEvent(
      0,std::vector<double>()));
}

 

 

 

 

 

./GamePaperRockScissors/zip.sh

 

#!/bin/sh
#zip packs all the files to port into a single .zip file,
#minicking the same folder structure
#Folder structure
# *
#   * Classes
#     * CppAbout
#     * CppWtAboutDialog
#   * Tools
#    * ToolTimePoll
echo "Mimicking file structure"
mkdir temp_zip
mkdir temp_zip/Classes
mkdir temp_zip/Classes/CppAbout
mkdir temp_zip/Classes/CppWtAboutDialog
mkdir temp_zip/Tools
mkdir temp_zip/Tools/ToolTimePoll

echo "Copying files"
cp ../../Classes/CppAbout/*.* temp_zip/Classes/CppAbout
cp ../../Classes/CppWtAboutDialog/*.* temp_zip/Classes/CppWtAboutDialog
cp ../../Tools/ToolTimePoll/*.* temp_zip/Tools/ToolTimePoll

echo "Compressing files"
cd temp_zip

zip -r ToolTimePollSource_1_1 Classes
zip -r ToolTimePollSource_1_1 Tools

echo "Copying zipped file from temp folder to this folder"

cd ..
cp temp_zip/ToolTimePollSource_1_1.zip ToolTimePollSource_1_1.zip

echo "Cleaning up"

#Classes
rm temp_zip/Classes/CppAbout/*.*
rm temp_zip/Classes/CppWtAboutDialog/*.*
rmdir temp_zip/Classes/CppAbout
rmdir temp_zip/Classes/CppWtAboutDialog
rmdir temp_zip/Classes

#Tools
rm temp_zip/Tools/ToolTimePoll/*.*
rmdir temp_zip/Tools/ToolTimePoll
rmdir temp_zip/Tools
rm temp_zip/*.*
rmdir temp_zip
echo "Done"

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml