Tic Tac Toe Game Code (Game Model & View Class) S60 2.1

Login to reply to this topic.
Mon, 2006-09-18 18:35
Joined: 2006-03-23
Forum posts: 7
---------------------GameModel.h-----------------
Code:
#include <e32def.h>
#include <e32std.h>
class TGameModel
{
public:
enum TPlayer
{
KPlayer1=0,
KPlayer2=1
};
enum Marks
{
KCircle=0,
  KCross=1
};
TGameModel();
void SetCell(TInt,TInt,TInt);
TBool isWinner(TInt);
TBool AreAllCellFilled();
void Reset();

public:
TInt Cells[3][3];
TBool  CurrentPlayer;
};
---------------GameModel.cpp--------------------------
Code:
#include "GameModel.h"

TGameModel::TGameModel()
{
this->CurrentPlayer=TGameModel::KPlayer1;
for (TInt i=0;i<=2;i++)
for(TInt j=0;j<=2;j++)
      Cells[i][j]=-1;

}
void TGameModel::SetCell(TInt Row,TInt Col,TInt Who)
{
Cells[Row][Col]=Who;
}

TBool TGameModel::isWinner(TInt who)
{
// check columns
TInt i;
for ( i=0; i < 3; i++)
{
if( (Cells[i][0] == who ) &&
(Cells[i][1] == who ) &&
(Cells[i][2] == who ) )
return ETrue;
}

// check rows
for (i=0; i < 3; i++)
{
if( ( Cells[0][i] == who ) &&
( Cells[1][i] == who ) &&
( Cells[2][i] == who ) )
return ETrue;
}

// check first diagonal i.e. left to right
if( ( Cells[0][0] == who ) &&
( Cells[1][1] == who ) &&
( Cells[2][2] == who ) )
return ETrue;

// check last diagonal i.e. right to left
if( ( Cells[0][2] == who ) &&
( Cells[1][1] == who ) &&
( Cells[2][0] == who ) )
return ETrue;
return EFalse;
}
void  TGameModel::Reset()
{
this->CurrentPlayer=TGameModel::KPlayer1;
for (TInt i=0;i<=2;i++)
for(TInt j=0;j<=2;j++)
      Cells[i][j]=-1;
}
TBool TGameModel::AreAllCellFilled()
{
TInt chk=0;
for (TInt i=0;i<=2;i++)
for(TInt j=0;j<=2;j++)
      if(Cells[i][j]<0)
        return EFalse;
return ETrue;
}

----------------------------------------------------
Code:
/* =======================
* File: SymGameAppView.h
* Author: Shailesh
* ====================== */

#ifndef __SYMGAME_APPVIEW_H__
#define __SYMGAME_APPVIEW_H__
#include <coecntrl.h>
#include "GameModel.h"
class CSymGameAppView : public CCoeControl
{
public:
TGameModel* GModel;
TInt crow,ccol; // current row , column
static CSymGameAppView* NewL(const TRect& aRect);
static CSymGameAppView* NewLC(const TRect& aRect);
~CSymGameAppView();
public:
void Draw(const TRect& aRect) const;
TKeyResponse CSymGameAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType);
private:
void ConstructL(const TRect& aRect);
CSymGameAppView();
void MarkCell(const TRect& aRect,TInt Mark) const;
void DrawBoard(const TRect&) const;
void DrawMessages(const TRect& rect) const;   
};
#endif
-------------------------------------------------------
Code:
/*============ File: SymGameAppView.cpp
* Created: 02/27/06
* Author: Shailesh
================================*/
#include <avkon.hrh>
#include <aknnotewrappers.h>

#include <coemain.h>
#include <SymGame.rsg>
#include "SymGameAppView.h"

CSymGameAppView* CSymGameAppView::NewL(const TRect& aRect)
    {
    CSymGameAppView* self = CSymGameAppView::NewLC(aRect);
    CleanupStack::Pop(self);
    return self;
    }

CSymGameAppView* CSymGameAppView::NewLC(const TRect& aRect)
    {
    CSymGameAppView* self = new (ELeave) CSymGameAppView;
    CleanupStack::PushL(self);
    self->ConstructL(aRect);
    return self;
    }

CSymGameAppView::CSymGameAppView()
    {
    }

CSymGameAppView::~CSymGameAppView()
    {
    delete GModel;
    }

void CSymGameAppView::ConstructL(const TRect& aRect)
    {
    CreateWindowL();
    SetRect(aRect);
    ActivateL();
    GModel=new (ELeave) TGameModel;
    crow=0;
    ccol=0;
    }
void CSymGameAppView::Draw(const TRect& aRect) const
    {
CWindowGc& gc = SystemGc();
    gc.Clear();
    DrawBoard(aRect);
    DrawMessages(aRect);
    }
void CSymGameAppView::DrawBoard(const TRect& aRect) const
{
CWindowGc& gc=SystemGc();
TInt i,j;
    TInt CellWidth,CellHeight;
    TRect Cell;
TRgb col(0,255,0);
    CellWidth=aRect.iBr.iX/3;
    CellHeight=(aRect.iBr.iY-30)/3;
    Cell.iTl.iX=1;
    Cell.iTl.iY=1;
    Cell.iBr.iX=CellWidth;
    Cell.iBr.iY=CellHeight;
    for (i=0;i<=2;i++)
    {
    for(j=0;j<=2;j++)
    {
    if(i==ccol && j==crow)
    gc.SetPenStyle(CGraphicsContext::EDashedPen);
    gc.DrawRect(Cell);
    gc.SetPenStyle(CGraphicsContext::ESolidPen);
MarkCell(Cell,GModel->Cells[j][i]);
Cell.iTl.iX=Cell.iTl.iX+CellWidth;
    Cell.iBr.iX=Cell.iBr.iX+CellWidth;
    }
    Cell.iTl.iX=1;
    Cell.iBr.iX=Cell.iTl.iX+CellWidth-1;
    Cell.iTl.iY=Cell.iTl.iY+CellHeight ;
    Cell.iBr.iY=Cell.iBr.iY+CellHeight;
    }
}
void CSymGameAppView::MarkCell(const TRect& Cell,TInt Mark) const
{
CWindowGc& gc=SystemGc();
    if (Mark==1)
    {
TPoint st(Cell.iTl.iX,Cell.iTl.iY);
    TPoint ed(Cell.iBr.iX,Cell.iBr.iY);
    gc.MoveTo(st);
    gc.DrawLineTo(ed);
    st.SetXY(Cell.iTl.iX,Cell.iBr.iY);
    ed.SetXY(Cell.iBr.iX,Cell.iTl.iY);
    gc.MoveTo(st);
    gc.DrawLineTo(ed);
    }
    if(Mark==0)
    {
    gc.DrawEllipse(Cell);
    }
}

TKeyResponse CSymGameAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,
TEventCode aType)
{
switch(aType)
{
case EEventKeyDown:
switch(aKeyEvent.iScanCode)
{
case EStdKeyRightArrow:
if (crow==2)
   crow=0;
else
   crow++;
DrawNow();
return EKeyWasConsumed;
case EStdKeyLeftArrow:
if (crow==0)
   crow=2;
else
   crow--;
DrawNow();
return EKeyWasConsumed;
case EStdKeyUpArrow:
if (ccol==0)
   ccol=2;
else
   ccol--;
DrawNow();
return EKeyWasConsumed;
case EStdKeyDownArrow:
if (ccol==2)
   ccol=0;
else
   ccol++;
DrawNow();
return EKeyWasConsumed;
case EStdKeyDevice3:
if(GModel->Cells[crow][ccol]==-1)
{
GModel->Cells[crow][ccol]=GModel->CurrentPlayer;
GModel->CurrentPlayer=!GModel->CurrentPlayer;
}
DrawNow();
return EKeyWasConsumed;
}
}
    if (GModel->isWinner(TGameModel::KPlayer1))
    {
           _LIT(message,"Player One Winner ! ");
           CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
           informationNote->ExecuteLD(message);
           GModel->Reset();
    }
    if (GModel->isWinner(TGameModel::KPlayer2))
    {
           _LIT(message,"Player Two Winner ! ");
           CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
           informationNote->ExecuteLD(message);
           GModel->Reset();            
    }
    if (GModel->AreAllCellFilled())
    {
           _LIT(message,"No Winner ! ");
           CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
           informationNote->ExecuteLD(message);
           GModel->Reset();            
    }
return EKeyWasNotConsumed;
}
void CSymGameAppView::DrawMessages(const TRect& rect) const
{
CWindowGc& gc=SystemGc();
gc.UseFont(iEikonEnv->NormalFont());
if(GModel->CurrentPlayer==TGameModel::KPlayer1)
gc.DrawText(_L("Current Player 1"),TPoint(rect.iTl.iX+5,rect.iBr.iY-30+15));
else
gc.DrawText(_L("Current Player 2"),TPoint(rect.iTl.iX+5,rect.iBr.iY-30+15));
}
  • Login to reply to this topic.