-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
121 lines (105 loc) · 3.64 KB
/
game.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
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
#include <windows.h>
#include "game.h"
#include <bits/stdc++.h>
using namespace std;
HANDLE hConsole;
void displayTable()
{
setCursor(0, 9);
setColor(0, 0, 1, 0);
cout << "\n +---+---+---+---+---+---+---+\n | | | | | | | |\n +---+---+---+---+---+---+---+\n | | | | | | | |\n +---+---+---+---+---+---+---+\n | | | | | | | |\n +---+---+---+---+---+---+---+\n | | | | | | | |\n +---+---+---+---+---+---+---+\n | | | | | | | |\n +---+---+---+---+---+---+---+\n | | | | | | | |\n +---+---+---+---+---+---+---+\n ";
setColor(1, 1, 1, 0);
cout << "\n (1) (2) (3) (4) (5) (6) (7)\n\n";
}
void setColor(bool red, bool green, bool blue, bool highlight)
{
SetConsoleTextAttribute(hConsole,
(red ? FOREGROUND_RED : 0) |
(green ? FOREGROUND_GREEN : 0) |
(blue ? FOREGROUND_BLUE : 0) |
(highlight ? (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED) : 0));
}
void setCursor(int col, int line)
{
COORD coord;
coord.X = col;
coord.Y = line;
SetConsoleCursorPosition(hConsole, coord);
}
void setup()
{
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
static CONSOLE_FONT_INFOEX fontex;
fontex.cbSize = sizeof(CONSOLE_FONT_INFOEX);
GetCurrentConsoleFontEx(hConsole, 0, &fontex);
fontex.FontWeight = 700;
fontex.dwFontSize.X = 24;
fontex.dwFontSize.Y = 24;
SetCurrentConsoleFontEx(hConsole, NULL, &fontex);
SetConsoleTitleA("Connect Four");
HWND console = GetConsoleWindow();
MoveWindow(console, 0, 0, 750, 750, TRUE);
DWORD style = GetWindowLong(console, GWL_STYLE);
style &= ~WS_MAXIMIZEBOX;
SetWindowLong(console, GWL_STYLE, style);
SetWindowPos(console, NULL, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_FRAMECHANGED);
system("CLS");
}
void makeMove(int col, int height[], long long bitboard[], int *counter, int moves[])
{
vector<int> v = listMoves(height);
if (!count(v.begin(), v.end(), col))
return;
long long m = 1LL << height[col]++;
bitboard[(*counter) & 1] ^= m;
moves[(*counter)++] = col;
for (int a = 0; a < 2; a++)
{
setColor(1, a, 0, 0);
for (int i = 18, k = 0; i <= 42; i += 4, k++)
{
for (int j = 21; j >= 11; j -= 2, k++)
{
if (bitboard[a] & (1LL << k))
{
if (height[col] == k + 1)
{
setColor(1, a, 0, 1);
setCursor(i - 1, j);
cout << " O ";
setColor(1, a, 0, 0);
}
else
{
setCursor(i - 1, j);
cout << " O ";
}
}
}
}
}
setColor(1, 1, 1, 0);
}
vector<int> listMoves(int height[])
{
vector<int> moves;
long long TOP = 283691315109952LL;
for (int col = 0; col <= 6; col++)
{
if ((TOP & (1LL << height[col])) == 0)
moves.push_back(col);
}
return moves;
}
bool isWin(long long bitboard)
{
int directions[4] = {1, 7, 6, 8};
long long bb;
for (int direction : directions)
{
bb = bitboard & (bitboard >> direction);
if ((bb & (bb >> (2 * direction))) != 0)
return true;
}
return false;
}