943,650 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 547
  • C RSS
Jul 22nd, 2009
0

Machine Project..need help

Expand Post »
Hi, I'm a student in Computer Science and we have a machine project due on August 20. The problem is, the professor only taught us the basics of C programming, the functions, switch statements and whatnot, and now they're giving us a project about making a game. The game is just like Tetris, only there are no blocks, instead we are matching them to trash bins. Because the overall project was too difficult to do, I thought of learning how to code falling texts first. Here is my code so far.

**mp.h is a user-defined header file that was given to us.
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <mp.h>
  4. #include <conio.h>
  5.  
  6. void setBackground(unsigned short int color);
  7. int pickRandom(int min, int max);
  8. void delay(unsigned long milisec);
  9. void playSoundBeep();
  10. void gotoxy(int x,int y);
  11. void setx(int x);
  12. void sety(int y);
  13. char getKeypressed();
  14. void setScoreTo(int s); // sets the score to s
  15. void changeScoreBy(int x); // increments the score by x. If x is positive the score is increased. If x is negative the score is decreased.
  16. void showScore(); // shows the current score
  17. int getScore(); // returns the current score
  18. void clrscr(void);
  19. int getx(void);
  20. int gety(void);
  21.  
  22. int getObject(int *x)
  23. {
  24. *x = pickRandom(0, 79);
  25.  
  26. return(*x);
  27. }
  28. int main()
  29. {
  30. int live = 3;
  31. int x1 = 0, y = 0;
  32. int x = pickRandom(0, 79);
  33. setBackground(BLUE);
  34.  
  35. do{
  36. clrscr();
  37. gotoxy(getObject(&x), y);
  38. printf("T");
  39. gotoxy(&x, y++);
  40. delay(1000);
  41. }
  42.  
  43. while(y < 24);
  44.  
  45. getch();
  46.  
  47. }

The code I wrote enabled me to print my letter one line and clearing it before going to the next line. The only problem is, I can't get my letter to just stay in one straight line going down. I know I need to make my random X as it is but I don't know how. Any suggestions ? Thanks for all the help provided.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
louieansonng is offline Offline
4 posts
since Jul 2009
Jul 22nd, 2009
1

Re: Machine Project..need help

Quote ...
The game is just like Tetris, only there are no blocks, instead we are matching them to trash bins.
the individual words, I understand.

the sentence, makes my brain hurt.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jul 22nd, 2009
0

Re: Machine Project..need help

Click to Expand / Collapse  Quote originally posted by jephthah ...
the individual words, I understand.

the sentence, makes my brain hurt.
Sorry for the wrong grammar. What I meant to say was it is like Tetris in the sense that objects falling down, but they are not to be piled into lines, we have to move them into 3 positions depending on the object.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
louieansonng is offline Offline
4 posts
since Jul 2009
Jul 22nd, 2009
0

Re: Machine Project..need help

> The problem is, the professor only taught us the basics of C programming, the functions, switch statements and whatnot
That's not the only problem.
First it was a half-assed job of teaching you at all,
Then another half-assed job in teaching you obsolete TurboC as well - sheesh.

> Any suggestions ?
Pencil and paper.

Make a list of the things you need to do.
Think about how you would write the code.
Make little test programs to test your ideas. If that seems too complicated, then break the problem down into smaller steps.

When you've got a pretty good idea about how all the little bits work individually, think about how they might talk to one another (parameters, results etc), and start building up a program in stages.

Test regularly, and when you have reached a stage where an intermediate program does something useful, save a copy of it.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 22nd, 2009
0

Re: Machine Project..need help

Yes, they didn't teach us Turbo C at all. We use Dev-C++ for compiling.

This is what I am doing right now, making small test programs, I just can't get the text to "fall down" in a straight line. If my gotoxy had a permanent number like 5, then the program would have worked. Problem is if I make my x a random number from 0 to 79, I can't get it to fall to the bottom of the screen using the x it got after picking a random number. This program I am doing right now is just 20% of the whole game, so I kinda need concrete answers rather than complaints....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
louieansonng is offline Offline
4 posts
since Jul 2009
Jul 23rd, 2009
0

Re: Machine Project..need help

Well one of your gotoxy is at the very least a compiler warning, if not an outright error.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 23rd, 2009
0

Re: Machine Project..need help

I removed the reference on the first post. here's my final:
  1.  
  2. int main()
  3. {
  4. int lives = 3;
  5. int x1 = 0, y = 0;
  6. int x = getx();
  7. setBackground(BLUE);
  8.  
  9.  
  10. do{
  11. x:
  12. clrscr();
  13. gotoxy(pickRandom(0, 79), y);
  14. printf("T");
  15. gotoxy(x, y++);
  16. delay(200);
  17. }
  18. while(y < 24);
  19.  
  20. if( lives == 3)
  21. goto x;
  22.  
  23. getch();
  24.  
  25. }

Same problem here. How do I set the x it chose in the pickRandom?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
louieansonng is offline Offline
4 posts
since Jul 2009
Jul 23rd, 2009
0

Re: Machine Project..need help

You have a label and a variable with the same name.

You have a LABEL (and the goto that goes with it)

Your goto is a backward jump

Your goto is INTO the middle of a loop.

First you need to learn some elements of basic program construction. Bashing in random snippets, compiling them and then asking on a forum "what's wrong now" won't get it done.

FWIW
  1. int x = pickRandom(0, 79);
  2. int y = 0;
  3. do {
  4. gotoxy(x,y);
  5. printf("T");
  6. delay(200);
  7. printf("\b ");
  8. y = y + 1;
  9. } while ( y < 24 );
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Save a File (Code in C)
Next Thread in C Forum Timeline: Wondering about execve command





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC