devc++五子棋人机对战代码
时间: 2023-12-22 17:04:52 浏览: 113
很抱歉,根据提供的引用内容,没有找到关于DevC++的五子棋人机对战代码的信息。但是,我可以为您提供一个基于C++的五子棋人机对战的示例代码,您可以在DevC++中运行。
```cpp
#include <iostream>
using namespace std;
const int SIZE = 15;
char board[SIZE][SIZE];
// 初始化棋盘
void initBoard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = '+';
}
}
}
// 打印棋盘
void printBoard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}
// 判断是否胜利
bool checkWin(int row, int col, char player) {
int count = 0;
// 横向判断
for (int i = col - 4; i <= col; i++) {
if (i >= 0 && i + 4 < SIZE) {
count = 0;
for (int j = i; j <= i + 4; j++) {
if (board[row][j] == player) {
count++;
}
}
if (count == 5) {
return true;
}
}
}
// 纵向判断
for (int i = row - 4; i <= row; i++) {
if (i >= 0 && i + 4 < SIZE) {
count = 0;
for (int j = i; j <= i + 4; j++) {
if (board[j][col] == player) {
count++;
}
}
if (count == 5) {
return true;
}
}
}
// 左上到右下判断
for (int i = row - 4, j = col - 4; i <= row, j <= col; i++, j++) {
if (i >= 0 && i + 4 < SIZE && j >= 0 && j + 4 < SIZE) {
count = 0;
for (int k = 0; k < 5; k++) {
if (board[i + k][j + k] == player) {
count++;
}
}
if (count == 5) {
return true;
}
}
}
// 右上到左下判断
for (int i = row - 4, j = col + 4; i <= row, j >= col; i++, j--) {
if (i >= 0 && i + 4 < SIZE && j >= 0 && j - 4 >= 0) {
count = 0;
for (int k = 0; k < 5; k++) {
if (board[i + k][j - k] == player) {
count++;
}
}
if (count == 5) {
return true;
}
}
}
return false;
}
// 人机对战
void playWithAI() {
int row, col;
char player = 'X';
while (true) {
cout << "当前棋盘:" << endl;
printBoard();
if (player == 'X') {
cout << "轮到玩家下棋,请输入行和列(以空格分隔):";
cin >> row >> col;
} else {
cout << "轮到电脑下棋:" << endl;
// 这里可以编写AI下棋的逻辑
// 例如,随机生成行和列
row = rand() % SIZE;
col = rand() % SIZE;
}
if (row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] == '+') {
board[row][col] = player;
if (checkWin(row, col, player)) {
cout << "恭喜玩家 " << player << " 获胜!" << endl;
break;
}
if (player == 'X') {
player = 'O';
} else {
player = 'X';
}
} else {
cout << "无效的位置,请重新输入!" << endl;
}
}
}
int main() {
initBoard();
playWithAI();
return 0;
}
```
阅读全文