Jump to content
IGNORED

Anybody good at C++? I need help


Recommended Posts

5 hours ago, carlsson said:

The best way would be to compile the code, run the program and see if the output matches the original program. I understand that the 12th row is special, for "spawn points" where the symbols 0-9 and A-F may be used but exactly how those spawn points correspond to the DATA is a little hard for me to understand:

 

8 * "0" = 8 (i.e. "0" means multiply by 1)

8 * "1" = 24 (multiply by 3, I would have thought it should be 2)

8 * "A" = 168 (multiply by 21, I would have thought it should be 11)

 

Possibly "each column is 11 bits for playfield blocks, meaning we have 5 bits leftover for other things" is a hint, packing 17 values in the upper 5 bits. Then my code needs to be improved a little to be fully dynamic, and also I don't know if the playfield will work for more rows.

 

Perhaps @Lillapojkenpåön doesn't need those spawn points anyway, and will only use X on the map which makes it easier.

 

The second byte only uses the lowest 3 bits for map information (8+3=11).  The upper 5 are a special flag for the row based on column 12.  So you shift the special flag left by 3 and OR with the lower 3 to get the data value.

 

Link to comment
Share on other sites

12 minutes ago, ChildOfCv said:

 

The second byte only uses the lowest 3 bits for map information (8+3=11).  The upper 5 are a special flag for the row based on column 12.  So you shift the special flag left by 3 and OR with the lower 3 to get the data value.

 

I still don't get it, and I don't think it makes any difference in this case

why is it so chaotic looking

....0000.0000.0000.000000....0000................11111........000000........00000000000...0000.....000000000...............A....

 

who cares about row 12, it's not being displayed, Sprybug even wrote somewhere that he uses the remaining 5 bits for other things in his game,

nothing special is being done to the first byte, so shouldn't be needed for the second,

and the purpose of this thread is to make a program that handles all available resolutions,

there can be different number of bits remaining on the last one, or none.

 

Link to comment
Share on other sites

Using AverageSoftware's code, it appears you only need to change the one method:

 

unsigned char string_to_byte(std::string value)
{
    unsigned char byte = 0;

    std::for_each(value.crbegin(), value.crend(),
                  [&byte] (char c)
                  {
                      byte <<= 1;

                      if (c == 'X')
                      {
                          byte |= 1;
                      }
                  });

    return byte;
}

And that should solve the huge number problem.

  • Thanks 1
Link to comment
Share on other sites

C is OK with me. It is all that cout <<, classes and templates that I don't enjoy that much. Back in the 1990's when I was studing computer science we had a class in object oriented programming using C++ and we implemented Tetris in a way so each block would be an instance of its own class with a parent class for all blocks. Even the teacher admitted it wasn't the best example of object orientation but we managed to solve it.

  • Like 2
Link to comment
Share on other sites

On 3/6/2020 at 10:30 AM, carlsson said:

I made a small script in PHP which appears to do the job. It should be dynamic so it takes any amount of rows, but better test run it a couple of times. Before anyone says "UGGH! PHP!?!", go ahead and convert it to your preferred programming language.

 

Not that anyone asked, but... here's a stab at the problem using Python:

 

# Assumes each row has same number of columns

INFILE = 'my_cool_level.txt'
OUTFILE = 'leveldata.txt'
BYTES_PER_OUTPUT_ROW = 64
EMPTY = '.'
BYTE_SEPARATOR = ','

# Read in data, chopping off any newlines
indata = []
with open(INFILE) as infile:
   for row in infile:
       indata.append(row.rstrip('\n'))
                    

# Build a list of output bytes
outdata = []
for i in range(len(indata[0])):
    
    column = []
    
    for j in range(len(indata)):
        if indata[j][i] == EMPTY:
            column.append('0')
        else:
            column.append('1')
            
    while len(column) > 0:
        if len(column) >= 8:
            byte = column[0:8]
            column = column[8:]
        else:
            byte = column + ['0'] * (8 - len(column))
            column = []
        byte = byte[::-1]
        byte = ''.join(byte)
        outdata.append(int(byte, 2))


# Write out the bytes
with open(OUTFILE, 'w') as outfile:
    byte_counter = 0
    for byte in outdata:
        outfile.write(str(byte))
        byte_counter += 1
        if byte_counter == BYTES_PER_OUTPUT_ROW:
            outfile.write('\n')
            byte_counter = 0
        else:
            outfile.write(BYTE_SEPARATOR)

print('Done!')

 

Edited by JeffJetton
  • Like 3
Link to comment
Share on other sites

Trying my code on Lillapojkenpåön's 88-row example yields the following. Hope it's correct! ?

 

0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,224,0,0,0,0,0,0,0,0,0,0,224,0,0,0,0,0,0,0,0,0,0,224,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0
0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,136,0,0,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0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0
0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,192,0,0,0,0,0,0,0,0,0,0,224,0,0,0,0,0,0
0,0,0,0,240,0,0,0,0,0,0,0,0,0,0,248,0,0,0,0,0,0,0,0,0,0,252,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0
0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0
0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,128

 

Link to comment
Share on other sites

I didn't feel good about not being able to do this myself, or even understanding the C code I got, so I slowly made my own version from scratch, now it feels a little better.

 

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;


int main(int argc, char *argv[]) {

     if (argc < 3)
    {
        return 1;
    }

  vector<string> lines(0);
  string line;


   ifstream input(argv[1]);
   ofstream output(argv[2]);

  if (input.is_open())
  {
     while(getline (input,line))
     {
             while(line.length() == 0)
             {
             getline (input,line);   //get next line if line is empty (skip spaces between lines)
             }
     lines.push_back(line);
     }



    int m=lines.size();    //get height of vector
    int n=lines[0].length();    //get length of strings



    unsigned char temp = 0;
    int bitCounter=0;
    int byteCounter = 0;


    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {

                if (lines[j].at(i) == 'X')    //nested forloops goes through the vector vertically
                {
                          temp |= (1 << bitCounter);
                }
                else
                {
                          temp &=  ~(1 << bitCounter);
                }
                bitCounter++;

                if (bitCounter==8 || j==m-1)    //output the temp byte when filled, or if we're at the end of the column before that
                {
                    if (byteCounter==0) {output << " ";}    //indent every line
                 bitCounter=0;
                 output << +temp;    //the + converts char to int
                 byteCounter++;


                    if (byteCounter==64)    //64 numbers per line
                    {
                      output << endl;
                      byteCounter=0;
                    }
                    else
                    {
                      output << ',';
                    }
                 temp=0;
                }

        }

    }

  input.close();
  output.close();
  }
  return 0;
}

 

Edited by Lillapojkenpåön
Link to comment
Share on other sites

With this:

while(getline (input,line))
{
  while(line.length() == 0)
  {
    getline (input,line);   //get next line if line is empty (skip spaces between lines)
  }
  lines.push_back(line);
}

 

you'll likely have problems if there are empty lines are at the end of the file. I would change it to:

while(getline (input,line))
{
  if (line.length() > 0)
    lines.push_back(line);
}

 

  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...