基本信息
源码名称:五子棋(c builder2010开发)
源码大小:5.66KB
文件格式:.cpp
开发语言:C/C++
更新时间:2025-02-21
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 1 元 
   源码介绍
c builder2010开发的五子棋游戏

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

// 定义棋盘大小
const int BOARD_SIZE = 15;
// 定义棋子大小
const int PIECE_SIZE = 50;
// 棋盘二维数组,0表示空,1表示黑子,2表示白子
int board[BOARD_SIZE][BOARD_SIZE] = {0};
// 当前轮到的玩家,1表示黑子,2表示白子
int currentPlayer = 2;

// 判断是否有玩家获胜的函数
bool checkWin(int x, int y, int player) {
    int count;

    // 检查横向
    count = 1;
    // 向左检查
    for (int i = x - 1; i >= 0 && board[i][y] == player; i--) {
        count ;
    }
    // 向右检查
    for (int i = x 1; i < BOARD_SIZE && board[i][y] == player; i ) {
        count ;
    }
    if (count >= 5) return true;

    // 检查纵向
    count = 1;
    // 向上检查
    for (int i = y - 1; i >= 0 && board[x][i] == player; i--) {
        count ;
    }
    // 向下检查
    for (int i = y 1; i < BOARD_SIZE && board[x][i] == player; i ) {
        count ;
    }
    if (count >= 5) return true;

    // 检查正斜向(左上到右下)
    count = 1;
    // 向左上检查
    for (int i = x - 1, j = y - 1; i >= 0 && j >= 0 && board[i][j] == player; i--, j--) {
        count ;
    }
    // 向右下检查
    for (int i = x 1, j = y 1; i < BOARD_SIZE && j < BOARD_SIZE && board[i][j] == player; i , j ) {
        count ;
    }
    if (count >= 5) return true;

    // 检查反斜向(右上到左下)
    count = 1;
    // 向右上检查
    for (int i = x 1, j = y - 1; i < BOARD_SIZE && j >= 0 && board[i][j] == player; i , j--) {
        count ;
    }
    // 向左下检查
    for (int i = x - 1, j = y 1; i >= 0 && j < BOARD_SIZE && board[i][j] == player; i--, j ) {
        count ;
    }
    if (count >= 5) return true;

    return false;
}

// 绘制棋盘的函数
void drawBoard(TImage *image) {
    image->Canvas->Pen->Color = clBlack;
    for (int i = 0; i < BOARD_SIZE; i ) {
        // 绘制横线
        image->Canvas->MoveTo(PIECE_SIZE, PIECE_SIZE i * PIECE_SIZE);
        image->Canvas->LineTo(PIECE_SIZE (BOARD_SIZE - 1) * PIECE_SIZE, PIECE_SIZE i * PIECE_SIZE);
        // 绘制竖线
        image->Canvas->MoveTo(PIECE_SIZE i * PIECE_SIZE, PIECE_SIZE);
        image->Canvas->LineTo(PIECE_SIZE i * PIECE_SIZE, PIECE_SIZE (BOARD_SIZE - 1) * PIECE_SIZE);
    }
}

// 绘制棋子的函数
void drawPiece(TImage *image, int x, int y, int player) {
    int px = PIECE_SIZE x * PIECE_SIZE;
    int py = PIECE_SIZE y * PIECE_SIZE;
    if (player == 1) {
        image->Canvas->Brush->Color = clBlack;
    } else {
        image->Canvas->Brush->Color = clWhite;
    }
    image->Canvas->Ellipse(px - PIECE_SIZE / 2, py - PIECE_SIZE / 2, px PIECE_SIZE / 2, py PIECE_SIZE / 2);
}


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
// 初始化函数
   // 清空棋盘数组
    for (int i = 0; i < BOARD_SIZE; i ) {
for (int j = 0; j < BOARD_SIZE; j ) {
            board[i][j] = 0;
        }
    }
// 初始化当前玩家为黑子
currentPlayer = 1;
// 绘制棋盘
drawBoard(ImageBoard);
// 更新当前轮到的玩家信息
LabelTurn->Caption = "当前轮到黑子";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ImageBoardMouseDown(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y)
{
 if (Button == mbLeft) {
        // 计算落子坐标
        int x = (X - PIECE_SIZE PIECE_SIZE / 2) / PIECE_SIZE;
        int y = (Y - PIECE_SIZE PIECE_SIZE / 2) / PIECE_SIZE;

        // 检查坐标是否合法且该位置为空
        if (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && board[x][y] == 0) {
            // 在棋盘数组中记录落子信息
            board[x][y] = currentPlayer;
            // 绘制棋子
            drawPiece(ImageBoard, x, y, currentPlayer);

            // 检查是否有玩家获胜
            if (checkWin(x, y, currentPlayer)) {
                if (currentPlayer == 1) {
                    ShowMessage("黑子获胜!");
                } else {
                    ShowMessage("白子获胜!");
                }
                // 游戏结束,重新初始化
                FormCreate(Sender);
            } else {
                // 切换玩家
                currentPlayer = 3 - currentPlayer;
                if (currentPlayer == 1) {
                    LabelTurn->Caption = "当前轮到黑子";
                } else {
                    LabelTurn->Caption = "当前轮到白子";
                }
            }
        }
    }
}

// 绘制事件处理函数
void __fastcall TForm1::ImageBoardPaint(TObject *Sender)
{
    drawBoard(ImageBoard);
    for (int i = 0; i < BOARD_SIZE; i ) {
for (int j = 0; j < BOARD_SIZE; j ) {
            if (board[i][j]!= 0) {
                drawPiece(ImageBoard, i, j, board[i][j]);
            }
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormResize(TObject *Sender)
{
     // 清空棋盘数组
    for (int i = 0; i < BOARD_SIZE; i ) {
for (int j = 0; j < BOARD_SIZE; j ) {
            board[i][j] = 0;
        }
    }
// 初始化当前玩家为黑子
currentPlayer = 1;
// 绘制棋盘
drawBoard(ImageBoard);
// 更新当前轮到的玩家信息
LabelTurn->Caption = "当前轮到黑子";
}
//---------------------------------------------------------------------------