#include<iostream>
#include<windows.h>
#include<vector>
#include"board.h"
using namespace std;
void gameBody();
void placeChess(int pos);
int winner();
bool isDraw();
// for negaMax
int evaluate(int playerNow);
// for minMax
int evaluate();
void GenerateMoves(vector<int> &steps);
int negaMax(int depth, int playerNow);
int maxMin(int depth, int playerNow);
// 加入剪枝
int negaMax(int depth, int playerNow, int alpha, int beta);
namespace {
Board board;
int playerNow = 1;
int bestMove;
int depth = 9;
}
void gameBody() {
while (winner()==0 && !isDraw()) {
if (playerNow > 0) {
cout << board;
cout << std::endl << "--------請輸入您要落子的位置(0-8)------------" << std::endl;
int pos;
cin >> pos;
placeChess(pos);
depth--;
}
else {
maxMin(depth, playerNow);
//negaMax(depth, playerNow);
placeChess(bestMove);
depth--;
}
}
cout << board;
}
void placeChess(int pos) {
board.board[pos]= playerNow;
playerNow = -playerNow;
}
int winner(){
for (int i = 0; i < 3; i++) {
if (board.getCol(i) == -3 || board.getRow(i) == -3||board.getMainLine()==-3||board.getCountLine()==-3) {
return -1;
}
if (board.getCol(i) == 3 || board.getRow(i) == 3 || board.getMainLine() == 3 || board.getCountLine() == 3) {
return 1;
}
}
return 0;
}
bool isDraw() {
for (int i = 0; i < 9; i++) {
if (board.board[i] == 0) {
return false;
}
}
return (winner() == 0)&& true;
}
void GenerateMoves(vector<int> &steps) {
for (int i = 0; i < 9; i++) {
if (board.board[i] == 0) {
steps.push_back(i);
}
}
}
int evaluate(int playerNow) {
if (winner() ==1) {
return 10000* playerNow;
}
else if (winner() == -1) {
return -10000* playerNow;
}
else {
return 0;
}
}
int evaluate() {
if (winner() == 1) {
return 10000 ;
}
else if (winner() == -1) {
return -10000;
}
else {
return 0;
}
}
int maxMin(int depth, int playerNow) {
if (winner()!=0 || depth == 0) {
return evaluate();
}
int bestValue = 0;
if (playerNow == -1) {
bestValue = 10000;
}
else {
bestValue = -10000;
}
vector<int> steps;
vector<int>& stepVector = steps;
GenerateMoves(stepVector);// 獲得走步
while(stepVector.size()>0) {
int mv = stepVector[stepVector.size() - 1];
stepVector.pop_back();
board.board[mv] = playerNow;// makeMove
int value = maxMin(depth - 1, -playerNow);
board.board[mv] = 0;// unMakeMove
if (playerNow == 1) {
if (value > bestValue) {
bestValue = value;
if (depth == ::depth) {
bestMove = mv;
}
}
}
else {
if (value < bestValue) {
bestValue = value;
if (depth == ::depth) {
bestMove = mv;
}
}
}
}
return bestValue;
}
/*int negaMax(int depth,int playerNow) {
int best = -100000;
if (depth == 0 || winner() != 0) {
return evaluate(playerNow);
}
else {
vector<int> steps;
vector<int>& stepVector = steps;
GenerateMoves(stepVector);// 獲得走步
while (stepVector.size()>0) {
int mv = stepVector[stepVector.size()-1];
stepVector.pop_back();
board.board[mv] = playerNow;// makeMove
int value = -negaMax(depth - 1,-playerNow);
board.board[mv] = 0;// unMakeMove
if (value >= best) {
best = value;
if (depth == ::depth) {
bestMove = mv;
}
}
}
}
return best;
}*/
// 加入alpha-beta剪枝
int negaMax(int depth, int playerNow) {
negaMax(depth,playerNow,-10000000,10000000);
return 0;
}
int negaMax(int depth,int playerNow,int alpha,int beta) {
int best = -100000;
if (depth == 0 || winner() != 0) {
return evaluate(playerNow);
}
else {
vector<int> steps;
vector<int>& stepVector = steps;
GenerateMoves(stepVector);// 獲得走步
while (stepVector.size()>0) {
int mv = stepVector[stepVector.size()-1];
stepVector.pop_back();
board.board[mv] = playerNow;// makeMove
int value = -negaMax(depth - 1,-playerNow,-beta,-alpha);
board.board[mv] = 0;// unMakeMove
if (value >= beta) {
if (depth == ::depth) {
bestMove = mv;
}
return beta;
}
if (value > alpha) {
if (depth == ::depth) {
bestMove = mv;
}
alpha = value;
}
}
}
return alpha;
}
int main() {
gameBody();
system("pause");
return 0;
}
Alpha-beta剪枝-井字棋
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.