juego de othello

CaYi
26 de Mayo del 2004
Auxilio!!!!...alguna vez.. jugaron othello??.. bueno es ese jueguito en un tablero.. de 2 jugadores, en el cual si las fichas de mi jugador rodean ficha o fichas del contrincante.. sus fichas se convierten en mias.. y gana quien mas fichas tenga despues de haberse llenado el tablero!.. si me ayudan lo mas pronto posible... les traere suerte!! =D.. jiiii... auxilitititito porfitas!

rolopro
26 de Mayo del 2004
te pusite de suerte porque tuve la suerte de implementarlo en cunjunto con un socio,pero lo hicimo en c++ builder a si que no se como mandarlo yo no tengo internet a full pero puedo mandarte las clases

//-----------esta es la clase logica que simula el juego----
#ifndef RulesH
#define RulesH

class TJuego
{
private:
int cas[10][10];
int player;
int cant[3];
protected:
bool MayMove(int y,int x);
bool Search(int ad,int ay,int ax);
public:
TJuego();
bool Play(int ay,int ax);
bool GameOver();
long GetSquare(int ay,int ax);
void Score(int &w,int &b);
int NowPlayer() {return player;};
};
//---------------------------------------------------------------------------
#endif
//------ahora la implementacion---------------------------
//---------------------------------------------------------------------------


#pragma hdrstop

#include "Rules.h"
#include <mem.h>

int y[8] = {-1,-1,0,1,1,1,-1};
int x[8] = {0,-1,-1,-1,0,1,1,1};
int not[3] = {0,2,1};

TJuego::TJuego()
{
player = 1;
cant[0] = 0;
cant[1] = 2;
cant[2] = 2;
setmem(cas,sizeof(cas),0);
cas[4][4] = 1;
cas[5][5] = 1;
cas[4][5] = 2;
cas[5][4] = 2;
}
bool TJuego::MayMove(int ny,int nx)
{
if (cas[ny][nx] != 0)
return false;
for(int i=0;i<8;i++)
{
int ay,ax;
ay = ny + y[i];
ax = nx + x[i];
if (ay >= 0 && ay < 10 && ax >= 0 && ax < 10)
{
if (cas[ay][ax] != 0)
{
return true;
}
}
}
return false;
}
//---------------------------------------------------------------------------
bool TJuego::Search(int ad,int ay,int ax)
{
int ny = ay,nx = ax;
int cant = 0;
do
{
cant++;
ny = ny + y[ad];
nx = nx + x[ad];
}
while(ny >= 0 && ny < 10 && nx >= 0 && nx < 10 && cas[ny][nx] == not[player]);
if(ny >= 0 && ny < 10 && nx >= 0 && nx < 10 && cas[ny][nx]== player && cant>1)
return true;
return false;
}
//--------------------------------------------------------------
bool TJuego::Play(int ay,int ax)
{
bool result = false;
if(MayMove(ay,ax))
{
for(int i = 0; i< 8;i++)
{
int ny = ay,nx = ax;
int val;
if(Search(i,ay,ax))
{
cas[ay][ax] = player;
result = true;
do
{
ny = ny + y[i];
nx = nx + x[i];
val = cas[ny][nx];
cas[ny][nx] = player;
cant[player]++;
cant[not[player]]--;
}
while(val== not[player]);
cant[player]--;
cant[not[player]]++;
}
}
if (result)
{
cant[player]++;
player = not[player];
}
return result;
}
return false;
}
//-------------------------------------------------------
bool TJuego::GameOver()
{
if (cant[1] + cant[2] == 100)
{
return true;
}

for (int i=0; i<10; i++)
{
for (int j=0; j<10; j++)
{
bool result = false;
if(MayMove(i,j))
{
for(int k = 0; k< 8;k++)
{
if(Search(k,i,j))
{
return false;
}
}
}
}
}

return true;
}


long TJuego::GetSquare(int ay,int ax)
{
return cas[ay][ax];
}
void TJuego::Score(int &w,int &b)
{
w = cant[1];
b = cant[2];
}
#pragma package(smart_init)
//ahora te dire que el componente para simular el tablero se llama tdrawfil---
//---------Aqui te va la clase de la forma---------------
//-------el .h----------

#ifndef FormH
#define FormH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "Form.h"
#include <Grids.hpp>
#include "Rules.h"
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TDrawGrid *DrawGrid1;
TPanel *Panel1;
TLabel *Turno;
TLabel *Label2;
TLabel *Label3;
TLabel *Label4;
TLabel *Label5;
TLabel *ScoreB;
TLabel *ScoreN;
TLabel *Label1;
void __fastcall FormPaint(TObject *Sender);
void __fastcall DrawGrid1SelectCell(TObject *Sender, int ACol,
int ARow, bool &CanSelect);
private: // User declarations
TJuego Game;
void fill(int cx,int cy,int radio,int color);
void Refresh();
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//------------el .cpp-------------------
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Form.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void TForm1::fill(int cx,int cy,int radio,int color)
{
for (int i=cy-radio; i<cy+radio; i++)
{
for (int j=cx-radio; j<cx+radio; j++)
{
if ((i-cy)*(i-cy)+(j-cx)*(j-cx)<=radio*radio)
{
DrawGrid1->Canvas->Pixels[j][i] = color;
}
}
}
}

void TForm1::Refresh()
{
for(int i = 0;i<10;i++)
{
int ry = DrawGrid1->DefaultRowHeight;
int cx = DrawGrid1->DefaultColWidth;
int line = DrawGrid1->GridLineWidth;
for(int j = 0; j < 10;j++)
{
int y = (line + ry)*i;
int x = (line + cx)*j;
int col = Game.GetSquare(i,j);
if (col==1)
{
DrawGrid1->Canvas->Ellipse(x+5,y+5,x+cx-5,y+ry-5);
fill(x+cx/2,y+ry/2,(ry-11)/2,0xffffff);
}
else
if (col==2)
{
fill(x+cx/2,y+ry/2,(ry-10)/2,0);
}

}
}
if (Game.NowPlayer()==1)
{
Turno->Caption = "Blancas";
}
else
{
Turno->Caption = "Negras";
}
int w,b;
Game.Score(w,b);
ScoreB->Caption = w;
ScoreN->Caption = b;
ScoreB->Font->Color = clBlack;
ScoreN->Font->Color = clBlack;
if (w>b)
{
ScoreB->Font->Color = clRed;
}
else
if (b>w)
{
ScoreN->Font->Color = clRed;
}
if (Game.GameOver())
{
Label1->Visible = true;
}
}


void __fastcall TForm1::FormPaint(TObject *Sender)
{
Refresh();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DrawGrid1SelectCell(TObject *Sender, int ACol,
int ARow, bool &CanSelect)
{
Game.Play(ARow,ACol);
Refresh();
}
//---------------------------------------------------------------------------


espero que te sirva de algo