利用C++builderVCL实现19*19的五子棋并且能够选择是否记录,保存下棋历史,对于已保存的下棋历史程序可以逐步呈现下棋历史的功能的源代码
时间: 2024-02-12 20:07:08 浏览: 88
以下是一个基本的C++ Builder VCL实现19*19五子棋并且能够选择是否记录,保存下棋历史,对于已保存的下棋历史程序可以逐步呈现下棋历史的功能的源代码,仅供参考:
```cpp
#include <vcl.h>
#include <vector>
#include <fstream>
#pragma hdrstop
#include "Unit1.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
using namespace std;
TForm1 *Form1;
int board[19][19] = {0}; // 棋盘,0表示空,1表示黑子,2表示白子
int currentColor = 1; // 当前下棋方,1表示黑方,2表示白方
bool recordHistory = false; // 是否记录下棋历史
vector<pair<int, int>> history; // 下棋历史
void DrawBoard(); // 绘制棋盘
void DrawChess(int row, int col); // 绘制棋子
void PutChess(int row, int col); // 下棋
bool CheckWin(int row, int col); // 判断是否获胜
void Restart(); // 重新开始游戏
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
DrawBoard();
}
void TForm1::DrawBoard()
{
const int size = 480;
int cellSize = size / 19;
Image1->Canvas->Brush->Color = clWhite;
Image1->Canvas->FillRect(Rect(0, 0, size, size));
Image1->Canvas->Pen->Color = clBlack;
for (int i = 0; i < 19; i++) {
Image1->Canvas->MoveTo(cellSize / 2, cellSize / 2 + i * cellSize);
Image1->Canvas->LineTo(size - cellSize / 2, cellSize / 2 + i * cellSize);
Image1->Canvas->MoveTo(cellSize / 2 + i * cellSize, cellSize / 2);
Image1->Canvas->LineTo(cellSize / 2 + i * cellSize, size - cellSize / 2);
}
}
void TForm1::DrawChess(int row, int col)
{
const int size = 480;
int cellSize = size / 19;
int x = cellSize / 2 + col * cellSize;
int y = cellSize / 2 + row * cellSize;
if (board[row][col] == 1) {
Image1->Canvas->Brush->Color = clBlack;
Image1->Canvas->Pen->Color = clBlack;
} else {
Image1->Canvas->Brush->Color = clWhite;
Image1->Canvas->Pen->Color = clWhite;
}
Image1->Canvas->Ellipse(x - cellSize / 2, y - cellSize / 2, x + cellSize / 2, y + cellSize / 2);
}
void TForm1::PutChess(int row, int col)
{
if (board[row][col] != 0) {
return;
}
board[row][col] = currentColor;
DrawChess(row, col);
if (recordHistory) {
history.push_back(make_pair(row, col));
}
if (CheckWin(row, col)) {
if (currentColor == 1) {
ShowMessage("黑方获胜!");
} else {
ShowMessage("白方获胜!");
}
Restart();
} else {
currentColor = (currentColor == 1) ? 2 : 1;
}
}
bool TForm1::CheckWin(int row, int col)
{
int count = 0;
// 横向
for (int i = col - 4; i <= col + 4; i++) {
if (i < 0 || i >= 19) {
continue;
}
if (board[row][i] == currentColor) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
// 纵向
count = 0;
for (int i = row - 4; i <= row + 4; i++) {
if (i < 0 || i >= 19) {
continue;
}
if (board[i][col] == currentColor) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
// 斜向
count = 0;
for (int i = -4; i <= 4; i++) {
int r = row + i;
int c = col + i;
if (r < 0 || r >= 19 || c < 0 || c >= 19) {
continue;
}
if (board[r][c] == currentColor) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
// 反斜向
count = 0;
for (int i = -4; i <= 4; i++) {
int r = row + i;
int c = col - i;
if (r < 0 || r >= 19 || c < 0 || c >= 19) {
continue;
}
if (board[r][c] == currentColor) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
return false;
}
void TForm1::Restart()
{
memset(board, 0, sizeof(board));
DrawBoard();
currentColor = 1;
recordHistory = false;
history.clear();
}
void __fastcall TForm1::Image1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
const int size = 480;
int cellSize = size / 19;
int col = (X - cellSize / 2) / cellSize;
int row = (Y - cellSize / 2) / cellSize;
if (col < 0 || col >= 19 || row < 0 || row >= 19) {
return;
}
PutChess(row, col);
}
void __fastcall TForm1::NewGameClick(TObject *Sender)
{
Restart();
}
void __fastcall TForm1::RecordHistoryClick(TObject *Sender)
{
recordHistory = !recordHistory;
if (recordHistory) {
history.clear();
}
}
void __fastcall TForm1::SaveClick(TObject *Sender)
{
if (history.size() == 0) {
ShowMessage("没有下棋历史!");
return;
}
ofstream fout("history.txt");
if (!fout) {
ShowMessage("文件打开失败!");
return;
}
for (int i = 0; i < history.size(); i++) {
fout << history[i].first << " " << history[i].second << endl;
}
ShowMessage("下棋历史已保存!");
}
void __fastcall TForm1::LoadClick(TObject *Sender)
{
ifstream fin("history.txt");
if (!fin) {
ShowMessage("文件打开失败!");
return;
}
Restart();
while (!fin.eof()) {
int row, col;
fin >> row >> col;
if (board[row][col] == 0) {
PutChess(row, col);
}
}
ShowMessage("下棋历史已加载!");
}
void __fastcall TForm1::PlayClick(TObject *Sender)
{
if (history.size() == 0) {
ShowMessage("没有下棋历史!");
return;
}
Restart();
for (int i = 0; i < history.size(); i++) {
Sleep(500); // 控制下棋速度
PutChess(history[i].first, history[i].second);
}
ShowMessage("下棋历史已逐步呈现!");
}
```
这个程序实现了基本的19*19五子棋游戏功能,包括下棋、判断胜负、重新开始游戏等功能,还可以选择是否记录下棋历史,保存和加载下棋历史,并且可以逐步呈现下棋历史。具体实现细节还需要根据具体情况进行调整。
阅读全文