C++五子棋代码

本文将详细讲解如何使用C++编程实现五子棋游戏,涉及关键算法和数据结构的应用,包括棋盘初始化、落子判断、胜负检查等核心功能的实现。
摘要由CSDN通过智能技术生成
#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
const int N = 15;//棋盘大小
const char ChessBoardFlag = ' ';
const char flag1 = 'O';
const char flag2 = 'X';
 
typedef struct Coordinate
{
    int x;
    int y;
}ChessCoordi;
 
class FiveChess
{
public:
    FiveChess()
    {
        InitChessBoard();
    }
 
    void Play()
    {
        ChessCoordi Pos1;
        ChessCoordi Pos2;
        while (1){
            int mode = ChoseMode();
            while (1){
                if (mode == 1){//玩家VS电脑
                    static size_t count = 1;
                    PalyerGo(Pos1, 1, flag1);
                    if (count++ >= 9 && GetWiner(Pos1, 1, flag1))
                        break;
                    ComputerGo(Pos2, flag2);
                    if (count++ >= 10 && GetWiner(Pos2, 0, flag2))
                       break;
                }
                else if (mode == 2){//玩家VS玩家
                    static size_t count = 1;
                    PalyerGo(Pos1, 1, flag1);
                    if (count++ >= 9 && GetWiner(Pos1,1, flag1))
                        break;
                    PalyerGo(Pos2,2 ,flag2);
                    if (count++ >= 10 && GetWiner(Pos2,2, flag2))
                        break;
                }
            }
            cout << "再来一局 y or no" << endl;
            char chose = 'y';
            cin >> chose;
            if (chose == 'n')
                break;
        }
    }
 
    void PrintChessBoard()
    {
        system("cls");
        for (size_t i = 0; i < N + 1; ++i)
        {
            for (size_t j = 0; j < N + 1; ++j)
      
  • 13
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值