Jump to content

  • Log In with Google      Sign In   
  • Create Account

Álvaro

Member Since 07 Mar 2002
Offline Last Active Today, 01:55 PM

Posts I've Made

In Topic: Why can't I print out my std::string vector?

Today, 01:56 PM

Try to put together the smallest program that shows the problem. It is likely that you will figure out what the problem is in the process of producing that program. If not, post it here, so we can reproduce the problem on our own.

In Topic: Distance to a pixel

Today, 07:50 AM

You should be able to figure this one out yourself. You can either read the documentation carefully to see what the values mean, or you can reverse engineer it: Set up a few test situations where you know how far the pixel is and look at what values you get for those cases. You can probably figure out a formula easily from a few examples.

In Topic: C++ 2048 game - movement function issue

Yesterday, 06:50 PM

Well, I had a bit of time, so I wrote it.

#include <iostream>

void move_row(int *array, int step) {
  int write_i = 0;
  int previous = 0;

  for (int read_i = 0; read_i < 4; ++read_i) {
    int entry = array[step*read_i];
    if (entry == 0)
      continue;
    if (entry == previous) {
      array[step*(write_i - 1)] *= 2;
      previous = 0;
    }
    else {
      array[step*(write_i++)] = entry;
      previous = entry;
    }
  }
  for (; write_i < 4; ++write_i)
    array[step*write_i] = 0;
}

void print_row(int *a) {
  for (int i = 0; i < 4; ++i)
    std::cout << a[i] << ' ';
  std::cout << '\n';
}

int main() {
  int a[4] = {4, 1, 1, 0};

  print_row(a);
  move_row(&a[3], -1);
  print_row(a);
}


In Topic: C++ 2048 game - movement function issue

Yesterday, 10:20 AM

Skipping over the empty entries of an array is a common algorithm. There is a neat way to do it using a loop with two indices, where you read using one index and write using the other one.

int write_i = 0;
for (int read_i = 0; read_i < n; ++read_i) {
  if (array[read_i] != Empty)
    array[write_i++] = array[read_i];
}

for (; write_i < n; ++write_i)
  array[write_i] = Empty;


A small modification of that code will do what you need for 2048. Also, instead of writing the loop four times, try to parametrize it so the same piece of code can do all four directions.

If you have trouble with it, I can post some code. But give it a try yourself first.

In Topic: C++ 2048 game - movement function issue

Yesterday, 06:32 AM

Please, learn how to report problems better.

What's a case where it doesn't work? What is it doing and what did you want it to do?

PARTNERS