Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) Reversi source code (version 1.0)

 

Reversi version 1.0 source code.

 

Operating system: Windows XP

IDE: C++ Builder 6.0 Enterprise Edition

Project type: GUI application

Compiler: supplied with C++ Builder 6.0 Enterprise Edition

Libraries used:

 

 

 

 

 

ProjectReversi.cpp

 

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("UnitFormReversi.cpp", FormReversi);
USEFORM("UnitFormAbout.cpp", FormAbout);
USEFORM("UnitFormRestart.cpp", FormRestart);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
        try
        {
                 Application->Initialize();
                 Application->Title = "Reversi";
                 Application->CreateForm(__classid(TFormReversi), &FormReversi);
                 Application->CreateForm(__classid(TFormRestart), &FormRestart);
                 Application->Run();
        }
        catch (Exception &exception)
        {
                 Application->ShowException(&exception);
        }
        catch (...)
        {
                 try
                 {
                         throw Exception("");
                 }
                 catch (Exception &exception)
                 {
                         Application->ShowException(&exception);
                 }
        }
        return 0;
}
//---------------------------------------------------------------------------

 

 

 

 

 

UnitFormAbout.cpp

 

//---------------------------------------------------------------------------
/*
    Reversi, a simple board game
    Copyright (C) 2007  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/>.
*/
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UnitFormAbout.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormAbout *FormAbout;
//---------------------------------------------------------------------------
__fastcall TFormAbout::TFormAbout(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

 

 

 

 

 

UnitFormAbout.h

 

//---------------------------------------------------------------------------
/*
    Reversi, a simple board game
    Copyright (C) 2007  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/>.
*/
//---------------------------------------------------------------------------
#ifndef UnitFormAboutH
#define UnitFormAboutH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
//---------------------------------------------------------------------------
class TFormAbout : public TForm
{
__published: // IDE-managed Components
        TImage *Image1;
        TPanel *PanelTop;
        TPanel *PanelTopLeft;
        TPanel *PanelBottom;
        TMemo *Memo1;
        TPanel *Panel1;
        TPanel *Panel2;
        TPanel *Panel3;
        TPanel *Panel4;
private: // User declarations
public: // User declarations
        __fastcall TFormAbout(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormAbout *FormAbout;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

UnitFormRestart.cpp

 

//---------------------------------------------------------------------------
/*
    Reversi, a simple board game
    Copyright (C) 2007  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/>.
*/
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UnitFormRestart.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormRestart *FormRestart;
//---------------------------------------------------------------------------
__fastcall TFormRestart::TFormRestart(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TFormRestart::PanelYesClick(TObject *Sender)
{
  ModalResult = EditSize->Text.ToIntDef(8);
}
//---------------------------------------------------------------------------
void __fastcall TFormRestart::PanelNoClick(TObject *Sender)
{
  ModalResult = -1;
}
//---------------------------------------------------------------------------

 

 

 

 

 

UnitFormRestart.h

 

//---------------------------------------------------------------------------
/*
    Reversi, a simple board game
    Copyright (C) 2007  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/>.
*/
//---------------------------------------------------------------------------
#ifndef UnitFormRestartH
#define UnitFormRestartH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TFormRestart : public TForm
{
__published: // IDE-managed Components
        TPanel *PanelTop;
        TPanel *PanelBottom;
        TPanel *PanelYes;
        TPanel *PanelNo;
        TLabeledEdit *EditSize;
        void __fastcall PanelYesClick(TObject *Sender);
        void __fastcall PanelNoClick(TObject *Sender);
private: // User declarations
public: // User declarations
        __fastcall TFormRestart(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormRestart *FormRestart;
//---------------------------------------------------------------------------
#endif

 

 

 

 

 

UnitFormReversi.cpp

 

//---------------------------------------------------------------------------
/*
    Reversi, a simple board game
    Copyright (C) 2007  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/>.
*/
//---------------------------------------------------------------------------

#include <memory>
#include <cassert>
#include <vcl.h>
#pragma hdrstop

#include "UnitFormReversi.h"
#include "UnitFormAbout.h"
#include "UnitFormRestart.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormReversi *FormReversi;
//---------------------------------------------------------------------------
__fastcall TFormReversi::TFormReversi(TComponent* Owner)
        : TForm(Owner), mReversi(8), mPlayer(player1)
{
  OnResize(0);
  SetPixel(ImageRed ,0,0,255,0  ,0  );
  SetPixel(ImageBlue,0,0,0  ,0  ,255);
  SetPixel(ImageRedActive ,0,0,255,0  ,0  );
  SetPixel(ImageBlueActive,0,0,0  ,0  ,255);
  ImageRed->Refresh();
  ImageBlue->Refresh();
  ImageRedActive->Refresh();
  ImageBlueActive->Refresh();

}
//---------------------------------------------------------------------------
void __fastcall TFormReversi::FormResize(TObject *Sender)
{
  const int size = mReversi.GetSize();
  const std::vector<std::vector<Square> >& board = mReversi.GetBoard();
  ImageBoard->Picture->Bitmap->Width  = size;
  ImageBoard->Picture->Bitmap->Height = size;
  for (int y=0; y!=size; ++y)
  {
    for (int x=0; x!=size; ++x)
    {
      const Square s = GetSquareXy(board,x,y);
      switch (s)
      {
        case player1: //Red
          SetPixel(ImageBoard,x,y,((x+y)%2==0?255:224),0,0);
          break;
        case player2: //Blue
          SetPixel(ImageBoard,x,y,0,0,((x+y)%2==0?255:224));
          break;
        case empty: //Empty
          SetPixel(ImageBoard,x,y,0,((x+y)%2==0?16:32),0);
          break;
      }
    }
  }
  ImageBoard->Refresh();
}
//---------------------------------------------------------------------------
void SetPixel(
  TImage * const image,
  const int x,
  const int y,
  const unsigned char red,
  const unsigned char green,
  const unsigned char blue)
{
  assert(image!=0 && "Image is NULL");
  assert(image->Picture->Bitmap!=0 && "Bitmap is NULL");
  assert(image->Picture->Bitmap->PixelFormat == pf24bit && "Bitmap must be 24 bit");
  assert( x >= 0 && "x coordinat is below zero");
  assert( y >= 0 && "y coordinat is below zero");
  assert( x < image->Picture->Bitmap->Width  && "x coordinat is beyond image width");
  assert( y < image->Picture->Bitmap->Height && "y coordinat is beyond image height");

  static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*3+2] = red;
  static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*3+1] = green;
  static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*3+0] = blue;
}
//---------------------------------------------------------------------------

void __fastcall TFormReversi::ImageBoardMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
  //
  const int size   = mReversi.GetSize();
  const int width  = ImageBoard->Width;
  const int height = ImageBoard->Height;
  const double blockWidth  = static_cast<double>(width )
    / static_cast<double>(size);
  const double blockHeight = static_cast<double>(height)
    / static_cast<double>(size);
  const int x = static_cast<int>( static_cast<double>(X) / blockWidth );
  const int y = static_cast<int>( static_cast<double>(Y) / blockHeight);
  assert ( x < size);
  assert ( y < size);

  if (mReversi.IsValidMove(x,y,mPlayer)==true)
  {
    mReversi.DoMove(x,y,mPlayer);

    ImageRedActive->Visible  = !ImageRedActive->Visible;
    ImageBlueActive->Visible = !ImageBlueActive->Visible;
    mPlayer = GetOtherPlayer(mPlayer);

    LabelRed->Caption  = IntToStr(mReversi.Count(player1));
    LabelBlue->Caption = IntToStr(mReversi.Count(player2));
    OnResize(0);
  }

}
//---------------------------------------------------------------------------
void __fastcall TFormReversi::ImageActiveClick(TObject *Sender)
{
  //Pass
  ImageRedActive->Visible  = !ImageRedActive->Visible;
  ImageBlueActive->Visible = !ImageBlueActive->Visible;
  mPlayer = GetOtherPlayer(mPlayer);
}
//---------------------------------------------------------------------------
void __fastcall TFormReversi::LabelRestartClick(TObject *Sender)
{
  std::auto_ptr<TFormRestart> f(new TFormRestart(0));
  f->ShowModal();

  const int result = f->ModalResult;
  if (result == -1) return;
  if (result < 4) mReversi = Reversi(4);
  else mReversi = Reversi(result);

  mPlayer = player1;
  ImageRedActive->Visible  = true;
  ImageBlueActive->Visible = false;
  LabelRed->Caption  = IntToStr(mReversi.Count(player1));
  LabelBlue->Caption = IntToStr(mReversi.Count(player2));
  OnResize(0);
}
//---------------------------------------------------------------------------
void __fastcall TFormReversi::LabelAboutClick(TObject *Sender)
{
  std::auto_ptr<TFormAbout> f(new TFormAbout(0));
  f->ShowModal();
}
//---------------------------------------------------------------------------

 

 

 

 

 

UnitFormReversi.h

 

//---------------------------------------------------------------------------
/*
    Reversi, a simple board game
    Copyright (C) 2007  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/>.
*/
//---------------------------------------------------------------------------
#ifndef UnitFormReversiH
#define UnitFormReversiH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
//---------------------------------------------------------------------------
#include "UnitReversi.h"
//---------------------------------------------------------------------------
class TFormReversi : public TForm
{
__published: // IDE-managed Components
        TImage *ImageBoard;
        TPanel *PanelBottom;
        TPanel *PanelMenu;
        TImage *ImageRed;
        TLabel *LabelRed;
        TLabel *LabelBlue;
        TImage *ImageBlue;
        TImage *ImageRedActive;
        TImage *ImageBlueActive;
        TLabel *LabelRestart;
        TLabel *LabelAbout;
        void __fastcall FormResize(TObject *Sender);
        void __fastcall ImageBoardMouseDown(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y);
        void __fastcall ImageActiveClick(TObject *Sender);
        void __fastcall LabelRestartClick(TObject *Sender);
        void __fastcall LabelAboutClick(TObject *Sender);
private: // User declarations
  Reversi mReversi;
  Square mPlayer;
public: // User declarations
  __fastcall TFormReversi(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormReversi *FormReversi;
//---------------------------------------------------------------------------
void SetPixel(
  TImage * const image,
  const int x,
  const int y,
  const unsigned char red,
  const unsigned char green,
  const unsigned char blue);

#endif

 

 

 

 

 

UnitReversi.cpp

 

//---------------------------------------------------------------------------
/*
    Reversi, a simple board game
    Copyright (C) 2007  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
//---------------------------------------------------------------------------
#pragma hdrstop

#include "UnitReversi.h"
//---------------------------------------------------------------------------
#include <cassert>
//---------------------------------------------------------------------------
Reversi::Reversi(const int size)
  : mBoard(size,std::vector<Square>(size,empty))
{
  assert( size > 0);
  assert( mBoard.size() == mBoard[0].size() );

  const int x = size / 2 - 1;
  const int y = size / 2 - 1;
  SetSquare(x  ,y  ,player1);
  SetSquare(x+1,y  ,player2);
  SetSquare(x  ,y+1,player2);
  SetSquare(x+1,y+1,player1);
}
//---------------------------------------------------------------------------
const std::vector<std::vector<Square> >& Reversi::GetBoard() const
{
  return mBoard;
}
//---------------------------------------------------------------------------
const Square Reversi::GetSquare(const int x, const int y) const
{
  return GetSquareXy(mBoard,x,y);
}
//---------------------------------------------------------------------------
//Simply sets a square
void Reversi::SetSquare(const int x, const int y, const Square player)
{
  assert(player != empty);

  mBoard[y][x] = player;

  assert(GetSquare(x,y)==player);
}
//---------------------------------------------------------------------------
//Do a complete move
void Reversi::DoMove(const int x, const int y, const Square player)
{
  assert(IsValidMove(x,y,player)==true || "Invalid move!");

  if (IsValidMoveUp(       x,y,player) == true) DoMoveUp(       x  ,y-1,player);
  if (IsValidMoveUpLeft(   x,y,player) == true) DoMoveUpLeft(   x-1,y-1,player);
  if (IsValidMoveLeft(     x,y,player) == true) DoMoveLeft(     x-1,y  ,player);
  if (IsValidMoveDownLeft( x,y,player) == true) DoMoveDownLeft( x-1,y+1,player);
  if (IsValidMoveDown(     x,y,player) == true) DoMoveDown(     x  ,y+1,player);
  if (IsValidMoveDownRight(x,y,player) == true) DoMoveDownRight(x+1,y+1,player);
  if (IsValidMoveRight(    x,y,player) == true) DoMoveRight(    x+1,y  ,player);
  if (IsValidMoveUpRight(  x,y,player) == true) DoMoveUpRight(  x+1,y-1,player);
  SetSquare(x,y,player);
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
const bool Reversi::IsValidMove(const int x, const int y, const Square player) const
{
  if (IsValidMoveUp(       x,y,player)==true) return true;
  if (IsValidMoveUpLeft(   x,y,player)==true) return true;
  if (IsValidMoveLeft(     x,y,player)==true) return true;
  if (IsValidMoveDownLeft( x,y,player)==true) return true;
  if (IsValidMoveDown(     x,y,player)==true) return true;
  if (IsValidMoveDownRight(x,y,player)==true) return true;
  if (IsValidMoveRight(    x,y,player)==true) return true;
  if (IsValidMoveUpRight(  x,y,player)==true) return true;
  return false;
}
//---------------------------------------------------------------------------
const bool Reversi::IsValidMoveUp(const int x, const int y, const Square player) const
{
  int b = y - 1;
  if (b < 1) return false;
  if (GetSquare(x,b) != GetOtherPlayer(player)) return false;
  while (b > 0)
  {
    b--;
    if (GetSquare(x,b) == player) return true;
    if (GetSquare(x,b) == empty) return false;
  }
  return false;
}
//---------------------------------------------------------------------------
const bool Reversi::IsValidMoveUpLeft(const int x, const int y, const Square player) const
{
  int a = x - 1;
  int b = y - 1;
  if (a < 1 || b < 1) return false;
  if (GetSquare(a,b) != GetOtherPlayer(player)) return false;
  while (a > 0 && b > 0)
  {
    a--;
    b--;
    if (GetSquare(a,b) == player) return true;
    if (GetSquare(a,b) == empty) return false;
  }
  return false;
}
//---------------------------------------------------------------------------
const bool Reversi::IsValidMoveLeft(const int x, const int y, const Square player) const
{
  int a = x - 1;
  if (a < 1) return false;
  if (GetSquare(a,y) != GetOtherPlayer(player)) return false;
  while (a > 0)
  {
    a--;
    if (GetSquare(a,y) == player) return true;
    if (GetSquare(a,y) == empty) return false;
  }
  return false;
}
//---------------------------------------------------------------------------
const bool Reversi::IsValidMoveDownLeft(const int x, const int y, const Square player) const
{
  const int size = mBoard.size();
  int a = x - 1;
  int b = y + 1;

  if (a < 1) return false;
  if (b > size -1 ) return false;
  if (GetSquare(a,b) != GetOtherPlayer(player)) return false;
  while (a > 0 && b < size - 1)
  {
    a--;
    b++;
    if (GetSquare(a,b) == player) return true;
    if (GetSquare(a,b) == empty) return false;
  }
  return false;
}
//---------------------------------------------------------------------------
const bool Reversi::IsValidMoveDown(const int x, const int y, const Square player) const
{
  const int size = mBoard.size();
  int b = y + 1;
  if (b > size -1 ) return false;
  if (GetSquare(x,b) != GetOtherPlayer(player)) return false;
  while (b < size - 1)
  {
    b++;
    if (GetSquare(x,b) == player) return true;
    if (GetSquare(x,b) == empty ) return false;
  }
  return false;
}
//---------------------------------------------------------------------------
const bool Reversi::IsValidMoveDownRight(const int x, const int y, const Square player) const
{
  const int size = mBoard.size();
  int a = x + 1;
  int b = y + 1;
  if (a > size - 1) return false;
  if (b > size -1 ) return false;
  if (GetSquare(a,b) != GetOtherPlayer(player)) return false;
  while (a < size - 1 && b < size - 1)
  {
    b++;
    a++;
    if (GetSquare(a,b) == player) return true;
    if (GetSquare(a,b) == empty ) return false;
  }
  return false;
}
//---------------------------------------------------------------------------
const bool Reversi::IsValidMoveRight(const int x, const int y, const Square player) const
{
  const int size = mBoard.size();
  int a = x + 1;
  if (a > size - 1) return false;
  if (GetSquare(a,y) != GetOtherPlayer(player)) return false;
  while (a < size - 1)
  {
    a++;
    if (GetSquare(a,y) == player) return true;
    if (GetSquare(a,y) == empty) return false;
  }
  return false;
}
//---------------------------------------------------------------------------
const bool Reversi::IsValidMoveUpRight(const int x, const int y, const Square player) const
{
  const int size = mBoard.size();
  int a = x + 1;
  int b = y - 1;
  if (a > size - 1) return false;
  if (b < 1) return false;
  if (GetSquare(a,b) != GetOtherPlayer(player)) return false;
  while (a < size - 1 && b > 0)
  {
    a++;
    b--;
    if (GetSquare(a,b) == player) return true;
    if (GetSquare(a,b) == empty) return false;
  }
  return false;
}
//---------------------------------------------------------------------------
const std::vector< std::pair<int,int> > Reversi::GetValidMoves(const Square player) const
{
  const int size = mBoard.size();
  std::vector< std::pair<int,int> > v;
  for (int y=0; y!=size; ++y)
  {
    for (int x=0; x!=size; ++x)
    {
      if (IsValidMove(x,y,player)==true)
      {
        v.push_back( std::make_pair(x,y) );
      }
    }
  }
  return v;
}
//---------------------------------------------------------------------------
void Reversi::DoMoveUp(const int x, int y, const Square player)
{
  while ( GetSquare(x,y) == GetOtherPlayer(player) )
  {
    SetSquare(x,y,player);
    --y;
  }
}
//---------------------------------------------------------------------------
void Reversi::DoMoveUpLeft(int x, int y, const Square player)
{
  while ( GetSquare(x,y) == GetOtherPlayer(player) )
  {
    SetSquare(x,y,player);
    --y;
    --x;
  }
}
//---------------------------------------------------------------------------
void Reversi::DoMoveLeft(int x, const int y, const Square player)
{
  while ( GetSquare(x,y) == GetOtherPlayer(player) )
  {
    SetSquare(x,y,player);
    --x;
  }

}
//---------------------------------------------------------------------------
void Reversi::DoMoveDownLeft(int x, int y, const Square player)
{
  while ( GetSquare(x,y) == GetOtherPlayer(player) )
  {
    SetSquare(x,y,player);
    ++y;
    --x;
  }
}
//---------------------------------------------------------------------------
void Reversi::DoMoveDown(const int x, int y, const Square player)
{
  while ( GetSquare(x,y) == GetOtherPlayer(player) )
  {
    SetSquare(x,y,player);
    ++y;
  }
}
//---------------------------------------------------------------------------
void Reversi::DoMoveDownRight(int x, int y, const Square player)
{
  while ( GetSquare(x,y) == GetOtherPlayer(player) )
  {
    SetSquare(x,y,player);
    ++y;
    ++x;
  }
}
//---------------------------------------------------------------------------
void Reversi::DoMoveRight(int x, const int y, const Square player)
{
  while ( GetSquare(x,y) == GetOtherPlayer(player) )
  {
    SetSquare(x,y,player);
    ++x;
  }
}
//---------------------------------------------------------------------------
void Reversi::DoMoveUpRight(int x, int y, const Square player)
{
  while ( GetSquare(x,y) == GetOtherPlayer(player) )
  {
    SetSquare(x,y,player);
    --y;
    ++x;
  }
}
//---------------------------------------------------------------------------
const int Reversi::GetSize() const
{
  return mBoard.size();
}
//---------------------------------------------------------------------------
const int Reversi::Count(const Square player) const
{
  const int size = GetSize();

  int sum = 0;

  for (int y=0; y!=size; ++y)
  {
    for (int x=0; x!=size; ++x)
    {
      if (mBoard[x][y] == player) ++sum;
    }
  }
  return sum;
}
//---------------------------------------------------------------------------
const Square GetOtherPlayer(const Square player)
{
  switch (player)
  {
    case player1: return player2;
    case player2: return player1;
  }
  assert(!"Invalid player");
  return empty;
}
//---------------------------------------------------------------------------
const Square GetSquareXy(
  const std::vector<std::vector<Square> >& board,
  const int x, const int y)
{
  const int size = board.size();
  assert(x >= 0);
  assert(y >= 0);
  assert(x < size);
  assert(y < size);
  return board[y][x];
}
//---------------------------------------------------------------------------


#pragma package(smart_init)

 

 

 

 

 

UnitReversi.h

 

//---------------------------------------------------------------------------
/*
    Reversi, a simple board game
    Copyright (C) 2007  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
//---------------------------------------------------------------------------
#ifndef UnitReversiH
#define UnitReversiH
//---------------------------------------------------------------------------
#include <vector>
enum Square { empty, player1, player2 };
//---------------------------------------------------------------------------
struct Reversi
{
  Reversi(const int size);
  const std::vector<std::vector<Square> >& GetBoard() const;
  const Square GetSquare(const int x, const int y) const;
  void DoMove(const int x, const int y, const Square player);
  void SetSquare(const int x, const int y, const Square player);
  const bool IsValidMove(const int x, const int y, const Square player) const;
  const std::vector< std::pair<int,int> > GetValidMoves(const Square player) const;
  const int GetSize() const;
  const int Count(const Square player) const;

  private:
  std::vector<std::vector<Square> > mBoard;

  const bool IsValidMoveUp(const int x, const int y, const Square player) const;
  const bool IsValidMoveUpLeft(const int x, const int y, const Square player) const;
  const bool IsValidMoveLeft(const int x, const int y, const Square player) const;
  const bool IsValidMoveDownLeft(const int x, const int y, const Square player) const;
  const bool IsValidMoveDown(const int x, const int y, const Square player) const;
  const bool IsValidMoveDownRight(const int x, const int y, const Square player) const;
  const bool IsValidMoveRight(const int x, const int y, const Square player) const;
  const bool IsValidMoveUpRight(const int x, const int y, const Square player) const;

  void DoMoveUp(const int x, int y, const Square player);
  void DoMoveUpLeft(int x, int y, const Square player);
  void DoMoveLeft(int x, const int y, const Square player);
  void DoMoveDownLeft(int x, int y, const Square player);
  void DoMoveDown(const int x, int y, const Square player);
  void DoMoveDownRight(int x, int y, const Square player);
  void DoMoveRight(int x, const int y, const Square player);
  void DoMoveUpRight(int x, int y, const Square player);

};

const Square GetSquareXy(const std::vector<std::vector<Square> >& board, const int x, const int y);
const Square GetOtherPlayer(const Square player);

#endif

 

 

 

 

 

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict