0

The program is to print a '*' in xy cordinate and when user input "d" it should move one spade to the right.

#include<stdio.h>
void draw(int x,int y)
{
int i,j;
if(x==0 && y==0)
{
    printf("point is at origin");

}
else
{
for(i=1;i<=y;i++)
    printf("\n");
for(j=1;j<=x;j++)
    printf(" ");
printf("*");

}
} 

int main()
{
int x,y;
char a='a';
printf("eneter the x coordinate : ");
scanf("%d", &x);
printf("eneter the y coordinate : ");
scanf("%d", &y);

while(1)
{   
printf("\033[H\033[J");                     //clear screen
scanf("%c",&a);

draw(x,y);
scanf("%c",&a);
if(a=='d' || a=='D')
{
    x++;
}
}   


return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • [ncurses](http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/keys.html) is your friend – David Ranieri Aug 14 '17 at 19:12
  • cant use external libraries. – Pankaj Panwar Aug 14 '17 at 19:21
  • `termios` is an external library :P – David Ranieri Aug 14 '17 at 19:21
  • actually i am new to c so still finding my way out. – Pankaj Panwar Aug 14 '17 at 19:24
  • Exactly what @KeineLust just said. Curses allows you to "draw" characters anywhere on the terminal, and get individual keystrokes as the user types. The main ncurses developer helped with the [programmer's guide to ncurses](http://www.c-for-dummies.com/ncurses/) book, the example programs of which are online, and look like a good resource for learning how to use ncurses. – Nominal Animal Aug 14 '17 at 19:25
  • 1
    @PankajPanwar There is no way to work in canonical mode without using an external library – David Ranieri Aug 14 '17 at 19:26
  • 1
    @KeineLust thanks for the support, looks like i have to use curses for it. – Pankaj Panwar Aug 14 '17 at 19:29
  • How is it not working? What is the programming doing and what is it doing wrong? – Brad S. Aug 14 '17 at 19:33
  • actually the program has to take two integer inputs in 'x' and 'y' and then place a '*' in xy coordinate , then if i press 'd' it should move one space forward butrightnow it requires 'd' and enter key to move.@BradS. – Pankaj Panwar Aug 14 '17 at 19:39
  • See the answer to this question for how to read one char at a time without needing Enter: https://stackoverflow.com/questions/1798511/how-to-avoid-press-enter-with-any-getchar – payne Aug 14 '17 at 22:18
  • @payne thanks for the link .. now i am using terminos for this operation and it seems to work fine for me . – Pankaj Panwar Aug 14 '17 at 22:39

0 Answers0