写一个人机对决的井字棋C++程序
时间: 2023-08-31 12:11:16 浏览: 84
好的,以下是一个简单的人机对战的井字棋C++程序:
```c++
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
// 定义棋盘大小
const int BOARD_SIZE = 3;
// 定义棋子类型
const char EMPTY = '-';
const char HUMAN = 'X';
const char COMPUTER = 'O';
// 定义游戏状态
enum GameState {
PLAYING, DRAW, HUMAN_WIN, COMPUTER_WIN
};
// 初始化棋盘
void initBoard(vector<vector<char>>& board) {
board.resize(BOARD_SIZE);
for (int i = 0; i < BOARD_SIZE; ++i) {
board[i].resize(BOARD_SIZE, EMPTY);
}
}
// 打印棋盘
void printBoard(const vector<vector<char>>& board) {
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
cout << board[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
// 获取空闲位置
vector<pair<int, int>> getEmptyCells(const vector<vector<char>>& board) {
vector<pair<int, int>> emptyCells;
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
if (board[i][j] == EMPTY) {
emptyCells.push_back(make_pair(i, j));
}
}
}
return emptyCells;
}
// 判断是否胜利
bool isWin(const vector<vector<char>>& board, char player) {
// 判断行
for (int i = 0; i < BOARD_SIZE; ++i) {
bool win = true;
for (int j = 0; j < BOARD_SIZE; ++j) {
if (board[i][j] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
}
// 判断列
for (int j = 0; j < BOARD_SIZE; ++j) {
bool win = true;
for (int i = 0; i < BOARD_SIZE; ++i) {
if (board[i][j] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
}
// 判断对角线
bool win = true;
for (int i = 0; i < BOARD_SIZE; ++i) {
if (board[i][i] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
win = true;
for (int i = 0; i < BOARD_SIZE; ++i) {
if (board[i][BOARD_SIZE - i - 1] != player) {
win = false;
break;
}
}
if (win) {
return true;
}
return false;
}
// 获取下一步最优位置
pair<int, int> getBestMove(const vector<vector<char>>& board) {
vector<pair<int, int>> emptyCells = getEmptyCells(board);
// 如果棋盘已满
if (emptyCells.empty()) {
return make_pair(-1, -1);
}
// 如果有下一步可以直接胜利的位置
for (int i = 0; i < emptyCells.size(); ++i) {
int x = emptyCells[i].first;
int y = emptyCells[i].second;
vector<vector<char>> nextBoard = board;
nextBoard[x][y] = COMPUTER;
if (isWin(nextBoard, COMPUTER)) {
return make_pair(x, y);
}
}
// 如果对手有下一步可以直接胜利的位置,则堵住
for (int i = 0; i < emptyCells.size(); ++i) {
int x = emptyCells[i].first;
int y = emptyCells[i].second;
vector<vector<char>> nextBoard = board;
nextBoard[x][y] = HUMAN;
if (isWin(nextBoard, HUMAN)) {
return make_pair(x, y);
}
}
// 否则随机下一步
int index = rand() % emptyCells.size();
return emptyCells[index];
}
int main() {
srand(time(0));
vector<vector<char>> board;
initBoard(board);
GameState state = PLAYING;
char currentPlayer = HUMAN;
while (state == PLAYING) {
if (currentPlayer == HUMAN) {
cout << "Human's turn:" << endl;
printBoard(board);
int x, y;
cout << "Please enter row and column (1-" << BOARD_SIZE << "): ";
cin >> x >> y;
--x;
--y;
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE || board[x][y] != EMPTY) {
cout << "Invalid move!" << endl;
continue;
}
board[x][y] = HUMAN;
if (isWin(board, HUMAN)) {
state = HUMAN_WIN;
}
currentPlayer = COMPUTER;
} else {
cout << "Computer's turn:" << endl;
printBoard(board);
pair<int, int> move = getBestMove(board);
if (move.first == -1 && move.second == -1) {
state = DRAW;
} else {
board[move.first][move.second] = COMPUTER;
if (isWin(board, COMPUTER)) {
state = COMPUTER_WIN;
}
currentPlayer = HUMAN;
}
}
}
printBoard(board);
if (state == DRAW) {
cout << "Draw!" << endl;
} else if (state == HUMAN_WIN) {
cout << "Human wins!" << endl;
} else if (state == COMPUTER_WIN) {
cout << "Computer wins!" << endl;
}
return 0;
}
```
这个程序实现了简单的人机对战,玩家使用 X 表示,电脑使用 O 表示,玩家先手。程序会提示玩家输入行和列,玩家输入后程序会判断合法性并在棋盘中标记。当有一方胜利或者棋盘已满时,游戏结束并输出结果。在电脑回合,程序会根据当前棋盘状态选择最优位置进行落子。
阅读全文