精华内容
下载资源
问答
  • 五子棋 c++ 五子棋 c++ 五子棋 c++
  • c++ 五子棋

    2012-05-25 14:56:31
    C++编写的五子棋游戏,非常的好玩哟.大家快来下载哟,哈哈
  • c++五子棋

    2020-12-31 22:36:29
    五子棋一.前言二.代码 一.前言 1.可以和你的朋友来一句噢! 2.这是用了坐标的方法,所以代码简单,适合新手 二.代码 #include<iostream> #define N 10//棋盘规格 using namespace std; static int chessboard...

    一.前言

    1.可以和你的朋友来一句噢!
    2.这是用了坐标的方法,所以代码简单,适合新手

    二.代码

    #include<iostream>
    #define N 10//棋盘规格
    using namespace std;
    static int chessboard[N][N];//棋盘
    struct chess{//棋子
        int x,y;
        int color;//0为无子,1为白,-1为黑
    };
    void init_chessboard()//初始化棋盘函数;
    {
        int i,j;
        for(i=0;i<N;i++)
            for(j=0;j<N;j++)
            {
                chessboard[i][j]=0;
            }
    }
    bool is_win(struct chess che)//判断胜负函数,ture 未分出胜负,可以继续下棋;false分出胜负
    {
        int x,y,num;
        int xmin,xmax,ymin,ymax,min,max;
        //begin判断左右方向
        num=0;
        xmin=(che.y-4>0)?che.y-4:0;//左边界
        xmax=(che.y+4<N-1)?che.y+4:N-1;//右边界
        //cout<<xmin<<"左右"<<xmax<<endl;
        for(y=xmin;y<=xmax;y++)
        {
            if(chessboard[che.x][y]==che.color&&num<5)
            {
                num++;
                //cout<<num<<endl;
                if(num==5)
                    return false;
            }
            else
                num=0;
        }
        //end 判断左右方向
        //begin判断上下方向
        num=0;
        ymin=(che.x-4>0)?che.x-4:0;//上边界
        ymax=(che.x+4<N-1)?che.x+4:N-1;//下边界
        //cout<<ymin<<"上下"<<ymax<<endl;
        for(x=ymin;x<=ymax;x++)
        {
            if(chessboard[x][che.y]==che.color&&num<5)
            {
                num++;
                //cout<<num<<endl;
                if(num==5)
                    return false;
            }
            else
                num=0;
        }
        //end 判断上下方向
        //begin判断135度方向
        num=0;
        xmin=(che.y<4)?che.y:4;//左边界到点的距离
        xmax=(N-1-che.y<4)?N-1-che.y:4;//右边界到点的距离
        ymin=(che.x<4)?che.x:4;//上边界到点的距离
        ymax=(N-1-che.x<4)?N-1-che.x:4;//下边界到点的距离
        min=xmin<ymin?xmin:ymin;//左上方边界到点的距离
        max=xmax<ymax?xmax:ymax;//右下放边界到点的距离
        //cout<<"左上边界距离"<<min<<endl;
        //cout<<"右下边界距离"<<max<<endl;
        for(x=che.x-min,y=che.y-min;x<=che.x+max;x++,y++)//左上到右下遍历
        {
            if(chessboard[x][y]==che.color&&num<5)
            {
                num++;
            //    cout<<num<<endl;
                if(num==5)
                    return false;
            }
            else
                num=0;
        }
        //end判断135度方向
    //begin判断45度方向
        num=0;
        min=ymin<xmax?ymin:xmax;//右上距离
        max=xmin<ymax?xmin:ymax;//左下距离
        //cout<<"右上距离"<<min<<endl;
        //cout<<"左下距离"<<max<<endl;
        for(x=che.x-min,y=che.y+min;x<=che.x+max;x++,y--)//由右上到左下判断
        {
            if(chessboard[x][y]==che.color&&num<5)
            {
                num++;
            //    cout<<num<<endl;
                if(num==5)
                return false;
            }
            else
                num=0;
        }
        //end 45度方向
        return true;
    }
    bool is_right_chess(struct chess che)
    {
        if(che.x>=0&&che.x<N&&che.y>=0&&che.y<N&&chessboard[che.x][che.y]==0){
            chessboard[che.x][che.y]=che.color;
            return true;
        }
        else
        {
            cout<<"落子不合法,重新下子!"<<endl;
            return false;
        }
    }
    void show_chessboard()
    {
        int i,j;
        cout<<"   0 1 2 3 4 5 6 7 8 9"<<endl;
        for(i=0;i<N;i++)
        {
            cout<<i<<"  ";
            for(j=0;j<N;j++)
            {
                //cout<<chessboard[i][j]<<" ";
                if(chessboard[i][j]==-1)
                    cout<<"*"<<" ";
                else if(chessboard[i][j]==1)
                    cout<<"o"<<" ";
                else
                    cout<<"-"<<" ";
            }
            cout<<endl;
        }
    }
    struct chess put_chess(int colo)
    {
        if(colo==1)
            cout<<"白方下子"<<endl;
        else if(colo==-1)
            cout<<"黑方下子"<<endl;
        struct chess che;
        cin>>che.x;
        cin>>che.y;
        che.color=colo;
        return che;
    }
    int renrenModle()
    {
        init_chessboard();
        struct chess pre;
        //int colo;
        while(1)
        {
            show_chessboard();
            do{//黑方下棋
                pre=put_chess(-1);
            }while(!is_right_chess(pre));//下子不合法,重下
            show_chessboard();
            if(!is_win(pre))//黑方胜,胜时返回0,未分胜负返回1;
            {
                cout<<"黑方胜"<<endl;
                return -1;
            }
            do{//白方下棋
                pre=put_chess(1);
            }while(!is_right_chess(pre));//下子不合法,重下
            show_chessboard();
            if(!is_win(pre))//白方胜
            {
                cout<<"白方胜"<<endl;
                return 1;
            }
        }
    }
    int main()
    {
        renrenModle();
        return 0;
    }
    
    展开全文
  • C++ 五子棋

    2021-05-18 19:29:08
    //创建对话框,绘制五子棋,并实现五子棋的输赢判断 #include<windows.h> #include <tchar.h> //棋盘和棋手,黑子先手 LPCWSTR lps[11] = { L" 1",L" 2",L" 3",L" 4",L" 5",L" 6",L" 7",L" 8",L" 9",L...
    //创建对话框,绘制五子棋,并实现五子棋的输赢判断
    #include<windows.h>
    #include <tchar.h>
    
    //棋盘和棋手,黑子先手
    LPCWSTR lps[11] = { L" 1",L" 2",L" 3",L" 4",L" 5",L" 6",L" 7",L" 8",L" 9",L"10",L"11" };
    int chessboard[11][11] = {
    		0,0,0,0,0,0,0,0,0,0,0,
    		0,0,0,0,0,0,0,0,0,0,0,
    		0,0,0,0,0,0,0,0,0,0,0,
    		0,0,0,0,0,0,0,0,0,0,0,
    		0,0,0,0,0,0,0,0,0,0,0,
    		0,0,0,0,0,0,0,0,0,0,0,
    		0,0,0,0,0,0,0,0,0,0,0,
    		0,0,0,0,0,0,0,0,0,0,0,
    		0,0,0,0,0,0,0,0,0,0,0,
    		0,0,0,0,0,0,0,0,0,0,0,
    		0,0,0,0,0,0,0,0,0,0,0
    }, chequer = -1;
    
    //重新开始
    void reset()
    {
    	for (int i = 0; i <= 10; i++)
    		for (int j = 0; j <= 10; j++)
    			chessboard[i][j] = 0;
    	chequer = -1;
    }
    //判断输赢
    int judgeWinner()
    {
    	//横向
    	for (int i = 0; i <= 10; i++)
    		for (int j = 0; j <= 6; j++)
    		{
    			if (chessboard[i][j] == 1 && chessboard[i][j + 1] == 1 && chessboard[i][j + 2] == 1 && chessboard[i][j + 3] == 1 && chessboard[i][j + 4] == 1)
    				return 1;
    			if (chessboard[i][j] == -1 && chessboard[i][j + 1] == -1 && chessboard[i][j + 2] == -1 && chessboard[i][j + 3] == -1 && chessboard[i][j + 4] == -1)
    				return -1;
    		}
    	//纵向
    	for (int j = 0; j <= 10; j++)
    		for (int i = 0; i <= 6; i++)
    		{
    			if (chessboard[i][j] == 1 && chessboard[i + 1][j] == 1 && chessboard[i + 2][j] == 1 && chessboard[i + 3][j] == 1 && chessboard[i + 4][j] == 1)
    				return 1;
    			if (chessboard[i][j] == -1 && chessboard[i + 1][j] == -1 && chessboard[i + 2][j] == -1 && chessboard[i + 3][j] == -1 && chessboard[i + 4][j] == -1)
    				return -1;
    		}
    	//右斜线
    	for (int i = 0; i <= 6; i++)
    		for (int j = 0; j <= 6; j++)
    		{
    			if (chessboard[i][j] == 1 && chessboard[i + 1][j + 1] == 1 && chessboard[i + 2][j + 2] == 1 && chessboard[i + 3][j + 3] == 1 && chessboard[i + 4][j + 4] == 1)
    				return 1;
    			if (chessboard[i][j] == -1 && chessboard[i + 1][j + 1] == -1 && chessboard[i + 2][j + 2] == -1 && chessboard[i + 3][j + 3] == -1 && chessboard[i + 4][j + 4] == -1)
    				return -1;
    		}
    	//左斜线
    	for (int i = 0; i <= 6; i++)
    		for (int j = 4; j <= 10; j++)
    		{
    			if (chessboard[i][j] == 1 && chessboard[i + 1][j - 1] == 1 && chessboard[i + 2][j - 2] == 1 && chessboard[i + 3][j - 3] == 1 && chessboard[i + 4][j - 4] == 1)
    				return 1;
    			if (chessboard[i][j] == -1 && chessboard[i + 1][j - 1] == -1 && chessboard[i + 2][j - 2] == -1 && chessboard[i + 3][j - 3] == -1 && chessboard[i + 4][j - 4] == -1)
    				return -1;
    		}
    
    	bool flag = true;
    	for (int i = 0; i <= 11; i++)
    		for (int j = 0; j <= 11; j++)
    			if (chessboard[i][j] == 0)
    				return 0;
    	//下满了
    	return -2;
    }
    //五子棋窗口处理
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM  wParam, LPARAM  lParam)
    {
    	HDC hDC;
    	PAINTSTRUCT ps;
    	HPEN hPen;
    	HBRUSH hBrush;
    	RECT clientDimension;	  	//存放客户区的尺寸
    	POINT begin;
    	TEXTMETRIC tm;
    	int x, y, k;				//鼠标位置,变量
    
    
    	switch (message)
    	{
    		//按下左键下棋
    	case WM_LBUTTONDOWN:
    	{
    		x = LOWORD(lParam);
    		y = HIWORD(lParam);
    
    		if (x > 50 && x < 670 && y > 50 && y < 670)
    			if (chessboard[(y - 30) / 60][(x - 30) / 60] == 0)
    			{
    				chessboard[(y - 30) / 60][(x - 30) / 60] = chequer;
    				chequer = -chequer;
    			}
    		break;
    	}
    		//抬起左键,重新绘制并判断输赢
    	case WM_LBUTTONUP:
    	{
    		k = judgeWinner();
    		if (k == 1)
    		{
    			MessageBox(hWnd, L"白色棋手获胜!", L"游戏结束", 0);
    			Sleep(500);
    			reset();
    		}
    		else
    			if (k == -1)
    			{
    				MessageBox(hWnd, L"黑色棋手获胜!", L"游戏结束", 0);
    				Sleep(500);
    				reset();
    			}
    			else
    				if (k == -2)
    				{
    					MessageBox(hWnd, L"平局!", L"游戏结束", 0);
    					Sleep(500);
    					reset();
    				}
    
    		InvalidateRect(hWnd, NULL, WM_PAINT);//刷新用户区
    		break;
    	}
    	case WM_PAINT:
    	{
    		hDC = BeginPaint(hWnd, &ps);//选中区域
    
    		GetClientRect(hWnd, &clientDimension);	//获取客户区的尺寸
    		if ((clientDimension.right - clientDimension.left) < 700 || (clientDimension.bottom - clientDimension.top) < 700) //判断屏幕尺寸
    		{
    			MessageBox(hWnd, L"窗口尺寸太小,无法绘制!", L"错误信息", 0);
    			EndPaint(hWnd, &ps); 			//结束绘图
    			break;
    		}
    		else
    		{
    			GetTextMetrics(hDC, &tm);	//获取默认字体,写入tm结构
    			//是谁该下棋
    			if (chequer == 1)
    				TextOut(hDC, 20, 20, L"白方", 2);
    			else
    				TextOut(hDC, 680, 20, L"黑方", 2);
    			//绘制棋盘
    			hPen = CreatePen(PS_SOLID, 1, RGB(127, 127, 127));//画笔
    			SelectObject(hDC, hPen);
    
    			begin.x = 60;
    			begin.y = 60;
    			for (int i = 0; i <= 10; i++)
    			{
    				MoveToEx(hDC, begin.x, begin.y, NULL);
    				TextOut(hDC, begin.x - 40, begin.y - 7, lps[i], 2);
    				LineTo(hDC, 600 + begin.x, begin.y);
    				begin.y += 60;
    			}
    			begin.x = 60;
    			begin.y = 60;
    			for (int i = 0; i <= 10; i++)
    			{
    				MoveToEx(hDC, begin.x, begin.y, NULL);
    				TextOut(hDC, begin.x - 7, begin.y - 40, lps[i], 2);
    				LineTo(hDC, begin.x, 600 + begin.y);
    				begin.x += 60;
    			}
    			//绘制黑点
    			hPen = CreatePen(PS_SOLID, 3, RGB(0, 0, 0));
    			SelectObject(hDC, hPen);
    			Ellipse(hDC, 360 - 2, 360 - 2, 360 + 2, 360 + 2);
    			Ellipse(hDC, 180 - 2, 180 - 2, 180 + 2, 180 + 2);
    			Ellipse(hDC, 540 - 2, 180 - 2, 540 + 2, 180 + 2);
    			Ellipse(hDC, 180 - 2, 540 - 2, 180 + 2, 540 + 2);
    			Ellipse(hDC, 540 - 2, 540 - 2, 540 + 2, 540 + 2);
    
    			hPen = CreatePen(PS_SOLID, 1, RGB(127, 127, 127));
    			SelectObject(hDC, hPen);
    			//绘制棋子
    			for (int i = 0; i <= 10; i++)
    			{
    				for (int j = 0; j <= 10; j++)
    				{
    					if (chessboard[i][j] == 1)//绘制白棋子
    					{
    						hBrush = CreateSolidBrush(RGB(255, 255, 255));
    						SelectObject(hDC, hBrush);
    						begin.x = 60 + j * 60;
    						begin.y = 60 + i * 60;
    						MoveToEx(hDC, begin.x, begin.y, NULL);
    						Ellipse(hDC, begin.x - 20, begin.y - 20, begin.x + 20, begin.y + 20);
    					}
    					else
    						if (chessboard[i][j] == -1)//绘制黑棋子
    						{
    							hBrush = CreateSolidBrush(RGB(0, 0, 0));
    							SelectObject(hDC, hBrush);
    							begin.x = 60 + j * 60;
    							begin.y = 60 + i * 60;
    							MoveToEx(hDC, begin.x, begin.y, NULL);
    							Ellipse(hDC, begin.x - 20, begin.y - 20, begin.x + 20, begin.y + 20);
    						}
    				}
    			}
    			hBrush = NULL;
    			DeleteObject(hBrush);
    			EndPaint(hWnd, &ps);
    		}
    		break;
    	}
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		//缺省时采用系统消息缺省处理函数
    	default: 
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    
    void defineWindow(WNDCLASSEX &awin, HINSTANCE& hInstance, TCHAR szWindowClass[])
    {
    	/*
    	*  窗口类的定义
    	*/
    	awin.cbSize = sizeof(WNDCLASSEX);									//窗口类的大小
    	awin.style = 0;														//窗口类型为默认类型
    	awin.lpfnWndProc = WndProc;											//窗口处理函数为WndProc
    	awin.cbClsExtra = 0;												//窗口类无扩展
    	awin.cbWndExtra = 0;												//窗口实例无扩展
    	awin.hInstance = hInstance;											//当前实例句柄
    	awin.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); //窗口的图标为默认图标
    	awin.hCursor = LoadCursor(NULL, IDC_ARROW);							//窗口采用箭头光标
    	awin.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);			//窗口背景为白色 
    	awin.lpszMenuName = NULL;											//窗口中无菜单
    	awin.lpszClassName = szWindowClass;									//窗口类名
    	awin.hIconSm = LoadIcon(awin.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    }
    
    void createWindow(HWND& hWnd, TCHAR szWindowClass[], TCHAR szTitle[], HINSTANCE hInstance) {
    	hWnd = CreateWindow(
    		szWindowClass,						//窗口类名
    		szTitle,							//窗口实例的标题名
    		WS_OVERLAPPEDWINDOW,				//窗口的风格
    		50,
    		50,
    		750,
    		750,
    		NULL,								//此窗口无父窗口
    		NULL,								//此窗口无主菜单
    		hInstance,							//创建此窗口的应用程序的当前句柄
    		NULL								//不使用该值
    	);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow) {
    
    	WNDCLASSEX  awin;							//窗口数据结构
    	HWND hWnd;
    	MSG msg;
    	TCHAR szWindowClass[] = L"窗口类";   //窗口类名
    	TCHAR szTitle[] = L"五子棋游戏";			//窗口标题名
    
    	//-------进行窗口类的定义 -------
    	defineWindow(awin, hInstance, szWindowClass);
    
    	//-------进行窗口类的注册 -------
    	if (!RegisterClassEx(&awin)) {
    		MessageBox(NULL, L"窗口注册类", L"窗口注册", NULL);
    		//发出警告
    		MessageBeep(0);
    		return 1;
    	}
    
    	//---------------- 创建窗口 -------------------
    	createWindow(hWnd, szWindowClass, szTitle, hInstance);
    
    	if (!hWnd) {
    		//如果创建失败
    		MessageBox(NULL, L"创建窗口类", L"创建窗口", NULL);
    		//发出警告
    		MessageBeep(0);
    		return 1;
    	}
    	//--------------- 显示窗口 ----------------------
    	ShowWindow(hWnd, nCmdShow);
    	//-------------- 绘制用户区 ---------------------
    	UpdateWindow(hWnd);
    	//----------------- 消 息 循 环 ---------------------- 
    	while (GetMessage(&msg, NULL, 0, 0)) {
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return msg.wParam; //消息循环结束即程序终止时将信息返回系统
    }
    
    

     

    展开全文
  • c++五子棋课设

    2018-01-26 19:04:36
    c++五子棋课程设计,希望对学习c++的同学有帮助,很实用
  • C++编写的。对于c++初学者有很好的帮助,用C++编写的。对于c++初学者有很好的帮助,
  • c++五子棋程序

    2017-12-11 09:42:52
    中国科学院大学c++五子棋程序课程设计,杨老师的课程,包含人人对战和人机对战
  • C++五子棋资源.rar

    2021-07-18 21:31:57
    与文章《C++五子棋》配套使用,开发过程中的素材
  • C++五子棋.exe

    2020-04-16 17:32:53
    主要是用于C++五子棋游戏的制作,这是一个执行文件,能够直接打开来玩,源代码发在另外一个帖子勒,如果有感兴趣的朋友可以看一看
  • C++五子棋代码

    2013-11-21 20:00:27
    C++五子棋代码
  • c++五子棋游戏

    2016-01-04 10:29:58
    c++五子棋游戏 可以实现双人和单人对电脑
  • C++五子棋程序

    2012-05-16 20:13:15
    C++五子棋程序
  • C++五子棋编写

    2015-11-04 22:48:00
    C++五子棋编写(控制台版_CMD命令框版)【简单五子棋游戏_适合初学者学习_代码有详细注释
  • C++五子棋源码

    2012-11-20 12:21:37
    C++五子棋源码,可用,界面类似QQ游戏里面那个
  • C++五子棋小游戏代码

    2019-08-18 13:25:14
    C++五子棋小游戏代码,喜欢的可以来看一看,学习一下。
  • C++五子棋源代码

    2015-09-11 16:51:20
    C++五子棋源代码,可以运行的源代码,在这里跟大家分享一下
  • C++ 五子棋代码

    2011-12-16 20:44:57
    C++ 五子棋代码 可运行,简单易懂,适合初学者
  • C++五子棋小程序 通过评分算法找到最佳下棋位置 支持选择先手方
  • c++五子棋代码截图

    2014-06-26 08:17:29
    c++五子棋代码,五子棋代码设计程序截图,控制台应用程序
  • Visual C++ 五子棋游戏

    2008-12-11 16:23:47
    Visual C++ 五子棋游戏 Visual C++ 五子棋游戏 Visual C++ 五子棋游戏
  • c++五子棋小游戏课程设计,包含文档,程序能运行,原创
  • C++五子棋课程设计实验报告 很完整的 与另一个资源是配套的 “C++五子棋程序”
  • 内容索引:VC/C++源码,游戏编程,五子棋,MVC 基于MVC的C++五子棋游戏源码,游戏界面还挺专业,在VC6.0下顺利编译,运行截图如上示,很不错的游戏编程资料。
  • c++ 五子棋 课程设计

    2010-12-11 18:32:32
    C++ 五子棋 课程设计 详解 内有代码注释
  • C++五子棋人机对战代码,里面有c++五子棋人机对战的详细代码。

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 4,401
精华内容 1,760
关键字:

c++五子棋

c++ 订阅