-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hex
157 lines (135 loc) · 4.51 KB
/
Hex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <limits>
using namespace std;
// Define constants for board size and player colors
const int BOARD_SIZE = 11;
const char EMPTY = '.';
const char BLUE = 'B';
const char RED = 'R';
// Define the board class
class Board {
private:
vector<vector<char>> board;
public:
// Constructor to initialize the board
Board() : board(BOARD_SIZE, vector<char>(BOARD_SIZE, EMPTY)) {}
// Function to display the board
void display() {
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
cout << board[i][j] << " ";
}
cout << endl;
}
}
// Function to check if a move is valid
bool isValidMove(int row, int col) {
return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == EMPTY;
}
// Function to make a move
void makeMove(int row, int col, char player) {
board[row][col] = player;
}
// Function to check if there's a winner
bool checkWinner(char player) {
// Implement your logic to check for a winner
return false;
}
};
// Function to get user input for a move
pair<int, int> getUserMove() {
int row, col;
cout << "Enter row and column (0-indexed) for your move: ";
cin >> row >> col;
return make_pair(row, col);
}
// Function to generate a random move for the computer player
pair<int, int> getRandomMove(const Board& board) {
int row, col;
do {
row = rand() % BOARD_SIZE;
col = rand() % BOARD_SIZE;
} while (!board.isValidMove(row, col));
return make_pair(row, col);
}
// Function to evaluate a move using Monte Carlo method
double evaluateMove(const Board& board, int row, int col, char player, int trials) {
// Implement your Monte Carlo evaluation logic here
double wins = 0.0;
for (int i = 0; i < trials; ++i) {
// Simulate a random game and count wins
// This could involve making random moves until the game is over
}
return wins / trials;
}
// Function to find the best move using the min-max algorithm with alpha-beta pruning
pair<int, int> findBestMove(const Board& board, int depth, char player, double alpha, double beta) {
if (depth == 0) {
// Return evaluation score for leaf nodes
return make_pair(-1, -1);
}
pair<int, int> bestMove = make_pair(-1, -1);
double bestScore = -numeric_limits<double>::infinity();
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
if (board.isValidMove(i, j)) {
// Try the move
Board tempBoard = board;
tempBoard.makeMove(i, j, player);
// Evaluate the move
double score = evaluateMove(tempBoard, i, j, player, 1000);
// Maximize for blue player (computer)
if (player == BLUE) {
if (score > bestScore) {
bestScore = score;
bestMove = make_pair(i, j);
}
alpha = max(alpha, bestScore);
}
// Minimize for red player (human)
else {
if (score < bestScore) {
bestScore = score;
bestMove = make_pair(i, j);
}
beta = min(beta, bestScore);
}
// Alpha-beta pruning
if (beta <= alpha) {
break;
}
}
}
}
return bestMove;
}
// Function to play the game
void playGame() {
Board board;
srand(time(0)); // Seed random number generator
// Main game loop
while (true) {
// Human player's turn
board.display();
pair<int, int> humanMove = getUserMove();
board.makeMove(humanMove.first, humanMove.second, BLUE);
if (board.checkWinner(BLUE)) {
cout << "Congratulations! You win!" << endl;
break;
}
// Computer player's turn
pair<int, int> computerMove = findBestMove(board, 3, RED, -numeric_limits<double>::infinity(), numeric_limits<double>::infinity());
board.makeMove(computerMove.first, computerMove.second, RED);
if (board.checkWinner(RED)) {
cout << "Sorry, you lose. The computer wins!" << endl;
break;
}
}
}
int main() {
playGame();
return 0;
}