论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

一个用C++编写的五子棋游戏(2)

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:46:59

////////////////////////////////////////////////////
//
// Main.Cpp  by Yuheng Zhao
//
////////////////////////////////////////////////////

#include "types.h"
#include "mouse.h"
#include "shell.h"
#include "luffar.h"

int main(void)
{
 InitMouse();
 InitGraphics();
 DrawBackground();

   SetPoint(1,1);                   // L刧ger musen p?punkten 1,1.
 ShowPoint();

 BOOL bWantToExit = FALSE;
 CPlayer player1(1);
 CPlayer player2(2);
 player2.ChangePlayer(TRUE);
 CBoard board(&player1,&player2);
 int winner=-1;
 int nCode=-1;
 int x,y,b=-1;

 CButton player1Button(CRect(480,50,610,70),"Player 1");
 CButton player2Button(CRect(480,75,610,95),"Computer");
 CButton newButton(CRect(450,430,530,450),"New");
 CButton quitButton(CRect(540,430,620,450),"Quit");

 board.Draw();
 newButton.Draw();
 quitButton.Draw();
 player1Button.Draw();
 player2Button.Draw();

 Message("Luffar Schack! 0.99");
 Message("Written by");
 Message("Yuheng Zhao 1997-5");
 Message("------------------");
 Message("New game ...");

 while (!bWantToExit)
 {
  ReadMouse(x,y,b);

  if (b==1)
  {
   if (quitButton.Clicked(x,y,b))
   {
    Message("Exit...");
    player2.ChangePlayer(FALSE);
    bWantToExit = TRUE;
    delay(200);
   }
   if (newButton.Clicked(x,y,b))
   {
    board.ResetBoard();
   }
   if (player1Button.Clicked(x,y,b))
   ;
   if (player2Button.Clicked(x,y,b))
   {
    if (player2.IsComputer())
    {
     player2.ChangePlayer(FALSE);
     player2Button.SetWindowText("Player 2");
    }else
    {
     player2.ChangePlayer(TRUE);
     player2Button.SetWindowText("Computer");
    };
    delay(200);
   }
   b=-1;
  }
  winner=board.Go();
   if (winner==1 || winner==2)
   {
    Message((winner==1)?"Player 1 wins.":
     (player2.IsComputer())?"Computer wins":"Player 2 wins");
    nCode=(winner==1)?ShowWinDlg(&player1):ShowWinDlg(&player2);
    if (nCode==1)
     board.ResetBoard();
   }
 }

 HidePoint();
 closegraph();
 return 0;
}
-------------------------------------------------------------------

////////////////////////////////////////////////////
//
//  Shell.Cpp  by Yuheng Zhao
//
////////////////////////////////////////////////////

#include "types.h"
#include "mouse.h"
#include "shell.h"

CMessagePad mp;

// INITERAR C++ GRAFIK
//   #################################################
void InitGraphics()
{
 int gdriver = VGA, gmode=VGAHI, errorcode;

 if (!(registerbgidriver(EGAVGA_driver)<0))
 {
  initgraph(&gdriver, &gmode, "");
  errorcode = graphresult();
  if (errorcode == grOk)
   return;
 }
 cout << "Error detected when initialize graphics...\n";
 cout << "No VGA compatible graphics card is found...\n";
 ReadKey();
 exit(1);
}

// RENSAR SK嶳MEN
//   ################################################
void Cls()
{

int maxx, maxy;
maxx = getmaxx();
maxy = getmaxy();
setcolor(0);
for(int tmp=0;tmp<(maxy+1);tmp++)
line(0,tmp,maxx,tmp);

}

// L嶴ER ETT TECKEN FR廙 TANGET BORDET
//   ################################################
char ReadKey()
{
 unsigned char tmp;
 asm{
   mov ax,0
   mov ah,0
   int 16h
   mov tmp,al
  }
return tmp;
}

void DrawBackground()
{
 Cls();

     //  Bakgrunden
 setfillstyle(LTBKSLASH_FILL,GREEN);
 bar(0,0,SCR_MAX_X,SCR_MAX_Y);

 //   Titel
 setfillstyle(SOLID_FILL,WHITE);
 bar(0,0,SCR_MAX_X,15);
 setcolor(BLACK);
 outtextxy(5,4,"Luffar schack 0.99 (Beta release)  by Yuheng Zhao");
 line(0,15,SCR_MAX_X,15);

 //   About info
 CRect rect(450,40,620,105);
 rect.Draw(BROWN,NOCOLOR,TRUE,5);

 mp.Draw();
}

void Rectangle(int x0,int y0,int x1,int y1,int color,BOOL shadow,int deep)
{
 setfillstyle(SOLID_FILL,color);
 bar(x0,y0,x1,y1);

 if(shadow)
 {
  //   Skuggan
  setcolor(BLACK);
  setfillstyle(INTERLEAVE_FILL,color);

  int poly[12];
  poly[0]=x1; //  F攔sta punkten
  poly[1]=y0;
  poly[2]=x1+deep; //  Andra punkten
  poly[3]=y0+deep;
  poly[4]=x1+deep; //  Tredje punkten
  poly[5]=y1+deep;
  poly[6]=x0+deep; //  Fj剅de punkten
  poly[7]=y1+deep;
  poly[8]=x0; //  Femte punkten
  poly[9]=y1;
  poly[10]=x1;
  poly[11]=y1;

  fillpoly(6,poly);
 }
}

int ShowWinDlg(CPlayer* winner)
{
 CRect rect(200,200,440,300);
 CRect title(200,200,440,215);
 CButton okButton(CRect(215,270,315,290),"OK");
 CButton quitButton(CRect(325,270,425,290),"Cancel");
 void *oldImage;
 unsigned int size;
 int newGame=-1;
 int x,y,b;

 /* calculate the size of the image */
 size = imagesize(rect.x0,rect.y0,rect.x1+10,rect.y1+10);

 /* allocate memory to hold the image */
 oldImage = malloc(size);

 /* grab the image */
 HidePoint();
 getimage(rect.x0,rect.y0,rect.x1+10,rect.y1+10,oldImage);

//   Rita dialogen
 rect.Draw(LBLUE,WHITE,TRUE,5);
 title.Draw(WHITE,WHITE);

 setcolor(BLACK);
 settextjustify(LEFT_TEXT,TOP_TEXT);
 outtextxy(title.x0+10,title.y0+5,(winner->IsComputer())?"You lose !!!":
            ((winner->WhichPlayer()==1)?"Player 1 wins !!!":
              "Player 2 wins !!!"));
 outtextxy(rect.x0+18,rect.y0+36,"Do you want to play again?");

 setcolor(WHITE);
 outtextxy(rect.x0+17,rect.y0+35,"Do you want to play again?");

 okButton.Draw();
 quitButton.Draw();
 ShowPoint();

 while (newGame==-1)
 {
  ReadMouse(x,y,b);

  if (b==1)
  {
   if (okButton.Clicked(x,y,b))
    newGame = 1;
   if (quitButton.Clicked(x,y,b))
    newGame = 0;
  }
 }

 HidePoint();
 putimage(rect.x0,rect.y0,oldImage,COPY_PUT);
 ShowPoint();

 /* clea n up */
 free(oldImage);
 WaitDlg(0,300);

 return newGame;
}

//  nMode = 0 visa dlg med delay
//  nMode = 1 visa dlg
//  nMode = 2 Hide dlg
void WaitDlg(int nMode, int nDelay)
{
 CRect rect(240,210,400,280);
 CRect title(240,210,400,225);
 static void *oldImage;
 unsigned int size;
 int x,y,b;

 if (nMode == 0 || nMode == 1)
 {
  /* calculate the size of the image */
  size = imagesize(rect.x0,rect.y0,rect.x1+10,rect.y1+10);

  /* allocate memory to hold the image */
  oldImage = malloc(size);

  /* grab the image */
  HidePoint();
  getimage(rect.x0,rect.y0,rect.x1+10,rect.y1+10,oldImage);

//  Rita dialogen
  rect.Draw(LBLUE,WHITE,TRUE,5);
  title.Draw(WHITE,WHITE);

  setcolor(BLACK);
  settextjustify(LEFT_TEXT,TOP_TEXT);
  outtextxy(rect.x0+21,rect.y0+36,"Please wait...");

  setcolor(WHITE);
  outtextxy(rect.x0+20,rect.y0+35,"Please wait...");

  ShowPoint();
 }

 if (nMode == 0)
  delay(nDelay);

 if (nMode == 0 || nMode == 2)
 {
  HidePoint();
  putimage(rect.x0,rect.y0,oldImage,COPY_PUT);
  ShowPoint();

  /* clean up */
  free(oldImage);
 }
}

// n<100
void IntToChar(int n, char* ch)
{
 ch[0]=(char)(n/10+'0');
 ch[1]=(char)(n-(n/10)*10+'0');
 ch[2]='\0';
}

void Message(char* msg)
{
 mp.Message(msg);
}

-------------------------------------------------------------------

////////////////////////////////////////////////////
//
//  Types.Cpp  by Yuheng Zhao
//
////////////////////////////////////////////////////

#include "TYPES.H"
#include "MOUSE.H"

BOOL CPoint::operator== (const CPoint& pt) const
{
 if ((x==pt.x) && (y==pt.y))
  return TRUE;
 return FALSE;
}

const CPoint& CPoint::operator= (const CPoint& pt)
{
 if (this != &pt)
 {
  x = pt.x;
  y = pt.y;
 }
 return *this;
}

const CRect& CRect::operator= (const CRect& rect)
{
 if (this != &rect)
 {
  x0 = rect.x0;
  y0 = rect.y0;
  x1 = rect.x1;
  y1 = rect.y1;
 }
 return *this;
}

BOOL CRect::PtInRect(const CPoint& pt)
{
 if (pt.x>=x0 && pt.x<=x1 && pt.y>=y0 && pt.y<=y1)
  return TRUE;
 return FALSE;
}

void CRect::Draw(int color,int lineColor,BOOL shadow,int deep)
{
 setfillstyle(SOLID_FILL,color);
 bar(x0,y0,x1,y1);

 if(shadow)
 {
  //   Skuggan
  int poly[8];
  poly[0]=x1; //   F攔sta punkten
  poly[1]=y0;
  poly[2]=x1+deep; //   Andra punkten
  poly[3]=y0+deep;
  poly[4]=x1+deep; //   Tredje punkten
  poly[5]=y1+deep;
  poly[6]=x1;
  poly[7]=y1;
  setcolor(DGRAY);
  setfillstyle(SOLID_FILL,DGRAY);
  fillpoly(4,poly);

  poly[0]=x1+deep; //   Tredje punkten
  poly[1]=y1+deep;
  poly[2]=x0+deep; //   Fj剅de punkten
  poly[3]=y1+deep;
  poly[4]=x0; //   Femte punkten
  poly[5]=y1;
  poly[6]=x1;
  poly[7]=y1;

  setcolor(BLACK);
  setfillstyle(SOLID_FILL,BLACK);
  fillpoly(4,poly);
 }

  if (lineColor!=-1)
 {
  setcolor(lineColor);
  rectangle(x0,y0,x1,y1);
 }

}

CButton::CButton(const CRect& rect,char* ch):m_Rect(rect)
{
 m_pchName = new char[strlen(ch)+1];
 strcpy(m_pchName,ch);
 m_bPressed=FALSE;
}

void CButton::Draw()
{
 HidePoint();
 settextjustify(CENTER_TEXT,CENTER_TEXT);
 if (!m_bPressed)
 {
  m_Rect.Draw(WHITE,BLACK,TRUE,2);

//   Skriv ut titel
  setcolor(BLACK);
  outtextxy((m_Rect.x1-m_Rect.x0)/2+m_Rect.x0,
     (m_Rect.y1-m_Rect.y0)/2+m_Rect.y0,
     m_pchName);
 }else
 {
  m_Rect.Draw(BLACK,BLACK,TRUE,1);

//   Skriv ut titel
  setcolor(LGRAY);
  outtextxy((m_Rect.x1-m_Rect.x0)/2+m_Rect.x0,
     (m_Rect.y1-m_Rect.y0)/2+m_Rect.y0,
     m_pchName);
 }
   settextjustify(LEFT_TEXT,TOP_TEXT);
 ShowPoint();
}

void CButton::SetWindowText(char* title)
{
   delete m_pchName;
 m_pchName = new char[strlen(title)+1];
 strcpy(m_pchName,title);
 Draw();
}

BOOL CButton::Clicked(int x,int y,int b)
{
 if (m_Rect.PtInRect(CPoint(x,y)) && b!=0)
 {
  if (!m_bPressed)
  {
   m_bPressed = TRUE;
   Draw();
  }
  return TRUE;
 }else
 if (m_bPressed)
 {
  m_bPressed = FALSE;
  Draw();
 }
 return FALSE;
}

-----------------------------------------------------------------------------

////////////////////////////////////////////////////
//
//  Mouse.Cpp  by Yuheng Zhao
//
////////////////////////////////////////////////////

#include "TYPES.h"
#include "mouse.h"

// INITERAR ALLA MS KOMPATIBLA MUS F橰 BRUK I PROGRAMMET
//   #################################################
void InitMouse()
{
 unsigned int error=0;
 asm{
  mov ax,0h
  int 33h
  mov error,ax
   }
 if (!error)
 {
  cout << "Error mouse not found! \n ";
  exit(1);
 }

}

//  S嶵TER PEKAREN P?ANGIVNA PLATSEN
//  ######################################################
void SetPoint(unsigned int x,unsigned int y)
{
asm{
   mov ax,4h
   mov cx,x
   mov dx,y
   int 33h

  }
}

// VISAR MUSPEKAREN (TESTAT I TEXT MODE)
//   #################################################
void ShowPoint()
{
 asm{
  mov ax,1
  int 33h
  }
}


// G橫MER MUSPEKAREN VAR DEN 嶯 嶳
//   ##################################################
void HidePoint()
{
 asm{
   mov ax,02h
   int 33h
  }
}

// G橫MER MUSPEKAREN OM DEN KOMMER IN I EN VISS AREA
//   ################################################
void HidePointXY(unsigned int cordx,unsigned int cordy,unsigned int x,unsigned int y)
{
asm{
  mov ax,10h
  mov cx,cordx
  mov dx,cordy
  mov SI,x
  mov DI,y
  int 33h
 }
}

//   ANGER VAR MUS PEKAREN 嶳 OCH OM N廏ON HAR KLICKT P?MUSEN
//   #######################################################
void ReadMouse(int &xcordi,int &ycordi,int &bbutt)
{
unsigned int x,y,b;
asm{
  mov ax,3h
  int 33h
  mov x,cx
  mov y,dx
  mov b,bx
 }
xcordi=x;
ycordi=y;
bbutt=b;
}


//   ANGER VAR DEN ACTUELLA KNAPPEN SIST TRYCKDES NER
//   #######################################################
void PressXY(int &xcordi,int &ycordi,unsigned int bbutt)
{
unsigned int x,y;
xcordi=0;
ycordi=0;

asm{
  mov ax,5h
  MOV BX,bbutt
  int 33h
  mov x,cx
  mov y,dx

  }
xcordi=x;
ycordi=y;
}


//   ANGER VAR DEN ACTUELLA KNAPPEN SIST SL嶱TES
//   #######################################################
void ReleaseXY(int &xcordi,int &ycordi,unsigned int bbutt)
{
unsigned int x,y;
xcordi=0;
ycordi=0;

asm{
  mov ax,06h
  mov bx,bbutt
  int 33h
  mov x,cx
  mov y,dx

  }
xcordi=x;
ycordi=y;
}

//   ANGER GR嶯SERNA F橰 MUSPEKAREN
//   #######################################################
void Limits(unsigned int minx,unsigned maxX,unsigned miny,unsigned maxY)
{
asm{
  mov ax,07h
  mov  cx,minx
  mov  dx,maxX
  int 33h
  mov ax,08h
  mov cx,miny
  mov dx,maxY
  int 33h
 }
}

//   ANGER STORLEKEN P?MUSBUFFRTERN I BYTE
// #####################################################
int MouseSize()
{
unsigned int tmp=0;
asm{
  mov ax,15h
  int 33h
  mov tmp,bx
 }
return tmp;
}

视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058