In: Computer Science
I am building a game in C programming language where I need to
add objects of various length into a game board. The game board is
8X8 and we must account for the boundaries for the board and not go
over them with our objects. The boards upper left corner is at 0x0
and we must return 1 if it fits and -1 if it does not fit.
I have the following 2 functions to start with:
```int add_object_vert(int r, int s, int len){}```
and
```int add_object_horiz(int r, int s, int len){}```
Thanks in advance!
Output
code
#include <stdio.h>
int add_object_horiz();
int add_object_vert();
int inputrow();
int inputcol();
int inputval();
int x[8][8]={0};
int main()
{
int side=0;
printf("If you want object inset horizontaly press 1 or press 2 for
vertically \n");
scanf("%d",&side);
if(side==1)
{
add_object_horiz();
}
else{
add_object_vert();
}
return 0;
}
int add_object_horiz(){
int r=inputrow();
int s=inputcol();
int val=inputval();
if((r<8)&&(s<8))
{
x[r][s]=val;
add_object_horiz();
}
else{
int i, j;
for ( i = 0; i < 8; i++ ) {
for ( j = 0; j < 8; j++ ) {
printf("x[%d][%d] = %d\n", i,j, x[i][j] );
}
}
return -1;
}
}
int add_object_vert(){
int r=inputrow();
int s=inputcol();
int val=inputval();
if((r<8)&&(s<8))
{
x[r][s]=val;
add_object_horiz();
}
else{
int i, j;
for ( i = 0; i < 8; i++ ) {
for ( j = 0; j < 8; j++ ) {
printf("x[%d][%d] = %d\n", i,j, x[i][j] );
}
}
return -1;
}
}
int inputrow()
{
int r;
printf("Enter the row number\n");
scanf("%d",&r);
return r;
}
int inputcol()
{
int s;
printf("Enter the column number\n");
scanf("%d",&s);
return s;
}
int inputval()
{
int len;
printf("Enter the length\n");
scanf("%d",&len);
return len;
}