wip
This commit is contained in:
		
							
								
								
									
										50
									
								
								rooster.c
									
									
									
									
									
								
							
							
						
						
									
										50
									
								
								rooster.c
									
									
									
									
									
								
							| @@ -15,6 +15,9 @@ typedef struct rooster_data { | ||||
|     toestand state; | ||||
| } rooster; | ||||
|  | ||||
|  | ||||
| rooster *rooster_maak(char* input); | ||||
|  | ||||
| /* | ||||
|  * Sets a grids width and height | ||||
|  * | ||||
| @@ -201,6 +204,53 @@ int rooster_plaats(rooster *rp, int x, int y, char c) { | ||||
|     return 0; | ||||
| } | ||||
|  | ||||
| char *rooster_als_string(const rooster *rp) { | ||||
| 	if (rp != NULL && rp->rost != NULL) { | ||||
| 		char *string = malloc(sizeof(rp->rost)); | ||||
|         if (string == NULL) { | ||||
|             return NULL; | ||||
|         } | ||||
|         memcpy(string, rp->rost, sizeof(rp->rost)); | ||||
|         return string; | ||||
| 	} | ||||
|     return NULL; | ||||
| } | ||||
|  | ||||
| char *rooster_vraag_rij(const rooster *rp, int y) { | ||||
| 	if (rp != NULL && rp->rost != NULL && rooster_bevat(rp, 0, y) == 1) { | ||||
| 	    // we're going to remove the newline so this is long enough | ||||
| 		char *row = malloc((rp->width + 1) * sizeof(char)); | ||||
| 		memcpy(row, &rp->rost[internal_location(rp, 0, y)], rp->width * sizeof(char)); | ||||
| 	    row[rp->width] = '\0'; | ||||
| 		return row; | ||||
| 	} | ||||
| 	return NULL; | ||||
| } | ||||
|  | ||||
| rooster *rooster_kopieer(const rooster *rp) { | ||||
| 	if (rp != NULL && rp->rost != NULL) { | ||||
| 	    const size_t grid_memory = ((rp->width + 1) * rp->height + 1) * sizeof(char); | ||||
|  | ||||
|         char *grid = malloc(grid_memory); | ||||
|         if (grid == NULL) { | ||||
|             return NULL; | ||||
|         } | ||||
|  | ||||
|         rooster *new_rooster = malloc(sizeof(*rp)); | ||||
|         if (new_rooster == NULL) { | ||||
|             return NULL; | ||||
|         } | ||||
|  | ||||
| 	    memcpy(grid, rp->rost, grid_memory); | ||||
|  | ||||
|         memcpy(new_rooster, rp, sizeof(*rp)); | ||||
|  | ||||
|         new_rooster->rost = grid; | ||||
|         return new_rooster; | ||||
|     } | ||||
| 	return NULL; | ||||
| } | ||||
|  | ||||
| void rooster_zoek(const rooster *rp, char c, int *x, int *y) { | ||||
|     if (rp == NULL || rp->rost == NULL) { | ||||
|         *x = -1; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user