I have made a function called proc_num which pushes all the numbers in a grid to the left and adds two numbers if they are equal just like the original 2048 game. If a change has been made it returns true otherwise it returns false.
The issue is. In 2048 the numbers are in a grid and each row in the grid needs to be pushed to the left individually. Hence I introduced two limits ei and bi which allows us to only push a range of values to be able to left push each row individually.
Now proc_num works great I tested it. However my proc_num_all which is meant to change the range values to change which row we are pushing outputs 0000... everytime. I went through the debugging process as I was taught but in all honesty I cant find what it is that is failing this programme.
I have tried everything within my means and I cannot figure out what is wrong.
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <string>
bool proc_num(std::vector<int>& v, int bi, int ei);
bool proc_num_all(std::vector<int>&v);
int main(){
std::vector<int> v{1,0,1,0,1,0,1,0,1} ;
bool c = proc_num_all(v);
if( c ){
std::cout << "true" << std::endl;
}
if(!c){
std::cout << "false" << std::endl;
}
for( int i =0; i<v.size(); i++){
std::cout <<v[i];
}
return 0;
}
bool proc_num(std::vector<int>& v, int bi, int ei){
std::vector<int> tmp;
std::vector<int> tmp2;
int counter = 0;
// takes out the zeros
for(int i = bi; i< ei ; i++){
if(v[i]!= 0){
tmp.push_back(v[i]);
counter++;
}
}
// returns false if all equal to zero
if( counter == 0){
return false;
}
// takes out values for later testing
for( int i = 0; i<v.size(); i++){
tmp2.push_back(v[i]);
}
// adds if equal
for(int i = 0; i < (tmp.size() - 1); i++){
if (tmp[i] == tmp[i+1] && tmp.size() != 0){
tmp[i] = tmp[i]+tmp[i];
tmp.erase( tmp.begin() + (i+1));
}
}
int x = 0;
//prints values back in
for( int i = bi; i< tmp.size(); i++){
v[i] = tmp[x];
x++;
}
// adds the zeros at the end
for( int i = x; i<ei; i++){
v[i] = 0;
}
//checks if there has been a change
if(v==tmp2){
return false;
}
return true;
}
bool proc_num(std::vector<int>&v){
int length = std::sqrt(v.size());
bool check = false;
for( int i = 0; i < length; i++){
int bi = i*length ;
int ei = bi +length ;
if(proc_num(v, bi, ei)){
check = true;
}
}
return check;
}
Aucun commentaire:
Enregistrer un commentaire