Thread: Need help to Solve this coding !!

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    4

    Question Need help to Solve this coding !!

    First of all this is an assignment (game) which i have to submit.

    I have develop so far of this game but there are some instructions/conditions (functions) i need to complete.. but i dont understand them to do coding. So anyone please help me with this coding. Please run the coding then you will understand the game.

    Instructions (which i cannot do);


    • The occurrence of integer 1 in the entire board should not be exceeding 20% of the board’s size
    (eg: For 5 x 5 board, the occurrence of integer 1 should not be more than 5 times)


    • The largest integer in the entire board should occur only once.




    • Two steps will be counted (double-penalty) when the player lands at the same square for the second time onward




    • The information of previous players is stored in a plain text data file. The information of all previous players should be displayed at the beginning of the game. User / player will also be prompted to key in their information to be stored in the text data.

    Code:
    /*File: Assignment
    */
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include<ctype.h>
    #define NROW 5
    #define NCOL 5
    
    typedef struct
    {
    	int a[NROW][NCOL], x, y, random, count, limit, px, py;
    	int b[NROW][NCOL];
    	char name[20], direct, new;
    }game;
    
    
    void disp_arr(game arr);
    void logic(game *arr);
    
    
    int main(void)
    {
    	game arr;
    	printf ("\n******************WELCOME TO NUMBER PATH GAME******************\n");
    	printf("\n\nPlease key in your name :");
    	scanf("%s", &arr.name);
    	printf("\n\n***Good day %s, let's start a new game. All the best!!!***\n\n", arr.name);
    	getch();
    	srand( (int) time(NULL));
    	do
    	{
    		for (arr.x = 0; arr.x < NROW; arr.x++)
    		{
    			for (arr.y = 0; arr.y < NCOL; arr.y++)
    			{
    				arr.random = rand() % (4 - (1) + 1)  + 1;
    				arr.a[arr.x][arr.y] = arr.random;
    			}
    		}
    		arr.px = 0;
    		arr.py = 0;
    		disp_arr(arr);
    		arr.count = 0;
    		do
    		{
    			system ("cls"); /* command on some operating systems that clears the screen.*/
    			printf("\n\t\t\t*******GAME PLAY******\n\n");
    			disp_arr(arr);
    			printf("\nNext move:\nPress 2 - Down\nPress 4 - Left\nPress 6 - Right\nPress 8 - Up\nPress Q to Give up...\n\nEnter Direction/Choice: ");
    			fflush(stdin);
    			arr.direct = toupper(getch());
    			logic(&arr);
    			arr.count++;
    		}
    		while (arr.direct != 'Q' && (arr.px != NROW - 1 || arr.py != NCOL - 1));
    		/*Winning or loosing & requsting for new game*/
    		if (arr.px == NROW - 1 || arr.py == NCOL - 1)
    		{
    			system ("cls");
    			printf("\n\t\t\t******** RESULTS ********\n\n");
    			printf("\n\nCongratulation! you won the game!\n");
    			disp_arr(arr);
    			printf("\nNumber of steps taken: %i\n\n", arr.count);
    		}
    		else
    		{
    			system ("cls");
    			printf("\n\t\t\t******** GAME OVER ********\n\n");
    			printf("\n\nYou have given up...\n\n");
    		}
    		printf("Do you want to play a new game? (Y/N): ");
    		arr.new = toupper(getch());
    	}
    	while (arr.new == 'Y');
    	return 0;
    }
    
    
    
    
    void disp_arr(game arr)
    {
    	int x, y;
    	printf("\n");
    	arr.a[NROW-1][NCOL-1] = 0;
    	arr.b[NROW][NCOL] = 0;
    	for (x = 0; x < NROW; x++)
    	{
    		for (y = 0; y < NCOL; y++)
    		{
    			if (x == arr.px && y == arr.py)
    				printf("(%2i )", arr.a[x][y]);
    			else
    				printf(" %2i  ", arr.a[x][y]);
    		}
    		printf("\n\n");
    	}
    }
    
    
    
    void logic(game *arr)
    {
    	if (arr->direct == '8' && arr->px >= (arr->a[arr->px][arr->py]))
    		arr->px = arr->px - arr->a[arr->px][arr->py];
    	else if (arr->direct == '2' && (5 - arr->px) > (arr->a[arr->px][arr->py]))
    		arr->px = arr->px + arr->a[arr->px][arr->py];
    	else if (arr->direct == '4' && arr->py >= (arr->a[arr->px][arr->py]))
    		arr->py = arr->py - arr->a[arr->px][arr->py];
    	else if (arr->direct == '6' && (5 - arr->py) > (arr->a[arr->px][arr->py]))
    		arr->py = arr->py + arr->a[arr->px][arr->py];
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't need help to solve your logic. You need to get away from the computer, and work it out on paper.

    If I gave you a bushel of apples, and told you to arrange them so there was only one of the largest size of apple in the bushel basket, you'd have no trouble doing it. A child could do it.

    Same logic is needed here, with your game.

    Study the problem, by doing something very similar, by hand. It's really quite simple. Once you understand the logic, THEN write out your pseudo code, and finally, change it into C code, function by function - testing regularly, as you go.

    You don't have a C problem. You have a "I don't know the problem well enough and how to go from there, to writing the program", problem.

    Sadly for humans, that just takes time and sweat. Come back when you have a C problem, by all means.

    BTW, that process I described is called Top Down Design, and works pretty well, imo. Put off the details of the code - the final display interface layout, etc., and work on the program flow first, then work down to the details, last. Not all problems need it, but for those that are harder to understand, it's a real help.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You don't have a C problem. You have a "I don't know the problem well enough and how to go from there, to writing the program", problem.
    Translation: You don't have a programming problem. You just don't understand the problem well enough to write a solution

    (Sorry Adak, my friend, that one needed some help.)

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    Quote Originally Posted by Adak View Post
    You don't need help to solve your logic. You need to get away from the computer, and work it out on paper.

    If I gave you a bushel of apples, and told you to arrange them so there was only one of the largest size of apple in the bushel basket, you'd have no trouble doing it. A child could do it.

    Same logic is needed here, with your game.

    Study the problem, by doing something very similar, by hand. It's really quite simple. Once you understand the logic, THEN write out your pseudo code, and finally, change it into C code, function by function - testing regularly, as you go.

    You don't have a C problem. You have a "I don't know the problem well enough and how to go from there, to writing the program", problem.

    Sadly for humans, that just takes time and sweat. Come back when you have a C problem, by all means.

    BTW, that process I described is called Top Down Design, and works pretty well, imo. Put off the details of the code - the final display interface layout, etc., and work on the program flow first, then work down to the details, last. Not all problems need it, but for those that are harder to understand, it's a real help.
    Okay bro, this is the thing... I am totally new to this c programming which means not a quite pro.. I wrote what i understood by now.. just asked for the small help for this.. Because i thought the people in this forum will help me.

    I understand the problems in this program.. and i have done most of them well. just this is my confusing parts. okay.

    I just asked help!! I did not told you to do my home work because i have done this assignment so far. So don't be like this rude to people. If you do not want to help me out or something just ignore this!! okay.

    "Study the problem, by doing something very similar, by hand. It's really quite simple. Once you understand the logic, THEN write out your pseudo code, and finally, change it into C code, function by function - testing regularly, as you go."

    (do not have to tell me this as it is obvious)


    Thanks for the comment by the way!!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Put each one into steps of how you actually figure out that problem on your own:
    • The occurrence of integer 1 in the entire board should not be exceeding 20% of the board’s size
    (eg: For 5 x 5 board, the occurrence of integer 1 should not be more than 5 times)
    How would you do that? How do you know if an integer is on the board X times? Well that's obvious, run through all the tiles and count up how many times it's there. How do you know what percentage it makes up? The same way you find out the percentage of anything, division. What other way could you use to do this? Well, you could simply make a list of numbers as you add them to the board, and check to see if the number you are about to add is already in your list (just like you would check to see how many times it was on the board) and if it's already too many times, don't put it there.
    • The largest integer in the entire board should occur only once.
    If you have done the steps above, this one should be simple, because you are doing the same thing here, but instead of having 20%, you have a single occurrence to look for.
    • Two steps will be counted (double-penalty) when the player lands at the same square for the second time onward
    How do you know if you have landed on Boardwalk already? You remember. What's memory? Keeping track of something. So how do you know if someone has already landed there? Keep track of it. How would you do that? Well you make a list of where they have moved.

    See, I didn't need to do any programming to sort out those problems. That's why they told you to figure it out in words first, before you try to turn it into code. Because you can't turn it into code if you don't know what you are supposed to be doing. If you can't put it into words, then you don't know what you are supposed to be doing.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by neonad View Post
    If you do not want to help me out or something just ignore this!! okay.
    Actually you just got some of the best advice you could hope for.

    If you have this impression of programmers as people who get a 2 sentence description of a complex problem and immediately start hammering out code only to return in a couple of hours with a working solution... you are totally wrong.

    I've been programming in C since 2004 ... in other languages for a long time before that... I've never yet taken on a task and "just started coding"... I always start out by thinking about the problem and possible solutions. There is no programmer on this planet who can code the solution to a problem he does not understand... It simply cannot be done.

    The suggestion that you are stalled because you don't fully understand the problem is probably spot on... that's what usually stops me.

    Adak wasn't hassling you... he's the one who's likely to help you the most.
    (Translation: Don't trip over your pride... listen to what he's saying)

    There's an old saying: "Give a man a fish and you've fed him for a day. Teach a man how to fish and you've fed him for a lifetime."

    Take the advice, you'll be glad you did.
    Last edited by CommonTater; 05-06-2011 at 04:25 PM.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    Okay thanx all of you!! I am sorry if i done something wrong!!

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by neonad View Post
    Okay thanx all of you!! I am sorry if i done something wrong!!
    You haven't done anything wrong and nobody is angry with you...

    You asked for help, you got some solid advice.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, never fflush stdin. It is undefined behaviour which means it could just as well burn my eyes when I'm reading it.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    Guys Please let me know how can i limit occurance of random numbers in 2d array because my instructor told me to do that if you want to limit it.

    This is what he told me:

    Since it is quite complicated to check the number of 1 and the number of 4 within the same loop. I would suggest you to change to another approach, that is the get another nested for loop to go through the whole array and count the number of 1 and the number of 4 existed so far.
    (please take note that you might need to use some other variables for your nested for loop other than arr.x and arr.y, if you were to do the search within your existing nested for loop)

    Give it a try, do come and see me should you still fail to do it.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by neonad View Post
    Guys Please let me know how can i limit occurance of random numbers in 2d array because my instructor told me to do that if you want to limit it.

    This is what he told me:
    ...
    Give it a try, do come and see me should you still fail to do it.
    I'm going to tell you the same thing. Give it a try, and post your attempt here. By the way, did you go see him since you failed? Or did you just not try at all?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-10-2011, 07:28 AM
  2. can you solve this per
    By nkrao123@gmail. in forum C Programming
    Replies: 4
    Last Post: 11-16-2009, 12:50 AM
  3. Please help me to solve my Q
    By Baby in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2009, 03:38 AM
  4. How to solve this?
    By fenikkusu in forum C Programming
    Replies: 16
    Last Post: 01-09-2009, 04:15 AM