Luko Butkaus darbas
unknown
plain_text
3 years ago
47 kB
8
Indexable
#include <bits/stdc++.h>
#include <iostream>
#include <time.h>
#include <conio.h>
#include <windows.h>
using namespace std;
void Programos_meniu()
{
system("CLS");
system("COLOR 0A");
cout << ("-------------------------------------------\n");
cout << ("| PROGRAMAVO: |\n");
cout << ("| LUKAS BUTKUS KZA |\n");
cout << ("-------------------------------------------\n");
cout << ("*******************************************\n");
cout << ("* [0] - Informacija apie autoriu *\n");
cout << ("* [1] - Inicialai *\n");
cout << ("* [2] - Graza *\n");
cout << ("* [3] - Skaiciuotuvas *\n");
cout << ("* [4] - Slaptazodzio generatorius *\n");
cout << ("* [5] - Sugeneruoti slaptazodziai *\n");
cout << ("* [6] - Sugeneruotu slaptazodziu analize*\n");
cout << ("* [7] - Ping Pong *\n");
cout << ("* [8] - Flappy Bird *\n");
cout << ("* [9] - Pabaiga *\n");
cout << ("* [10] - Zaidimai *\n");
cout << ("*******************************************\n");
}
void Autorius()
{
system("CLS");
system("COLOR 0A");
ifstream fin("Autorius.txt");
string eilute;
while (!fin.eof())
{
getline(fin, eilute);
cout << eilute << endl;
}
cout << endl << "Noredami gryzti i pagrindini meniu paspauskie ENTER" << endl;
system ("pause");
fin.close();
}
void Inicialai()
{
system("CLS");
system("COLOR 0A");
cout << (" \n");
cout << (" ******** \n");
cout << (" ******** \n");
cout << (" ******** \n");
cout << (" ******** \n");
cout << (" ******** \n");
cout << (" ******** \n");
cout << (" ******** \n");
cout << (" ******** \n");
cout << (" ******** \n");
cout << (" ******** \n");
cout << (" ******** \n");
cout << (" ******** *************** \n");
cout << (" ********************* ****************** \n");
cout << (" ********************* ***** ***** \n");
cout << (" ********************* ***** ***** \n");
cout << (" ********************* ***** ***** \n");
cout << (" ***** ***** \n");
cout << (" ***** ***** \n");
cout << (" ***** ***** \n");
cout << (" *************** \n");
cout << (" *************** \n");
cout << (" ***** ***** \n");
cout << (" ***** ***** \n");
cout << (" ***** ***** \n");
cout << (" ***** ***** \n");
cout << (" ***** ***** \n");
cout << (" ***** ***** \n");
cout << (" ****************** \n");
cout << (" *************** \n");
system("pause");
}
void Graza ()
{
int g;
int k500, k200, k100, k50, k20, k10, k5;
system("CLS");
system("COLOR 0A");
cout <<"Iveskite graza \n";
cin >> g;
k500 = g / 500;
g = g % 500;
k200 = g / 200;
g = g % 200;
k100 = g / 100;
g = g % 100;
k50 = g / 50;
g = g % 50;
k20 = g / 20;
g = g % 20;
k10 = g / 10;
g = g % 10;
k5 = g / 5;
g = g % 5;
cout << "Graza: " << endl;
cout << "500 $ ----->" << k500 << endl;
cout << "200 $ ----->" << k200 << endl;
cout << "100 $ ----->" << k100 << endl;
cout << "50 $ ----->" << k50 << endl;
cout << "20 $ ----->" << k20 << endl;
cout << "10 $ ----->" << k10 << endl;
cout << "5 $ ----->" << k5 << endl;
}
void Skaiciuotuvas ()
{
system("CLS");
system("COLOR 0A");
int x1, x2;
char pabaiga = 'n';
char zenklas;
while (pabaiga != 't')
{
cout << "Iveskite 1 skaiciu: ";
cin >> x1;
cout << "Iveskite operacijos zenkla: ";
cin >> zenklas;
cout << "Iveskite 2 skaiciu: ";
cin >> x2;
if (zenklas == '+')
{
cout << x1 <<"+"<< x2 << "="<< x1 + x2 <<endl;
}
if (zenklas == '-')
{
cout << x1 <<"-"<< x2 << "="<< x1 - x2 <<endl;
}
if (zenklas == '*')
{
cout << x1 <<"*"<< x2 << "="<< x1 * x2 <<endl;
}
if (zenklas == '/')
{
cout << x1 <<"/"<< x2 << "="<< x1 / x2 <<endl;
}
if (zenklas == '^')
{
cout << x1 <<"^"<< x2 << "="<< pow(x1, x2) <<endl;
}
cout <<" \n Ar norite baigti (t/n):";
cin >> pabaiga;
cout <<'\n';
}
}
void Generatorius(int ilgis)
{
ofstream fout("Slaptazodziai.txt", ios::app);
string slaptazodis ="";
char simbolis;
srand (time (NULL));
while (slaptazodis.size()<ilgis)
{
simbolis=rand();
if (simbolis>='a' && simbolis<='z')
slaptazodis += simbolis;
}
fout << endl << slaptazodis;
cout <<"Sugeneruotas slaptazodis: ["<< slaptazodis <<"] irasytas i faila Slaptazodziai.txt" << endl;
fout.close();
}
void Skaitymas (vector <string> &S)
{
ifstream fin("Slaptazodziai.txt");
string eilute;
while (!fin.eof())
{
getline(fin, eilute);
S.push_back(eilute);
}
fin.close();
}
void Spauzdinimas(vector <string> S)
{
for (int i=0; i<S.size(); i++)
cout << S[i] << endl;
}
bool palyginti (string a, string b)
{
return a.size () < b.size ();
}
int Tumpiausias(int A[], int n)
{
int vieta = 0;
for (int i = 1; i < n; i++)
if (A[i] < A[vieta])
vieta = i;
return A[vieta];
}
int Ilgiausias(int A[], int n)
{
int vieta = 0;
for (int i = 1; i < n; i++)
if (A[i] > A[vieta])
vieta = i;
return A[vieta];
}
int Suma(int A[], int n)
{
int s=0;
for (int i=0; i<n; i++)
s += A[1];
return s;
}
int Kiekis (char x, string Slaptazodis)
{
int k = 0;
for (int i=0; i<Slaptazodis.size(); i++)
{
if (Slaptazodis[i]== x) k++;
}
return k;
}
int Suma (char x, vector <string> Slaptazodis)
{
int s = 0;
for (int i=0; i<Slaptazodis.size(); i++)
{
s += Kiekis (x, Slaptazodis[i]);
}
return s;
}
double Vidurkis (vector <string> Slaptazodis)
{
double s = 0;
for (int i=0; i<Slaptazodis.size(); i++)
{
s += Slaptazodis[i].size();
}
return s/Slaptazodis.size();
}
using namespace std;
enum eDir { STOP = 0, LEFT = 1, UPLEFT = 2, DOWNLEFT = 3, RIGHT = 4, UPRIGHT = 5, DOWNRIGHT = 6 };
class cBall
{
private:
int x, y;
int originalX, originalY;
eDir direction;
public:
cBall(int posX, int posY)
{
originalX = posX;
originalY = posY;
x = posX;
y = posY;
direction = STOP;
}
void Reset()
{
x = originalX;
y = originalY;
direction = STOP;
}
void changeDirection(eDir d)
{
direction = d;
}
void randomDirection()
{
direction = (eDir)((rand() % 6) + 1);
}
inline int getX()
{
return x;
}
inline int getY()
{
return y;
}
inline eDir getDirection()
{
return direction;
}
void Move()
{
switch (direction)
{
case STOP:
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UPLEFT:
x--;
y--;
break;
case DOWNLEFT:
x--;
y++;
break;
case UPRIGHT:
x++;
y--;
break;
case DOWNRIGHT:
x++;
y++;
break;
default:
break;
}
}
friend ostream& operator<<(ostream& o, cBall c)
{
o << "Ball [" << c.x << "," << c.y << "][" << c.direction << "]";
return o;
}
};
class cPaddle
{
private:
int x, y;
int originalX, originalY;
public:
cPaddle()
{
x = y = 0;
}
cPaddle(int posX, int posY) : cPaddle()
{
originalX = posX;
originalY = posY;
x = posX;
y = posY;
}
inline void Reset()
{
x = originalX;
y = originalY;
}
inline int getX()
{
return x;
}
inline int getY()
{
return y;
}
inline void moveUp()
{
y--;
}
inline void moveDown()
{
y++;
}
friend ostream& operator<<(ostream& o, cPaddle c)
{
o << "Paddle [" << c.x << "," << c.y << "]";
return o;
}
};
class cGameManger
{
private:
int width, height;
int score1, score2;
char up1, down1, up2, down2;
bool quit;
cBall* ball;
cPaddle* player1;
cPaddle* player2;
public:
cGameManger(int w, int h)
{
srand(time(NULL));
quit = false;
up1 = 'w';
up2 = 'i';
down1 = 's';
down2 = 'k';
score1 = score2 = 0;
width = w;
height = h;
ball = new cBall(w / 2, h / 2);
player1 = new cPaddle(1, h / 2 - 3);
player2 = new cPaddle(w - 2, h / 2 - 3);
}
~cGameManger()
{
delete ball, player1, player2;
}
void ScoreUp(cPaddle* player)
{
if (player == player1)
score1++;
else if (player == player2)
score2++;
ball->Reset();
player1->Reset();
player2->Reset();
}
void Draw()
{
//system("cls");
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { 0,0 });
for (int i = 0; i < width + 2; i++)
cout << "\xB2";
cout << endl;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int ballx = ball->getX();
int bally = ball->getY();
int player1x = player1->getX();
int player2x = player2->getX();
int player1y = player1->getY();
int player2y = player2->getY();
if (j == 0)
cout << "\xB2";
if (ballx == j && bally == i)
cout << "O"; //kamuolys-ball
else if (player1x == j && player1y == i)
cout << "\xDB"; //zaidejas1
else if (player2x == j && player2y == i)
cout << "\xDB"; //zaidejas2
else if (player1x == j && player1y + 1 == i)
cout << "\xDB"; //zaidejas1
else if (player1x == j && player1y + 2 == i)
cout << "\xDB"; //zaidejas1
else if (player1x == j && player1y + 3 == i)
cout << "\xDB"; //zaidejas1
else if (player2x == j && player2y + 1 == i)
cout << "\xDB"; //zaidejas1
else if (player2x == j && player2y + 2 == i)
cout << "\xDB"; //zaidejas1
else if (player2x == j && player2y + 3 == i)
cout << "\xDB"; //zaidejas1
else
cout << " ";
if (j == width - 1)
cout << "\xB2";
}
cout << endl;
}
for (int i = 0; i < width + 2; i++)
cout << "\xB2";
cout << endl;
cout << "Score 1: " << score1 << endl << "Score 2: " << score2 << endl;
}
void Input()
{
ball->Move();
int ballx = ball->getX();
int bally = ball->getY();
int player1x = player1->getX();
int player2x = player2->getX();
int player1y = player1->getY();
int player2y = player2->getY();
if (_kbhit())
{
char current = _getch();
if (current == up1)
if (player1y > 0)
player1->moveUp();
if (current == up2)
if (player2y > 0)
player2->moveUp();
if (current == down1)
if (player1y + 4 < height)
player1->moveDown();
if (current == down2)
if (player2y + 4 < height)
player2->moveDown();
if (ball->getDirection() == STOP)
ball->randomDirection();
if (current == 'q')
quit = true;
}
}
void Logic()
{
int ballx = ball->getX();
int bally = ball->getY();
int player1x = player1->getX();
int player2x = player2->getX();
int player1y = player1->getY();
int player2y = player2->getY();
//kaires puses valdymas
for (int i = 0; i < 4; i++)
if (ballx == player1x + 1)
if (bally == player1y + i)
ball->changeDirection((eDir)((rand() % 3) + 4));
//desines puses valdymas
for (int i = 0; i < 4; i++)
if (ballx == player2x - 1)
if (bally == player2y + i)
ball->changeDirection((eDir)((rand() % 3) + 1));
//apatine siena
if (bally == height - 1)
ball->changeDirection(ball->getDirection() == DOWNRIGHT ? UPRIGHT : UPLEFT);
//virsutine siena
if (bally == 0)
ball->changeDirection(ball->getDirection() == UPRIGHT ? DOWNRIGHT : DOWNLEFT);
//desine siena
if (ballx == width - 1)
ScoreUp(player1);
//kaire siena
if (ballx == 0)
ScoreUp(player2);
}
void Run()
{
while (!quit)
{
Draw();
Input();
Logic();
Sleep(50);
}
}
};
int main()
{
setlocale(LC_ALL, "English");
int m;
meniu:
Programos_meniu();
cout<< "Pasirinkite programos meniu numeri: \n";
cin>> m;
if (m==0)
{
Autorius();
goto meniu;
}
if (m==1)
{
Inicialai ();
4;
goto meniu;
}
if (m==2)
{
Graza();
system("pause");
goto meniu;
}
if (m==3)
{
Skaiciuotuvas ();
goto meniu;
}
if (m==4)
{
system("CLS");
int ilgis;
cout <<"Iveskite generuojamo slaptazodzio ilgi: "<<endl;
cin >> ilgis;
Generatorius (ilgis);
system ("pause");
goto meniu;
}
if (m==5)
{
system("CLS");
vector <string> S;
Skaitymas (S);
sort(S.begin(), S.end(), palyginti);
Spauzdinimas(S);
system("pause");
goto meniu;
}
if (m==6)
{
system("CLS");
vector <string> S;
Skaitymas (S);
sort(S.begin(), S.end(), palyginti);
cout<<"ANALIZE"<< endl;
cout<<" Slaptazodziu kiekis: "<< S.size() <<endl;
cout<<" Trumpiausias slaptazodis: "<< S[0] <<" | ilgis - " << S[0].size()<< endl;
int paskutinis = S.size()-1;
cout<<" Ilgiausias slaptazodis: "<< S[paskutinis] <<" | ilgis - " << S[paskutinis].size()<< endl;
cout<<" Vidutinis slaptazodis: "<< " | ilgis - " << Vidurkis(S) << endl;
Kiekis ('b',"ABCDabcd12345");
cout<<" Slaptazodziuose yra raidziu L: " << Suma ('L', S) << endl;
system("pause");
goto meniu;
}
if (m==7)
{
system("COLOR 0F");
cGameManger c(40, 20);
c.Run();
Sleep(100);
return 0;
goto meniu;
}
if (m==8)
{
system("COLOR 0F");
{
srand(time(0));//kad randomizintu
char mv;
char repeat='y';
int highscore=0;
while (repeat=='y')
{
int score=0;
bool wasit=true;
int x=9;
int rintangan[3]; // y - asies kliutis
int rintang[3]; // x - asies kliutis
rintang[0]=10;
rintang[1]=18;
rintang[2]=27; // pradedancioji x asies kliutis
for (int i=0; i<3; i++)
{
rintangan[i]=(rand()-1)%12+2;
}
string map[20][30];
for (int i=0; i<20; i++)
{
for (int j=0; j<30; j++)
{
if ( i==0 || i==19|| j==29 || j==0)
map[i][j]="* ";
else
map[i][j]=" ";
}
}
while (wasit == true)
{
for (int i =0; i<3; i++)
{
for (int j=1; j<19; j++)
{
map[j][rintang[i]]=" ";
}
}
for (int i=0; i<3; i++) //kad kliutis judeti i kaire, desine
{
rintang[i]--;
}
for (int i =0; i<3; i++) //paverskite kliūtį atgal į dešinę pusę, jei ji palietė kairę pusę
{
for (int j=1; j<19; j++)
{
map[j][rintang[i]]="* ";
}
}
for (int i=0; i<3; i++) //kad kliutyje padarytu skyle
{
map[rintangan[i]][rintang[i]]=" ";
map[rintangan[i]+1][rintang[i]]=" ";
map[rintangan[i]+2][rintang[i]]=" ";
map[rintangan[i]+3][rintang[i]]=" ";
map[rintangan[i]+4][rintang[i]]=" ";
}
for (int i=0; i<3; i++)
{
if (rintang[i]==1)
{
rintang[i]=28;
rintangan[i]=(rand()-1)%12+2;
}
}
map[x][3]=" ";
if (kbhit())
{
mv=getch();
x=x-3;
}
x++;
if(x<1)
x=1;
for (int i=0; i<3; i++)
{
if (rintang[i]==3)
if (map[x][3]==" ")
score=score+1;
if (map[x][3]=="* ")
wasit=false;
}
map[x][3]="& ";
for (int i=1; i<19; i++)
{
map[i][1]=" ";
}
for (int i=0; i<20; i++)
{
for (int j=0; j<30; j++)
{
cout << map[i][j];
}
cout <<endl;
}
cout << "Score = "<<score;
Sleep (100);
system ("cls");
}
cout << "Score = " <<score<<endl;
if (highscore<score)
highscore=score;
cout << "Highscore= "<<highscore<<endl;
cout << "retry?(y/n) = ";
cin >>repeat;
}
goto meniu;
return 0;
}
}
if (m==9)
{
system("CLS");
return 0;
}
if (m==10)
system("CLS");
cout << "Welcome to our game library!" << endl;
cout << "Please select a game from the following options:" << endl;
cout << "1. Flappy Bird" << endl;
cout << "2. Ping Pong" << endl;
cout << "3. Snake Game" << endl;
// User input
int selection;
cin >> selection;
// Game selection
if (selection == 1)
{
// Flappy Bird
system("CLS");
int m;
back:
cout << "You have selected Flappy Bird." << endl;
cout << "Please select an option:" << endl;
cout << "1. Buy the game" << endl;
cout << "2. Try the game" << endl;
// User input
int option;
cin >> option;
// Option selection
if (option == 1)
{
int m;
prices1:
// Buy the game
system("CLS");
// vector of prices
vector<double> prices = {1.99, 0.99, 0.89};
// sort the prices in ascending order
sort(prices.begin(), prices.end());
cout << "You have selected to buy Flappy Bird." << endl;
cout << "Please select a store from the following options:" << endl;
cout << "1. Store 1 (price: $" << prices[0] << ")" << endl;
cout << "2. Store 2 (price: $" << prices[1] << ")" << endl;
cout << "3. Store 3 (price: $" << prices[2] << ")" << endl;
cout << "4. Find cheapest price" << endl;
// User input
int store;
cin >> store;
// Store selection
{
system("CLS");
if (store == 1)
{
cout << "You have purchased Flappy Bird from Store 1 for $1.99. Thank you for your purchase!" << endl;
}
else if (store == 2)
{
cout << "You have purchased Flappy Bird from Store 2 for $0.99. Thank you for your purchase!" << endl;
}
else if (store == 3)
{
cout << "You have purchased Flappy Bird from Store 3 for $0.89. Thank you for your purchase!" << endl;
}
if (store == 4)
{
// print the cheapest price
cout << "The cheapest price is $" << prices[0] << endl;
system ("pause");
goto prices1;
}
else
{
cout << "Invalid store selection. Please try again." << endl;
}
system ("pause");
goto meniu;
}
}
else if (option == 2)
{
// Try the game
//cout << "You have selected to try Flappy Bird. Enjoy the game!" << endl;
{
system("COLOR 0F");
{
srand(time(0));//kad randomizintu
char mv;
char repeat='y';
int highscore=0;
while (repeat=='y')
{
int score=0;
bool wasit=true;
int x=9;
int rintangan[3]; // y - asies kliutis
int rintang[3]; // x - asies kliutis
rintang[0]=10;
rintang[1]=18;
rintang[2]=27; // pradedancioji x asies kliutis
for (int i=0; i<3; i++)
{
rintangan[i]=(rand()-1)%12+2;
}
string map[20][30];
for (int i=0; i<20; i++)
{
for (int j=0; j<30; j++)
{
if ( i==0 || i==19|| j==29 || j==0)
map[i][j]="* ";
else
map[i][j]=" ";
}
}
while (wasit == true)
{
for (int i =0; i<3; i++)
{
for (int j=1; j<19; j++)
{
map[j][rintang[i]]=" ";
}
}
for (int i=0; i<3; i++) //kad kliutis judeti i kaire, desine
{
rintang[i]--;
}
for (int i =0; i<3; i++) //paverskite kliūtį atgal į dešinę pusę, jei ji palietė kairę pusę
{
for (int j=1; j<19; j++)
{
map[j][rintang[i]]="* ";
}
}
for (int i=0; i<3; i++) //kad kliutyje padarytu skyle
{
map[rintangan[i]][rintang[i]]=" ";
map[rintangan[i]+1][rintang[i]]=" ";
map[rintangan[i]+2][rintang[i]]=" ";
map[rintangan[i]+3][rintang[i]]=" ";
map[rintangan[i]+4][rintang[i]]=" ";
}
for (int i=0; i<3; i++)
{
if (rintang[i]==1)
{
rintang[i]=28;
rintangan[i]=(rand()-1)%12+2;
}
}
map[x][3]=" ";
if (kbhit())
{
mv=getch();
x=x-3;
}
x++;
if(x<1)
x=1;
for (int i=0; i<3; i++)
{
if (rintang[i]==3)
if (map[x][3]==" ")
score=score+1;
if (map[x][3]=="* ")
wasit=false;
}
map[x][3]="& ";
for (int i=1; i<19; i++)
{
map[i][1]=" ";
}
for (int i=0; i<20; i++)
{
for (int j=0; j<30; j++)
{
cout << map[i][j];
}
cout <<endl;
}
cout << "Score = "<<score;
Sleep (100);
//system ("cls");
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {0,0});
}
cout << "Score = " <<score<<endl;
if (highscore<score)
highscore=score;
cout << "Highscore= "<<highscore<<endl;
cout << "retry?(y/n) = ";
cin >>repeat;
}
system("COLOR 0A");
system("CLS");
goto back;
return 0;
}
}
}
goto meniu;
return 0;
}
else if (selection == 2)
{
// Ping Pong
system("CLS");
cout << "You have selected Ping Pong." << endl;
cout << "Please select an option:" << endl;
cout << "1. Buy the game" << endl;
cout << "2. Try the game" << endl;
// User input
int option;
cin >> option;
// Option selection
if (option == 1)
{
int m;
prices:
// Buy the game
system("CLS");
// vector of prices
vector<double> prices = {1.99, 0.99, 0.89};
// sort the prices in ascending order
sort(prices.begin(), prices.end());
cout << "You have selected to buy Ping Pong." << endl;
cout << "Please select a store from the following options:" << endl;
cout << "1. Store 1 (price: $" << prices[0] << ")" << endl;
cout << "2. Store 2 (price: $" << prices[1] << ")" << endl;
cout << "3. Store 3 (price: $" << prices[2] << ")" << endl;
cout << "4. Find cheapest price" << endl;
// User input
int store;
cin >> store;
// Store selection
{
system("CLS");
if (store == 1)
{
cout << "You have purchased Ping Pong from Store 1 for $1.99. Thank you for your purchase!" << endl;
}
else if (store == 2)
{
cout << "You have purchased Ping Pong from Store 2 for $0.99. Thank you for your purchase!" << endl;
}
else if (store == 3)
{
cout << "You have purchased Ping Pong from Store 3 for $0.89. Thank you for your purchase!" << endl;
}
if (store == 4)
{
// print the cheapest price
cout << "The cheapest price is $" << prices[0] << endl;
system ("pause");
goto prices;
}
else
{
cout << "Invalid store selection. Please try again." << endl;
}
system ("pause");
goto meniu;
}
}
else if (option == 2)
{
{
srand(time(NULL));
int papanmin1,papanmax1,papanmin2,papanmax2;
papanmin1=9;
papanmax1=11;
papanmin2=9;
papanmax2=11;
char key;
char key1;
int x,y;
x=18;
y=10;
int direction=(rand()%4)+1;
int pantulankanan=0;
int pantulankiri=0;
while(true)
{
if(x==34&&y==2)
{
if(direction==1) direction=3;
}
else if(x==34&&y==19)
{
if(direction==2) direction=4;
}
else if(x==2&&y==2)
{
if(direction==4) direction=2;
}
else if(x==2&&y==19)
{
if(direction==4) direction=2;
}
else if(x==5&&y==papanmax1)
{
if(direction==3) direction=1;
}
else if(x==5&&y==papanmin1)
{
if(direction==4) direction=2;
}
else if(x==31&&y==papanmin2)
{
if(direction==1) direction=3;
}
else if(x==31&&y==papanmax2)
{
if(direction==2) direction=4;
}
if(direction==2)
{
x++;
y++;
}
else if(direction==1)
{
x++;
y--;
}
else if(direction==3)
{
x--;
y++;
}
else if(direction==4)
{
x--;
y--;
}
//papan kiri
if(kbhit())
{
key=getch();
}
if(papanmin1>=2&&papanmax1<=19)
{
if(papanmin1-1!=1)
{
if(key=='w'||key=='W')
{
papanmin1-=1;
papanmax1-=1;
}
}
if(papanmax1+1!=20)
{
if(key=='s'||key=='S')
{
papanmin1+=1;
papanmax1+=1;
}
}
}
if(papanmin2>=2&&papanmax2<=19)
{
if(papanmin2-1!=1)
{
if(key=='i'||key=='I')
{
papanmin2-=1;
papanmax2-=1;
}
}
if(papanmax2+1!=20)
{
if(key=='k'||key=='K')
{
papanmin2+=1;
papanmax2+=1;
}
}
}
for(int i=1; i<=20; i++)
{
for(int j=1; j<=35; j++)
{
if(i==1||i==20||j==1||j==35) cout << "*";
else if(j==4 && (i>=papanmin1&&i<=papanmax1)) cout << ".";
else if(j==32 && (i>=papanmin2&&i<=papanmax2)) cout << ".";
else if(j==x&&i==y) cout << "o";
else cout << " ";
}
cout <<endl;
}
cout << "Point Player 1: " << pantulankanan <<endl;
cout << "Point Player 2: " << pantulankiri <<endl;
if(x==34&&y>=3&&y<=18)
{
if(direction==2)
{
direction=3;
pantulankanan++;
}
else
{
direction=4;
pantulankanan++;
}
}
else if(y==19&&x>=3&&x<=33)
{
if(direction==3)
{
direction=4;
}
else
{
direction=1;
}
}
else if(y==2&&x>=3&&x<=33)
{
if(direction==1)
{
direction=2;
}
else
{
direction=3;
}
}
else if((y==papanmin1)&&x==5||(y==papanmin1+1)&&x==5||(y==papanmax1)&&x==5)
{
if(direction==4)
{
direction=1;
}
else
{
direction=2;
}
}
else if(x==2&&y>=3&&y<=18)
{
if(direction==4)
{
direction=1;
pantulankiri++;
}
else
{
direction=2;
pantulankiri++;
}
}
else if((y==papanmin2)&&x==31||(y==papanmin2+1)&&x==31||(y==papanmax2)&&x==31)
{
if(direction==1)
{
direction=4;
}
else
{
direction=3;
}
}
if(pantulankiri==5)
{
system("cls");
cout << "Player 1 Won!!";
return 0;
}
if(pantulankanan==5)
{
system("cls");
cout << "Player 2 Won!!";
return 0;
}
Sleep(100);
system("cls");
}
return 0;
}
}
}
else if (selection == 3)
{
// Snake Game
system("CLS");
cout << "You have selected Snake Game." << endl;
cout << "Please select an option:" << endl;
cout << "1. Buy the game" << endl;
cout << "2. Try the game" << endl;
// User input
int option;
cin >> option;
// Option selection
if (option == 1)
{
int m;
prices2:
// Buy the game
system("CLS");
// vector of prices
vector<double> prices = {1.99, 0.99, 0.89};
// sort the prices in ascending order
sort(prices.begin(), prices.end());
cout << "You have selected to buy Snake Game." << endl;
cout << "Please select a store from the following options:" << endl;
cout << "1. Store 1 (price: $" << prices[0] << ")" << endl;
cout << "2. Store 2 (price: $" << prices[1] << ")" << endl;
cout << "3. Store 3 (price: $" << prices[2] << ")" << endl;
cout << "4. Find cheapest price" << endl;
// User input
int store;
cin >> store;
// Store selection
{
system("CLS");
if (store == 1)
{
cout << "You have purchased Snake Game from Store 1 for $1.99. Thank you for your purchase!" << endl;
}
else if (store == 2)
{
cout << "You have purchased Snake Game from Store 2 for $0.99. Thank you for your purchase!" << endl;
}
else if (store == 3)
{
cout << "You have purchased Snake Game from Store 3 for $0.89. Thank you for your purchase!" << endl;
}
if (store == 4)
{
// print the cheapest price
cout << "The cheapest price is $" << prices[0] << endl;
system ("pause");
goto prices2;
}
else
{
cout << "Invalid store selection. Please try again." << endl;
}
system ("pause");
goto meniu;
}
}
else if (option == 2)
{
{
int score,highscore;
char retry='y';
cout <<endl;
cout <<endl;
cout <<" ***** * * *** * * ***** \n";
cout <<" * ** * * * * * * \n";
cout <<" ***** * * * ***** ** *** \n";
cout <<" * * ** * * * * * \n";
cout <<" ***** * * * * * * ****** \n";
cout <<endl;
cout <<" Press Any Key to Continue"<<endl;
getch();
while (retry=='y' || retry == 'Y')
{
srand(time(NULL));
score=0;
int pion=(rand()-1)%18+1;//random letak awal makanan
int poin=(rand()-1)%18+1;
int x[100];//asumsikan panjang snake tidak lebih dari 100
int y[100];
x[0]=10;//declare letak awal snake
y[0]=10;
x[1]=9;
y[1]=10;
x[2]=8;
y[2]=10;
int ular=3 ;
string a[20][20];//lebar map
char ctr='d';//gerakan awal snake
while (x[0]!=0 && x[0]!=19 && y[0]!=0 && y[0]!=19)
{
for (int i=0; i<20; i++)
{
for (int j=0; j<20; j++)
{
if (i==0 || j==0 || i==19 || j==19)
a[i][j]="* ";//mencetak batas pinggir
else
a[i][j]=" ";//mencetak jalan
}
}
a[poin][pion]="o "; //mencetak makanan
for(int s=ular; s>0; s--)
{
x[s]=x[s-1];//agar badan dan ekor mengikuti kepala snake
y[s]=y[s-1];
}
for (int r=0; r<=ular; r++)
{
a[y[r]][x[r]]="* ";
}
if (kbhit())//meminta inputan w,a,s,d untuk gerak snake
{
ctr=getch();
}
if(ctr=='w')
y[0]--;
if(ctr=='s')
y[0]++;
if(ctr=='a')
x[0]--;
if(ctr=='d')
x[0]++;
for(int m=0; m<20; m++)
{
for (int n=0; n<20; n++)
{
cout << a[m][n]; //menampilkan game
}
cout <<endl;
}
if (a[0][0]==a[poin][pion])
{
ular++;
poin=rand()%19;
if (poin%19==0)
poin++;
pion=rand()%19;
if (pion%19==0)
pion++;
score=score+1;
}
for (int gh=1; gh<ular; gh++) //agar snake mati saat menabrak dirinya
{
if (x[0]==x[gh] && y[0]==y[gh] )
{
x[0]=0;
}
}
Sleep(60);
//system("cls");
//KOMANDA NUO TRIGDZIU===================================================
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {0,0});
}
cout << "GAMEOVER!" <<endl;
cout << "Score = " << score<<endl;
if (score > highscore)
{
highscore=score;
}
cout << "High Score = "<<highscore<<endl;
cout << "retry (y/n)";
cin >>retry;
}
return 0;
}
}
}
}
Editor is loading...