• Game programming C challenge for Beginner/Intermediate

    ShikhaTan Member

    Here is a fun C challenge, for beginners/intermediates

    Okay we have a matrix of 4 x 4 which holds 15 numbers, with 1 number missing from the matrix which(for example) looks like this:

    3 4 5 7
    1 5 9 8
    2 6 11 12
    15 13 14

    the last corner number is to be empty (which your random generator is to take care off remember that in an array the last position will be array[3][3]

    the numbers in each position are to be randomly generated at initialization by random function generator(which you are to write)

    now the code (which you are also to write) is to take in two instructions

    either up or down or left or right which will be entered into console and indicated by either ‘U’ or ‘D’ or ‘L’ or ‘R’ or ‘Q'(quit)

    any other input should be rejected by your scanning function(not scanf but your own written function using getchar() )

    and the player(you) is to give instructions U or D or L or R or Q until either the puzzle is solved(for which you will need to write a function to check) or the person gives up by giving a ‘Q’ as input

    after input is taken(whether correct or incorrect), another function should display the current state of the matrix , also when game is either given up(‘Q’) or won the state of the puzzle is to be displayed also

    the person will have won when all the numbers are aligned correctly in increasing order(from 1-15), and the only movement that is allowed within the matrix is if the empty position is under it, above it, to the right of it or to the left of it, meaning obviously it is to work like the real thing(as i am sure many of you have seen before or played with) and you have a function which checks bounds to make sure you do not move a number in an illegal direction or out of bounds of the matrix(bounds being the limits of the matrix

    no movement is to be allowed unless that empty space is there , and when a movement does occur , it is to move only 1 element to the empty space directly above it or below it or to the left of it or to the right of it (how it works in real life)

    post your solution in a file and attach it, do not paste code directly into the replies so that others cant see it unless they download your file, and please do not paste trojan files or virus files, be a good sport will you?

    Bonus Challenge

    Make your input function take a number with ‘U’ or ‘D’ or ‘L’ or ‘R’ which(write function that checks bounds also are legal) looks like this ‘3U’ or ‘2D’ which (after checking the movement is within the bounds) moves more than 1 of the elements, so that if ‘3D’ is entered it(after checking the move is legal) will move 3 elements down one block each for each element etc, otherwise normal movement is to take place.

  • ShikhaTan Member

    This below is not the solution!!! this is a hint for the solution to help you out if you need some hints , or to get you on the right path

    you got two weeks to do this and get it right, Good Luck!!! then the challenge is over, you still need to implement a lot of the code, bound checking, scanner functions, also this below is shifting whole rows, while you need to be shifting one element at a time, or if you take up the challenge implement both the one element at a time and complete rows, also the initialize functions, the random generator function, and obviously the individual element function, and direction apply and check function

    #include 
    
    #define SIZE 4
    
    void shiftRowRight(int [][4], int, int,  int);
    void shiftRowLeft(int [][4], int, int,  int);
    void shiftRowDown(int [][4], int, int, int);
    void shiftRowUp(int [][4], int, int, int);
    void displayArray();
    
    int main(int argc, char * argv[]){
    
       //if you change size make sure to iniitalize array elements
    
       int arr[SIZE][SIZE]={{1,2,3,4},
                                         {5,6,7,8},
                                         {9,10,11,12,},
                                         {13,14,15,16}
                                       };
    
       int j=0,k=0, i;
    
       
       displayArray(arr, j, k, SIZE);
       printf("\nbegin shift left\n\n");
    
       //shift left
       for(i = 0; i< SIZE ; i++)
       {
          shiftRowLeft(arr, i, j,  SIZE);
          displayArray(arr, j, k, SIZE);
          printf("\nend shift left\n\n");
       }
    
       //shift right
       for(i = 0; i< SIZE ; i++)
       {
          shiftRowRight(arr, i, j,  SIZE);
          displayArray(arr, j, k, SIZE);
          printf("\nend shift right\n\n");
       }
    
       //shift down
       for(i = 0; i< SIZE ; i++)
       {
          shiftRowDown(arr, j, i,  SIZE);
          displayArray(arr, j, k, SIZE);
          printf("\nend shift down\n\n");
       }
    
       //shift up
       for(i = 0; i< SIZE ; i++)
       {
          shiftRowUp(arr, j, i,  SIZE);
          displayArray(arr, j, k, SIZE);
          printf("\nend shift up\n\n");
       }
    
       return 0;
    }
    void shiftRowRight(int arr[][SIZE], int column, int row,   int length){
       int temp = arr[column][row];
       --length;
       ++row;
       
       if(length>1){
          shiftRowRight(arr, column, row,  length);
          arr[column][row] = temp;
       }
       else{
           arr[column][0] = arr[column][row];
           arr[column][row] = temp;
    
       }
    
    }
    void shiftRowLeft(int arr[][SIZE], int column, int row,  int length){
       --length;
       row++;
       int temp = arr[column][length];
    
       if(length>0){
          shiftRowLeft(arr, column, row, length);
          arr[column][length-1]= temp;
       }
       else{
          arr[column][row-1]= arr[column][0];
       }
    
    }
    void shiftRowUp(int arr[][SIZE], int column, int row, int length){
       
       int temp = arr[length-1][row];
       
       --length;
       ++column;
    
       if(length>0){
          shiftRowUp(arr, column, row, length);
          arr[length-1][row]= temp;
       }
       else{
          arr[column-1][row]= arr[0][row];
       }
    
    }
    //works
    void shiftRowDown(int arr[][SIZE], int column, int row, int length){
       
       int temp = arr[column][row];
       length--;
       column++;
    
       if(length>1){
          shiftRowDown(arr, column, row, length);
          arr[column][row] = temp;
       }
       else{
          arr[0][row] = arr[column][row];
          arr[column][row] = temp;
       }
    }
    void displayArray(int arr[][SIZE]){
    
    int j, k;
    
       for(j = 0; j<4;j++){
          for(k =0; k<4; k++){
             printf("%4d ", arr[j][k]);
          }
          printf("\n");
       }
    }
    
  • Adelaid Member

    You gave me good memories. The first time I made similar exercises. I now work as a programmer, and I do a lot of interviews. In the interviews I don’t ask this type of exercise because I think they are too academic and too distant from real programming. I’m not saying that they aren’t good for the mind, but I think that are not useful to know if someone is a good programmer. I tend to ask question to see their reasoning and then I ask something about their precedent programs like the ones on github. Do you feel offended if I don’t do this exercise? I fear that by doing it and posting a solution, I can spoil the fun of someone that need this exercise more than me.

  • ShikhaTan Member

    If you say that there is nothing academic about this you have never worked as a programmer I suppose. The real mathematics problems are those of euler-project, not a simple exercise with arrays. I am referring to the only good solution that in case of size changes it doesn’t need a rewriting of the program. There is no need to copy the whole array. Don’t forget that not all programming languages where designed by mathematicians. You say that the most used language today(the ones that were not designed by mathematicians) are not serious? Before offending people get your facts straight and be respectful. If people don’t offend you why are you flaming in the first place?

  • Amit Member

    I cannot believe that what you see wrong with this is that it is “too academic” and “too mathematical” that is the most ignorant thing i have ever heard anyone say in this day and age, where somebody sees order and good practice as a bad thing, if your struggling to understand this just tell me and i will explain it to you , its all good i am here to help you understand, whats bothering you? is it the recursive functions? and this is the difference between self thought programmers and formally educated programmers, namely the clear and scientific approach to code for readability and understanding which everyone would prefer over “hacked code” that comes from lack of understanding of computer science topics such as how memory is stored and how recursion works

    again saying that this is a “simple exercise with arrays” once again your ignorance betrays you and is not doing you any favors in the intelligence department, if it was so easy why didnt you write the complete solution to the original question i posted

    the project euler questions are all number theory questions that people in the past have worked out without using computers , doing it on a computer is without writing formal mathematical induction methods and even easier because it can test all cases for you , so i dont see how they are of great challenge (up to question 495 is not that difficult to get their solutions), maybe there are a few which are a bit harder but if your know number theory algebra and calculus, euclidean geometry(which all of these topics are thought in 1st year university courses), most of those problems can be dealt with(some very easily which i can see just by looking at the questions)

    and i don’t know what your babbling about about size changes, i made this program very much that if changes in size of array, it would still be able to handle any two dimensional array of whatever size and still shift in horizontal and vertical directions, and hence why it appears “academic and mathematical” to you

  • Adelaid Member

    You don’t know what you are talking about. I don’t want revenge, I want that people contribute. You are not clearly contributing, you are only flaming and this will lead to you getting banned, if you don’t want to be banned, provide facts and be respectful. Where I said that it is too mathematical? Did you read my post? If I’m ignorant you are delusional, you are inventing things on the fly. Where did I say that is too mathematical?
    Order and good practice here? If you tidy your room does this make you a good programmer? This exercise is programming like a simple multiplication in regards to mathematics. This is a simple exercise with array, you have an array, you work with an array and you use function with an array. Who is the ignorant?
    The project euler questions are not all number theory.

    I said academic, if you had work as a programmer you will surely know that those simple exercise are far from a real program. You know how successful companies interview? Do you think they want a monkey that do simple exercise or someone that really know to make a program? Can you post your github profile so that all the people on this forum can see your great skills and can see that you know how to make a real software? Do you think to be special?

    There are other programmers in this forum, do you want to bet on what they will say?

  • ShikhaTan Member

    I prefer books. I don’t want to see a video tutorial I want to see your programs. I’m not the one acting like the special one and I’m not the one that says stupid to the other person. To make an argument you need facts: you can’t say you don’t think like me so you are stupid. Remember that you don’t know where I work. It can be a stupid company or a great one, how can you judge an entire company on the way I do interview? Tell me, do you prefer a programmer that can give you an entire software or do you prefer a programmer that can do books’ exercises? I prefer someone who can really code if he can also do exercise like yours then is a point in his favor. But if he’s only capable to do those exercise, how can I pay someone to read stackoverlfow instead of coding?

    Thank you, I don’t think I will need a course, if you want to make a course for other people then make it, I don’t think I will need it…

  • Amit Member

    Two things came to my mind: a binary logarithm and an implicit O, it could also mean other things. Computer science a lot of time is distant from the real programming, if I interviewed a person for an academy then I would ask CS questions. But in a company there is a need to see if the person can program rather than knowing his computer science understanding. Computer science’s theory tend to be farther from programming than the job position IT company interview…

  • Adelaid Member

    Those company want success, there are branch where you need a computer scientist and you need to be academical in the interview. But for the vast majority of fields, computer science knowledge is not a good indicator of programming skills. If you are talking about fresh programmers, then yes, in the past they asked academic exercise because there weren’t other means. Nowadays the interview at Google tend to not revolve too much about computer science and tend to be focused on real programming. Is valued more an active github profile than a degree.

    Times are changing and so has changed the interview process. Where the real talent is(people that got called appositely for their precedent works), there is near zero computer science theory. Google, etc. want to get things done. A freshman is not regarded anymore, I am in direct contact with one of those companies and they are changing the hiring process. The person with a degree in computer science that get hired is a thing of the past(when they need real talent they tend to overlook all the paper; when they need only to move money then a young student is enough). Startups are another example of how the hiring process is changing. A person with a math degree and a computer science one is good. A person with a computer science degree is also good. But you can’t say that a person without those is not good. Some of the best programmers around don’t even have/need a degree.

  • ShikhaTan Member

    What i am saying is that computer science knowledge is necessary, if your going to be building an engine you need to know about physics, just knowing what a piston is not enough, and computer science to a programmer is like physics to an engineer

    it makes a world of difference , and i remember when i first started programming i used to think “why would anyone make a science out of this, its redundant” but then i see now why that is so important, you have to know maths really well to be a successful programmer, besides programming languages where designed by mathematicians

    my other degree is physics, and that helps me in engine design astronomically, because no amount of experience as a programmer is going to make you better prepared for engine design without physics

    programming languages are nothing more than tools like a hammer or spanner is to a construction worker , however if you know mathematics , then you know the though process of how to design your software, if you know physics then you can bring art and science together into one , a programming language on its own means nothing, even if you know every single programming language but dont know mathematics you are as good as a programmer with no hands , you can try this and that but in hind site you are useless

    it is so important to understand binary well, and you cant understand binary system without knowing mathematics , algorithms are so important and they also heavily rely on functions(calculus) and knowing geometrical analysis , you have to understand bounds and boundaries, and you have to have a good sense of mathematical induction which you cant develop without doing maths for a while

    unless your dealing with software that a child can make then you need none of the above just to know the language

    usually your dealing with software that is processing data in great chunks , reading writing to files, creating trees, libraries, manipulation files all which has to work intricately with other parts of the program and code , its just really complex, i mean just to design a system that does something as simple as moving pixels on a screen(in large chunks) is not a simple process(what i am working on) there is so much to be aware of and mathematical knowledge makes it so much easier to deal with

  • Amit Member

    I have a degree in mathematics and one in computer in science. Yes, to knowing the theory behind programming it can be surely useful, but we must accept that the time are changing. The new generation of programmers is a generation that work more time out the university than in the university. When we were young, the only place to study was the university and the library and the only way to make a program was at the university or in a company. The rapid changes that we went into seems incredible. We have now Internet, GitHub, Stack Overflow. I as an interviewer, now, I have a much powerful tool to know if someone is a great programmer: I can browse his/her projects and review them together with the candidate. Nowadays is easy to spot a secretly terrible engineer: I can see his programs and ask them about it. When I need a candidate for the research sector, I ask them about computer science topics, but if he/she is an excellent programmer, how can I say that he/she is not good because he/she doesn’t know the big O notation and nonetheless can deliver a complete framework? I tend to judge people also on their computer science knowledge but it’s not the only thing I look for.

Viewing 10 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish