c++语言编程的具有对局回放功能的五子棋小游戏
时间: 2024-03-23 17:43:41 浏览: 15
好的,我可以为您提供一个简单的五子棋小游戏的C++代码,其中包括对局回放功能。代码如下:
```c++
#include <iostream>
#include <windows.h>
using namespace std;
const int ROW = 15; // 棋盘行数
const int COL = 15; // 棋盘列数
const int WIN = 5; // 获胜所需棋子数
const int MAX_STEP = ROW * COL; // 最大步数
const char BLACK = 'X'; // 黑棋
const char WHITE = 'O'; // 白棋
// 棋盘
char board[ROW][COL];
// 当前下棋方
char current_player;
// 当前步数
int current_step;
// 悔棋步数
int regret_step;
// 悔棋记录
int regret_record[MAX_STEP][3];
// 初始化棋盘
void InitBoard()
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
board[i][j] = '+';
}
}
}
// 输出棋盘
void PrintBoard()
{
system("cls"); // 清屏
cout << " ";
for (int j = 0; j < COL; j++)
{
cout << j + 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;
}
}
// 判断当前位置是否越界
bool IsOutOfRange(int x, int y)
{
return x < 0 || x >= ROW || y < 0 || y >= COL;
}
// 判断当前位置是否已经下过棋子
bool IsPlaceOccupied(int x, int y)
{
return board[x][y] != '+';
}
// 判断当前位置是否可以下棋子
bool IsPlaceAvailable(int x, int y)
{
return !IsOutOfRange(x, y) && !IsPlaceOccupied(x, y);
}
// 判断当前位置是否是获胜位置
bool IsWinPosition(int x, int y)
{
char player = current_player;
int count = 1;
// 判断横向是否有连续的五个棋子
for (int i = y - 1; i >= 0 && board[x][i] == player; i--)
{
count++;
}
for (int i = y + 1; i < COL && board[x][i] == player; i++)
{
count++;
}
if (count >= WIN)
{
return true;
}
// 判断纵向是否有连续的五个棋子
count = 1;
for (int i = x - 1; i >= 0 && board[i][y] == player; i--)
{
count++;
}
for (int i = x + 1; i < ROW && board[i][y] == player; i++)
{
count++;
}
if (count >= WIN)
{
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 < ROW && j < COL && board[i][j] == player; i++, j++)
{
count++;
}
if (count >= WIN)
{
return true;
}
// 判断右上左下是否有连续的五个棋子
count = 1;
for (int i = x - 1, j = y + 1; i >= 0 && j < COL && board[i][j] == player; i--, j++)
{
count++;
}
for (int i = x + 1, j = y - 1; i < ROW && j >= 0 && board[i][j] == player; i++, j--)
{
count++;
}
if (count >= WIN)
{
return true;
}
return false;
}
// 判断当前局面是否已经分出胜负
bool IsGameOver()
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (IsPlaceAvailable(i, j) || IsWinPosition(i, j))
{
return false;
}
}
}
return true;
}
// 下棋子
void PlacePiece(int x, int y)
{
board[x][y] = current_player;
current_step++;
regret_step = 0;
regret_record[current_step][0] = x;
regret_record[current_step][1] = y;
regret_record[current_step][2] = current_player == BLACK ? 1 : 2;
}
// 悔棋
void Regret()
{
if (current_step == 0)
{
cout << "已经回到了最初的局面,不能再悔棋了!" << endl;
return;
}
int x = regret_record[current_step][0];
int y = regret_record[current_step][1];
board[x][y] = '+';
current_step--;
regret_step++;
cout << "悔棋成功!" << endl;
}
// 重玩
void Replay()
{
InitBoard();
current_player = BLACK;
current_step = 0;
regret_step = 0;
}
// 对局回放
void Playback()
{
int step = 1;
while (step <= current_step)
{
int x = regret_record[step][0];
int y = regret_record[step][1];
current_player = regret_record[step][2] == 1 ? BLACK : WHITE;
PlacePiece(x, y);
PrintBoard();
Sleep(1000); // 暂停1秒钟
step++;
}
}
int main()
{
InitBoard();
current_player = BLACK;
current_step = 0;
regret_step = 0;
int x, y;
while (true)
{
PrintBoard();
cout << "当前下棋方:" << current_player << endl;
cout << "请输入落子位置(x y):";
cin >> x >> y;
x--;
y--;
if (IsPlaceAvailable(x, y))
{
PlacePiece(x, y);
if (IsWinPosition(x, y))
{
PrintBoard();
cout << "恭喜" << current_player << "获得胜利!" << endl;
break;
}
if (IsGameOver())
{
PrintBoard();
cout << "本局游戏结束,平局!" << endl;
break;
}
current_player = current_player == BLACK ? WHITE : BLACK;
}
else
{
cout << "落子位置非法,请重新输入!" << endl;
}
if (regret_step > 0)
{
cout << "是否悔棋(y/n)?";
char choice;
cin >> choice;
if (choice == 'y' || choice == 'Y')
{
Regret();
current_player = current_player == BLACK ? WHITE : BLACK;
}
}
}
cout << "是否回放对局(y/n)?";
char choice;
cin >> choice;
if (choice == 'y' || choice == 'Y')
{
Replay();
}
return 0;
}
```
希望能对您有所帮助!