就是正常的井字棋,帮我用C++写一个
时间: 2024-04-13 17:31:18 浏览: 32
当然可以!下面是一个简单的C++代码示例,实现了井字棋游戏的基本功能:
```cpp
#include <iostream>
#include <vector>
using namespace std;
// 定义棋盘大小
const int SIZE = 3;
// 定义棋盘格子状态
enum class CellState {
Empty,
X,
O
};
// 定义棋盘类
class TicTacToe {
public:
TicTacToe() {
// 初始化棋盘
board.resize(SIZE, vector<CellState>(SIZE, CellState::Empty));
}
// 显示棋盘
void displayBoard() {
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
switch (board[i][j]) {
case CellState::Empty:
cout << " ";
break;
case CellState::X:
cout << "X";
break;
case CellState::O:
cout << "O";
break;
}
if (j != SIZE - 1) {
cout << " | ";
}
}
cout << endl;
if (i != SIZE - 1) {
cout << "---------" << endl;
}
}
cout << endl;
}
// 下棋
bool makeMove(int row, int col, CellState player) {
if (row < 0 || row >= SIZE || col < 0 || col >= SIZE || board[row][col] != CellState::Empty) {
return false;
}
board[row][col] = player;
return true;
}
// 判断游戏是否结束
bool isGameOver(CellState player) {
// 判断行
for (int i = 0; i < SIZE; ++i) {
bool win = true;
for (int j = 0; j < SIZE; ++j) {
if (board[i][j] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
}
// 判断列
for (int i = 0; i < SIZE; ++i) {
bool win = true;
for (int j = 0; j < SIZE; ++j) {
if (board[j][i] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
}
// 判断对角线
bool win = true;
for (int i = 0; i < SIZE; ++i) {
if (board[i][i] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
win = true;
for (int i = 0; i < SIZE; ++i) {
if (board[i][SIZE - i - 1] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
return false;
}
private:
vector<vector<CellState>> board;
};
int main() {
TicTacToe game;
int row, col;
CellState currentPlayer = CellState::X;
while (true) {
game.displayBoard();
cout << "Player " << (currentPlayer == CellState::X ? "X" : "O") << ", please enter your move (row col): ";
cin >> row >> col;
if (game.makeMove(row, col, currentPlayer)) {
if (game.isGameOver(currentPlayer)) {
cout << "Player " << (currentPlayer == CellState::X ? "X" : "O") << " wins!" << endl;
break;
}
currentPlayer = (currentPlayer == CellState::X) ? CellState::O : CellState::X;
} else {
cout << "Invalid move. Please try again." << endl;
}
}
return 0;
}
```
这段代码实现了一个基本的井字棋游戏,支持两位玩家轮流下棋,并判断游戏是否结束。希望对您有所帮助!如果您有任何问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)