C++五子棋小游戏

 简简单单编写了一个五子棋的小游戏,运行还比较正常!

代码如下,想要的直接复制哦!

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int ROW = 15;
const int COL = 15;

void drawBoard(char board[ROW][COL]);
void playerMove(char board[ROW][COL], char player, int &row, int &col);
bool checkWin(char board[ROW][COL], char player, int row, int col);

int main()
{
    char board[ROW][COL] = { {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                             {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                             {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                             {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                             {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                             {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                             {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                             {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                             {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                             {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                             {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                             {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                             {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                             {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                             {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '} };
    char player1 = 'X';
    char player2 = 'O';
    char winner = ' ';
    int row, col, moveCount = 0;
    bool player1Turn = true;

    // 随机决定先手玩家
    srand(time(0));
    player1Turn = (rand() % 2 == 0);

    // 游戏主循环
    do
    {
        system("cls"); // 清屏
        drawBoard(board);
        if (player1Turn)
        {
            cout << "玩家1(X)请下棋:";
            playerMove(board, player1, row, col);
        }
        else
        {
            cout << "玩家2(O)请下棋:";
            playerMove(board, player2, row, col);
        }

        // 放置棋子
        board[row][col] = player1Turn ? player1 : player2;
        moveCount++;

        // 检查是否有胜者
        if (checkWin(board, player1Turn ? player1 : player2, row, col))
        {
            winner = player1Turn ? player1 : player2;
            break;
        }

        // 切换下一位玩家
        player1Turn = !player1Turn;
    } while (moveCount < ROW*COL);

    // 游戏结束,显示胜者或平局
    system("cls");
    drawBoard(board);
    if (winner == player1)
    {
        cout << "恭喜玩家1获胜!" << endl;
    }
    else if (winner == player2)
    {
        cout << "恭喜玩家2获胜!" << endl;
    }
    else
    {
        cout << "平局!" << endl;
    }

    return 0;
}

void drawBoard(char
board[ROW][COL])
{
cout << " ";
for (int i = 0; i < COL; i++)
{
cout << i+1 << " ";
}
cout << endl;
for (int i = 0; i < ROW; i++)
{
cout << i+1 << " ";
for (int j = 0; j < COL; j++)
{
cout << "|" << board[i][j];
}
cout << "|" << endl;
}
}

void playerMove(char board[ROW][COL], char player, int &row, int &col)
{
char buffer[256];
do
{
cout << "请输入行号和列号(用空格隔开):";
cin.getline(buffer, 256);
row = buffer[0] - '1';
col = buffer[2] - '1';
} while (row < 0 || row >= ROW || col < 0 || col >= COL || board[row][col] != ' ');
}

bool checkWin(char board[ROW][COL], char player, int row, int col)
{
int count;
// 横向检查
count = 1;
for (int j = col-1;j>=0;j--)
{
    if (board[row][j] == player)
    {
        count++;
    }
    else
    {
        break;
    }
}
for (int j = col+1;j<COL;j++)
{
    if (board[row][j] == player)
    {
        count++;
    }
    else
    {
        break;
    }
}
if (count >= 5) return true;

// 纵向检查
count = 1;
for (int i = row-1;i>=0;i--)
{
    if (board[i][col] == player)
    {
        count++;
    }
    else
    {
        break;
    }
}
for (int i = row+1;i<ROW;i++)
{
    if (board[i][col] == player)
    {
        count++;
    }
    else
    {
        break;
    }
}
if (count >= 5) return true;

// 斜向检查(\)
count = 1;
for (int i = row-1, j = col-1;i>=0 && j>=0;i--, j--)
{
    if (board[i][j] == player)
    {
        count++;
    }
    else
    {
        break;
    }
}
for (int i = row+1, j = col+1;i<ROW && j<COL;i++, j++)
{
    if (board[i][j] == player)
    {
        count++;
    }
    else
    {
        break;
    }
}
if (count >= 5) return true;

// 斜向检查(/)
count = 1;
for (int i = row-1, j = col+1;i>=0 && j<COL;i--, j++)
{
    if (board[i][j] == player)
    {
        count++;
    }
    else
    {
        break;
    }
}
for (int i = row+1, j = col-1;i<ROW && j>=0;i++, j--)
{
    if (board[i][j] == player)
    {
        count++;
    }
    else
    {
        break;
    }
}
if (count >= 5) return true;

// 没有胜者
return false;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值