I need to program a sign in with password function for my 2048 program in C++. T
ID: 3575152 • Letter: I
Question
I need to program a sign in with password function for my 2048 program in C++. The program works I just need that one function. Here is my source code...
#include<iostream>
#include<ctime>
#include<unistd.h>
#include<cstdlib>
#include<cstdio>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
using namespace std;
int press_enter;
int random_index(int);
class Game;
class Game_AI
{
int answer;
int fail;
char control;
public:
int max;
int win;
int plus;
int score;
int grid[4][4];
int bgrid[4][4];
Game_AI(): score(0),plus(0),win(2048),max(0),answer(0),fail(1) {}
void logic(Game*);
void game_end(Game*);
void key_press();
void start_grid();
void refresh_grid();
void fill_space();
void generate();
void find_highest_number();
void backup_grid();
void goback();
int full();
int block_moves();
};
class Game : public Game_AI
{
char option;
string name;
public:
void display_grid();
void help_screen();
void win_screen();
void loser_screen();
char try_again_screen(int);
};
void Game_AI::key_press() //Input without pressing enter key
{
system("stty raw");
cin>>control;
system("stty cooked");
}
void Game_AI::logic(Game *execute)
{
switch(control) //Intitialize player controls
{
case 'w':
case 'a':
case 's':
case 'd':
execute->backup_grid();
execute->fill_space();
execute->refresh_grid();
execute->fill_space();
execute->find_highest_number();
execute->display_grid();
usleep(1000*160);
if(execute->full()&&fail)
{
answer=-1;
break;
}
else if(execute->block_moves())
{
execute->generate();
break;
}
else
{
answer=0;
break;
}
case 'g':
if(execute->block_moves())
score-=plus;
execute->goback(); //Resume to previous state.
break;
case 'x':
execute->start_grid(); //Clear grid, go back to start screen
score=0;
plus=0;
break;
case 'y':
execute->help_screen(); //Temporary leave game to help screen
press_enter=getchar();
execute->display_grid();
break;
case 'z':
answer=-1; //Quit game completely
break;
}
}
void Game_AI::start_grid() //Display start grid, number generation is random
{
int i,j;
plus=0; //Stats all set at zero
score=0;
max=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
grid[i][j]=0;
i=random_index(4);
j=random_index(4);
grid[i][j]=2;
i=random_index(4);
j=random_index(4);
grid[i][j]=2;
}
void Game::display_grid() //Display the grid.
{
system("clear");
cout<<" ::[ 2048 ]:: ";
if(plus)
cout<<"+"<<plus<<"!";
else
cout<<" ";
cout<<" SCORE::"<<score<<" ";
for(int i=0;i<4;i++)
{
cout<<" |";
for(int j=0;j<4;j++)
{
if(grid[i][j])
printf("%4d |",grid[i][j]);
else
printf("%4c |",' ');
}
cout<<endl<<endl;
}
cout<<" Controls (+ :: -) g- goback x - restart W ^ y - help z - quit "
<<" A S D < v > ."
<<" ";
}
int random_index(int x)
{
int index;
index=rand()%x+0;// this function chooses 0,1, or 2 randomly
return index; // assigns key in that index to input variable
}
void Game_AI::backup_grid()
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
bgrid[i][j]=grid[i][j];
}
void Game_AI::fill_space() //User input to direct tiles.
{
switch(control)
{
case 'w': // Case of Moving UP
for(int i=0;i<4;i++) // move from bottom to top of a column
for(int j=0;j<4;j++)
{
if(!grid[j][i]) // If tile is empty
{
for(int k=j+1;k<4;k++) // Move below to find a non-zero tile
if(grid[k][i])
{
grid[j][i]=grid[k][i]; // Move the non-zero element to the empty tile
grid[k][i]=0; // Assign the non-zero element with zero
break;
}
}
}break;
case 's': // Case of Moving DOWN
for(int i=0;i<4;i++) // move from top to bottom of a column
for(int j=3;j>=0;j--)
{
if(!grid[j][i]) // If tile is empty
{
for(int k=j-1;k>=0;k--) // Move above to find a nonzero tile
if(grid[k][i])
{
grid[j][i]=grid[k][i]; // Move the non-zero tile to the empty tile
grid[k][i]=0; // Assign the non-zero element with zero
break;
}
}
}break;
case 'a': // Case of Moving left
for(int i=0;i<4;i++) // move from right to left of a row
for(int j=0;j<4;j++)
{
if(!grid[i][j]) // If tile is empty
{
for(int k=j+1;k<4;k++) // Move right to find a non-zero tile
if(grid[i][k])
{
grid[i][j]=grid[i][k]; // Move the non-zero tile to the empty tile
grid[i][k]=0; // Assign the non-zero element with zero
break;
}
}
}break;
case 'd': // Case of Moving right
for(int i=0;i<4;i++) // move from left to right of a row
for(int j=3;j>=0;j--)
{
if(!grid[i][j]) // If tile is empty
{
for(int k=j-1;k>=0;k--) // Move left to find a non-zero tile
if(grid[i][k])
{
grid[i][j]=grid[i][k]; // Move the non-zero tile to the empty tile
grid[i][k]=0; // Assign the non-zero element with zero
break;
}
}
}break;
}
}
void Game_AI::game_end (Game *screen) //Check for game over.
{
if(max==win) //The game is won, 2048 has been acheived.
{
screen->win_screen();
if(screen->try_again_screen(0)=='n')
answer=-1;
else
win*=2;
}
else if(answer==-1) //Grid is full and a new tile cannot be created.
{
screen->loser_screen();
if(screen->try_again_screen(1)=='y')
{
screen->start_grid();
answer=0;
}
}
if(answer==-1)
{
cout<<" > Thanks for playing. "
<<" ";
press_enter=getchar();
exit(0);
}
}
void Game_AI::refresh_grid() // Update tiles on the grid.
{
plus=0;
fail=1;
switch(control)
{
case 'w':
for(int i=0;i<4;i++)
for(int j=0;j<3;j++)
{
// When moving UP
if(grid[j][i]&&grid[j][i]==grid[j+1][i]) // Check Tile is non zero and adjacent tile is equal
{
grid[j][i]+=grid[j+1][i]; //Sum or double the tile if it is equal.
grid[j+1][i]=0; // assign second tile with zero
plus+=(((log2(grid[j][i]))-1)*grid[j][i]);
score+=(((log2(grid[j][i]))-1)*grid[j][i]);
}
}break;
case 's':
for(int i=0;i<4;i++)
for(int j=3;j>0;j--)
{
// When moving DOWN
if(grid[j][i]&&grid[j][i]==grid[j-1][i]) // Check Tile is non zero and adjacent tile is equal
{
grid[j][i]+=grid[j-1][i]; // Sum or double the tile if it is equal
grid[j-1][i]=0; // assign second tile with zero
plus+=(((log2(grid[j][i]))-1)*grid[j][i]);
score+=(((log2(grid[j][i]))-1)*grid[j][i]);
}
}break;
case 'a':
for(int i=0;i<4;i++)
for(int j=0;j<3;j++)
{
// When moving LEFT
if(grid[i][j]&&grid[i][j]==grid[i][j+1]) // Check Tile is non zero and adjacent tile is equal
{
grid[i][j]+=grid[i][j+1]; // Sum or double the tile if it is equal
grid[i][j+1]=0; // assign second tile with zero
plus+=((log2(grid[i][j]))-1)*grid[i][j];
score+=((log2(grid[i][j]))-1)*grid[i][j];
}
}break;
case 'd':
for(int i=0;i<4;i++)
for(int j=3;j>0;j--)
{
// When moving RIGHT
if(grid[i][j]&&grid[i][j]==grid[i][j-1]) // Check Tile is non zero and adjacent tile is equal
{
grid[i][j]+=grid[i][j-1]; // Sum or double the tile if it is equal
grid[i][j-1]=0; // assign second tile with zero
plus+=((log2(grid[i][j]))-1)*grid[i][j];
score+=(((log2(grid[i][j]))-1)*grid[i][j]);
}
}break;
}
}
void Game_AI::generate() //Create new tile
{
int i,j,k;
do //Check if the current grid is equal to the backup grid
{
i=random_index(4);
j=random_index(4);
k=random_index(10);
}while(grid[i][j]);
if(k<2)
grid[i][j]=4;
else
grid[i][j]=2;
}
void Game_AI::find_highest_number() //Initialize greatest number on tile
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(grid[i][j]>max)
max=grid[i][j];
}
int Game_AI::full()
{
int k=1;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(!grid[i][j])
k=0;
}
return k;
}
int main(){
Game exec;
srand(time(NULL));
exec.start_grid();
while(1)
{
exec.display_grid();
exec.key_press();
exec.logic(&exec);
exec.game_end(&exec);
};
return 0;
}
void Game::help_screen() //Display help screen
{
system("clear");
cout<<endl<<" ::[2048]:: ";
char text[]="> The game starts with 1 or 2 random numbers placed in a 4x4 grid .> Use the controls to move the numbers. ( Press W/A/S/D )> 2 numbers of the value will merge and add up.> New 2s and 4s will appear on an empty cell with each move you make.> Your objective is to make 2048 in a boxh without getting stuck!> Press Enter to continue...";
for(int i=0; text[i] ; i++)
{
if(text[i]=='>')
{
usleep(1000*500);
cout<<" ";
}
cout<<text[i];
}
}
void Game::win_screen() //Display win screen
{
system("clear");
cout<<endl<<endl;
cout<<" :: [ YOU MADE "<<win<<"! ] ::" //Player acheived 2048
<<" :: [YOU WIN] ::"
<<" TILE SCORE NAME"; //enter name
printf(" %4d %6d ",max,score);
cin>>name;
cout<<" > The maximum is 65,536 ! So go on :)";
}
void Game::loser_screen() //Display loser screen
{
system("clear");
cout<<" :: [ G A M E O V E R ] ::"
<<" TILE SCORE NAME";
printf(" %4d %6d ",max,score); //Enter name
cin>>name;
cout<<" > The maximum score is 3,932,156 ! So close :p";
}
char Game::try_again_screen(int lose) //Display try again screen
{
if(lose)
cout<<" > Try again "<<name<<" (y/n) ? :: "; //Try again? Players choice, yes or no.
else
cout<<" > Continue playing "<<name<<" (y/n) ? :: "; //Continue playing? Players choice, yes or no.
//Input without pressing the enter key
system("stty raw");
cin>>option;
system("stty cooked");
return option;
}
void Game_AI::goback() //Initialize go back function
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
grid[i][j]=bgrid[i][j];
}
int Game_AI::block_moves() //Initialize block movement function
{
int k=0;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(bgrid[i][j]!=grid[i][j])
{
k=1;
break;
}
return k;
}
Explanation / Answer
Try this code..
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string login_Id;
string password;
int attempts = 0;
while (attempts < 5)
{
cout << "Please enter your Login Id: ";
cin >> login_Id;
cout << "Please enter your Password: ";
cin >> password;
if (login_Id == "Akki" && password == "1234")
{
cout << "Welcome Akki! ";
break;
}
else if (login_Id == "Anu" && password == "dunn")
{
cout << "Welcome Anu! ";
break;
}
else
{
cout << "Invalid login attempt. Please try again. " << ' ';
attempts ++;
}
}
if (attempts == 5)
{
cout << " Warning ! Too many login attempts!!!! Please try after some time.";
return 0;
}
cout << "Thank you!! . ";
return 0;
}