Skip to content

Commit d9b7f0a

Browse files
committed
game.exe and code
1 parent 283bcda commit d9b7f0a

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed

src/connect-4-game.c

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/****************************************************
2+
* connect_4_game.c *
3+
* connect 4 simple game multi-players only *
4+
* Author: HASHIM *
5+
* Email : cse.hashim.hossam@gmail.com *
6+
****************************************************/
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <time.h>
10+
#include <unistd.h>
11+
char a_sym=(char)2;
12+
char b_sym=(char)6;
13+
char up_sym=(char)281;
14+
char empty_sym=(char)178;
15+
int isA = 1;
16+
int firstRun = 1;
17+
char panel[4][4];
18+
int game_status(char** p){
19+
if(p[0][0]==p[1][1]==p[2][2]==p[3][3]){
20+
if(p[0][0]==a_sym){return 1;}
21+
if(p[0][0]==b_sym){return 2;}
22+
}
23+
if(p[3][0]==p[2][1]==p[1][2]==p[0][3]){
24+
if(p[3][0]==a_sym){return 1;}
25+
if(p[3][0]==b_sym){return 2;}
26+
}
27+
for(int i = 0;i<4;i++){
28+
if(p[i][0]==p[i][1]==p[i][2]==p[i][3]){
29+
if(p[i][0]==a_sym){return 1;}
30+
if(p[i][0]==b_sym){return 2;}
31+
}
32+
if(p[0][i]==p[1][i]==p[2][i]==p[3][i]){
33+
if(p[0][i]==a_sym){return 1;}
34+
if(p[0][i]==b_sym){return 2;}
35+
}
36+
//for(int j = 0;j<4;j++){//droau case
37+
// //return 0;
38+
//}
39+
}
40+
return -1;
41+
}
42+
/**
43+
//p[i][j]
44+
i is row #
45+
j is col #
46+
**/
47+
void play(char** pan,int c){
48+
if(pan[3][c]!=empty_sym){
49+
printf("** Error Full column ");
50+
if(isA){
51+
isA=0;
52+
printf("please player B try again\n");
53+
}else{
54+
isA=1;
55+
printf("please player A try again\n");
56+
}
57+
}else{
58+
for(int i=0;i<4;i++){//row counter
59+
//for(int j=0;j<4;j++){//col counter
60+
//
61+
//}
62+
63+
if(pan[i][c]==empty_sym){//must add winning condition if some one won or drue it must not play that turn
64+
if(isA){
65+
pan[i][c] = b_sym;
66+
return;
67+
}else{
68+
pan[i][c] = a_sym;
69+
return;
70+
}
71+
}
72+
}
73+
}
74+
}
75+
int main(void)
76+
{
77+
system("cls");
78+
//char panel[4][4];//={{empty_sym,empty_sym,empty_sym,empty_sym},{empty_sym,empty_sym,empty_sym,empty_sym},{empty_sym,empty_sym,empty_sym,empty_sym},{empty_sym,empty_sym,empty_sym,empty_sym}};
79+
if(firstRun){
80+
for (int j1 = 0; j1 < 4; j1++) {
81+
for (int j2 = 0; j2 < 4; j2++) {
82+
panel[j1][j2]=empty_sym;
83+
}
84+
}
85+
firstRun = 0;
86+
}
87+
if(isA){
88+
printf("player A (%c) turn\n\n",a_sym);
89+
isA=0;
90+
}else{
91+
printf("player B (%c) turn\n\n",b_sym);
92+
isA=1;
93+
}
94+
int i=0;
95+
//display
96+
for(int k = 3;k>=0;k--){
97+
printf("\t\t");
98+
for(int k1 = 0;k1<4;k1++){
99+
printf("%c ",panel[k][k1]);
100+
}
101+
printf("\n\n");
102+
}
103+
printf("\n\t\t%c %c %c %c\n",up_sym,up_sym,up_sym,up_sym);
104+
printf("\t\t1 2 3 4\n");
105+
puts("please enter your column number:");
106+
int col;
107+
scanf("%d",&col);
108+
col--;
109+
//play(panel,col-1);
110+
if(panel[3][col]!=empty_sym){
111+
printf("** Error Full column ");
112+
if(isA){
113+
isA=0;
114+
printf("please player B try again\n");
115+
system("pause()");
116+
}else{
117+
isA=1;
118+
printf("please player A try again\n");
119+
system("pause()");
120+
}
121+
}else{
122+
for(int i=0;i<4;i++){//row counter
123+
//for(int j=0;j<4;j++){//col counter
124+
//
125+
//}
126+
127+
if(panel[i][col]==empty_sym){//must add winning condition if some one won or drue it must not play that turn
128+
if(isA){
129+
panel[i][col] = b_sym;
130+
break;
131+
}else{
132+
panel[i][col] = a_sym;
133+
break;
134+
}
135+
}
136+
}
137+
}
138+
int stts = -1;//=game_status(panel);
139+
if(panel[0][0]==panel[1][1]&&panel[1][1]==panel[2][2]&&panel[2][2]==panel[3][3]){
140+
if(panel[0][0]==a_sym){stts = 1;}
141+
else if(panel[0][0]==b_sym){stts = 2;}
142+
}
143+
else if(panel[3][0]==panel[2][1]&&panel[2][1]==panel[1][2]&&panel[1][2]==panel[0][3]){
144+
if(panel[3][0]==a_sym){stts = 1;}
145+
else if(panel[3][0]==b_sym){stts = 2;}
146+
}
147+
for(int i = 0;i<4;i++){
148+
if(panel[i][0]==panel[i][1]&&panel[i][1]==panel[i][2]&&panel[i][2]==panel[i][3]){
149+
if(panel[i][0]==a_sym){stts = 1;}
150+
else if(panel[i][0]==b_sym){stts = 2;}
151+
}
152+
else if(panel[0][i]==panel[1][i]&&panel[1][i]==panel[2][i]&&panel[2][i]==panel[3][i]){
153+
if(panel[0][i]==a_sym){stts = 1;}
154+
else if(panel[0][i]==b_sym){stts = 2;}
155+
}
156+
int dro = 1;
157+
for(int j = 0;j<4;j++){//droau case
158+
if(panel[3][j]==empty_sym){
159+
dro = 0;
160+
}
161+
}
162+
if(dro){
163+
stts = 0;
164+
}
165+
}
166+
167+
//stts = -1;
168+
if(stts==-1){//holding
169+
//puts("______________________________________");
170+
main();
171+
}else if(stts==1){//finished
172+
system("cls");
173+
puts("player A wins. congrats!");
174+
//display
175+
for(int k = 3;k>=0;k--){
176+
printf("\t\t");
177+
for(int k1 = 0;k1<4;k1++){
178+
printf("%c ",panel[k][k1]);
179+
}
180+
printf("\n\n");
181+
}
182+
system("pause()");
183+
}else if(stts==2){
184+
system("cls");
185+
puts("player B wins. congrats!");
186+
//display
187+
for(int k = 3;k>=0;k--){
188+
printf("\t\t");
189+
for(int k1 = 0;k1<4;k1++){
190+
printf("%c ",panel[k][k1]);
191+
}
192+
printf("\n\n");
193+
}
194+
system("pause()");
195+
}else if(stts==0){
196+
system("cls");
197+
puts("ops! no player wins!");
198+
//display
199+
for(int k = 3;k>=0;k--){
200+
printf("\t\t");
201+
for(int k1 = 0;k1<4;k1++){
202+
printf("%c ",panel[k][k1]);
203+
}
204+
printf("\n\n");
205+
}
206+
system("pause()");
207+
}
208+
return 0;
209+
}

0 commit comments

Comments
 (0)