Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C language Write an interactive drawing program that will accept characters f

ID: 3774595 • Letter: I

Question

In C language

Write an interactive drawing program that will accept characters from the keyboard until ESC is pressed. Redraw the board every keypress. Use the numpad keys, and switch on the characters 8.6.2. and 4 to update x and y variables, then write an asterisk to the global board data. Start at location 15.15. So, it the user press the 8 key (up), increment y, update board[y][x] = '*', then redraw the screen using a nested loop. Use the following code: char board[30][30];//Prints the global board using a nested loop void printboard() int row, col; for (row = 0; row

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

char board[30][30]; //Define the board size

void printBoard(){
   int row,col;
   for(row=0; row<30; row++){
       for(col =0 ; col<30; col++){
           printf("%c", board[row][col]);
       }
       printf(" ");
   }
}

int getInput(){
   printf("%s ", "Enter 8/6/4/2 to draw or ESC to quit : ");
   char key;
   scanf("%c",&key);
   return key;
}

void intializeBoard(){ //Initialize board with empty spaces
   int row,col;
   for(row=0; row<30; row++){
       for(col =0 ; col<30; col++){
           board[row][col]=' ';
       }
   }
}

int main()
{
   /* code */
   int x = 14, y=14;
   int key;
   intializeBoard();
   while((key=getInput())!= 27){ // Take character as input and check if it is ESC character
       switch(key){
           case '4': {
               if(y > 0) {
                  
                   y--;
                   board[x][y]='*';
               }
              
               break;
           }
           case '8': {
               if(x > 0) {
                   x--;
                   board[x][y]='*';
               }
               break;
           }
           case '2': {
               if(x < 29) {
                   x++;
                   board[x][y]='*';
               }
               break;
           }case '6': {
               if(y < 29) {
                   y++;
                   board[x][y]='*';
               }
               break;
           }
       }
       printBoard();
   }
   printf("%s ", "You exited the program.");
   return 0;
}