C + + Implementation 2048 Game example _c language

Source: Internet
Author: User
Tags pow rand

The game was a long time before the fire, a few days ago it was too boring, decided to put the game to write their own.

Write one hours before and after, about 300 lines, there is no complex algorithm, but really lazy to optimize, but the estimated optimization can be controlled under 200 lines, interested friends can optimize their own.

Description: I started to play the iOS app version of the Trhees, then play 2048, the two in the sliding rules of some differences, I this version is the combination of the two.

Finally, enjoy the demo!

Ugly interface, do not smile.

The following is the source code:

Copy Code code as follows:

/*by reason*/
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>//to read the arrow keys
#include <windows.h>
using namespace Std;
Srand ((unsigned) time (NULL)); Random number seeds can't be used here
int pane[4][4]; Board
int n=1; 2 N-Time Square
void ShowPane ()//display board
{
COUT&LT;&LT;SETW << "X2048 by Reason" <<endl;
COUT&LT;&LT;SETW << "|-----------------------|" <<endl;
for (int i=0;i<=3;i++)
{
COUT&LT;&LT;SETW () << "";
for (int j=0;j<=3;j++)
{
Setconsoletextattribute (GetStdHandle (std_output_handle), foreground_red);
if (pane[i][j]==0)
COUT&LT;&LT;SETW (2) << "|" &LT;&LT;SETW (4) << "";
Else
COUT&LT;&LT;SETW (2) << "|" &LT;&LT;SETW (4) <<pane[i][j];

if (j==3)
{
COUT&LT;&LT;SETW (2) << "|" <<endl;
COUT&LT;&LT;SETW << "|-----------------------|" <<endl;
}
}
}
}
void Newgame ()//Start Game
{
N=1;
for (int i=0;i<=3;i++)//Initialize Chessboard
for (int j=0;j<=3;j++)
pane[i][j]=0;
Srand ((unsigned) time (NULL));
int M=rand ()%4;
int N=rand ()%4;
int P=rand ()%4;
int Q=rand ()%4;
pane[m][n]=pane[p][q]=2;
ShowPane ();
}
int if2n (int x)//Determine if X is 2 n-th
{
int flag=0;
for (int n=1;n<=11;n++)
{
if (X==pow (2,n))
{
flag=1;
if (n>n)
N=n;
return flag;
}
}
return flag;
}
int Upmove ()//Move Up
{
int flag=0;
for (int j=0;j<=3;j++)
for (int i=0;i<3;i++)
{
if (if2n (Pane[i][j]+pane[i+1][j]) ==1)
{
PANE[I][J]=PANE[I][J]+PANE[I+1][J];
pane[i+1][j]=0;
flag=1;
}
}
return flag;
}
int Downmove ()//Move Down
{
int flag=0;
for (int j=0;j<=3;j++)
for (int i=3;i>0;i--)
{
if (if2n (Pane[i][j]+pane[i-1][j]) ==1)
{
PANE[I][J]=PANE[I][J]+PANE[I-1][J];
pane[i-1][j]=0;
flag=1;
}
}
return flag;
}
int Leftmove ()//move Left
{
int flag=0;
for (int i=0;i<=3;i++)
for (int j=0;j<3;j++)
{
if (if2n (pane[i][j]+pane[i][j+1]) ==1)
{
PANE[I][J]=PANE[I][J]+PANE[I][J+1];
pane[i][j+1]=0;
flag=1;
}
}
return flag;
}
int Rightmove ()//Move Right
{
int flag=0;
for (int i=0;i<=3;i++)
for (int j=3;j>0;j--)
{
if (if2n (pane[i][j]+pane[i][j-1]) ==1)
{
PANE[I][J]=PANE[I][J]+PANE[I][J-1];
pane[i][j-1]=0;
flag=1;
}
}
return flag;
}
int Testup ()//ability to move test up
{
int flag=0;
for (int j=0;j<=3;j++)
for (int i=0;i<3;i++)
{
if ((if2n (pane[i][j]+pane[i+1][j)) ==1) &&pane[i+1][j])
{
flag=1;
}
}
return flag;
}
int Testdown ()//test can move Down
{
int flag=0;
for (int j=0;j<=3;j++)
for (int i=3;i>0;i--)
{
if ((if2n (pane[i][j]+pane[i-1][j)) ==1) &&pane[i-1][j])
{
flag=1;
}
}
return flag;
}
int Testleft ()//test can move left
{
int flag=0;
for (int i=0;i<=3;i++)
for (int j=0;j<3;j++)
{
if ((if2n (pane[i][j]+pane[i][j+1)) ==1) &&pane[i][j+1])
{
flag=1;
}
}
return flag;
}
int testright ()//test can move right
{
int flag=0;
for (int i=0;i<=3;i++)
for (int j=3;j>0;j--)
{
if ((if2n (pane[i][j]+pane[i][j-1)) ==1) &&pane[i][j-1])
{
flag=1;
}
}
return flag;
}

int Panemax ()//Checkerboard Maximum number
{
int max=pane[0][0];
for (int i=0;i<=3;i++)
for (int j=0;j<=3;j++)
if (Pane[i][j]>max)
MAX=PANE[I][J];
return Max;
}
int Ifwin ()//Judging whether victory
{
int flag=0;
if (Panemax () ==2048)
{
COUT&LT;&LT;SETW << "You win!" <<endl;
flag=1;
}
return flag;
}
int Ifgameover ()//Judging whether the game is over
{
int flag=0;
if (Testup () + testdown () + testleft () + testright () = = 0)
{
COUT&LT;&LT;SETW (<< "Game over!") <<endl;
flag=1;
}
return flag;
}
void Addnewnumberup ()///Add new number after move up
{
Srand ((unsigned) time (NULL));
int n;
if (n==1)
N=1;
Else
N= (rand ()% (N) +1);
int Newnumber=pow (2,N);
for (int i=3;i>=0;i--)
for (int j=0;j<=3;j++)
if (pane[i][j]==0)
{
Pane[i][j]=newnumber;
Return
}
}
void Addnewnumberdown ()//Move Down Add new number
{
Srand ((unsigned) time (NULL));
int n;
if (n==1)
N=1;
Else
N= (rand ()% (N) +1);
int Newnumber=pow (2,N);
for (int i=0;i<=3;i++)
for (int j=0;j<=3;j++)
if (pane[i][j]==0)
{
Pane[i][j]=newnumber;
Return
}
}
void Addnewnumberleft ()///Add new number after left shift
{
Srand ((unsigned) time (NULL));
int n;
if (n==1)
N=1;
Else
N= (rand ()% (N) +1);
int Newnumber=pow (2,N);
for (int j=3;j>=0;j--)
for (int i=0;i<=3;i++)
if (pane[i][j]==0)
{
Pane[i][j]=newnumber;
Return
}
}
void Addnewnumberright ()//Right-shift Add new number
{
Srand ((unsigned) time (NULL));
int n;
if (n==1)
N=1;
Else
N= (rand ()% (N) +1);
int Newnumber=pow (2,N);
for (int j=0;j<=3;j++)
for (int i=0;i<=3;i++)
if (pane[i][j]==0)
{
Pane[i][j]=newnumber;
Return
}
}
int getdirection ()//Read direction
{
int ret = 0;
Todo
{
int ch = _getch ();
if (ISASCII (CH))
Continue
ch = _getch ();
Switch (CH)
{
Case 72:
ret = 2; Top
Break
Case 75:
ret = 1; Left
Break
Case 77:
ret = 3; Right
Break
Case 80:
ret = 4; Down
Break
Default
Break
}
while (ret = 0);
return ret;
}
int main ()//main function
{
System ("Color F9");
int makesure=1;
while (MakeSure)
{
System ("CLS");
Newgame ();
while (Ifwin () +ifgameover () ==0)
{
int c=getdirection ();
Switch (c)
{
Case 2:
if (Testup () ==1)
{Upmove ();
Addnewnumberup ();
System ("CLS");
ShowPane ();
}
Break
Case 4:
if (Testdown () ==1)
{
Downmove ();
Addnewnumberdown ();
System ("CLS");
ShowPane ();
}
Break
Case 1:
if (Testleft () ==1)
{
Leftmove ();
Addnewnumberleft ();
System ("CLS");
ShowPane ();
}
Break
Case 3:
if (Testright () ==1)
{
Rightmove ();
Addnewnumberright ();
System ("CLS");
ShowPane ();
}
Break
Default
Break
}
}
COUT&LT;&LT;SETW << "Your final score is:" <<panemax () <<endl;
COUT&LT;&LT;SETW << "To restart the game please enter 1, to end please enter 0. "<<endl;
cin>>makesure;
while (makesure!=1&&makesure!=0)
{
cout<< "Input is incorrect, please re-enter!" <<endl;
cin>>makesure;
}

}
cout<< "Goodbye!" "<<endl;
System ("pause");
return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.