-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect4.cpp
119 lines (112 loc) · 2.66 KB
/
connect4.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
#include <iostream>
#include <cstdlib>
#include <unistd.h>
int tab[6][7];
int choice, player;
bool end = false;
int a=0;
void check(int x)
{
if(tab[x-1][a]!=0 && a<6)
{
a++;
check(x);
} else if (player==1 && a<6)
{
tab[x-1][a]=1;
a=0;
}
else if (player==2 && a<6)
{
tab[x-1][a]=2;
a=0;
}
else
{
std::cout << "WRONG!" << std::endl;
a=0;
player++;
}
}
int win_check()
{
for(int i = 0; i<6; i++)
{
for(int j = 0; j<7; j++)
{
if(tab[j][i]==1 && tab[j+1][i+1]==1 && tab[j+2][i+2]==1 && tab[j+3][i+3]==1)
{
end = true;
std::cout << "\nRED " <<player<< std::endl;
}
if(tab[j][i]==1 && tab[j+1][i-1]==1 && tab[j+2][i-2]==1 && tab[j+3][i-3]==1)
{
std::cout << "\nRED " <<player<<std::endl;
end = true;
}
if(tab[j][i]==2 && tab[j+1][i-1]==2 && tab[j+2][i-2]==2 && tab[j+3][i-3]==2)
{
std::cout << "\nYELLOW " <<player<< std::endl;
end=true;
}
else if(tab[j][i]==2 && tab[j-1][i-1]==2 && tab[j-2][i-2]==2 && tab[j-3][i-3]==2)
{
end = true;
std::cout << "\nYELLOW " <<player<< std::endl;
}
else if(tab[j][i]==1 && tab[j][i-1]==1 && tab[j][i-2]==1 && tab[j][i-3]==1)
{
std::cout << "\nRED " <<player<< std::endl;
end = true;
}
else if(tab[j][i]==1 && tab[j-1][i]==1 && tab[j-2][i]==1 && tab[j-3][i]==1)
{
std::cout << "\nRED " <<player<< std::endl;
end = true;
}
else if(tab[j][i]==2 && tab[j][i-1]==2 && tab[j][i-2]==2 && tab[j][i-3]==2)
{
std::cout << "\nYELLOW " <<player<< std::endl;
end = true;
}
else if(tab[j][i]==2 && tab[j-1][i]==2 && tab[j-2][i]==2 && tab[j-3][i]==2)
{
std::cout << "\nYELLOW " <<player<< std::endl;
end = true;
}
}
}
}
int p_choice()
{
player = 1;
while(end!=true)
{
std::cout <<player;
std::cin >> choice;
if (choice>0 && choice<8)
{
check(choice);
if (player == 1)
{
player++;
}
else
{
player--;
}
}
else
{
std::cout << "DRAW" << std::endl;
}
win_check();
}
}
int main()
{
system("clear");
sleep(1);
p_choice();
return 0;
}