在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例C/C++游戏开发 → 五子棋(c builder2010开发)

五子棋(c builder2010开发)

C/C++游戏开发

下载此实例
  • 开发语言:C/C++
  • 实例大小:5.66KB
  • 下载次数:2
  • 浏览次数:70
  • 发布时间:2025-02-21
  • 实例类别:C/C++游戏开发
  • 发 布 人:zzn119
  • 文件格式:.cpp
  • 所需积分:1
 相关标签: 五子棋

实例介绍

【实例简介】c builder2010开发的五子棋游戏
【实例截图】from clipboard
【核心代码】//---------------------------------------------------------------------------

#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 = "当前轮到黑子";
}
//---------------------------------------------------------------------------


标签: 五子棋

实例下载地址

五子棋(c builder2010开发)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警