FIVECHESSDLG.CPP
资源名称:WuZiQi.rar [点击查看]
上传用户:gt3658
上传日期:2022-07-09
资源大小:97k
文件大小:31k
源码类别:
棋牌游戏
开发平台:
Visual C++
- // FiveChessDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "FiveChess.h"
- #include "FiveChessDlg.h"
- #include "WinXPButtonST.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- CWinXPButtonST m_Ok;
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- virtual BOOL OnInitDialog();
- afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- DDX_Control(pDX, IDOK, m_Ok);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- ON_WM_CTLCOLOR()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CFiveChessDlg dialog
- CFiveChessDlg::CFiveChessDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CFiveChessDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CFiveChessDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- closeflag = false;
- }
- void CFiveChessDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CFiveChessDlg)
- DDX_Control(pDX, IDC_BUTTON5, m_bt_newbegin);
- DDX_Control(pDX, IDC_BUTTON3, m_bt_regrete);
- DDX_Control(pDX, IDC_BUTTON2, m_bt_begin);
- DDX_Control(pDX, IDC_COMBO1, m_combox);
- DDX_Control(pDX, IDC_STATIC1, m_mainboard);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CFiveChessDlg, CDialog)
- //{{AFX_MSG_MAP(CFiveChessDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_CTLCOLOR()
- ON_WM_LBUTTONDOWN()
- ON_BN_CLICKED(IDC_BUTTON2, OnBegin)
- ON_BN_CLICKED(IDC_BUTTON3, OnRegret)
- ON_BN_CLICKED(IDC_BUTTON5, OnNewBegin)
- ON_WM_TIMER()
- ON_WM_CREATE()
- ON_WM_DESTROY()
- ON_MESSAGE(WM_LIBEN,OnLiben)
- ON_WM_CLOSE()
- ON_COMMAND(ID_MENUCANCEL, OnMenucancel)
- ON_COMMAND(ID_MENUITEM32774, OnMenuOPen)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CFiveChessDlg message handlers
- BOOL CFiveChessDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // TODO: Add extra initialization here
- is_myturn = false;
- SetWindowText("五子棋");
- SetTimer(1,500,0);
- CRect rc ;
- m_mainboard.GetClientRect(&rc);
- m_combox.AddString("初级");
- m_combox.AddString("高级");
- m_combox.SetCurSel(0);
- ((CButton *)GetDlgItem(IDC_RADIO2))->SetCheck(1);
- int x = rc.right ;
- int y = rc.bottom ;
- step_x = (x)/20 ;
- step_y = (y)/20;
- for(int i = 0 ;i < 20 ; i++)
- for (int j=0;j < 20; j++ )
- Fivearray[i][j] = 0 ;
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CFiveChessDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CFiveChessDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- DrawGrid();
- DrawAllChess();
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CFiveChessDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CFiveChessDlg::DrawGrid()
- {
- CDC *pdc = m_mainboard.GetDC();
- CRect rc ;
- m_mainboard.GetClientRect(&rc);
- int x = rc.right ;
- int y = rc.bottom ;
- int xx = rc.left ;
- int yy = rc.top+6 ;
- //画竖线
- for (int i = 0 ; i < 21; i++ )
- {
- pdc->MoveTo(xx,yy);
- pdc->LineTo(xx,y);
- xx += step_x ;
- }
- xx = rc.left ;
- yy = rc.top +6;
- //画横线
- for ( i = 0 ; i < 20 ; i++ )
- {
- pdc->MoveTo(xx,yy);
- pdc->LineTo(x,yy);
- yy += step_y ;
- }
- }
- void CFiveChessDlg::DrawChess(CPoint point)
- {
- int x = point.x -27;
- int y = point.y-19 ;
- int linex = y / step_y;
- int liney = x / step_x ;
- if ( (y - linex * step_y) > (step_y/2) )
- {
- linex++;
- }
- if ( (x - liney * step_x) > (step_x/2) )
- {
- liney++;
- }
- CDC * pdc = m_mainboard.GetDC();
- if ( linex <= 0 || linex >= 20 || liney <= 0 || liney >= 20||Fivearray[linex][liney] != 0 )
- {
- is_myturn = true;
- return ;
- }
- is_myturn = false;
- Fivearray[linex][liney] = 1;
- Step tstep;
- tstep.side = 1;
- tstep.x = linex;
- tstep.y = liney;
- StepList.AddTail(tstep);
- pdc->SelectStockObject(BLACK_BRUSH);
- pdc->Ellipse(liney*step_x-10,linex*step_y-10+6,liney * step_x +10,linex * step_y +10+6);
- }
- void CFiveChessDlg::DrawChess(int x,int y,bool is_current)
- {
- CDC * pdc = m_mainboard.GetDC();
- if (Fivearray[x][y] == 0)
- return;
- if ( Fivearray[x][y] == 1 )
- {
- pdc->SelectStockObject(BLACK_BRUSH);
- }
- else
- {
- pdc->SelectStockObject(WHITE_BRUSH);
- }
- if ( is_current == true)
- {
- CBrush br(RGB(211,198,244));
- pdc->SelectObject(br);
- pdc->Ellipse(y*step_x-10,x*step_y-10+6,y * step_x +10,x * step_y +10+6);
- }
- else
- pdc->Ellipse(y*step_x-10,x*step_y-10+6,y * step_x +10,x * step_y +10+6);
- }
- HBRUSH CFiveChessDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
- pDC->SetBkMode(TRANSPARENT);
- if (nCtlColor != CTLCOLOR_EDIT && nCtlColor != CTLCOLOR_LISTBOX)
- hbr = ::CreateSolidBrush(RGB(80,152,152));
- return hbr;
- }
- void CFiveChessDlg::DrawAllChess()
- {
- for (int i = 0 ; i <20 ; i++ )
- for (int j= 0; j<20 ; j++ )
- {
- if (Fivearray[i][j] != 0)
- {
- DrawChess(i,j,false);
- }
- }
- }
- void CFiveChessDlg::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- if ( is_myturn == false)
- {
- //MessageBox("请点开始按钮","提示",MB_OK);
- return;
- }
- DrawChess(point);
- if ( IsSuccess()|| is_myturn == true)
- return ;
- //初级
- if ( m_combox.GetCurSel() == 0 )
- {
- Step st;
- if (SearchValue(Fivearray,st,true) != 100000)
- SearchMaxValue(st,Fivearray);
- st.side = 3;
- Fivearray[st.x][st.y] = 3;
- StepList.AddTail(st);
- DrawChess(st.x,st.y,false);
- m_currentstep.x = st.x;
- m_currentstep.y = st.y;
- }
- //高级
- else
- {
- Step st;
- int score = SearchMaxValue(st,Fivearray);
- if ( score >= 1000 || score <= 20 )
- {
- st.side = 3;
- Fivearray[st.x][st.y] = 3;
- StepList.AddTail(st);
- DrawChess(st.x,st.y,false);
- m_currentstep.x = st.x;
- m_currentstep.y = st.y;
- }
- else
- {
- GameStatus temp;
- for (int i = 0 ; i < 20 ; i++)
- for (int jj = 0 ; jj< 20 ;jj++)
- temp.fivearray[i][jj] = Fivearray[i][jj];
- temp.deep = 0;
- temp.is_machine = true ;
- temp.score = 0;
- Step st1;
- Step st2;
- //机器方下得分
- int score1 = DeepSearch(temp,st1,true,0);
- //换人方下得分
- int score2 = DeepSearch(temp,st2,false,0);
- if ( score1 > score2 )
- {
- st1.side = 3;
- Fivearray[st1.x][st1.y] = 3;
- StepList.AddTail(st1);
- DrawChess(st1.x,st1.y ,false);
- m_currentstep.x = st1.x;
- m_currentstep.y = st1.y;
- }
- if ( score1 < score2 )
- {
- st2.side = 3;
- Fivearray[st2.x][st2.y] = 3;
- StepList.AddTail(st2);
- DrawChess(st2.x,st2.y,false );
- m_currentstep.x = st2.x;
- m_currentstep.y = st2.y;
- }
- if ( score1 == score2 )
- {
- if ( st1.deep <= st2.deep )
- {
- st1.side = 3;
- Fivearray[st1.x][st1.y] = 3;
- StepList.AddTail(st1);
- DrawChess(st1.x,st1.y,false);
- m_currentstep.x = st1.x;
- m_currentstep.y = st1.y;
- }
- else
- {
- st2.side = 3;
- Fivearray[st2.x][st2.y] = 3;
- StepList.AddTail(st2);
- DrawChess(st2.x,st2.y,false );
- m_currentstep.x = st2.x;
- m_currentstep.y = st2.y;
- }
- }//if
- }
- }
- if (! IsSuccess())
- {
- is_myturn = true;
- }
- CDialog::OnLButtonDown(nFlags, point);
- }
- bool CFiveChessDlg::IsSuccess()
- {
- if ( StepList.GetCount() == 0 )
- return false;
- Step temp = StepList.GetTail();
- int i = temp.x ;
- int j = temp.y ;
- int side = temp.side ;
- if ( LeftRight(i,j,side) >= 5 || UpDown(i,j,side) >= 5
- ||LupToRdown(i,j,side) >= 5 || RuptoLdown(i,j,side)>=5)
- {
- if ( side == 3 )
- {
- MessageBox("电脑胜利,结束战斗!","提示",MB_OK);
- }
- else
- {
- MessageBox("你胜利了,结束战斗!","提示",MB_OK);
- }
- is_myturn = false;
- Invalidate();
- return true;
- }
- return false ;
- }
- //从左到右相同棋子数
- int CFiveChessDlg::LeftRight(int i, int j,int side)
- {
- int tempi,count;
- tempi = i;
- count = 1;
- //toleft
- while ( --tempi > 0 && Fivearray[tempi][j] == side)
- {
- count ++;
- }
- tempi = i;
- while ( ++ tempi <20 && Fivearray[tempi][j] == side )
- {
- count ++;
- }
- return count;
- }
- //从上到下相同棋子数
- int CFiveChessDlg::UpDown(int i, int j,int side)
- {
- int tempj,count;
- tempj = j;
- count = 1;
- while ( --tempj > 0 && Fivearray[i][tempj] == side)
- {
- count ++;
- }
- tempj = j;
- while ( ++ tempj <20 &&Fivearray[i][tempj] == side )
- {
- count ++;
- }
- return count;
- }
- //从左上到右下相同的棋子数
- int CFiveChessDlg::LupToRdown(int i, int j,int side)
- {
- int tempi,tempj,count;
- tempi = i,tempj = j;
- count = 1;
- while ( --tempi > 0 && -- tempj >0 && Fivearray[tempi][tempj] == side)
- {
- count ++;
- }
- tempi = i,tempj = j;
- while ( ++ tempi<20 && ++ tempj < 20 &&Fivearray[tempi][tempj] == side )
- {
- count ++;
- }
- return count;
- }
- //从右上到左下的相同棋子数
- int CFiveChessDlg::RuptoLdown(int i, int j,int side)
- {
- int tempi,tempj,count;
- tempi = i,tempj = j;
- count = 1;
- while ( --tempi > 0 && ++tempj <20 && Fivearray[tempi][tempj] == side)
- {
- count ++;
- }
- tempi = i,tempj = j;
- while ( ++ tempi<20 && -- tempj > 0 && Fivearray[tempi][tempj] == side )
- {
- count ++;
- }
- return count;
- }
- int CFiveChessDlg::LeftToRight_Status(int i, int j ,int array[][20])
- {
- int tempj,count;
- tempj = j;
- count = 1;
- bool aliveone = false;
- bool alivetwo = false;
- bool beyond = false;
- int side = array[i][j];
- //toleft
- while ( --tempj > 0 && array[i][tempj] == side)
- {
- count ++;
- }
- if ( tempj > 0 && array[i][tempj] == 0)
- {
- aliveone = true;
- }
- else if ( tempj > 0 && array[i][tempj] == 1 )
- {
- beyond = true;
- }
- tempj = j;
- while ( ++ tempj <20 && array[i][tempj] == side )
- {
- count ++;
- }
- if ( tempj < 20 && array[i][tempj] == 0)
- {
- alivetwo = true;
- }
- else if ( tempj > 0 && array[i][tempj] == 1 )
- {
- beyond = true;
- }
- if ( count >= 5)
- {
- return 8 ;
- }
- if (count == 1 && aliveone == true && alivetwo == true )
- {
- return 0;
- }
- if ( count == 1 && beyond == true )
- {
- return 1 ;
- }
- if ( count > 1 && aliveone == true && alivetwo == true )
- {
- return count + 3;
- }
- if ( count > 1 && (aliveone == true || alivetwo == true ))
- {
- return count ;
- }
- return 0;
- }
- int CFiveChessDlg::UpToDown_Status(int i, int j ,int array[][20])
- {
- int tempi,count;
- tempi = i;
- count = 1;
- bool aliveone = false;
- bool alivetwo = false;
- bool beyond = false;
- int side = array[i][j];
- while ( --tempi > 0 && array[tempi][j] == side)
- {
- count ++;
- }
- if ( tempi > 0 && array[tempi][j] == 0 )
- {
- aliveone = true;
- }
- else if (tempi > 0 && array[tempi][j] == 1 )
- {
- beyond = true;
- }
- tempi = i;
- while ( ++ tempi < 20 && array[tempi][j] == side )
- {
- count ++;
- }
- if ( tempi < 20 && array[tempi][j] == 0 )
- {
- alivetwo = true;
- }
- else if ( tempi < 20 && array[tempi][j] == 1)
- {
- beyond = true ;
- }
- if ( count >= 5)
- {
- return 8 ;
- }
- if (count == 1 && aliveone == true && alivetwo == true )
- {
- return 0;
- }
- if ( count == 1 && beyond == true )
- {
- return 1 ;
- }
- if ( count > 1 && aliveone == true && alivetwo == true )
- {
- return count + 3;
- }
- if ( count > 1 && (aliveone == true || alivetwo == true ))
- {
- return count ;
- }
- return 0;
- }
- int CFiveChessDlg::LeftUpToRightDown_Status(int i, int j ,int array[][20])
- {
- int tempi,tempj,count;
- tempi = i,tempj = j;
- count = 1;
- bool aliveone = false;
- bool alivetwo = false;
- bool beyond = false;
- int side = array[i][j];
- while ( --tempi > 0 && -- tempj >0 && array[tempi][tempj] == side)
- {
- count ++;
- }
- if ( tempi > 0 && tempj >0 && array[tempi][tempj] == 0 )
- {
- aliveone = true;
- }
- else if ( tempi > 0 && tempj > 0 && array[tempi][tempj] == 1)
- {
- beyond = true;
- }
- tempi = i,tempj = j;
- while ( ++ tempi<20 && ++ tempj < 20 && array[tempi][tempj] == side )
- {
- count ++;
- }
- if ( tempi < 20 && tempj <20 && array[tempi][tempj] == 0)
- {
- alivetwo = true;
- }
- else if ( tempi < 20 && tempj < 20 && array[tempi][tempj] == 1 )
- {
- beyond = true;
- }
- if ( count >= 5)
- {
- return 8 ;
- }
- if (count == 1 && aliveone == true && alivetwo == true )
- {
- return 0;
- }
- if ( count == 1 && beyond == true )
- {
- return 1 ;
- }
- if ( count > 1 && aliveone == true && alivetwo == true )
- {
- return count + 3;
- }
- if ( count > 1 && (aliveone == true || alivetwo == true ))
- {
- return count ;
- }
- return 0;
- }
- int CFiveChessDlg::LeftDownToRightUp_Status(int i, int j ,int array[][20])
- {
- int tempi,tempj,count;
- tempi = i,tempj = j;
- count = 1;
- bool aliveone = false;
- bool alivetwo = false;
- bool beyond = false;
- int side = array[i][j];
- while ( ++tempi < 20 && --tempj > 0 && array[tempi][tempj] == side)
- {
- count ++;
- }
- if ( tempi < 20 && tempj > 0 && array[tempi][tempj] == 0)
- {
- aliveone = true;
- }
- else if ( tempi < 20 && tempj > 0 && array[tempi][tempj] == 1)
- {
- beyond = true ;
- }
- tempi = i,tempj = j;
- while ( -- tempi > 0 && ++ tempj < 20 && array[tempi][tempj] == side )
- {
- count ++;
- }
- if ( tempi > 0 && tempj < 20 && array[tempi][tempj] == 0 )
- {
- alivetwo = true;
- }
- else if ( tempi > 0 && tempj < 20 && array[tempi][tempj] == 1)
- {
- beyond = true;
- }
- if ( count >= 5)
- {
- return 8 ;
- }
- if (count == 1 && aliveone == true && alivetwo == true )
- {
- return 0;
- }
- if ( count == 1 && beyond == true )
- {
- return 1 ;
- }
- if ( count > 1 && aliveone == true && alivetwo == true )
- {
- return count + 3;
- }
- if ( count > 1 && (aliveone == true || alivetwo == true ))
- {
- return count ;
- }
- return 0;
- }
- int CFiveChessDlg::SearchValue(int array[][20],Step & st,bool machine)
- {
- int max_score = 0;
- int score = 0;
- GameStatus temp;
- temp.is_machine = machine;
- temp.deep = 0;
- for (int i = 0 ; i < 20 ; i++)
- for (int jj = 0 ; jj< 20 ;jj++)
- temp.fivearray[i][jj] = array[i][jj];
- for ( i = 1 ; i < 20 ; i++)
- for(int j =1; j < 20 ; j++)
- {
- if ( array[i][j] == 0)
- {
- temp.st.x = i ;
- temp.st.y = j;
- score = 0;
- if ( machine)
- {
- temp.fivearray[i][j] = 3;
- temp.st.side = 3;
- temp.is_machine = true;
- temp.score = 0;
- GetCurrentScore(temp);
- score = temp.score ;
- }
- else
- {
- temp.fivearray[i][j] = 1;
- temp.st.side = 1 ;
- temp.is_machine = false ;
- temp.score = 0;
- GetCurrentScore(temp);
- score = score - temp.score ;
- }
- if ( score == 100000 )
- {
- st.x = i;
- st.y = j;
- return score;
- }
- if ( score > max_score )
- {
- max_score = score ;
- st.x = i;
- st.y = j;
- }
- temp.fivearray[i][j] = 0;
- }
- }
- return max_score ;
- }
- void CFiveChessDlg::GetCurrentScore( GameStatus &board_situation)
- {
- int i = board_situation.st.x ;
- int j = board_situation.st.y;
- board_situation.score = 0;
- //状态
- //返回8 成5:five
- //返回7 活4:alivefour
- //返回6 活3:alivethree
- //返回5 活2:alivetwo
- //返回4 死4:deadfour
- //返回3 死3:deadthree
- //返回2 死2:deadtwo
- //返回0,
- //搜索出左到右的状态
- int lr =LeftToRight_Status(i , j ,board_situation.fivearray);
- if ( lr == 8 )
- {
- if ( board_situation.is_machine )
- board_situation.score = 100000;
- else
- board_situation.score = -100000;
- return ;
- }
- //从上到下
- int ud = UpToDown_Status(i,j ,board_situation.fivearray );
- if ( ud == 8 )
- {
- if ( board_situation.is_machine )
- board_situation.score = 100000;
- else
- board_situation.score = -100000;
- return ;
- }
- //从左上到右下
- int lutrd = LeftUpToRightDown_Status(i,j ,board_situation.fivearray );
- if ( lutrd == 8 )
- {
- if ( board_situation.is_machine )
- board_situation.score = 100000;
- else
- board_situation.score = -100000;
- return ;
- }
- //从左下到右上
- int ldtru =LeftDownToRightUp_Status(i,j ,board_situation.fivearray );
- if ( ldtru == 8 )
- {
- if ( board_situation.is_machine )
- board_situation.score = 100000;
- else
- board_situation.score = -100000;
- return ;
- }
- //是否活4
- if ( lr == 7 || ud == 7 || lutrd == 7 || ldtru == 7 )
- {
- if ( board_situation.is_machine )
- board_situation.score = 10000;
- else
- board_situation.score = -10000;
- return ;
- }
- //双死4
- if (
- ( lr == 4 && (ud == 4 || lutrd == 4 || ldtru == 4))
- || (ud == 4 && (lutrd == 4|| ldtru == 4))
- || (lutrd == 4 && ldtru == 4)
- )
- {
- if ( board_situation.is_machine )
- board_situation.score = 10000;
- else
- board_situation.score = -10000;
- return ;
- }
- //死4活3
- if (
- ( lr == 4 && (ud == 6 || lutrd == 6 || ldtru == 6))
- || ( ud == 4 && (lr == 6 || lutrd == 6 || ldtru == 6))
- || ( lutrd == 4 && (lr == 6 || ud== 6 || ldtru == 6))
- || ( ldtru== 4 && (lr == 6 || ud== 6 || lutrd == 6))
- )
- {
- if ( board_situation.is_machine )
- board_situation.score = 10000;
- else
- board_situation.score = -10000;
- return ;
- }
- //双活3
- if (
- ( lr == 6 && (ud == 6 || lutrd == 6 || ldtru == 6))
- || (ud == 6 && (lutrd == 6|| ldtru == 6))
- || (lutrd == 6 && ldtru == 6)
- )
- {
- if ( board_situation.is_machine )
- board_situation.score = 5000;
- else
- board_situation.score = -5000;
- return ;
- }
- //活3双活2
- if ( ( lr == 6 || ud == 6 || lutrd == 6 || ldtru == 6 ) &&
- ( ( lr == 5 && (ud == 5 || lutrd == 5 || ldtru == 5))
- || (ud == 5 && (lutrd == 5|| ldtru == 5))
- || (lutrd == 5 && ldtru == 5) )
- )
- {
- if ( board_situation.is_machine )
- board_situation.score = 5000;
- else
- board_situation.score = -5000;
- return ;
- }
- //活3死3
- if (
- ( lr == 3 && (ud == 6 || lutrd == 6 || ldtru == 6))
- || ( ud == 3 && (lr == 6 || lutrd == 6 || ldtru == 6))
- || ( lutrd == 3 && (lr == 6 || ud== 6 || ldtru == 6))
- || ( ldtru== 3 && (lr == 6 || ud== 6 || lutrd == 6))
- )
- {
- if ( board_situation.is_machine )
- board_situation.score = 1000;
- else
- board_situation.score = -1000;
- return ;
- }
- //死4
- if ( lr == 4 || ud == 4 || lutrd == 4 || ldtru == 4 )
- {
- if ( board_situation.is_machine )
- board_situation.score = 500;
- else
- board_situation.score = -500;
- return ;
- }
- //单活3
- if ( lr == 6 || ud == 6 || lutrd == 6 || ldtru == 6 )
- {
- if ( board_situation.is_machine )
- board_situation.score = 200;
- else
- board_situation.score = -200;
- return ;
- }
- //双活2
- if (
- ( lr == 5 && (ud == 5 || lutrd == 5 || ldtru == 5))
- || (ud == 5 && (lutrd == 5|| ldtru == 5))
- || (lutrd == 5 && ldtru == 5)
- )
- {
- if ( board_situation.is_machine )
- board_situation.score = 100;
- else
- board_situation.score = -100;
- return ;
- }
- //双死3
- if (
- ( lr == 3 && (ud == 3 || lutrd == 3 || ldtru == 3))
- || (ud == 3 && (lutrd == 3|| ldtru == 3))
- || (lutrd == 3 && ldtru == 3)
- )
- {
- if ( board_situation.is_machine )
- board_situation.score = 50;
- else
- board_situation.score = -50;
- return ;
- }
- //单活2
- if ( lr == 5 || ud == 5 || lutrd == 5 || ldtru == 5 )
- {
- if ( board_situation.is_machine )
- board_situation.score = 10;
- else
- board_situation.score = -10;
- return ;
- }
- //单死3
- if ( lr == 3 || ud == 3 || lutrd == 3 || ldtru == 3 )
- {
- if ( board_situation.is_machine )
- board_situation.score = 5;
- else
- board_situation.score = -5;
- return ;
- }
- //单死2
- if ( lr == 2 || ud == 2 || lutrd == 2 || ldtru == 2 )
- {
- if ( board_situation.is_machine )
- board_situation.score = 2;
- else
- board_situation.score = -2;
- return ;
- }
- //死1
- if ( lr == 1 || ud == 1 || lutrd == 1 || ldtru == 1 )
- {
- if ( board_situation.is_machine )
- board_situation.score = 1;
- else
- board_situation.score = -1;
- return ;
- }
- return ;
- }
- int CFiveChessDlg::SearchMaxValue(Step &st ,int array[][20])
- {
- int max_score = 0;
- int score = 0;
- GameStatus temp;
- temp.is_machine = true;
- temp.deep = 0;
- for (int i = 0 ; i < 20 ; i++)
- for (int jj = 0 ; jj< 20 ;jj++)
- temp.fivearray[i][jj] = array[i][jj];
- for ( i = 1 ; i < 20 ; i++)
- for(int j =1; j < 20 ; j++)
- {
- if ( array[i][j] == 0)
- {
- temp.st.x = i ;
- temp.st.y = j;
- score = 0;
- int score1 = 0,score2 = 0;
- temp.fivearray[i][j] = 3;
- temp.st.side = 3;
- temp.is_machine = true;
- temp.score = 0;
- GetCurrentScore(temp);
- score1 = temp.score ;
- temp.fivearray[i][j] = 1;
- temp.st.side = 1 ;
- temp.is_machine = false ;
- temp.score = 0;
- GetCurrentScore(temp);
- score2 = -temp.score ;
- //机器
- if ( score1 >= 10000 )
- {
- temp.fivearray[i][j] = 3;
- temp.st.side = 3;
- temp.is_machine = true;
- temp.score = 0;
- Step tempst1;
- int tempscore = SearchValue(temp.fivearray,tempst1,false);
- if ( score1 < tempscore)//如果当前盘面还有比现在分数大的,则防守
- {
- st.x = tempst1.x;
- st.y = tempst1.y ;
- return tempscore;
- }
- else
- {
- st.x = i;
- st.y = j;
- return score1;
- }
- }
- if ( score2 >= 100000 )
- {
- temp.fivearray[i][j] = 1;
- temp.st.side = 1 ;
- temp.is_machine = false ;
- temp.score = 0;
- Step tempst1;
- int tempscore = SearchValue(temp.fivearray,tempst1,true);
- if ( score2 <= tempscore)//
- {
- st.x = tempst1.x;
- st.y = tempst1.y ;
- return tempscore;
- }
- else
- {
- st.x = i;
- st.y = j;
- return score1;
- }
- }
- score = score1 + score2 ;
- if ( score > max_score )
- {
- max_score = score ;
- st.x = i;
- st.y = j;
- }
- temp.fivearray[i][j] = 0;
- }
- }
- return max_score ;
- }
- void CFiveChessDlg::OnBegin()
- {
- // TODO: Add your control notification handler code here
- for(int i = 0 ;i < 20 ; i++)
- for (int j=0;j < 20; j++ )
- Fivearray[i][j] = 0 ;
- StepList.RemoveAll();
- if ( ((CButton *)GetDlgItem(IDC_RADIO2))->GetCheck() == 1 )
- is_myturn = true;
- else
- {
- is_myturn = false;
- Fivearray[10][10] = 3;
- Step st;
- st.side = 3;
- st.x = 10;
- st.y = 10;
- st.deep = 0;
- StepList.AddTail(st);
- DrawChess(st.x,st.y,false);
- m_currentstep.x = st.x;
- m_currentstep.y = st.y;
- is_myturn = true;
- }
- Invalidate();
- }
- void CFiveChessDlg::OnRegret()
- {
- if (StepList.GetCount() == 0 )
- {
- MessageBox("你一个子都没下,悔什么悔啊,别乱搞","提示",MB_OK);
- return;
- }
- if ( StepList.GetCount() > 0)
- {
- Step tem = StepList.GetTail();
- StepList.RemoveTail();
- Fivearray[tem.x][tem.y] = 0 ;
- if ( StepList.GetCount() > 0)
- {
- tem = StepList.GetTail();
- if ( tem.side == 1 )
- {
- StepList.RemoveTail();
- Fivearray[tem.x][tem.y] = 0;
- }
- }
- }
- Invalidate();
- if ( StepList.GetCount() > 0 )
- {
- Step tem = StepList.GetTail();
- m_currentstep = tem;
- }
- is_myturn = true ;
- return ;
- }
- //返回分数
- int CFiveChessDlg::DeepSearch(GameStatus ts,Step &st,bool machine,int value)
- {
- ts.deep++;
- st.deep ++ ;
- int score =0 ;
- if ( ts.deep == 3 )
- {
- score = SearchValue ( ts.fivearray,st,machine);
- return score;
- }
- int min_score = 0;
- score = 0;
- //选出几局分较高的
- CList<GameStatus,GameStatus&>GameStatusList;
- for ( int i = 1 ; i < 20 ; i++)
- for(int j =1; j < 20 ; j++)
- {
- if ( ts.fivearray[i][j] == 0)
- {
- ts.st.x = i ;
- ts.st.y = j;
- score = 0;
- if ( machine )
- {
- ts.fivearray[i][j] = 3;
- ts.st.side = 3;
- ts.is_machine = true ;
- ts.score = 0;
- GetCurrentScore( ts );
- if ( score == 100000)
- {
- st.x = i;
- st.y = j;
- return score;
- }
- score = ts.score ;
- }
- else
- {
- ts.fivearray[i][j] = 1;
- ts.st.side = 1 ;
- ts.is_machine = false ;
- ts.score = 0;
- GetCurrentScore(ts);
- score = - ts.score ;
- ts.score = score;
- if ( score == 100000)
- {
- st.x = i;
- st.y = j;
- return score;
- }
- }
- ts.score = score ;
- if ( score == 0)
- {
- ts.fivearray[i][j] = 0;
- continue;
- }
- if ( GameStatusList.GetCount() == 0)
- {
- min_score = ts.score ;
- GameStatusList.AddTail(ts);
- }
- else
- {
- if ( score <= min_score )
- {
- min_score = score ;
- GameStatusList.AddTail(ts);
- }
- else
- {
- POSITION ps = GameStatusList.GetTailPosition();
- while ( ps != NULL )
- {
- GameStatus temp = GameStatusList.GetPrev(ps);
- if ( temp.score >= score )
- {
- GameStatusList.InsertAfter(ps,ts);
- break;
- }
- }
- if ( ps == NULL )
- {
- GameStatusList.AddHead( ts );
- }
- }
- }
- ts.fivearray[i][j] = 0;
- }
- }//for
- POSITION ps = GameStatusList.GetHeadPosition();
- Step tempstep = st;
- int tempscore = 0 ;
- int max = 0;
- for ( i = 0 ;i < 3; i ++ )
- {
- GameStatus temp = GameStatusList.GetNext(ps);
- value = temp.score ;
- tempscore = DeepSearch(temp,tempstep,! machine,temp.score );
- if ( tempscore > max )
- {
- max = tempscore;
- st = temp.st;
- st.deep = tempstep.deep ;
- }
- if ( ps == NULL )
- break;
- }
- return max ;
- }
- //重新开始
- void CFiveChessDlg::OnNewBegin()
- {
- for(int i = 0 ;i < 20 ; i++)
- for (int j=0;j < 20; j++ )
- Fivearray[i][j] = 0 ;
- StepList.RemoveAll();
- if ( ((CButton *)GetDlgItem(IDC_RADIO2))->GetCheck() == 1 )
- is_myturn = true;
- else
- {
- is_myturn = false;
- Fivearray[10][10] = 3;
- Step st;
- st.side = 3;
- st.x = 10;
- st.y = 10;
- st.deep = 0;
- StepList.AddTail(st);
- DrawChess(st.x,st.y,false);
- m_currentstep.x = st.x;
- m_currentstep.y = st.y;
- is_myturn = true;
- }
- Invalidate();
- }
- void CFiveChessDlg::OnTimer(UINT nIDEvent)
- {
- DrawChess(oldstep.x,oldstep.y,false);
- oldstep = m_currentstep;
- flag = !flag;
- DrawChess(m_currentstep.x,m_currentstep.y,flag);
- CDialog::OnTimer(nIDEvent);
- }
- BOOL CAboutDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // TODO: Add extra initialization here
- SetWindowText("五子棋");
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- BOOL CFiveChessDlg::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Add your specialized code here and/or call the base class
- cs.style = WS_POPUP;
- cs.dwExStyle =WS_EX_TOOLWINDOW;
- return CDialog::PreCreateWindow(cs);
- }
- BOOL CFiveChessDlg::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
- {
- // TODO: Add your specialized code here and/or call the base class
- return CDialog::Create(IDD, pParentWnd);
- }
- int CFiveChessDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CDialog::OnCreate(lpCreateStruct) == -1)
- return -1;
- tnd.cbSize = sizeof(NOTIFYICONDATA);
- tnd.uFlags =NIF_ICON|NIF_MESSAGE|NIF_TIP;
- tnd.hWnd = this->m_hWnd;
- tnd.uID = 0;
- tnd.uCallbackMessage = WM_LIBEN;
- //tnd.hIcon = LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON1));
- tnd.hIcon = (HICON)LoadImage(AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDI_ICON1),
- IMAGE_ICON,
- 16,
- 16,
- NULL);
- strcpy(tnd.szTip,"黑白五子棋");
- Shell_NotifyIcon(NIM_ADD,&tnd);
- // TODO: Add your specialized creation code here
- return 0;
- }
- LRESULT CFiveChessDlg::OnLiben(WPARAM wParam,LPARAM lParam)
- {
- UINT uID;
- UINT uMouseMsg;
- // POINT pt;
- uID = (UINT) wParam;
- uMouseMsg = (UINT) lParam;
- if ( uMouseMsg == WM_RBUTTONDOWN )
- {
- CMenu menu;
- VERIFY(menu.LoadMenu(IDR_MENU1));
- CPoint point;
- CMenu* pPopup = menu.GetSubMenu(0);
- ASSERT(pPopup != NULL);
- CWnd* pWndPopupOwner = this;
- while (pWndPopupOwner->GetStyle() & WS_CHILD)
- pWndPopupOwner = pWndPopupOwner->GetParent();
- ::GetCursorPos(&point);
- pPopup->TrackPopupMenu( TPM_RIGHTALIGN , point.x, point.y,
- pWndPopupOwner);
- }
- if ( uMouseMsg == WM_LBUTTONDOWN )
- {
- ShowWindow(SW_SHOWNORMAL);
- ::SetForegroundWindow(this->m_hWnd);
- }
- return 0;
- }
- void CFiveChessDlg::OnDestroy()
- {
- CDialog::OnDestroy();
- StepList.RemoveAll();
- // TODO: Add your message handler code here
- }
- HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
- pDC->SetBkMode(TRANSPARENT);
- if (nCtlColor != CTLCOLOR_EDIT && nCtlColor != CTLCOLOR_LISTBOX)
- hbr = ::CreateSolidBrush(RGB(128,128,200));
- return hbr;
- }
- BOOL CFiveChessDlg::OnCommand(WPARAM wParam, LPARAM lParam)
- {
- return CDialog::OnCommand(wParam, lParam);
- }
- void CFiveChessDlg::OnClose()
- {
- // TODO: Add your message handler code here and/or call default
- if (closeflag )
- {
- tnd.uFlags = NULL;
- Shell_NotifyIcon(NIM_DELETE,&tnd);
- CDialog::OnClose();
- KillTimer(1);
- }
- else
- {
- ShowWindow(SW_HIDE);
- }
- }
- void CFiveChessDlg::OnMenucancel()
- {
- // TODO: Add your command handler code here
- closeflag = TRUE; // really exit
- SendMessage(WM_CLOSE);
- }
- void CFiveChessDlg::OnMenuOPen()
- {
- ShowWindow(SW_SHOW);
- }