Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) TicTacToeValuer

 

TicTacToeValuer is a tool to demonstrate machine learning.

 

TicTacToeValuer uses a state-value graph to value each state.

 

 

 

 

Downloads

 

 

 

 

 

 

Older downloads

 

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: ./ToolTicTacToeValuer/ToolTicTacToeValuer.pro

 

QT       += core gui xml
CONFIG(debug, debug|release) {
  message(Building debug version)

} else {
  DEFINES += NDEBUG
  message(Building release version)
}
TEMPLATE = app
SOURCES += main.cpp\
        dialogmain.cpp \
    ../ToolTestTicTacToe/tictactoe.cpp \
    dialogabout.cpp \
    dialogwhatsnew.cpp \
    statevaluegraph.cpp \
    statevaluevertex.cpp \
    statevalueedge.cpp
HEADERS  += \
    dialogmain.h \
    ../ToolTestTicTacToe/tictactoe.h \
    dialogabout.h \
    dialogwhatsnew.h \
    statevaluegraph.h \
    statevaluevertex.h \
    statevalueedge.h
FORMS    += dialogmain.ui \
    dialogabout.ui \
    ../ToolTestTicTacToe/dialogwhatsnew.ui \
    dialogwhatsnew.ui
RESOURCES += \
    ToolTicTacToeValuer.qrc
#Disable depreciated warning, caused by boost/graph/adjacency_list.hpp
#  including the depreciated hash_set
QMAKE_CXXFLAGS_DEBUG   += -Wno-deprecated
QMAKE_CXXFLAGS_RELEASE += -Wno-deprecated

 

 

 

 

 

./ToolTicTacToeValuer/dialogabout.h

 

//---------------------------------------------------------------------------
/*
TicTacToeValuer, tool to solve TicTacToe
Copyright (C) 2010 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/ToolTicTacToeValuer.htm
//---------------------------------------------------------------------------
#ifndef DIALOGABOUT_H
#define DIALOGABOUT_H
//---------------------------------------------------------------------------
#include <string>
//---------------------------------------------------------------------------
#include <QDialog>
//---------------------------------------------------------------------------
namespace Ui {
  class DialogAbout;
}
//---------------------------------------------------------------------------
class DialogAbout : public QDialog
{
  Q_OBJECT

public:
  explicit DialogAbout(QWidget *parent = 0);
  ~DialogAbout();

protected:
  void changeEvent(QEvent *e);

private:
  Ui::DialogAbout *ui;

private slots:
  void onWhatsNew();
};
//---------------------------------------------------------------------------
///GetBoostVersion returns the version of the current Boost library.
///From http://www.richelbilderbeek.nl/CppGetBoostVersion.htm
const std::string GetBoostVersion();
//---------------------------------------------------------------------------
#endif // DIALOGABOUT_H

 

 

 

 

 

./ToolTicTacToeValuer/dialogabout.cpp

 

//---------------------------------------------------------------------------
/*
TicTacToeValuer, tool to solve TicTacToe
Copyright (C) 2010 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/ToolTicTacToeValuer.htm
//---------------------------------------------------------------------------
#include <algorithm>
#include <cstdlib>
#include <string>
//---------------------------------------------------------------------------
#include <boost/version.hpp>
//---------------------------------------------------------------------------
#include <QFile>
//---------------------------------------------------------------------------
#include "dialogabout.h"
#include "dialogmain.h"
#include "dialogwhatsnew.h"
#include "../ToolTestTicTacToe/tictactoe.h"
#include "ui_dialogabout.h"
//---------------------------------------------------------------------------
DialogAbout::DialogAbout(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogAbout)
{
  ui->setupUi(this);

  QObject::connect(ui->button_whats_new,SIGNAL(clicked()),
    this,SLOT(onWhatsNew()));
  QObject::connect(ui->button_about_qt,SIGNAL(clicked()),
    qApp,SLOT(aboutQt()));

  ui->label_title->setText(
    QString("TicTacToeValuer version ")
    + QString(DialogMain::GetVersion().c_str()));


  ui->label_boost_version->setText(
    QString("Boost version: ")
    + QString(GetBoostVersion().c_str()));


  ui->label_tictactoe_version->setText(
    QString("TicTacToe version: ")
    + QString(TicTacToe::GetVersion().c_str()));

}
//---------------------------------------------------------------------------
DialogAbout::~DialogAbout()
{
  delete ui;
}
//---------------------------------------------------------------------------
void DialogAbout::changeEvent(QEvent *e)
{
  QDialog::changeEvent(e);
  switch (e->type()) {
  case QEvent::LanguageChange:
    ui->retranslateUi(this);
    break;
  default:
    break;
  }
}
//---------------------------------------------------------------------------
void DialogAbout::onWhatsNew()
{
  DialogWhatsNew d;
  d.exec();
}
//---------------------------------------------------------------------------
///GetBoostVersion returns the version of the current Boost library.
///From http://www.richelbilderbeek.nl/CppGetBoostVersion.htm
const std::string GetBoostVersion()
{
  std::string s = BOOST_LIB_VERSION;
  std::replace(s.begin(),s.end(),'_','.');
  return s;
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolTicTacToeValuer/dialogmain.h

 

//---------------------------------------------------------------------------
/*
TicTacToeValuer, tool to solve TicTacToe
Copyright (C) 2010 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/ToolTicTacToeValuer.htm
//---------------------------------------------------------------------------
#ifndef DIALOGMAIN_H
#define DIALOGMAIN_H
//---------------------------------------------------------------------------
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <QDialog>
//---------------------------------------------------------------------------
#include "statevaluegraph.h"
#include "../ToolTestTicTacToe/tictactoe.h"
//---------------------------------------------------------------------------
namespace Ui {
  class DialogMain;
}
//---------------------------------------------------------------------------
class DialogMain : public QDialog
{
  Q_OBJECT

public:
  explicit DialogMain(QWidget *parent = 0);
  ~DialogMain();
  static const std::string GetVersion() { return "0.7"; }

protected:
  void changeEvent(QEvent *e);

private:
  Ui::DialogMain *ui;
  StateValueGraph m_graph;
  TicTacToe m_board;

private slots:
  void OnAboutClick();
  void OnStart();
  void DisplayGraph();

};
//---------------------------------------------------------------------------
///From http://www.richelbilderbeek.nl/CppFileToVector.htm
const std::vector<std::string> FileToVector(const std::string& fileName);
//---------------------------------------------------------------------------
#endif // DIALOGMAIN_H

 

 

 

 

 

./ToolTicTacToeValuer/dialogmain.cpp

 

//---------------------------------------------------------------------------
/*
TicTacToeValuer, tool to solve TicTacToe
Copyright (C) 2010 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/ToolTicTacToeValuer.htm
//---------------------------------------------------------------------------
#include <cassert>
#include <fstream>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/foreach.hpp>
#include <boost/tuple/tuple.hpp>
//---------------------------------------------------------------------------
#include <QDesktopWidget>
#include <QFile>
#include <QPainter>
//---------------------------------------------------------------------------
#include "dialogabout.h"
#include "dialogmain.h"
#include "ui_dialogmain.h"
//---------------------------------------------------------------------------
DialogMain::DialogMain(QWidget *parent) :
    QDialog(parent, Qt::Window),
    ui(new Ui::DialogMain)
{
  ui->setupUi(this);

  QObject::connect(ui->button_about,SIGNAL(clicked()),
    this,SLOT(OnAboutClick()));
  QObject::connect(ui->button_start,SIGNAL(clicked()),
    this,SLOT(OnStart()));

  //Put the dialog in the screen center
  const QRect screen = QApplication::desktop()->screenGeometry();
  this->setGeometry(screen);
  this->move( screen.center() - this->rect().center() );

  //this->DisplayGraph();
}
//---------------------------------------------------------------------------
DialogMain::~DialogMain()
{
  delete ui;
}
//---------------------------------------------------------------------------
void DialogMain::changeEvent(QEvent *e)
{
  QDialog::changeEvent(e);
  switch (e->type()) {
  case QEvent::LanguageChange:
    ui->retranslateUi(this);
    break;
  default:
    break;
  }
}
//---------------------------------------------------------------------------
void DialogMain::OnAboutClick()
{
  DialogAbout d;
  d.exec();
}
//---------------------------------------------------------------------------
void DialogMain::OnStart()
{
  std::queue<TicTacToe> todo;
  todo.push(m_board);

  for(int tick=0; !todo.empty(); ++tick)
  {
    if (tick%100==0) std::clog << "tick: " << tick << '\n';
    //std::clog << "TODO: " << todo.size() << '\n';
    const TicTacToe b = todo.front();
    //std::clog << b << '\n';
    //Obtain possible moves
    typedef boost::tuples::tuple<int,int,int> Tuple;
    std::vector<Tuple> v;
    for (int i=0; i!=9; ++i)
    {
      const int x = i/3;
      const int y = i%3;
      if (b.CanDoMove(x,y))
      {
        v.push_back(boost::tuples::make_tuple(x,i,y));
      }
    }
    //Limit graph
    std::random_shuffle(v.begin(),v.end());
    if (v.size() > 2) v.resize(2);


    if (v.empty())
    {
      todo.pop();
      //std::clog << "Pop, due to no moves, TODO: " << todo.size() << '\n';
      continue;
    }
    //std::clog << "New moves: " << v.size() << '\n';
    //Obtain next states
    const int state_now = b.GetSummarizedState();
    BOOST_FOREACH(const Tuple& p,v)
    {
      TicTacToe temp(b.GetSummarizedState());
      assert(temp.CanDoMove(boost::get<0>(p),boost::get<2>(p)));
      temp.DoMove(boost::get<0>(p),boost::get<2>(p));
      const int state_next = temp.GetSummarizedState();
      m_graph.AddEdge(state_now,boost::get<1>(p),state_next);
      todo.push(temp);
    }
    //std::clog << "Pop, due to finished move, TODO: " << todo.size() << '\n';
    todo.pop();
    //DisplayGraph();
    //QCoreApplication::processEvents();
  }
  DisplayGraph();
}
//---------------------------------------------------------------------------
void DialogMain::DisplayGraph()
{
  //Write graph to file
  {
    std::ofstream f("out.dot");
    f << m_graph;
  }
  //Load graph to QLabel
  {
    assert(QFile::exists("out.dot"));
    std::system("dot -Tpng out.dot > out.png");
    QImage png("out.png");
    ui->label_graph->setPixmap(QPixmap::fromImage(png));
  }
  //Text output
  ui->text_out->clear();
  ui->text_out->appendPlainText(
    QString("Number of vertices: ")
    + QString::number(m_graph.GetNumVertices()));
  ui->text_out->appendPlainText(
    QString("Number of edges: ")
    + QString::number(m_graph.GetNumEdges()));

  //Append text file
  {
    ui->text_out->appendPlainText(" ");
    ui->text_out->appendPlainText("Graph file ('out.dot') content:");
    ui->text_out->appendPlainText(" ");
    const std::vector<std::string> v = FileToVector("out.dot");
    BOOST_FOREACH(const std::string& s, v)
    {
      ui->text_out->appendPlainText(s.c_str());
    }
  }
}
//---------------------------------------------------------------------------
///From http://www.richelbilderbeek.nl/CppFileToVector.htm
const std::vector<std::string> FileToVector(const std::string& fileName)
{
  assert(QFile::exists(fileName.c_str()));
  std::vector<std::string> myVector;
  std::ifstream in(fileName.c_str());
  std::string myString;
  for (int i=0; !in.eof(); ++i)
  {
    std::getline(in,myString);
    myVector.push_back(myString);
  }
  return myVector;
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolTicTacToeValuer/dialogwhatsnew.h

 

//---------------------------------------------------------------------------
/*
TicTacToeValuer, tool to solve TicTacToe
Copyright (C) 2010 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/ToolTicTacToeValuer.htm
//---------------------------------------------------------------------------
#ifndef DIALOGWHATSNEW_H
#define DIALOGWHATSNEW_H
//---------------------------------------------------------------------------
#include <QDialog>
//---------------------------------------------------------------------------
namespace Ui {
  class DialogWhatsNew;
}
//---------------------------------------------------------------------------
class DialogWhatsNew : public QDialog
{
  Q_OBJECT

public:
  explicit DialogWhatsNew(QWidget *parent = 0);
  ~DialogWhatsNew();

protected:
  void changeEvent(QEvent *e);

private:
  Ui::DialogWhatsNew *ui;
};
//---------------------------------------------------------------------------
#endif // DIALOGWHATSNEW_H

 

 

 

 

 

./ToolTicTacToeValuer/dialogwhatsnew.cpp

 

//---------------------------------------------------------------------------
/*
TicTacToeValuer, tool to solve TicTacToe
Copyright (C) 2010 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/ToolTicTacToeValuer.htm
//---------------------------------------------------------------------------
#include "dialogwhatsnew.h"
#include "ui_dialogwhatsnew.h"
//---------------------------------------------------------------------------
DialogWhatsNew::DialogWhatsNew(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogWhatsNew)
{
  ui->setupUi(this);
}
//---------------------------------------------------------------------------
DialogWhatsNew::~DialogWhatsNew()
{
  delete ui;
}
//---------------------------------------------------------------------------
void DialogWhatsNew::changeEvent(QEvent *e)
{
  QDialog::changeEvent(e);
  switch (e->type()) {
  case QEvent::LanguageChange:
    ui->retranslateUi(this);
    break;
  default:
    break;
  }
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolTicTacToeValuer/main.cpp

 

//---------------------------------------------------------------------------
/*
TicTacToeValuer, tool to solve TicTacToe
Copyright (C) 2010 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/ToolTicTacToeValuer.htm
//---------------------------------------------------------------------------
#include <QApplication>
#include "dialogmain.h"
//---------------------------------------------------------------------------
int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  DialogMain w;
  w.show();
  return a.exec();
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolTicTacToeValuer/statevalueedge.h

 

//---------------------------------------------------------------------------
/*
TicTacToeValuer, tool to solve TicTacToe
Copyright (C) 2010 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/ToolTicTacToeValuer.htm
//---------------------------------------------------------------------------
#ifndef STATEVALUEEDGE_H
#define STATEVALUEEDGE_H
//---------------------------------------------------------------------------
#include <cstdlib>
//---------------------------------------------------------------------------
struct StateValueEdge
{
  StateValueEdge() : m_move(std::rand() % 100) {}
  StateValueEdge(const int move) : m_move(move) {}
  int m_move;
};
//---------------------------------------------------------------------------
#endif // STATEVALUEEDGE_H

 

 

 

 

 

./ToolTicTacToeValuer/statevalueedge.cpp

 

//---------------------------------------------------------------------------
/*
TicTacToeValuer, tool to solve TicTacToe
Copyright (C) 2010 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/ToolTicTacToeValuer.htm
//---------------------------------------------------------------------------
#include "statevalueedge.h"

 

 

 

 

 

./ToolTicTacToeValuer/statevaluegraph.h

 

//---------------------------------------------------------------------------
/*
TicTacToeValuer, tool to solve TicTacToe
Copyright (C) 2010 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/ToolTicTacToeValuer.htm
//---------------------------------------------------------------------------
#ifndef STATEVALUEGRAPH_H
#define STATEVALUEGRAPH_H
//---------------------------------------------------------------------------
#include <iosfwd>
//---------------------------------------------------------------------------
#include <boost/graph/adjacency_list.hpp>
//---------------------------------------------------------------------------
#include "statevalueedge.h"
#include "statevaluevertex.h"
//---------------------------------------------------------------------------
struct StateValueGraph
{
  StateValueGraph();
  int GetNumEdges() const;
  int GetNumVertices() const;
  void AddEdge(
    const int state,
    const int move,
    const int next_state);

  private:

  typedef boost::adjacency_list
  <
    //Store all edges as a std::vector
    boost::vecS,
    //Store all vertices in a std::vector
    boost::vecS,
    //Relations are both ways (in this example)
    //(note: but you can freely change it to boost::directedS)
    boost::directedS,
    //All vertices are of state StateValueVertex
    StateValueVertex,
    //All edges are simple
    StateValueEdge,
    //Graph itself has a std::string name
    boost::no_property
  > Graph;

  Graph m_graph;

  bool EdgeExists(const int state, const int state_next) const;

  bool VertexExists(const int state) const;

  friend std::ostream& operator<<(std::ostream&,const StateValueGraph&);
};
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const StateValueGraph& s);
//---------------------------------------------------------------------------
#endif // STATEVALUEGRAPH_H

 

 

 

 

 

./ToolTicTacToeValuer/statevaluegraph.cpp

 

//---------------------------------------------------------------------------
/*
TicTacToeValuer, tool to solve TicTacToe
Copyright (C) 2010 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/ToolTicTacToeValuer.htm
//---------------------------------------------------------------------------
#include <cassert>
//---------------------------------------------------------------------------
#include <boost/graph/graphviz.hpp>
//---------------------------------------------------------------------------
#include "statevaluegraph.h"
//---------------------------------------------------------------------------
StateValueGraph::StateValueGraph()

{
  assert(GetNumEdges()==0);
  assert(GetNumVertices()==0);
}
//---------------------------------------------------------------------------
void StateValueGraph::AddEdge(
  const int state,
  const int move,
  const int next_state)
{
  if (!VertexExists(state))
  {
    //Add vertex
    const Graph::vertex_descriptor i = boost::add_vertex(m_graph);
    m_graph[i] = StateValueVertex(state,0.0);
  }
  if (!VertexExists(next_state))
  {
    //Add vertex
    const Graph::vertex_descriptor i = boost::add_vertex(m_graph);
    m_graph[i] = StateValueVertex(next_state,0.0);
  }
  if (!EdgeExists(state,next_state))
  {
    const std::pair<Graph::vertex_iterator,Graph::vertex_iterator>
      p = boost::vertices(m_graph);
    const int sz = std::distance(p.first,p.second);

    Graph::vertex_descriptor v_state = 0;

    for (int i=0; i!=sz; ++i)
    {
      if (m_graph[i].m_state == state)
      {
        v_state = i;
        break;
      }
    }

    //Find next_state
    Graph::vertex_descriptor v_next_state = 0;
    for (int i=0; i!=sz; ++i)
    {
      if (m_graph[i].m_state == next_state)
      {
        v_next_state = i;
        break;
      }
    }
    //Add the edge
    boost::add_edge(v_state,v_next_state,StateValueEdge(move),m_graph);
  }
}
//---------------------------------------------------------------------------
bool StateValueGraph::EdgeExists(
  const int state,
  const int state_next) const
{
  assert(VertexExists(state));
  assert(VertexExists(state_next));
  const std::pair<Graph::edge_iterator,Graph::edge_iterator>
    p = boost::edges(m_graph);

  for (Graph::edge_iterator i = p.first; i!=p.second; ++i)
  {
    if ( m_graph[ boost::source(*i,m_graph) ].m_state == state
      && m_graph[ boost::target(*i,m_graph) ].m_state == state_next)
    {
      return true;
    }
  }
  return false;
  //for (int i=0; i!=sz; ++i)
  //{
  //  if (m_graph[i].m_state == state) return true;
  //}
  //return false;

}
//---------------------------------------------------------------------------
int StateValueGraph::GetNumEdges() const
{
  const std::pair<Graph::edge_iterator,Graph::edge_iterator>
    p = boost::edges(m_graph);
  return std::distance(p.first,p.second);
}
//---------------------------------------------------------------------------
int StateValueGraph::GetNumVertices() const
{
  const std::pair<Graph::vertex_iterator,Graph::vertex_iterator>
    p = boost::vertices(m_graph);
  return std::distance(p.first,p.second);
}
//---------------------------------------------------------------------------
bool StateValueGraph::VertexExists(const int state) const
{
  const std::pair<Graph::vertex_iterator,Graph::vertex_iterator>
    p = boost::vertices(m_graph);
  const int sz = std::distance(p.first,p.second);
  for (int i=0; i!=sz; ++i)
  {
    if (m_graph[i].m_state == state) return true;
  }
  return false;

}
//---------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const StateValueGraph& s)
{
  StateValueGraph& g = const_cast<StateValueGraph&>(s);
  boost::dynamic_properties p;
  p.property("label", boost::get(&StateValueEdge::m_move, g.m_graph));
  p.property("label", boost::get(&StateValueVertex::m_state, g.m_graph));
  //p.property("label", boost::get(&StateValueVertex::m_value, g.m_graph));
  p.property("node_id", boost::get(&StateValueVertex::m_state, g.m_graph));
  boost::write_graphviz(os,g.m_graph,p);
  return os;
}
//---------------------------------------------------------------------------

 

 

 

 

 

./ToolTicTacToeValuer/statevaluevertex.h

 

//---------------------------------------------------------------------------
/*
TicTacToeValuer, tool to solve TicTacToe
Copyright (C) 2010 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/ToolTicTacToeValuer.htm
//---------------------------------------------------------------------------
#ifndef STATEVALUEVERTEX_H
#define STATEVALUEVERTEX_H
//---------------------------------------------------------------------------
#include <string>
//---------------------------------------------------------------------------
struct StateValueVertex
{
  StateValueVertex(const int state = 0, const double value = 0.0)
    : m_state(state), m_value(value) {}
  int m_state;
  double m_value;
  const std::string ToStr() const;
};
//---------------------------------------------------------------------------
#endif // STATEVALUEVERTEX_H

 

 

 

 

 

./ToolTicTacToeValuer/statevaluevertex.cpp

 

//---------------------------------------------------------------------------
/*
TicTacToeValuer, tool to solve TicTacToe
Copyright (C) 2010 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/ToolTicTacToeValuer.htm
//---------------------------------------------------------------------------
#include <boost/lexical_cast.hpp>
//---------------------------------------------------------------------------
#include "statevaluevertex.h"
//---------------------------------------------------------------------------
const std::string StateValueVertex::ToStr() const
{
  return boost::lexical_cast<std::string>(m_state)
    + std::string(": ")
    + boost::lexical_cast<std::string>(m_value);
}
//---------------------------------------------------------------------------

 

 

 

 

 

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