電腦遊戲製作開發設計論壇 首頁 電腦遊戲製作開發設計論壇
任何可以在PC上跑的遊戲都可以討論,主要以遊戲之製作開發為主軸,希望讓台灣的遊戲人有個討論、交流、教學、經驗傳承的園地
 
 常見問題常見問題   搜尋搜尋   會員列表會員列表   會員群組會員群組   會員註冊會員註冊 
 個人資料個人資料   登入檢查您的私人訊息登入檢查您的私人訊息   登入登入 

Google
[VC++]超簡單的井字遊戲

 
發表新主題   回覆主題    電腦遊戲製作開發設計論壇 首頁 -> 遊戲程式中級班:資料結構、Win32 API、各種視窗函式庫
上一篇主題 :: 下一篇主題  
發表人 內容
mirror
散播福音的祭司


註冊時間: 2007-07-27
文章: 174

828.60 果凍幣

發表發表於: 2007-10-20, AM 4:53 星期六    文章主題: [VC++]超簡單的井字遊戲 引言回覆

Very Happy Very Happy
這次介紹一個很簡單的井字遊戲
是用MFC做的,只要開啟空白的專案
再引用即可
記得是MFC的哦!!別弄錯了哦!!

代碼:

#include <afxwin.h>
//=================================
#define  IDB_EXIT     3001
#define  IDB_CLEAR    3002
#define  IDB_B1       101
#define  IDB_B2       102
#define  IDB_B3       103
#define  IDB_B4       104
#define  IDB_B5       105
#define  IDB_B6       106
#define  IDB_B7       107
#define  IDB_B8       108
#define  IDB_B9       109
//=================================
//按鈕類別
class MyButton:public CButton
{
public:
   MyButton(int nID,int x,int y,int w,int h,LPCTSTR Caption,CWnd *p)
   {Create(Caption, WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(x,y,x + w,y + h), p , nID);}
};

//=================================
//視窗框架類別
class MyFrame:public CFrameWnd
{
public:
   bool BWplay;
   int BWcount;
public:
   MyFrame(int x,int y,int w,int h)
   {
      //**建立視窗**//

      Create(NULL,"井字遊戲",WS_BORDER||WS_OVERLAPPED||WS_CAPTION,CRect(x,y,x + w,y + h));
      

      //**建立按鈕**//

      //OnExit()
      CButton *b_exit = new MyButton(IDB_EXIT,10,10,100,40,"結束程式",this);
      //OnClear()
      CButton *b_clear = new MyButton(IDB_CLEAR,120,10,100,40,"清除",this);

      //B1 - B9
      CButton *b[9];
      
      int I;
      for(I=0;I<9;I++)
      {b[I] = new MyButton(101 + I, int(I % 3)*90 + 10, int(I / 3)*90 + 60,90,90,"",this);}
      
      OnClear();
   }

   ~MyFrame()
   {}

   void OnWin(CString S1,CString S2,CString S3);
   void OnExit();
   void OnClear();
   void OnButtonB1B9(UINT nID);
public:
   DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(MyFrame, CFrameWnd)
   ON_COMMAND(IDB_EXIT,OnExit)
   ON_COMMAND(IDB_CLEAR,OnClear)
   ON_COMMAND_RANGE(IDB_B1,IDB_B9,OnButtonB1B9)
END_MESSAGE_MAP()

//勝利判斷
void MyFrame::OnWin(CString S1,CString S2,CString S3)
{
  CString PlayerName;
  if(BWplay==false)
  {PlayerName="●";}
  else
  {PlayerName="○";}

  if((S1==S2) && (S1==S3) && (S1 != ""))
  {
     MessageBox(PlayerName + "贏了","勝利", MB_OK|MB_ICONEXCLAMATION);
    OnClear();
  }
}
//結束程式
void MyFrame::OnExit()
{
   MessageBox("結束程式","自爆", MB_OK|MB_ICONEXCLAMATION);
   PostMessage(WM_CLOSE);
}

//清除內容
void MyFrame::OnClear()
{
  int I;
  BWplay = true;
  BWcount = 0;
  for(I=IDB_B1;I<=IDB_B9;I++)
  {GetDlgItem(I)->SetWindowTextA("");}
}

//B1 - B9 按鈕內容
void MyFrame::OnButtonB1B9(UINT nID)
{
  CString s;
  GetDlgItem(nID)->GetWindowTextA(s);

  if(s == "")
  {
    if(BWplay==false)
    {
        GetDlgItem(nID)->SetWindowTextA("○");
         BWplay = !BWplay;
    }
    else
    {
       GetDlgItem(nID)->SetWindowTextA("●");
        BWplay = !BWplay;
    }

    BWcount++;
  }

  int I;
  CString S1,S2,S3;
  I = nID - 101;

  //橫
  I = (int(I/3)*3);
  GetDlgItem(100 + I +1)->GetWindowTextA(S1);
  GetDlgItem(100 + I +2)->GetWindowTextA(S2);
  GetDlgItem(100 + I +3)->GetWindowTextA(S3);
  OnWin(S1,S2,S3);

  //直
  I = nID - 100;
  I = (I - int((I-1)/3)*3);
  GetDlgItem(100 + I )->GetWindowTextA(S1);
  GetDlgItem(100 + I + 3)->GetWindowTextA(S2);
  GetDlgItem(100 + I + 6)->GetWindowTextA(S3);
  OnWin(S1,S2,S3);

  //斜1
  GetDlgItem(IDB_B1)->GetWindowTextA(S1);
  GetDlgItem(IDB_B5)->GetWindowTextA(S2);
  GetDlgItem(IDB_B9)->GetWindowTextA(S3);
  OnWin(S1,S2,S3);

  //斜2
  GetDlgItem(IDB_B3)->GetWindowTextA(S1);
  GetDlgItem(IDB_B5)->GetWindowTextA(S2);
  GetDlgItem(IDB_B7)->GetWindowTextA(S3);
  OnWin(S1,S2,S3);

  //平手
  if(BWcount == 9)
  {
     MessageBox("○●平手!重新開始!","清除", MB_OK|MB_ICONEXCLAMATION);
     OnClear();
  }
}

//=================================
//應用程式類別
class MyApp: public CWinApp
{
public:
   BOOL InitInstance()
   {
      CFrameWnd *Frame = new MyFrame(100,100,300,370);

      m_pMainWnd = Frame;
      Frame->ShowWindow(SW_SHOW);

      return TRUE;
   }
}app;



mirror 在 2007-10-26, PM 12:43 星期五 作了第 1 次修改
回頂端
檢視會員個人資料 發送私人訊息
還是零分
散播福音的祭司


註冊時間: 2007-09-19
文章: 164

653.83 果凍幣

發表發表於: 2007-10-20, PM 11:04 星期六    文章主題: 引言回覆

MFC我不會用耶
能不能把開空白專案的步驟說詳細點?
Embarassed 麻煩你
回頂端
檢視會員個人資料 發送私人訊息 參觀發表人的個人網站
fire231
散播福音的祭司


註冊時間: 2007-09-15
文章: 152

322.55 果凍幣

發表發表於: 2007-10-20, PM 11:07 星期六    文章主題: 引言回覆

我也看不懂= =

我只知道MFC要用VC++的樣子
回頂端
檢視會員個人資料 發送私人訊息
mirror
散播福音的祭司


註冊時間: 2007-07-27
文章: 174

828.60 果凍幣

發表發表於: 2007-10-21, PM 1:50 星期日    文章主題: 引言回覆

如果你是用vc2005,你可以這樣做
1.開啟新專案
2.win32 >> win32專案
3.應用程式類型 >> windows應用程式
4.空白專案(打勾)
5.然後在方案按右鍵[加入>>新增項目]
6.程式碼>>c++檔
7.把上敘原始檔貼入c++檔
8.方案上按右鍵選[屬性]
9.MFC的使用改為靜態或共用都可
10.字元集改為多位元組
11.開始編譯

vc6的使用方法是大同小異
附上執行的圖
回頂端
檢視會員個人資料 發送私人訊息
還是零分
散播福音的祭司


註冊時間: 2007-09-19
文章: 164

653.83 果凍幣

發表發表於: 2007-10-21, PM 9:06 星期日    文章主題: 引言回覆

謝謝
編譯正常了 Very Happy
回頂端
檢視會員個人資料 發送私人訊息 參觀發表人的個人網站
babu61509
散播福音的祭司


註冊時間: 2007-08-26
文章: 142

681.01 果凍幣

發表發表於: 2007-10-21, PM 11:49 星期日    文章主題: 引言回覆

偷偷改成C#...

姆...改了一些地方~_~
圈圈的按鍵改成判斷名稱(索引?文字比較好記咩...),陣列從0開始.

代碼:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace tgame
{
    public class Mybutton : Button
    {
        // 建構式
        public Mybutton(string name,int x,int y,int width, int height, string text,EventHandler eventhandler, Form form)
        {
            // 設定名稱
            this.Name = name;

            // 設定 位置 和 大小
            this.Left = x;
            this.Top = y;
            this.Width = width;
            this.Height = height;
           
            // 設定文字
            this.Text = text;

            // 加入事件 (使用委託)
            this.Click+=new EventHandler(eventhandler);

            // 加入表單
            form.Controls.Add(this);
        }
    }

    public class Myform : Form
    {
        public bool BWplay; // 是否為 ○ 下棋
        public int BWcount; // 白色下棋次數

        // 建構式
        public Myform(int x, int y ,int width, int height)
        {

            /////** 視窗設定 **/////

            // 取消 最大化 和 最小化 按鈕
            this.MaximizeBox = false;
            this.MinimizeBox = false;

            // 設定 位置 和 大小
            this.Left = x;
            this.Top = y;
            this.Width = width;
            this.Height = height;

            // 設定標題
            this.Text = "井字遊戲";

            // 取消圖示
            this.ShowIcon = false;

            /////** 建立按鈕 **/////

            // OnExit()
            Mybutton b_exit = new Mybutton("IDB_EXIT", 10, 10, 100, 40, "結束程式",new EventHandler(this.OnExit), this);
            // OnClear()
            Mybutton b_clear = new Mybutton("IDB_CLEAR", 120, 10, 100, 40, "清除",new EventHandler(this.OnClear), this);
            // B1 - b9
            Mybutton[] b = new Mybutton[9];

            int I;
            for(I=0;I<9;I++)
                b[I] = new Mybutton("IDB_B" + I, (int)(I%3)*90+10, (int)(I/3)*90+60,90,90,"",new EventHandler(this.OnButtonB1B9),this);            OnClear();

        }

        // 勝利判斷
        public void OnWin(string S1, string S2, string S3)
        {
            string PlayerName;
            if (BWplay == false)
                PlayerName = "●";
            else
                PlayerName = "○";
            // 三個都一樣,而且不是空字串就贏
            if (S1 == S2 && S1 == S3 && S1 != "")
            {
                MessageBox.Show(PlayerName + "贏了", "勝利", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                OnClear();
            }
        }

        // 結束程式
        public void OnExit(object sender, EventArgs e)
        {
            MessageBox.Show("結束程式", "自爆", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Application.Exit();
        }
        public void OnExit()
        {
            OnExit(this, new EventArgs());
        }

        // 清除內容
        public void OnClear(object sender, EventArgs e)
        {
            int I;
            BWplay = true;
            BWcount = 0;
            for (I = 0; I < 9; I++)
            {
                string name = "IDB_B" + I;
                this.Controls[name].Text = "";
            }
        }
        public void OnClear()
        {
            OnClear(this, new EventArgs());
        }
        // B1 - B9 按鈕內容
        public void OnButtonB1B9(object sender, EventArgs e)
        {
            // 取得傳入物件
            Mybutton s = (Mybutton)sender;

            if (s.Text == "")
            {
                if (BWplay == false)
                {
                    s.Text = "○";
                }
                else
                {
                    s.Text = "●";
                }
                // 換人下
                BWplay = !BWplay;
                // 下棋次數+1
                BWcount++;
            }

            int I;
            string S1, S2, S3;

            // 橫
            I = int.Parse(s.Name.Substring(5));
            I = (int)(I / 3) * 3 ;
            S1 = this.Controls["IDB_B" + I ].Text;
            S2 = this.Controls["IDB_B" + (I + 1)].Text;
            S3 = this.Controls["IDB_B" + (I + 2)].Text;
            OnWin(S1, S2, S3);

            // 直
            I = int.Parse(s.Name.Substring(5));
            I = I - (int)(I / 3) * 3 ;
            S1 = this.Controls["IDB_B" + I ].Text;
            S2 = this.Controls["IDB_B" + (I + 3 )].Text;
            S3 = this.Controls["IDB_B" + (I + 6 )].Text;
            OnWin(S1, S2, S3);

            // 斜1
            S1 = this.Controls["IDB_B0"].Text;
            S2 = this.Controls["IDB_B4"].Text;
            S3 = this.Controls["IDB_B8"].Text;
            OnWin(S1, S2, S3);

            // 斜2
            S1 = this.Controls["IDB_B2"].Text;
            S2 = this.Controls["IDB_B4"].Text;
            S3 = this.Controls["IDB_B6"].Text;
            OnWin(S1, S2, S3);

            // 平手
            if (BWcount == 9)
            {
                MessageBox.Show("○●平手!重新開始!", "清除", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                OnClear();
            }
        }
    }

    static class Program
    {
        // 程式從這邊開始執行
        public static void Main()
        {
            // 建立 Myform 型的 MyForm 表單
            Myform MyForm = new Myform(100,100,300,370);
            // 讓 MyForm 運作
            Application.Run(MyForm);
        }
    }
}

_________________
已經畢業了!!
回頂端
檢視會員個人資料 發送私人訊息
從之前的文章開始顯示:   
發表新主題   回覆主題    電腦遊戲製作開發設計論壇 首頁 -> 遊戲程式中級班:資料結構、Win32 API、各種視窗函式庫 所有的時間均為 台灣時間 (GMT + 8 小時)
1頁(共1頁)

 
前往:  
無法 在這個版面發表文章
無法 在這個版面回覆文章
無法 在這個版面編輯文章
無法 在這個版面刪除文章
無法 在這個版面進行投票
可以 在這個版面附加檔案
可以 在這個版面下載檔案


Powered by phpBB © 2001, 2005 phpBB Group
正體中文語系由 phpbb-tw 維護製作