Skip to content

Instantly share code, notes, and snippets.

@King252525
Created May 24, 2019 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save King252525/25d5c3d08e3c81340cee43ed30072435 to your computer and use it in GitHub Desktop.
Save King252525/25d5c3d08e3c81340cee43ed30072435 to your computer and use it in GitHub Desktop.
COnnect 4 help
#include <iostream>
//#include "graphics.h"
#include <Windows.h>
#include <string>
#include <thread>
#include <time.h>
#include <conio.h>
#include <mmsystem.h>
using namespace std;
//#pragma comment(lib, "graphics.lib")
//#pragma comment(lib, "winmm.lib")
/*Bool(Keep Looping)*/
bool check(int a, int b);
bool isKeyPressed(char character);
/*Declaraction*/
int drop(int b);
int nowplace = 1;
int place[6][7] = { 0 };
char player = 15;
/*Void Stuff*/
void gotoxy(short x, short y);
void print_string(string mystring);
void print_string2(string mystring);
void print_string3(string mystring);
void textcolor(int x);
void resizeConsole(int width, int height);
void print_box(int wid, int hei, int x, int y);
void keyimput();
void play_music();
void start_game();
void display();
void welcome_screen();
void menu();
void username();
void how_to_play();
void exit_game();
void score_board();
void credits();
void music_menu();
/*For point shut (rows and colums)*/
void gotoxy(short x, short y)
{
HANDLE hConsoleOutput;
COORD Cursor_an_Pos = { x - 1,y - 1 };
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
}
/*Delay time for the writing*/
void print_string(string mystring) {
for (int i = 0; i < mystring.length(); i++) {
cout << mystring[i];
Sleep(20);
}
}
/*Delay time for the writing*/
void print_string2(string mystring) {
for (int i = 0; i < mystring.length(); i++) {
cout << mystring[i];
Sleep(120);
}
}
/*Delay time for the writing*/
void print_string3(string mystring) {
for (int i = 0; i < mystring.length(); i++) {
cout << mystring[i];
Sleep(80);
}
}
/*Color for the game*/
void textcolor(int x)
{
HANDLE mau;
mau = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(mau, x);
}
/*Size of screen*/
void resizeConsole(int width, int height)
{
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r);
MoveWindow(console, r.left, r.top, width, height, TRUE);
}
/*Print box (ascii)*/
void print_box(int wid, int hei, int x, int y)
{
/*Draw Top LIne*/
gotoxy(x, y);
cout << (char)201;
for (int i = 0; i < wid - 2; i++) cout << (char)205;
cout << (char)187 << endl;
/*Draw Middle Line*/
for (int j = 0; j < hei - 2; j++) {
gotoxy(x, y + j + 1);
cout << (char)186;
for (int i = 0; i < wid - 2; i++) cout << " ";
cout << (char)186 << endl;
}
/*Draw Bottom Line*/
gotoxy(x, y + hei - 1);
cout << (char)200;
for (int i = 0; i < wid - 2; i++) cout << (char)205;
cout << (char)188 << endl;
}
/*Get Key States*/
bool isKeyPressed(char character)
{
return GetKeyState(character) & 0x8000;
}
/*Key Imputs For The Game*/
void keyimput()
{
char player = 15;
/*Key Left*/
if (isKeyPressed(VK_LEFT) == true)
{
if (nowplace > 1)
{
nowplace--;
gotoxy(10, 27);
cout << nowplace;
}
Sleep(200);
}
/*Key Right*/
if (isKeyPressed(VK_RIGHT) == true)
{
if (nowplace < 7)
{
nowplace++;
gotoxy(10, 27);
cout << nowplace;
}
Sleep(200);
}
/*Key Enter*/
if (isKeyPressed(VK_RETURN) == true)
{
drop(nowplace);
display();
Sleep(200);
}
}
/*Play Music*/
void play_music()
{
//PlaySound(TEXT("videoplayback.wav"), NULL, SND_SYNC);
}
/*Starts Game*/
string user_name1, user_name;
void start_game()
{
resizeConsole(1070, 720);
system("cls");
place[1][1] = 15;
/*Empty Place Inside The Boxes*/
for (int a = 0; a <= 5; a++) {
for (int b = 0; b <= 6; b++)
place[a][b] = ' ';
}
/*Prints The Board*/
display();
/*Declaring Veriables*/
int hold2 = 0;
int charsPlaced = 0;
/*No Winner In The Beginning*/
bool gamewon = false;
while (!gamewon) {
/*Checks Error*/
while (true) {
/*If Draw*/
if (charsPlaced == 42) break;
/*Gets User Imput*/
keyimput();
}
/*If Draw*/
if (charsPlaced == 42) break;
/*Drop The Player Store Row In Hold2*/
hold2 = drop(nowplace);
/*Error If Row Is FULL*/
if (hold2 == -1) cout << "Colomn is full\n Choose diffrent place.";
else {
/*Check If Game Is Running*/
gamewon = check(hold2, nowplace);
/*Truly Placed*/
charsPlaced++;
system("cls");
/*Display Lastest Board*/
display();
}
}
system("cls");
/*If Draw*/
if (place[6][7] == 42) {
cout << "No winner, Game was draw\n";
system("pause");
}
/*If Won By Player 2*/
if (player == 15)
{
string Menu[2] = { "Yes", "No" };
int pointer = 0;
while (true)
{
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
gotoxy(66, 14);
cout << "Game Won by : " << user_name1 << "\n";
cout << "Do You Wanna Play More ?\n\n";
for (int i = 0; i < 2; ++i)
{
/*Color Blue When Pointer On It*/
if (i == pointer)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
cout << Menu[i] << endl;
}
/*Color White When Pointer Not On it*/
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
cout << Menu[i] << endl;
}
}
while (true)
{
/*Key Up*/
if (isKeyPressed(VK_UP) == true)
{
pointer -= 1;
if (pointer == -1)
{
pointer = 1;
}
break;
}
/*Key Down*/
else if (isKeyPressed(VK_DOWN) == true)
{
pointer += 1;
if (pointer == 2)
{
pointer = 0;
}
break;
}
/*Key Enter*/
else if (isKeyPressed(VK_RETURN) == true)
{
switch (pointer)
{
/*Play Again*/
case 0:
{
start_game();
} break;
/*Goes Back to Menu*/
case 1:
{
menu();
} break;
}
}
}
Sleep(150);
}
system("pause");
}
/*If Won By Player 1*/
else
{
string Menu[2] = { "Yes", "No" };
int pointer = 0;
while (true)
{
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
gotoxy(66, 14);
cout << "Game Won by : " << user_name << "\n";
cout << "Do You Wanna Play More ?\n\n";
for (int i = 0; i < 2; ++i)
{
/*Color Blue When Pointer On It*/
if (i == pointer)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
cout << Menu[i] << endl;
}
/*Color White When Pointer Not On it*/
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
cout << Menu[i] << endl;
}
}
while (true)
{
/*Key Up*/
if (isKeyPressed(VK_UP) == true)
{
pointer -= 1;
if (pointer == -1)
{
pointer = 1;
}
break;
}
/*Key Down*/
else if (isKeyPressed(VK_DOWN) == true)
{
pointer += 1;
if (pointer == 2)
{
pointer = 0;
}
break;
}
/*Key Enter*/
else if (isKeyPressed(VK_RETURN) == true)
{
switch (pointer)
{
/*Play Again*/
case 0:
{
start_game();
} break;
/*Returns the Menu*/
case 1:
{
menu();
} break;
}
}
}
Sleep(150);
}
system("pause");
}
system("pause");
}
/*Prints The Game Shape*/
void display()
{
system("cls");
textcolor(15);
gotoxy(0, 0);
cout << " 1 2 3 4 5 6 7\n\n";
/*Boxes With The Ascii*/
for (int a = 0; a <= 5; a++)
{
for (int b = 0; b <= 6; b++) cout << char(218) << char(196) << char(191) << " ";
cout << '\n';
for (int b = 0; b <= 6; b++) cout << char(179) << (char)place[a][b] << char(179) << " ";
cout << '\n';
for (int b = 0; b <= 6; b++) cout << char(192) << char(196) << char(217) << " ";
cout << '\n';
}
/*Players Turns*/
if (player == 15) {
cout << "Your turn " << user_name1;
/*Piece For The User 2*/
player = 254;
}
else {
cout << "Your turn " << user_name;
/*Piece For The User 1*/
player = 15;
}
gotoxy(3,24);
textcolor(14);
cout << "Last known place";
print_box(5, 3, 8, 26);
gotoxy(10, 27);
cout << nowplace;
}
/*Win/Lose Check*/
bool check(int a, int b)
{
int vertical = 1;
int horizontal = 1;
int diagonal1 = 1;
int diagonal2 = 1;
char player = place[a][b];
/*Vertical*/
int i;
/*Horizontal*/
int ii;
/*Vertical Check (|)*/
for (i = a + 1; place[i][b] == player && i <= 5; i++, vertical++);//Check Down
for (i = a - 1; place[i][b] == player && i >= 0; i--, vertical++);//Check UP
if (vertical >= 4)return true;
/*Horizontal Check (-)*/
for (ii = b - 1; place[a][ii] == player && ii >= 0; ii--, horizontal++);//Check Left
for (ii = b + 1; place[a][ii] == player && ii <= 6; ii++, horizontal++);//Check Right
if (horizontal >= 4) return true;
/*Diagonal Check 1 (/)*/
for (i = a - 1, ii = b - 1; place[i][ii] == player && i >= 0 && ii >= 0; diagonal1++, i--, ii--);//Up And Left
for (i = a + 1, ii = b + 1; place[i][ii] == player && i <= 5 && ii <= 6; diagonal1++, i++, ii++);//Down And Left
if (diagonal1 >= 4) return true;
/*Diagonal Check 2 (\)*/
for (i = a - 1, ii = b + 1; place[i][ii] == player && i >= 0 && ii <= 6; diagonal2++, i--, ii++);//Up And Right
for (i = a + 1, ii = b - 1; place[i][ii] == player && i <= 5 && ii >= 0; diagonal2++, i++, ii--);//Up And Left
if (diagonal2 >= 4) return true;
return false;
}
/*Drop Check*/
int drop(int b)
{
int count = 5;
while (place[count][nowplace - 1] != 32 && count > -1) count--;
if (count == -1) {
return -1;
}
else {
place[count][nowplace - 1] = player;
return player;
}
//return 0;
}
/*Prits welcome screen*/
void welcome_screen()
{
resizeConsole(1120, 720);
/*Middle Box*/
print_box(19, 5, 67, 18);
/*Head Line*/
gotoxy(73, 20);
print_string2("Welcome");
_getch();
username();
}
/*Prints menu menu*/
void menu()
{
resizeConsole(1120, 720);
/*Menu Options*/
string Menu[6] = { "Start Game", "How To Play?", "Score Board", "Choose Music", "Credits", "Exit" };
int pointer = 0;
while (true)
{
system("cls");
/*Default Color White*/
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
gotoxy(66, 15);
cout << "Main Menu";
for (int i = 0; i < 6; ++i)
{
if (i == pointer)
{
/*When Pointer On A Option It Will Change The Color To Blue*/
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
gotoxy(65, 19 + i * 2);
cout << Menu[i] << endl;
}
else
{
/*If Pointer No On The Option It Will Keep The Color As White*/
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
gotoxy(65, 19 + i * 2);
cout << Menu[i] << endl;
}
}
while (true)
{
/*Key Up*/
if (isKeyPressed(VK_UP) == true)
{
pointer -= 1;
if (pointer == -1)
{
pointer = 5;
}
break;
}
/*Key Down*/
else if (isKeyPressed(VK_DOWN) == true)
{
pointer += 1;
if (pointer == 6)
{
pointer = 0;
}
break;
}
/*Key Enter*/
else if (isKeyPressed(VK_RETURN) == true)
{
switch (pointer)
{
/*Starts Game*/
case 0:
{
start_game();
} break;
/*Instructions*/
case 1:
{
how_to_play();
} break;
/*Score Board*/
case 2:
{
score_board();
} break;
/*Music Menu*/
case 3:
{
music_menu();
} break;
/*Credits*/
case 4:
{
credits();
} break;
/*Exit Game*/
case 5:
{
exit_game();
} break;
}
}
}
/*Delay Time For Scrolling*/
Sleep(150);
}
system("pause");
}
/*Prints music menu*/
void music_menu()
{
resizeConsole(1120, 720);
/*Menu Options*/
string Menu[6] = { "Music 1", "Music 2", "Music 3", "Music 4", "Music 5", "Return Menu" };
int pointer = 0;
while (true)
{
system("cls");
/*Default Color White*/
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
gotoxy(66, 15);
cout << "Musics\n\n";
for (int i = 0; i < 6; ++i)
{
if (i == pointer)
{
/*When Pointer On A Option It Will Change The Color To Blue*/
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
gotoxy(65, 19 + i * 2);
cout << Menu[i] << endl;
}
else
{
/*If Pointer No On The Option It Will Keep The Color As White*/
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
gotoxy(65, 19 + i * 2);
cout << Menu[i] << endl;
}
}
while (true)
{
/*Key Up*/
if (isKeyPressed(VK_UP) == true)
{
pointer -= 1;
if (pointer == -1)
{
pointer = 5;
}
break;
}
/*Key Down*/
else if (isKeyPressed(VK_DOWN) == true)
{
pointer += 1;
if (pointer == 6)
{
pointer = 0;
}
break;
}
/*Key Enter*/
else if (isKeyPressed(VK_RETURN) == true)
{
switch (pointer)
{
/*Music 1*/
case 0:
{
} break;
/*music 2*/
case 1:
{
} break;
/*Music 3*/
case 2:
{
} break;
/*music 4*/
case 3:
{
} break;
/*Music 5*/
case 4:
{
} break;
/*Main Menu*/
case 5:
{
Sleep(200);
menu();
} break;
}
}
}
/*Delay Time For Scrolling*/
Sleep(150);
}
system("pause");
}
/*Prints username menu*/
void username()
{
resizeConsole(1120, 720);
system("cls");
/*Username 1*/
do {
/*Top Box*/
print_box(30, 5, 63, 13);
/*Ask Question*/
gotoxy(67, 15);
cout << "User 1 type your name: ";
/*Bottom Box*/
print_box(30, 5, 63, 21);
/*Asking Imput*/
gotoxy(67, 23);
cin >> user_name1;
if (user_name1.length() > 15) {
{
system("cls");
/*Top Box*/
print_box(52, 5, 51, 13);
/*Asks Question*/
gotoxy(55, 15);
cout << "Max. characther is 15." << " User 1 Type your name: ";
/*Bottom Box*/
print_box(30, 5, 63, 21);
/*Asking Imput*/
gotoxy(67, 23);
cin >> user_name;
}
}
}
/*Checks If User Name If Its Bigger Then 15*/
while (user_name1.length() > 15);
/*Username 2*/
do {
system("cls");
/*Top Box*/
print_box(30, 5, 63, 13);
/*Ask Question*/
gotoxy(67, 15);
cout << "User 2 type your name: ";
/*Bottom Box*/
print_box(30, 5, 63, 21);
/*Asking Imput*/
gotoxy(67, 23);
cin >> user_name;
if (user_name.length() > 15) {
{
system("cls");
/*Top Box*/
print_box(52, 5, 51, 13);
/*Asks Question*/
gotoxy(55, 15);
cout << "Max. characther is 15." << " User 2 Type your name: ";
/*Bottom Box*/
print_box(30, 5, 63, 21);
/*Asking Imput*/
gotoxy(67, 23);
cin >> user_name;
}
}
}
/*Checks If Username If Its Bigger Then 15*/
while (user_name.length() > 15);
_getch();
Sleep(500);
menu();
}
/*Prints how to play menu*/
void how_to_play()
{
resizeConsole(1120, 720);
system("cls");
/*Top Box*/
print_box(20, 5, 62, 14);
/*Headline*/
gotoxy(66, 16);
print_string3("How To Play?");
/*Bottom Box*/
print_box(41, 7, 52, 20);
/*Right Move Help*/
gotoxy(55, 22);
print_string("To Go Right Press -> (Right Arrow)");
/*Left Move Help*/
gotoxy(55, 23);
print_string("To Go Left Press <- (Left Arrow)");
/*Select Help*/
gotoxy(55, 24);
print_string("To Select Press OK (Enter)");
getchar();
}
/*Prints exit menu*/
void exit_game()
{
resizeConsole(1120, 720);
system("cls");
gotoxy(58, 16);
print_string3("Thanks for using our protype. Goodbye !!!");
Sleep(1000);
/*Force Shutdown*/
exit(EXIT_FAILURE);
_getch();
}
/*Prints scrore board menu*/
void score_board()
{
resizeConsole(1070, 720);
system("cls");
/*Top Box*/
print_box(24, 5, 61, 10);
/*Score Board*/
gotoxy(67, 12);
print_string3("Score Board");
/*Bottom Box*/
print_box(30, 25, 58, 16);
/*Names*/
gotoxy(63, 18);
print_string3("Name :");
/*Scores*/
gotoxy(78, 18);
print_string3("Wins :");
/*Vladimir Putin*/
gotoxy(61, 21);
print_string3("Vladimir Putin");
gotoxy(78, 21);
print_string3("1543");
/*Kim Jong-Un*/
gotoxy(61, 23);
print_string3("Kim Jong-Un");
gotoxy(78, 23);
print_string3("1354");
/*Donald Trump*/
gotoxy(61, 25);
print_string3("Donald Trump");
gotoxy(78, 25);
print_string3("914");
/*Justin Trudeau*/
gotoxy(61, 27);
print_string3("Justin Trudeau");
gotoxy(78, 27);
print_string3("543");
/*Mr.Lauder*/
gotoxy(61, 29);
print_string3("Mr.Lauder");
gotoxy(78, 29);
print_string3("343");
/*Noah*/
gotoxy(61, 31);
print_string3("Noah");
gotoxy(78, 31);
print_string3("213");
/*Long*/
gotoxy(61, 33);
print_string3("Long");
gotoxy(78, 33);
print_string3("163");
/*Caner*/
gotoxy(61, 35);
print_string3("Caner");
gotoxy(78, 35);
print_string3("69");
/*You*/
gotoxy(61, 37);
print_string3("You");
gotoxy(78, 37);
print_string3("0");
_getch();
}
/*Prints credits menu*/
void credits()
{
resizeConsole(1070, 720);
system("cls");
/*Top Box*/
print_box(20, 5, 62, 13);
/*Headline*/
gotoxy(68, 15);
print_string3("Credits :");
/*Bottom Box*/
print_box(15, 7, 64, 19);
/*Caner*/
gotoxy(69, 21);
print_string3("Caner");
/*Noah*/
gotoxy(69, 22);
print_string3("Noah");
/*Long*/
gotoxy(69, 23);
print_string3("Long");
_getch();
}
/*Main function*/
int main()
{
/*for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) cout << place[i][j]<<endl;
}
system("pause");*/
/*Thread 1 Starts Project*/
thread t1(&start_game);
t1.join();
/*Thread 2 Play Music*/
/*thread t2(&play_music);
t2.join();*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment