-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
75 lines (68 loc) · 2.04 KB
/
main.cpp
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
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Board.h"
using namespace sf;
using namespace std;
int main() {
int width = 1024;
int height = 896;
RenderWindow window(VideoMode(width, height), "Connect 4");
Board gameBoard = Board();
gameBoard.setBoard();
// red = 1 | yellow = -1
int player = 1;
while (window.isOpen())
{
Event event;
Text text;
Font font;
font.loadFromFile("PressStart2P-Regular.ttf");
text.setFont(font);
text.setCharacterSize(32);
text.setFillColor(Color::White);
text.setPosition(360, 830);
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
if (event.type == Event::MouseButtonPressed && player == 1)
{
int col = gameBoard.selectCol(event.mouseButton.x);
if (!gameBoard.colFull(col))
{
gameBoard.makeMove(player, col);
player *= -1;
}
}
if (player == -1)
{
int bestMove = gameBoard.miniMax(player, gameBoard, 4, INT_MIN, INT_MAX).first;
if (!gameBoard.colFull(bestMove))
{
gameBoard.makeMove(player, bestMove);
player *= -1;
}
}
if (event.type == Event::KeyPressed && event.key.code == Keyboard::R)
{
gameBoard.resetBoard();
gameBoard.setBoard();
player = 1;
}
}
window.clear();
if (gameBoard.isTerminalState())
{
if (gameBoard.checkWin(1))
text.setString("Red wins!");
else if (gameBoard.checkWin(-1))
text.setString("Yellow wins!");
else
text.setString("Draw!");
}
window.draw(text);
gameBoard.drawBoard(window);
window.display();
}
return 0;
}