我需要满足以下要求,请用c++完成程序功能简介 该程序是tictactoe游戏,棋盘为3*3,当三个棋子连起来即为赢。 输入参数时,若输入的数字超出边界或在已有棋子的地方再输入,则提示出错,要求重新输入。 3、细化需求 1)两个玩家可以下棋,第一次下棋之前首先将棋盘清空。以后每次下棋时都要判断棋盘是否已满。 2)下棋时需要判断当前棋盘的棋子是否已满 3)下棋时指定规则,由谁先下棋,下棋时判断下棋的坐标是否正确,如果不正确提示玩家继续下棋;当下棋时的坐标正确时,保存棋子的坐标和当前已下棋子的总数。 4)每一次下棋都要判断当前玩家的棋子是否已经连线(横、竖、对角线),当连成线时,则当前玩家赢 5)如果当前玩家棋子没有连成线,则提示另一方玩家下棋,下棋过程判断同第3,4步。 6)结束一局后,判断是否进行下一局。 7)能够自定义棋盘大小,并且能统计输赢结果并打印输出
时间: 2023-09-18 15:10:17 浏览: 37
以下是用C++完成tictactoe游戏的程序,支持自定义棋盘大小及统计输赢结果并打印输出:
```c++
#include <iostream>
#include <vector>
using namespace std;
// 棋子类型
enum class Piece {
Empty,
X,
O
};
// 游戏状态
enum class GameState {
InProgress,
Draw,
XWin,
OWin
};
// 棋盘类
class Board {
public:
Board(int size) : size(size) {
board.resize(size, vector<Piece>(size, Piece::Empty));
}
// 打印棋盘
void print() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
switch (board[i][j]) {
case Piece::Empty:
cout << "-";
break;
case Piece::X:
cout << "X";
break;
case Piece::O:
cout << "O";
break;
}
cout << " ";
}
cout << endl;
}
}
// 下棋
bool placePiece(int row, int col, Piece piece) {
if (row < 0 || row >= size || col < 0 || col >= size || board[row][col] != Piece::Empty) {
return false;
}
board[row][col] = piece;
return true;
}
// 判断游戏状态
GameState getGameState() {
// 检查行
for (int i = 0; i < size; i++) {
Piece firstPiece = board[i][0];
if (firstPiece == Piece::Empty) {
continue;
}
bool line = true;
for (int j = 1; j < size; j++) {
if (board[i][j] != firstPiece) {
line = false;
break;
}
}
if (line) {
return firstPiece == Piece::X ? GameState::XWin : GameState::OWin;
}
}
// 检查列
for (int j = 0; j < size; j++) {
Piece firstPiece = board[0][j];
if (firstPiece == Piece::Empty) {
continue;
}
bool line = true;
for (int i = 1; i < size; i++) {
if (board[i][j] != firstPiece) {
line = false;
break;
}
}
if (line) {
return firstPiece == Piece::X ? GameState::XWin : GameState::OWin;
}
}
// 检查对角线
Piece firstPiece = board[0][0];
if (firstPiece != Piece::Empty) {
bool line = true;
for (int i = 1; i < size; i++) {
if (board[i][i] != firstPiece) {
line = false;
break;
}
}
if (line) {
return firstPiece == Piece::X ? GameState::XWin : GameState::OWin;
}
}
firstPiece = board[0][size - 1];
if (firstPiece != Piece::Empty) {
bool line = true;
for (int i = 1; i < size; i++) {
if (board[i][size - 1 - i] != firstPiece) {
line = false;
break;
}
}
if (line) {
return firstPiece == Piece::X ? GameState::XWin : GameState::OWin;
}
}
// 检查平局
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (board[i][j] == Piece::Empty) {
return GameState::InProgress;
}
}
}
return GameState::Draw;
}
private:
int size;
vector<vector<Piece>> board;
};
int main() {
int size;
cout << "请输入棋盘大小:";
cin >> size;
Board board(size);
board.print();
Piece currentPlayer = Piece::X;
int piecesPlaced = 0;
while (true) {
int row, col;
cout << "请 " << (currentPlayer == Piece::X ? "X" : "O") << " 下棋(行 列):";
cin >> row >> col;
if (board.placePiece(row, col, currentPlayer)) {
piecesPlaced++;
board.print();
GameState gameState = board.getGameState();
if (gameState == GameState::XWin) {
cout << "X 赢了!" << endl;
break;
} else if (gameState == GameState::OWin) {
cout << "O 赢了!" << endl;
break;
} else if (gameState == GameState::Draw) {
cout << "平局!" << endl;
break;
}
currentPlayer = currentPlayer == Piece::X ? Piece::O : Piece::X;
} else {
cout << "输入错误,请重新输入" << endl;
}
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)