版本比较
代码
正文
增加 43 行删除 276 行
1234567239101112131415161718192021222324252627282930313233343536373839404142434445464748495051456789101112131415161718192021222324252627282930313233343536375338555657585960616263646566676869707172737475767778798039404142828384858687434445464748899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128249505152```cpp#include <iostream>#include <vector>#include <limits>#include <algorithm>#include <ctime>
#include<bits/stdc++.h>using namespace std;
const int SIZE = 15;const int WIN = 1000000;const int LOSE = -1000000;const int MAX_DEPTH = 4; // 搜索深度,可调整
enum ChessType { EMPTY, BLACK, WHITE };
class Board {public: ChessType board[SIZE][SIZE];
Board() { for (int i = 0; i < SIZE; ++i) for (int j = 0; j < SIZE; ++j) board[i][j] = EMPTY; }
bool is_valid(int x, int y) const { return x >= 0 && x < SIZE && y >= 0 && y < SIZE; }
bool is_empty(int x, int y) const { return is_valid(x, y) && board[x][y] == EMPTY; }
bool check_win(int x, int y, ChessType type) const { const int dir[4][2] = {{1,0}, {0,1}, {1,1}, {1,-1}}; for (int d = 0; d < 4; ++d) { int count = 1; for (int step = 1; step <= 4; ++step) { int nx = x + dir[d][0] * step; int ny = y + dir[d][1] * step; if (!is_valid(nx, ny) || board[nx][ny] != type) break; ++count; } for (int step = 1; step <= 4; ++step) { int nx = x - dir[d][0] * step; int ny = y - dir[d][1] * step; if (!is_valid(nx, ny) || board[nx][ny] != type) break; ++count; } if (count >= 5) return true;string s;int num[6];int cnt=0;int f(){ if(s=="one"||s=="a"||s=="first"||s=="another") return 1; if(s=="two"||s=="both"||s=="second") return 2; if(s=="three"||s=="third") return 3; if(s=="four") return 4; if(s=="five") return 5; if(s=="six") return 6; if(s=="seven") return 7; if(s=="eight") return 8; if(s=="nine") return 9; if(s=="ten") return 10; if(s=="eleven") return 11; if(s=="twelve") return 12; if(s=="thirteen") return 13; if(s=="fourteen") return 14; if(s=="fifteen") return 15; if(s=="sixteen") return 16; if(s=="seventeen") return 17; if(s=="eighteen") return 18; if(s=="nineteen") return 19; if(s=="twenty") return 20; return -1;}int main(){ for(int i=0;i<6;i++){ cin>>s; if(s.back()=='.')s.pop_back(); int val=f(); if(val!=-1){ num[cnt++]=(val*val)%100; } return false; }};
class AI {private: ChessType ai_color;
int evaluate_line(const vector<ChessType>& line) const { int score = 0; int count = 0; ChessType prev = EMPTY;
for (size_t i = 0; i < line.size(); ++i) { if (line[i] == prev && line[i] != EMPTY) { ++count; } else { if (prev != EMPTY) { score += evaluate_pattern(count, prev == ai_color); } count = 1; prev = line[i]; } } if (prev != EMPTY) { score += evaluate_pattern(count, prev == ai_color); } return score; if(cnt==0){ cout<<0; return 0; }
int evaluate_pattern(int length, bool is_ai) const { const int score_table[6] = {0, 1, 10, 100, 1000, 10000}; if (length >= 5) return is_ai ? WIN : LOSE; int score = score_table[length]; return is_ai ? score : -score; sort(num,num+cnt); cout<<num[0]; for(int i=1;i<cnt;i++){ if(num[i]<10)cout<<0; cout<<num[i]; }
int evaluate(const Board& board) const { int total = 0;
// 横向评估 for (int i = 0; i < SIZE; ++i) { vector<ChessType> line; for (int j = 0; j < SIZE; ++j) line.push_back(board.board[i][j]); total += evaluate_line(line); }
// 纵向评估 for (int j = 0; j < SIZE; ++j) { vector<ChessType> line; for (int i = 0; i < SIZE; ++i) line.push_back(board.board[i][j]); total += evaluate_line(line); }
// 对角线评估 for (int k = 0; k < SIZE*2-1; ++k) { vector<ChessType> line; for (int i = max(0, k-SIZE+1); i <= min(k, SIZE-1); ++i) line.push_back(board.board[i][k-i]); total += evaluate_line(line); }
// 反对角线评估 for (int k = 0; k < SIZE*2-1; ++k) { vector<ChessType> line; for (int i = max(0, k-SIZE+1); i <= min(k, SIZE-1); ++i) line.push_back(board.board[i][SIZE-1-(k-i)]); total += evaluate_line(line); }
return total; }
void generate_moves(const Board& board, vector<pair<int, int> >& moves) const { for (int i = 0; i < SIZE; ++i) { for (int j = 0; j < SIZE; ++j) { if (board.board[i][j] != EMPTY) continue; bool near = false; for (int dx = -1; dx <= 1; ++dx) { for (int dy = -1; dy <= 1; ++dy) { int x = i + dx; int y = j + dy; if (board.is_valid(x, y) && board.board[x][y] != EMPTY) { near = true; break; } } if (near) break; } if (near) moves.push_back(make_pair(i, j)); } } }
int alpha_beta(Board& board, int depth, int alpha, int beta, bool maximizing) const { if (depth == 0) return evaluate(board);
vector<pair<int, int> > moves; generate_moves(board, moves); if (moves.empty()) return 0;
if (maximizing) { int value = numeric_limits<int>::min(); for (size_t i = 0; i < moves.size(); ++i) { int x = moves[i].first; int y = moves[i].second; board.board[x][y] = ai_color; if (board.check_win(x, y, ai_color)) { board.board[x][y] = EMPTY; return WIN; } value = max(value, alpha_beta(board, depth-1, alpha, beta, false)); board.board[x][y] = EMPTY; alpha = max(alpha, value); if (alpha >= beta) break; } return value; } else { int value = numeric_limits<int>::max(); for (size_t i = 0; i < moves.size(); ++i) { int x = moves[i].first; int y = moves[i].second; board.board[x][y] = (ai_color == BLACK) ? WHITE : BLACK; if (board.check_win(x, y, board.board[x][y])) { board.board[x][y] = EMPTY; return LOSE; } value = min(value, alpha_beta(board, depth-1, alpha, beta, true)); board.board[x][y] = EMPTY; beta = min(beta, value); if (alpha >= beta) break; } return value; } }
public: AI(ChessType color) : ai_color(color) {}
void make_move(Board& board) const { vector<pair<int, int> > moves; generate_moves(board, moves); if (moves.empty()) return;
int best_value = numeric_limits<int>::min(); pair<int, int> best_move = moves[0];
for (size_t i = 0; i < moves.size(); ++i) { int x = moves[i].first; int y = moves[i].second; board.board[x][y] = ai_color; if (board.check_win(x, y, ai_color)) { cout << "AI moves at (" << x << ", " << y << ")\n"; return; } int value = alpha_beta(board, MAX_DEPTH-1, numeric_limits<int>::min(), numeric_limits<int>::max(), false); board.board[x][y] = EMPTY;
if (value > best_value) { best_value = value; best_move = moves[i]; } }
board.board[best_move.first][best_move.second] = ai_color; cout << "AI moves at (" << best_move.first << ", " << best_move.second << ")\n"; }};
int main() { Board board; AI ai(WHITE); bool human_turn = true;
while (true) { if (human_turn) { int x, y; cout << "Enter your move (x y): "; cin >> x >> y; if (!board.is_valid(x, y)) { cout << "Invalid position!\n"; continue; } if (!board.is_empty(x, y)) { cout << "Position occupied!\n"; continue; } board.board[x][y] = BLACK; if (board.check_win(x, y, BLACK)) { cout << "You win!\n"; break; } } else { clock_t start = clock(); ai.make_move(board); clock_t end = clock(); cout << "AI thinking time: " << (double)(end - start)/CLOCKS_PER_SEC << "s\n"; }
// 打印棋盘 cout << " "; for (int j = 0; j < SIZE; ++j) cout << j%10; cout << "\n"; for (int i = 0; i < SIZE; ++i) { cout << i%10 << " "; for (int j = 0; j < SIZE; ++j) { if (board.board[i][j] == BLACK) cout << 'X'; else if (board.board[i][j] == WHITE) cout << 'O'; else cout << '.'; } cout << "\n"; }
if (!human_turn) { for (int i = 0; i < SIZE; ++i) { for (int j = 0; j < SIZE; ++j) { if (board.board[i][j] == WHITE && board.check_win(i, j, WHITE)) { cout << "AI wins!\n"; return 0; } } } }
human_turn = !human_turn; }
return 0;}
```