c++五子棋神经网络算法代码
时间: 2023-09-04 07:06:53 浏览: 98
以下是使用C++实现的五子棋神经网络算法代码,仅供参考:
```c++
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
const int BOARD_SIZE = 15;
const int INPUT_SIZE = BOARD_SIZE * BOARD_SIZE;
const int HIDDEN_LAYER_SIZE = 200;
const int OUTPUT_SIZE = BOARD_SIZE * BOARD_SIZE;
const double LEARNING_RATE = 0.1;
const double MOMENTUM = 0.9;
class NeuralNetwork {
private:
vector<double> inputNodes;
vector<double> hiddenNodes;
vector<double> outputNodes;
vector<double> hiddenBiases;
vector<double> outputBiases;
vector<vector<double>> hiddenWeights;
vector<vector<double>> outputWeights;
vector<double> hiddenErrorGradients;
vector<double> outputErrorGradients;
vector<vector<double>> hiddenWeightDelta;
vector<vector<double>> outputWeightDelta;
public:
NeuralNetwork() {
inputNodes.resize(INPUT_SIZE);
hiddenNodes.resize(HIDDEN_LAYER_SIZE);
outputNodes.resize(OUTPUT_SIZE);
hiddenBiases.resize(HIDDEN_LAYER_SIZE);
outputBiases.resize(OUTPUT_SIZE);
hiddenWeights.resize(HIDDEN_LAYER_SIZE, vector<double>(INPUT_SIZE));
outputWeights.resize(OUTPUT_SIZE, vector<double>(HIDDEN_LAYER_SIZE));
hiddenErrorGradients.resize(HIDDEN_LAYER_SIZE);
outputErrorGradients.resize(OUTPUT_SIZE);
hiddenWeightDelta.resize(HIDDEN_LAYER_SIZE, vector<double>(INPUT_SIZE));
outputWeightDelta.resize(OUTPUT_SIZE, vector<double>(HIDDEN_LAYER_SIZE));
srand(time(NULL));
for (int i = 0; i < HIDDEN_LAYER_SIZE; i++) {
hiddenBiases[i] = ((double) rand() / RAND_MAX) * 2 - 1;
for (int j = 0; j < INPUT_SIZE; j++) {
hiddenWeights[i][j] = ((double) rand() / RAND_MAX) * 2 - 1;
}
}
for (int i = 0; i < OUTPUT_SIZE; i++) {
outputBiases[i] = ((double) rand() / RAND_MAX) * 2 - 1;
for (int j = 0; j < HIDDEN_LAYER_SIZE; j++) {
outputWeights[i][j] = ((double) rand() / RAND_MAX) * 2 - 1;
}
}
}
void setInputNodes(vector<vector<int>>& board) {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
inputNodes[i * BOARD_SIZE + j] = board[i][j];
}
}
}
void feedForward() {
for (int i = 0; i < HIDDEN_LAYER_SIZE; i++) {
double sum = 0.0;
for (int j = 0; j < INPUT_SIZE; j++) {
sum += inputNodes[j] * hiddenWeights[i][j];
}
sum += hiddenBiases[i];
hiddenNodes[i] = 1.0 / (1.0 + exp(-sum));
}
for (int i = 0; i < OUTPUT_SIZE; i++) {
double sum = 0.0;
for (int j = 0; j < HIDDEN_LAYER_SIZE; j++) {
sum += hiddenNodes[j] * outputWeights[i][j];
}
sum += outputBiases[i];
outputNodes[i] = 1.0 / (1.0 + exp(-sum));
}
}
void backPropagate(vector<int>& target) {
for (int i = 0; i < OUTPUT_SIZE; i++) {
outputErrorGradients[i] = outputNodes[i] * (1 - outputNodes[i]) * (target[i] - outputNodes[i]);
for (int j = 0; j < HIDDEN_LAYER_SIZE; j++) {
outputWeightDelta[i][j] = LEARNING_RATE * outputErrorGradients[i] * hiddenNodes[j] + MOMENTUM * outputWeightDelta[i][j];
outputWeights[i][j] += outputWeightDelta[i][j];
}
outputBiases[i] += LEARNING_RATE * outputErrorGradients[i];
}
for (int i = 0; i < HIDDEN_LAYER_SIZE; i++) {
double sum = 0.0;
for (int j = 0; j < OUTPUT_SIZE; j++) {
sum += outputErrorGradients[j] * outputWeights[j][i];
}
hiddenErrorGradients[i] = hiddenNodes[i] * (1 - hiddenNodes[i]) * sum;
for (int j = 0; j < INPUT_SIZE; j++) {
hiddenWeightDelta[i][j] = LEARNING_RATE * hiddenErrorGradients[i] * inputNodes[j] + MOMENTUM * hiddenWeightDelta[i][j];
hiddenWeights[i][j] += hiddenWeightDelta[i][j];
}
hiddenBiases[i] += LEARNING_RATE * hiddenErrorGradients[i];
}
}
vector<int> getOutputNodes() {
vector<int> result(OUTPUT_SIZE);
for (int i = 0; i < OUTPUT_SIZE; i++) {
result[i] = round(outputNodes[i]);
}
return result;
}
};
class Game {
private:
vector<vector<int>> board;
int currentPlayer;
int winner;
bool gameOver;
NeuralNetwork neuralNetwork;
public:
Game() {
board.resize(BOARD_SIZE, vector<int>(BOARD_SIZE));
currentPlayer = 1;
winner = 0;
gameOver = false;
}
void play() {
while (!gameOver) {
printBoard();
if (currentPlayer == 1) {
playerMove();
} else {
computerMove();
}
updateGameState();
}
printBoard();
printResult();
}
private:
void printBoard() {
cout << " ";
for (int i = 0; i < BOARD_SIZE; i++) {
cout << i << " ";
}
cout << endl;
for (int i = 0; i < BOARD_SIZE; i++) {
cout << i << " ";
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 0) {
cout << ". ";
} else if (board[i][j] == 1) {
cout << "X ";
} else {
cout << "O ";
}
}
cout << endl;
}
}
void playerMove() {
int x, y;
cout << "Enter your move (row column): ";
cin >> x >> y;
board[x][y] = currentPlayer;
}
void computerMove() {
vector<int> input(BOARD_SIZE * BOARD_SIZE);
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
input[i * BOARD_SIZE + j] = board[i][j];
}
}
neuralNetwork.setInputNodes(board);
neuralNetwork.feedForward();
vector<int> output = neuralNetwork.getOutputNodes();
int maxIndex = 0;
for (int i = 0; i < BOARD_SIZE * BOARD_SIZE; i++) {
if (output[i] > output[maxIndex]) {
maxIndex = i;
}
}
int x = maxIndex / BOARD_SIZE;
int y = maxIndex % BOARD_SIZE;
board[x][y] = currentPlayer;
}
void updateGameState() {
int winner = checkWinner();
if (winner != 0) {
gameOver = true;
this->winner = winner;
} else {
currentPlayer = (currentPlayer == 1 ? 2 : 1);
}
}
int checkWinner() {
// Check rows
for (int i = 0; i < BOARD_SIZE; i++) {
int count = 0;
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == currentPlayer) {
count++;
} else {
count = 0;
}
if (count == 5) {
return currentPlayer;
}
}
}
// Check columns
for (int j = 0; j < BOARD_SIZE; j++) {
int count = 0;
for (int i = 0; i < BOARD_SIZE; i++) {
if (board[i][j] == currentPlayer) {
count++;
} else {
count = 0;
}
if (count == 5) {
return currentPlayer;
}
}
}
// Check diagonals
for (int i = 0; i < BOARD_SIZE - 4; i++) {
for (int j = 0; j < BOARD_SIZE - 4; j++) {
if (board[i][j] == currentPlayer &&
board[i + 1][j + 1] == currentPlayer &&
board[i + 2][j + 2] == currentPlayer &&
board[i + 3][j + 3] == currentPlayer &&
board[i + 4][j + 4] == currentPlayer) {
return currentPlayer;
}
}
}
// Check anti-diagonals
for (int i = 4; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE - 4; j++) {
if (board[i][j] == currentPlayer &&
board[i - 1][j + 1] == currentPlayer &&
board[i - 2][j + 2] == currentPlayer &&
board[i - 3][j + 3] == currentPlayer &&
board[i - 4][j + 4] == currentPlayer) {
return currentPlayer;
}
}
}
// Check for tie
int count = 0;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] != 0) {
count++;
}
}
}
if (count == BOARD_SIZE * BOARD_SIZE) {
gameOver = true;
return -1;
}
return 0;
}
void printResult() {
if (winner == -1) {
cout << "Tie!" << endl;
} else if (winner == 1) {
cout << "You win!" << endl;
} else {
cout << "Computer wins!" << endl;
}
}
};
int main() {
Game game;
game.play();
return 0;
}
```
该代码实现了一个简单的五子棋游戏,并使用神经网络算法实现了计算机的下棋。其中,神经网络包含一个输入层、一个隐藏层和一个输出层,每个节点采用sigmoid函数作为激活函数。神经网络的训练采用反向传播算法。
阅读全文
相关推荐















