-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
182 lines (156 loc) · 5.62 KB
/
main.cpp
File metadata and controls
182 lines (156 loc) · 5.62 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include "board.h"
int evaluateWindow(int window[4], int piece) {
int score = 0;
int pieceCount = 0;
int emptyCount = 0;
int opponentCount = 0;
for (int i = 0; i < 4; i++) {
if (window[i] == piece) pieceCount++;
else if (window[i] == 0) emptyCount++;
else opponentCount++;
}
// AI piece (2) scoring
if (piece == 2) {
if (pieceCount == 4) return 1000; // Win
if (pieceCount == 3 && emptyCount == 1) return 100; // 3-in-a-row
if (pieceCount == 2 && emptyCount == 2) return 10; // 2-in-a-row
if (pieceCount == 1 && emptyCount == 3) return 1; // 1 piece
}
// Player piece (1) scoring - defensive
else if (piece == 1) {
if (opponentCount == 4) return -1000; // Opponent wins
if (opponentCount == 3 && emptyCount == 1) return -100; // Block 3-in-a-row
if (opponentCount == 2 && emptyCount == 2) return -10; // Block 2-in-a-row
}
return score;
}
// Heuristic evaluation function
int scoreBoard(Board board, int piece) {
int score = 0;
// Horizontal evaluation
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 4; j++) {
int window[4] = {board.getCell(i, j), board.getCell(i, j+1),
board.getCell(i, j+2), board.getCell(i, j+3)};
score += evaluateWindow(window, 2);
score += evaluateWindow(window, 1);
}
}
// Vertical evaluation
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 7; j++) {
int window[4] = {board.getCell(i, j), board.getCell(i+1, j),
board.getCell(i+2, j), board.getCell(i+3, j)};
score += evaluateWindow(window, 2);
score += evaluateWindow(window, 1);
}
}
// Diagonal evaluation (top-left to bottom-right)
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
int window[4] = {board.getCell(i, j), board.getCell(i+1, j+1),
board.getCell(i+2, j+2), board.getCell(i+3, j+3)};
score += evaluateWindow(window, 2);
score += evaluateWindow(window, 1);
}
}
// Diagonal evaluation (top-right to bottom-left)
for (int i = 0; i < 3; i++) {
for (int j = 3; j < 7; j++) {
int window[4] = {board.getCell(i, j), board.getCell(i+1, j-1),
board.getCell(i+2, j-2), board.getCell(i+3, j-3)};
score += evaluateWindow(window, 2);
score += evaluateWindow(window, 1);
}
}
// Center column bonus
for (int i = 0; i < 6; i++) {
if (board.getCell(i, 3) == 2) score += 5;
if (board.getCell(i, 2) == 2) score += 3;
if (board.getCell(i, 4) == 2) score += 3;
}
return score;
}
int minimax(Board board, int depth, int alpha, int beta, bool maximizingPlayer) {
// Base cases
if (board.checkWin(2)) return 10000 - depth;
if (board.checkWin(1)) return depth - 10000;
if (board.isFull()) return 0;
if (depth == 0) return scoreBoard(board, 2);
if (maximizingPlayer) {
int maxEval = -100000;
for (int col = 0; col < 7; col++) {
Board tempBoard = board;
if (tempBoard.makeMove(col, 2)) {
int eval = minimax(tempBoard, depth - 1, alpha, beta, false);
maxEval = (eval > maxEval) ? eval : maxEval;
alpha = (eval > alpha) ? eval : alpha;
if (beta <= alpha) break;
}
}
return maxEval;
} else {
int minEval = 100000;
for (int col = 0; col < 7; col++) {
Board tempBoard = board;
if (tempBoard.makeMove(col, 1)) {
int eval = minimax(tempBoard, depth - 1, alpha, beta, true);
minEval = (eval < minEval) ? eval : minEval;
beta = (eval < beta) ? eval : beta;
if (beta <= alpha) break;
}
}
return minEval;
}
}
int main(){
std::cout << "Hello world!" << std::endl;
Board board;
int playerTurn = 1;
//Game Loop
while(!board.isFull()){
board.printBoard();
//Player Turn
if(playerTurn == 1){
std::cout << "Make a move (Enter column number for move 1-7): " << std::endl;
int moveCol;
std::cin >> moveCol;
if(moveCol < 1 || moveCol > 7){
std::cout << "Make valid move (1-7): " << std::endl;
std::cin >> moveCol;
}
board.makeMove(moveCol - 1, playerTurn);
if(board.checkWin(1)){
board.printBoard();
std::cout << "Player Won The Game!!!" << std::endl;
break;
}
playerTurn = 2;
}
//AI Turn
else{
std::cout << "AI is thinking..." << std::endl;
int bestMove = 0;
int bestScore = -1000;
for (int col = 0; col < 7; col++) {
Board tempBoard = board;
if (tempBoard.makeMove(col, 2)) {
int score = minimax(tempBoard, 8, -100000, 100000, false);
if (score > bestScore) {
bestScore = score;
bestMove = col;
}
}
}
board.makeMove(bestMove, 2);
if(board.checkWin(2)){
board.printBoard();
std::cout << "Bot Won The Game!!!" << std::endl;
break;
}
playerTurn = 1;
}
}
return 0;
}