Tetris function move right in C [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Currently i have the move left function
int8_t move_piece_left(piece_type* piecePtr) {
int8_t row;
/*
* Check if piece is all the way to the left If so, return.
* (Remember, column number is based on bit position - higher
* numbers to the left.
*/
if(piecePtr->right_column + piecePtr->x_dimension >= BOARD_WIDTH) {
return 0;
}
/*
* Make the move.
*/
for(row=0; row < piecePtr->y_dimension; row++) {
piecePtr->rowdata[row] <<= 1;
}
piecePtr->right_column++;
return 1;
}
I assumed for the move right it would be a simple change, which I am sure it is but I don't think its going so well. So obviously I need to check if the piece is all the way to the right not the left, and perhaps its
piecePtr->right_column--;
To move right since left is ++ right would be -- I think?
I've tried a few changes here and there but not much has worked, so I am beginning to think I'm not fully understanding the code.
Can someone give a more of a deeper explain what the code is doing (I can read the comments I wanted a more of an in depth explanation).
EDIT there are appropriate checks in order to move left or right. e.g. checking if a piece is there

rowData seems to be key here: It appears to contain all pixels of the piece, in binary form (one integer per row). So, if you move the piece to the right, you have to shift its values right instead of left, in addition to changing the test and incrementation.
Also note that this code does not check for existing pieces in the way: It only checks for bounds.
The moving code shold be something like this:
int8_t move_piece_right(piece_type* piecePtr) {
int8_t row;
/*
* Check if piece is all the way to the right If so, return.
* (Remember, column number is based on bit position - higher
* numbers to the left.
*/
if(piecePtr->right_column == 0) {
return 0;
}
/*
* Make the move.
*/
for(row=0; row < piecePtr->y_dimension; row++) {
piecePtr->rowdata[row] >>= 1;
}
piecePtr->right_column--;
return 1;
}

Related

While loop increments in c [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am trying to make my "Steps" or increments in a while loop such that my values of are like N=2, 4, 8, 16... basically powers of 2 till 2^20. I have tried to do
for(j=1;j<=20;j++){
m=pow(2,2*i);
MAX=pow(2,20);
INC=pow(2,i);
while(m<=MAX){
then have my code running inside this. But in the output it gives me 2,4,6,8,10,12.. does anyone know what the right way is to do this?
Thanks!
You can start with a value of 2 for m and multiply m by two every iteration. As an optimization you can replace the multiplication by two with an left shift:
int MAX = pow(2, 20);
int m = 2;
while(m <= MAX) {
//do your work
m <<= 1;
}
In each iteration of the while loop; multiply your variable by two.
int m = 2;
while(m < maxpow)
{
m = m * 2;
printf("%d\n", m);
}

How to delete row number using C programing? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a Structure, in the StructureI have an Array,
I read a text file and then open it in `Array Into Structure ',
What I have is a list of names, Last, results.
so what is the best way to find a row number and select which row to delete and delete it? I said, Array into a Structure.?
I know I can use memmove and realloc but how do I use these?
Well, all you can do is move the following elements towards the start, and decrease the "logical" length. The logical length is different from the physical length, which is the maximum number of elements the array can hold, based on how much memory has been allocated.
So, assuming an array starting at array and with count elements, code to delete the n:th element would be:
if( n < count - 1)
memmove(array + n, array + n + 1, ((count - n) - 1) * sizeof *array);
--count;
This copies the following elements (unless you're deleting the very last one, in which case there's nothing to copy) and then decreases the logical length.

Problems with arrays in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Given a shifted array (for example):
[17 34 190 1 4]
which shifted from (we don't know original)
[1 4 17 34 190]
What would be a good function to find the position of that number?
For example if I pass 1, it would return 3th position.
linear search for answer would always work, but I believe you can get there in O(log) time.
Some sort of binary search for the shift point via checking if the value of the shift sorted array goes against what it is supposed it. Like creating a trie. Keep forming the sorted tree until you find the "illegal" node (man this is glossing over a lot of details - I know). That tells you where the inflection point is and you now treat the array as 2 sorted vectors. Quickly check to see if the value to find is larger than the max entry of each so we know which vector to search. BSearch the sorted vector for your value and return its index.
The hard part is finding the inflection point. :)
You would have to scan the array.
size_t pos_in_arr(int *arr, size_t arr_size, int match)
{
size_t i;
for (i = 0; i < arr_size; i++)
if (arr[i] == match)
break;
return i;
}
This function would return the position as asked, or one more than the maximum position in case the element is not found.
The solution is what you ask, but it is probably not what you need, because it does not use in any way the fact that the array has been shifted. I suspect the original problem to be more complex.
For example if you knew that in the original array one element was fifth and now is seventh, and the element you are looking for was twenty-third, you could answer "twenty-fifth" without actually scanning the array up to the twenty-fifth position, which could be the point of the whole exercise. But to build such a solution, one would need to know more about the problem.

Removing objects from an array using a for loop [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I found some code similar to this (not exactly just the weird logic has been replicated) :
for(int counter = 0 ; counter < array.length() ; counter ++ ) {
array.removeObjectAtIndex(i);
counter -- ;
}
Is this bad code ? What should one do assuming there is no primitive method to empty the whole array or that we need to do some extra cleanup after removing each element ?
If you go from the top down, you won't need counter--, and it's more efficient because it won't have to shift the array elements above each time removeObjectAtIndex is called.
Interfering with a loop counter inside the loop body is never a good idea.
Despite not knowing what array is, a better way would be to continuously remove the array head until the array is empty.
int length = array.length();
for(int counter = 0 ; counter < length ; counter++ )
{
array.removeObjectAtIndex(0);
}
Well, the first thing to note here is that at the end of every loop there's a counter++ and a counter--. Effectively canceling each other out.
I suspect a junior programmer was taught that for loops require the format
for(int i=0;i<len;i++) {/*code*/}
Going forth on that logic, due to the length for the array shifting by one with each pass in the loop, he required counter to get decremented as well (as to not generate fake results).
for(int c=0;c<a.length();c++){a.remove(i);c--;}
If you remove c-- from this implementation, you would remove only half the indexes. Counter would go up as length would go down, effectively meeting half way.
Now a different implementation that feels less rigid
for(;array.length()>0;){array.removeObjectAtIndex(i);}
// also this
for(;array.length();array.removeObjectAtIndex(i));
OP mentioned .length() might be inefficient, so:
for(int len=array.length();len--;array.removeObjectAtIndex(i));
For's are incredibly powerful, I personally find them highly underused in some situations.
I agree with acraig5075's comment about interfering with a loop counter inside the loop body is bad habbit
i think bottom up approach as below code will work fine
for(int counter = array.length - 1; counter >= 0; counter--)
{
array.removeObjectAtIndex(counter);
}

Search for a specific character in a 2D array [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I was wondering how you would search through a 2D array (used as a parameter for a function), and find a specific character, e.g. an exclamation mark?
Say I have a 2D array island[20][40] and I want to find the character X. My approach would be to use a nested for loop, to go through each element and an if statement. E.g.
for (i = 0; i < 20; i++) {
for (j = 0; j < 40; j++) {
//Not sure what goes here (I want a function that identifies the element in the array)
if ((some variable) == 88)
printf("The treasure is at: (%d, %d)", i, j);
Thanks for your help :)
-island[20][40] works fine. I just want to know how to search through it for a specific character.
use the condition
if (island[i][j] == 88);
If your array(s) are not ordered then you have no choice but to search through sequentially, there are no short cut.

Resources