2048 game

#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>


using namespace std;




void board(int numbers[16]){
system("cls");

for(int i = 0; i < 16; i++){
if(numbers[i]==0)cout << " ";
else cout << numbers[i];

if(i == 3 || i == 7 || i == 11 || i == 15) cout << endl;
}



}

void genrnd(int numbers[16]){

vector<int> rndlist;

for(int i = 0; i < 16; i++){
if(numbers[i]==0){
rndlist.push_back(i);
}
}

int randindex, two_four;

random_shuffle ( rndlist.begin(), rndlist.end() );

srand (time(NULL));

randindex = rand() % rndlist.size();

two_four = rand() % 2;
if(two_four>0)two_four=4;
else two_four = 2;

numbers[rndlist[randindex]]=two_four;

}




void align(int numbers[16], int id) {

int d = 0;

if(id == 1){

for(int i = 0; i < 4; i++){
for(int k = i+12; k > i; k-=4){

if(numbers[k-4]==0){
numbers[k-4]=numbers[k];
numbers[k]=0;
}

else if(numbers[k-4]==numbers[k]){
numbers[k-4]+=numbers[k];
numbers[k]=0;
//k-=4;

}


}

}
}

if(id == 2){

for(int k = 16; k > 0; k-=4){
for(int i = k-1; i > k-4; i--){

if(numbers[i-1]==0){
numbers[i-1]=numbers[i];
numbers[i]=0;
}

else if(numbers[i-1]==numbers[i]){
numbers[i-1]+=numbers[i];
numbers[i]=0;
//i-=1;
}

}
}
}

if(id == 3){

for(int i = 0; i < 4; i++){
for(int k = i; k <= i+8; k+=4){

if(numbers[k+4]==0){
numbers[k+4]=numbers[k];
numbers[k]=0;
}

else if(numbers[k+4]==numbers[k]){
numbers[k+4]+=numbers[k];
numbers[k]=0;
//k+=4;

}


}

}
}

if(id == 4){

for(int k = 0; k < 16; k+=4){
for(int i = k; i < k+3; i++){

if(numbers[i+1]==0){
numbers[i+1]=numbers[i];
numbers[i]=0;
}

else if(numbers[i+1]==numbers[i]){
numbers[i+1]+=numbers[i];
numbers[i]=0;
//i+=1;
}

}
}
}

genrnd(numbers);
board(numbers);


}


int main(){

/// RANDOM START

int numbers[16] = {0};

int randindex;

srand (time(NULL));

for(int i = 0; i < 2; i++){

randindex = rand() % 16;

if(numbers[randindex]==2)numbers[randindex]=4;

else
numbers[randindex] = 2;

}

/// RANDOM END

board(numbers);


bool lost = 0;
int id = 0;
while(!lost){
switch ( games ){

case 'w':

align(numbers, 1);


break;
case 'a':

align(numbers, 2);

break;
case 's':

align(numbers, 3);

break;
case 'd':

align(numbers, 4);

break;
default:

board(numbers);
cout << endl << endl << "Only WASD keys are recognized at the moment" << endl;
break;


}




}


this my code so far. I am trying to compile and can't figure out the problem.
add some lines in your code
in line 5
 
#include <time.h> 


Add to main before the while
 
char games;


Add to the while() before switch()
 
scanf("%c",&games);
okay I will see if this works and get back to do
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <time.h>
using namespace std;




void board(int numbers[16]){
system("cls");

for(int i = 0; i < 16; i++){
if(numbers[i]==0)cout << " ";
else cout << numbers[i];

if(i == 3 || i == 7 || i == 11 || i == 15) cout << endl;
}



}

void genrnd(int numbers[16]){

vector<int> rndlist;

for(int i = 0; i < 16; i++){
if(numbers[i]==0){
rndlist.push_back(i);
}
}

int randindex, two_four;

random_shuffle ( rndlist.begin(), rndlist.end() );

srand (time(NULL));

randindex = rand() % rndlist.size();

two_four = rand() % 2;
if(two_four>0)two_four=4;
else two_four = 2;

numbers[rndlist[randindex]]=two_four;

}




void align(int numbers[16], int id) {

int d = 0;

if(id == 1){

for(int i = 0; i < 4; i++){
for(int k = i+12; k > i; k-=4){

if(numbers[k-4]==0){
numbers[k-4]=numbers[k];
numbers[k]=0;
}

else if(numbers[k-4]==numbers[k]){
numbers[k-4]+=numbers[k];
numbers[k]=0;
//k-=4;

}


}

}
}

if(id == 2){

for(int k = 16; k > 0; k-=4){
for(int i = k-1; i > k-4; i--){

if(numbers[i-1]==0){
numbers[i-1]=numbers[i];
numbers[i]=0;
}

else if(numbers[i-1]==numbers[i]){
numbers[i-1]+=numbers[i];
numbers[i]=0;
//i-=1;
}

}
}
}

if(id == 3){

for(int i = 0; i < 4; i++){
for(int k = i; k <= i+8; k+=4){

if(numbers[k+4]==0){
numbers[k+4]=numbers[k];
numbers[k]=0;
}

else if(numbers[k+4]==numbers[k]){
numbers[k+4]+=numbers[k];
numbers[k]=0;
//k+=4;

}


}

}
}

if(id == 4){

for(int k = 0; k < 16; k+=4){
for(int i = k; i < k+3; i++){

if(numbers[i+1]==0){
numbers[i+1]=numbers[i];
numbers[i]=0;
}

else if(numbers[i+1]==numbers[i]){
numbers[i+1]+=numbers[i];
numbers[i]=0;
//i+=1;
}

}
}
}

genrnd(numbers);
board(numbers);


}

char games;
int main(){

/// RANDOM START

int numbers[16] = {0};

int randindex;

srand (time(NULL));

for(int i = 0; i < 2; i++){

randindex = rand() % 16;

if(numbers[randindex]==2)numbers[randindex]=4;

else
numbers[randindex] = 2;

}

/// RANDOM END

board(numbers);

scanf ("%c",games);
bool lost = 0;
int id = 0;
while(!lost){
switch ( games ){

case 'w':

align(numbers, 1);


break;
case 'a':

align(numbers, 2);

break;
case 's':

align(numbers, 3);

break;
case 'd':

align(numbers, 4);

break;
default:

board(numbers);
cout << endl << endl << "Only WASD keys are recognized at the moment" << endl;
break;


}




}


I did very thing but it not saying that scanf is not being declared in the scope
scanf is not declare 。add #include 《stdio.h》
or use the cin replace the scanf 。。the cin grammar I forget
Last edited on
I was able to fix all the errors but now my program is glitching.
sorry ,I do not know what you say,can you statement clearly ?
Now your program is 0 error and warning, but something wrong is alread happend,
the lasted day I use the microsoft visiol studio 2010 run your program , it is successful.
I got the result , when I press ‘w', it is move.
Topic archived. No new replies allowed.