五子棋

2017-06-15  本文已影响30人  Fa1se003

被逼无奈写了个五子棋,代码烂到没朋友。

MAIN.h

// MAIN.h: interface for the MAIN class.

#if !defined(AFX_MAIN_H__05EB1A85_5357_4BD4_856F_85A3B9BF6D29__INCLUDED_)
#define AFX_MAIN_H__05EB1A85_5357_4BD4_856F_85A3B9BF6D29__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <afxwin.h>


class CMyApp:public CWinApp
{
public:
    virtual BOOL InitInstance();
};


class MyWindow:public CFrameWnd
{
    DECLARE_MESSAGE_MAP();
    afx_msg void OnPaint(void);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

    bool checkWin(int x,int j,int color);
    void initArr();
public:
    MyWindow();

};


#endif // !defined(AFX_MAIN_H__05EB1A85_5357_4BD4_856F_85A3B9BF6D29__INCLUDED_)

MAIN.cpp

// MAIN.cpp: implementation of the MAIN class.
//
//////////////////////////////////////////////////////////////////////

#include "MAIN.h"

#define GRIDlENGTH  60

bool who = false;

typedef struct 
{
    long x; //x坐标
    long y; //y坐标
    bool isSet; //是否被占用
    int color;  //被谁占用 1 白色  2 黑色
} StuInfo;


StuInfo listInfo[10][10] = {0};

BOOL CMyApp::InitInstance()
{   
    m_pMainWnd = new MyWindow();
    m_pMainWnd->ShowWindow(m_nCmdShow);
    m_pMainWnd->UpdateWindow();
    return TRUE;
}

void MyWindow::initArr()
{
    //初始化数组
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {
            listInfo[i][j].x = GRIDlENGTH*j + 50;
            listInfo[i][j].y = GRIDlENGTH*i + 50;
            listInfo[i][j].color = 0;
            listInfo[i][j].isSet = 0;
        }
    }
}

MyWindow::MyWindow()
{
    initArr();
    Create(NULL,"WZQ");
}

CMyApp theApp;
BEGIN_MESSAGE_MAP(MyWindow,CFrameWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

void MyWindow::OnPaint(void)
{
    CPaintDC dc(this);  
    CPen pen(PS_SOLID,1,RGB(180,180,180)),*oldPen = dc.SelectObject(&pen);
    int i;
    
    for(i = 0;i<10;i++)
    {
        dc.MoveTo(listInfo[i][0].x,listInfo[i][0].y);
        dc.LineTo(listInfo[i][9].x,listInfo[i][9].y);
        
        dc.MoveTo(listInfo[0][i].x,listInfo[0][i].y);       
        dc.LineTo(listInfo[9][i].x,listInfo[9][i].y);
    }
    
    CBrush wbrush(RGB(255, 255, 255));//白
    CBrush bbrush(RGB(0, 0, 0));//黑
    
    int size = 20;
    int j;
    for(i=0;i<10;i++)
    {
        for(j=0;j<10;j++)
        {   
            if(listInfo[i][j].isSet)
            {
                if(listInfo[i][j].color==1)
                {
                    dc.SelectObject(&wbrush);
                }
                else
                {
                    dc.SelectObject(&bbrush);
                }
                dc.Ellipse(CRect( listInfo[i][j].x-size, listInfo[i][j].y-size, listInfo[i][j].x+size, listInfo[i][j].y+size)); 
            }
        }
    }
    
    
    dc.SelectObject(oldPen);        
}


void MyWindow::OnLButtonDown(UINT nFlags, CPoint point)
{   
    CClientDC dc(this);
    CPen pen(PS_SOLID,1,RGB(180,180,180)),*oldPen = dc.SelectObject(&pen);
    
    CBrush wbrush(RGB(255, 255, 255));//白
    CBrush bbrush(RGB(0, 0, 0));//黑
    
    int distance = 10; //点击区域上下左右范围扩大10
    int size = 20; //点上下左右各扩大20
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {   
            if(abs(listInfo[i][j].x - point.x) < distance && abs(listInfo[i][j].y - point.y) < distance )
            {   
                if(listInfo[i][j].isSet)//如果被设置了就跳过
                {
                    continue;
                }

                if(who)//判断是否是白棋
                {
                    dc.SelectObject(&wbrush);
                }
                else
                {
                    dc.SelectObject(&bbrush);
                }
                dc.Ellipse(CRect( listInfo[i][j].x-size, listInfo[i][j].y-size, listInfo[i][j].x+size, listInfo[i][j].y+size)); 
                listInfo[i][j].isSet = true;
                listInfo[i][j].color = who?1:2;         
                
                bool res = checkWin(i,j,listInfo[i][j].color);
                if(res)
                {
                    //胜利之后重新初始化棋盘,发送重绘消息
                    initArr();
                    Invalidate(TRUE);
                    PostMessage(WM_PAINT,0,0);
                    who = false;
                }
                else
                {
                    who = who?false:true;
                }
            }
        }
    }
    
    dc.SelectObject(oldPen);
    
}


bool MyWindow::checkWin(int x,int y,int color)
{
    char winMsg[100] = {0};
    (color==1)?strcpy(winMsg,"白棋胜利"):strcpy(winMsg,"黑棋胜利");
    
    char str[200] = {0};


    //left 
    int left = 0;
    int i;
    for(i=1;i<5;i++)
    {
        if(y-i<0)
        {
            break;
        }

        if(listInfo[x][y-i].color == color)
        {
            left++;
            if(left >= 4)//左边查到四个就直接返回true
            {
                sprintf(str,"%s,left",winMsg);
                ::MessageBox(0,str,"Tips",0);
                return true;
            }
        }
        else
        {
            break;
        }
    }

    //rigth
    int right = 0;
    for(i=1;i<5;i++)
    {
        if(y+i>9)
        {
            break;
        }
        
        if(listInfo[x][y+i].color == color)
        {
            right++;
            if(right >= 4)//左边查到四个就直接返回true
            {
                sprintf(str,"%s,right",winMsg);
                ::MessageBox(0,str,"Tips",0);
                return true;
            }
        }
        else
        {
            break;
        }
    }

    if(left+right>=4)
    {
        sprintf(str,"%s,left+right",winMsg);
        ::MessageBox(0,str,"Tips",0);
        return true;
    }

    //左上斜
    int leftTop = 0;
    for(i=1;i<5;i++)
    {
        if(y-i<0 || x-i<0)
        {
            break;
        }
        if(listInfo[x-i][y-i].color == color)
        {
            leftTop++;
            if(leftTop >= 4)//左上查到四个就直接返回true
            {
                sprintf(str,"%s,leftTop",winMsg);
                ::MessageBox(0,str,"Tips",0);
                return true;
            }
        }
        else
        {
            break;
        }
    }

    //右下斜
    int rightBottom = 0;
    for(i=1;i<5;i++)
    {
        if(y+i>9|| x+i>9)
        {
            break;
        }
        if(listInfo[x+i][y+i].color == color)
        {
            rightBottom++;
            if(rightBottom >= 4)//右下查到四个就直接返回true
            {
                sprintf(str,"%s,rightBottom",winMsg);
                ::MessageBox(0,str,"Tips",0);
                return true;
            }
        }
        else
        {
            break;
        }
    }

    
    if(leftTop+rightBottom>=4)
    {
        sprintf(str,"%s,leftTop+rightBottom",winMsg);
        ::MessageBox(0,str,"Tips",0);
        return true;
    }


    
    //左下斜
    int leftBottom = 0;
    for(i=1;i<5;i++)
    {
        if(x+i>9 || y-i<0)
        {
            break;
        }
        if(listInfo[x+i][y-i].color == color)
        {
            leftBottom++;
            if(leftBottom >= 4)//左下查到四个就直接返回true
            {
                sprintf(str,"%s,leftBottom",winMsg);
                ::MessageBox(0,str,"Tips",0);
                return true;
            }
        }
        else
        {
            break;
        }
    }

    //右上斜
    int rightTop = 0;
    for(i=1;i<5;i++)
    {
        if(x-i<0 || y+i>9)
        {
            break;
        }
        if(listInfo[x-i][y+i].color == color)
        {
            rightTop++;
            if(rightTop >= 4)//左下查到四个就直接返回true
            {
                sprintf(str,"%s,rightTop",winMsg);
                ::MessageBox(0,str,"Tips",0);
                return true;
            }
        }
        else
        {
            break;
        }
    }


    if(leftBottom + rightTop>=4)
    {
        sprintf(str,"%s,leftBottom+rightTop",winMsg);
        ::MessageBox(0,str,"Tips",MB_OK);
        return true;
    }


    //上
    int top = 0;
    for(i=1;i<5;i++)
    {
        if(x-i<0)
        {
            break;
        }
        if(listInfo[x-i][y].color == color)
        {
            top++;
            if(top >= 4)//上查到四个就直接返回true
            {
                sprintf(str,"%s,top",winMsg);
                ::MessageBox(0,str,"Tips",0);
                return true;
            }
        }
        else
        {
            break;
        }
    }


    //下
    int bottom = 0;
    for(i=1;i<5;i++)
    {
        if(x+i>9)
        {
            break;
        }
        if(listInfo[x+i][y].color == color)
        {
            bottom++;
            if(bottom >= 4)//xia查到四个就直接返回true
            {
                sprintf(str,"%s,bottom",winMsg);
                ::MessageBox(0,str,"Tips",0);
                return true;
            }
        }
        else
        {
            break;
        }
    }

    if(top+bottom>=4)
    {
        sprintf(str,"%s,top+bottom",winMsg);
        ::MessageBox(0,str,"Tips",0);
        return true;
    }

    return 0;
}

image.png image.png image.png
上一篇 下一篇

猜你喜欢

热点阅读