Week 5 Code Examples

#include <stdio.h>

int main(void) {
    int* number_ptr;
    char *letter_ptr;
    int number = 6;
    char letter = 'c';
    number_ptr = &number;
    letter_ptr = &letter;
    printf("number is %d and is stored at %p\n", number, number_ptr);
    printf("letter is %c and is stored at %p\n", letter, letter_ptr);
    return 0;
}
#include <stdio.h>

int main(void) {
    int number = 6;
    int *number_ptr = &number;
    printf("number_ptr deferences to %d, and is stored at %p\n", *number_ptr, number_ptr);
    return 0;
}
#include <stdio.h>

#define MAP_ROWS 8
#define MAP_COLUMNS 8

#define UP 'w'
#define DOWN 's'
#define LEFT 'a'
#define RIGHT 'd'

#define X_COORD 'x'
#define Y_COORD 'y'

enum tile {
    EMPTY,
    WALL,
    DIET_COKE,
    TRAP,
    PLAYER
};

struct location {
    enum tile tile;
};

struct player {
    int x;
    int y;
    int diet_cokes_drank;
};

void initialise_map(struct location map[MAP_ROWS][MAP_COLUMNS]);
void print_map(struct location map[MAP_ROWS][MAP_COLUMNS]);
struct player place_player(struct location map[MAP_ROWS][MAP_COLUMNS], struct player);
void place_wall(struct location map[MAP_ROWS][MAP_COLUMNS]);
int place_diet_cokes(struct location map[MAP_ROWS][MAP_COLUMNS]);
int get_valid_coordinate(char coordinate_type);
void play_game(struct location map[MAP_ROWS][MAP_COLUMNS], int diet_cokes, struct player);
int check_bounds(struct player, struct location map[MAP_ROWS][MAP_COLUMNS]);
void place_traps(struct location map[MAP_ROWS][MAP_COLUMNS]);

void print_welcome(void);

int main(void) {
    struct location map[MAP_ROWS][MAP_COLUMNS];
    struct player player;
    player.x = 0;
    player.y = 0;
    player.diet_cokes_drank = 0;
    initialise_map(map);
    print_welcome();
    player = place_player(map, player);
    printf("The player is at %d, %d\n", player.x, player.y);
    print_map(map);
    place_wall(map);
    print_map(map);
    int total_diet_cokes = place_diet_cokes(map);
    print_map(map);
    place_traps(map);
    print_map(map);
    play_game(map, total_diet_cokes, player);
    return 0;
}

int get_valid_coordinate(char coordinate_type) {
    int is_valid = 0;
    int coordinate;
    //Asks for a valid co-ordinate
    while (is_valid == 0) {
        printf("Enter %c coordinate: ", coordinate_type);
        scanf("%d", &coordinate);
        if (coordinate < 0 || coordinate > 7) {
            printf("Invalid %c co-ordinate! Try again\n", coordinate_type);
        }
        else {
            is_valid = 1;
        }
    }
    return coordinate;
}

struct player place_player(struct location map[MAP_ROWS][MAP_COLUMNS], struct player player) {
    printf("Time to place the player!\n");
    int starting_x;
    int starting_y;
    starting_x = get_valid_coordinate(X_COORD);
    starting_y = get_valid_coordinate(Y_COORD);
    map[starting_y][starting_x].tile = PLAYER;
    player.x = starting_x;
    player.y = starting_y;
    return player;
}

void place_wall(struct location map[MAP_ROWS][MAP_COLUMNS]) {
    printf("Time to place the walls!\n");
    int max_walls = 63;
    int is_valid = 0;
    int num_walls;
    while (is_valid == 0) {
        printf("Enter a number of walls to place: ");
        scanf("%d", &num_walls);
        if (num_walls < 0 || num_walls > max_walls) {
            printf("Invlaid number of walls, try again!\n");
        }
        else {
            is_valid = 1;
        }
    }
    int walls_placed = 0;
    int wall_x;
    int wall_y;
    while (walls_placed < num_walls) {
        wall_x = get_valid_coordinate(X_COORD);
        wall_y = get_valid_coordinate(Y_COORD);
        if (map[wall_y][wall_x].tile == EMPTY) {
            map[wall_y][wall_x].tile = WALL;
            walls_placed++;
            printf("Wall %d successfully placed!\n", walls_placed);
        }
        else {
            printf("Space already occupied, try again!\n");
        }
    }
}

void place_traps(struct location map[MAP_ROWS][MAP_COLUMNS]) {
    printf("Time to place the traps!\n");
    int max_traps = 63;
    int is_valid = 0;
    int num_traps;
    while (is_valid == 0) {
        printf("Enter a number of traps to place: ");
        scanf("%d", &num_traps);
        if (num_traps < 0 || num_traps > max_traps) {
            printf("Invlaid number of traps, try again!\n");
        }
        else {
            is_valid = 1;
        }
    }
    int traps_placed = 0;
    int trap_x;
    int trap_y;
    while (traps_placed < num_traps) {
        trap_x = get_valid_coordinate(X_COORD);
        trap_y = get_valid_coordinate(Y_COORD);
        if (map[trap_y][trap_x].tile == EMPTY) {
            map[trap_y][trap_x].tile = TRAP;
            traps_placed++;
            printf("Trap %d successfully placed!\n", traps_placed);
        }
        else {
            printf("Space already occupied, try again!\n");
        }
    }
}

int place_diet_cokes(struct location map[MAP_ROWS][MAP_COLUMNS]) {
    printf("Time to place the Diet Cokes!\n");
    int max_diet_cokes = 63;
    int is_valid = 0;
    int num_diet_cokes;
    while (is_valid == 0) {
        printf("Enter a number of Diet Cokes to place: ");
        scanf("%d", &num_diet_cokes);
        if (num_diet_cokes < 0 || num_diet_cokes > max_diet_cokes) {
            printf("Invlaid number of Diet Cokes, try again!\n");
        }
        else {
            is_valid = 1;
        }
    }
    int diets_cokes_places = 0;
    int diet_coke_x;
    int diet_coke_y;
    while (diets_cokes_places < num_diet_cokes) {
        diet_coke_x = get_valid_coordinate(X_COORD);
        diet_coke_y = get_valid_coordinate(Y_COORD);
        if (map[diet_coke_y][diet_coke_x].tile == EMPTY) {
            map[diet_coke_y][diet_coke_x].tile = DIET_COKE;
            diets_cokes_places++;
            printf("Diet Coke %d successfully placed!\n", diets_cokes_places);
        }
        else {
            printf("Space already occupied, try again!\n");
        }
    }
    return diets_cokes_places;
}



void play_game(struct location map[MAP_ROWS][MAP_COLUMNS], int diet_cokes, struct player player) {
    char movement;
    int is_valid = 0;
    printf("Enter a movement (w/a/s/d): ");
    while (player.diet_cokes_drank != diet_cokes && scanf(" %c", &movement) == 1) {
        map[player.y][player.x].tile = EMPTY;
        if (movement == UP) {
            player.y -= 1;
            if (check_bounds(player, map) == 0) {
                player.y += 1;
                printf("Invalid move (out of bounds or wall), try again!\n");
            }
        }
        else if (movement == DOWN) {
            player.y += 1;
            if (check_bounds(player, map) == 0) {
                player.y -= 1;
                printf("Invalid move (out of bounds or wall), try again!\n");
            }
        }
        else if (movement == LEFT) {
            player.x -= 1;
            if (check_bounds(player, map) == 0) {
                player.x += 1;
                printf("Invalid move (out of bounds or wall), try again!\n");
            }
        }
        else if (movement == RIGHT) {
            player.x += 1;
            if (check_bounds(player, map) == 0) {
                player.x -= 1;
                printf("Invalid move (out of bounds or wall), try again!\n");
            }
        }
        else {
            printf("Invalid movement, try again!\n");
        }
        if (map[player.y][player.x].tile == DIET_COKE) {
            player.diet_cokes_drank++;
        }
        if (map[player.y][player.x].tile == TRAP) {
            map[player.y][player.x].tile = EMPTY;
            player.x = MAP_ROWS - 1 - player.x;
            player.y = MAP_COLUMNS - 1 - player.y;
        }
        map[player.y][player.x].tile = PLAYER;

        print_map(map);
        is_valid++;
        if (player.diet_cokes_drank != diet_cokes) {
            printf("Enter a movement (w/a/s/d): ");
        }
        else if (player.diet_cokes_drank == diet_cokes ) {
            printf("\nYou win!\n");
        }
    }
}

int check_bounds(struct player player, struct location map[MAP_ROWS][MAP_COLUMNS]) {
    int is_valid = 0;
    if (player.x >= 0 && player.x <= 7) {
        is_valid = 1;
    }
    if (player.y >= 0 && player.y <= 7) {
        is_valid = 1;
    }
    if (map[player.y][player.x].tile == WALL) {
        is_valid = 0;
    }
    return is_valid;
}


// STARTER FUNCTIONS - DO NOT CHANGE

void initialise_map(struct location map[MAP_ROWS][MAP_COLUMNS]) {
    for (int r = 0; r < MAP_ROWS; r++) {
        for (int c = 0; c < MAP_COLUMNS; c++) {
            map[r][c].tile = EMPTY;
        }
    }
}

void print_map(struct location map[MAP_ROWS][MAP_COLUMNS]) {
    for (int r = 0; r < MAP_ROWS; r++) {
        for (int c = 0; c < MAP_COLUMNS; c++) {
            if (map[r][c].tile == PLAYER) {
                printf(" P ");
            } else if (map[r][c].tile == WALL) {
                printf(" # ");
            } else if (map[r][c].tile == DIET_COKE) {
                printf(" D ");
            } else if (map[r][c].tile == TRAP) {
                printf("(!)");
            } else {
                printf(" . ");
            }
        }
        printf("\n");
    }
}

void print_welcome(void) {
    printf("_______________________________\n\n");
    printf("Welcome to the Diet Coke Quest!\n\n");
    printf("_______________________________\n\n");
}
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int x = 10;
    int *ptr = malloc(x * sizeof(int));
    printf("%p\n", ptr);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *pointer = malloc(12);
    printf("%p\n", pointer);
    free(pointer);
    return 0;
}
#include <stdio.h>

int *create_array(void) {
    int numbers[10] = {0};
    // Return pointer to the array
    return numbers;
}

int main(void) {
    int *array = create_array();
    printf("%d\n", array[0]);
    return 0;
}
#include <stdio.h>

int main(void) {
    int number = 2;
    int *number_ptr = &number;
    printf("number = %d and number_ptr = %p\n", number, number_ptr);
    return 0;
}
#include <stdio.h>

int main(void) {
    int *box_ptr;
    int box = 12;
    box_ptr = &box;
    printf("The variable box is stored at %p and has a value of %d\n", box_ptr, *box_ptr);
    return 0;
}
#include <stdio.h>

int main(void) {

    int array[10] = {0};

    // Display the size of various data types and variables.
    printf("The size of an int is %lu bytes\n", sizeof(int));
    printf("The size of an array of int is %lu bytes\n", sizeof(array));
    printf("The size of 10 ints is %lu bytes\n", 10 * sizeof(int));
    printf("The size of a double is %lu bytes\n", sizeof(double));
    printf("The size of a char is %lu bytes\n", sizeof(char));

    return 0;
}
#include <stdio.h>
#include <string.h>

#define MAX 35

// 1. Define the struct
struct pet {
    char name[MAX];
    char species[MAX]; // "Dog" or "Cat" or ???
    int age;
    double weight;
};

int main(void) {
    struct pet my_pet;

    struct pet *pet_ptr = &my_pet;

    strcpy(pet_ptr->name, "Buddy");
    strcpy(pet_ptr->species, "Dog");
    pet_ptr->age = 3;
    pet_ptr->weight = 12.50;

    printf("--- Pet Record ---\n");
    printf("Name:    %s\n", pet_ptr->name);
    printf("Species: %s\n", pet_ptr->species);
    printf("Age:     %d years old\n", pet_ptr->age);
    printf("Weight:  %.2lf kg\n", pet_ptr->weight);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct season {
    int number;
    int rating;
};

int main(void) {
    struct season survivor;
    struct season *survivor_ptr = &survivor;
    survivor_ptr->number = 17;
    survivor_ptr->rating = 10;
    printf("%d has a rating of %d\n", survivor_ptr->number, survivor_ptr->rating);
    return 0;
}
#include <stdio.h>
#include <string.h>

#define MAX 35

// 1. Define the struct
struct pet {
    char name[MAX];
    char species[MAX]; // "Dog" or "Cat" or ???
    int age;
    double weight;
};

int main(void) {
    struct pet my_pet;

    struct pet *pet_ptr = &my_pet;

    strcpy((*pet_ptr).name, "Buddy");
    strcpy((*pet_ptr).species, "Dog");
    (*pet_ptr).age = 3;
    (*pet_ptr).weight = 12.50;

    printf("--- Pet Record ---\n");
    printf("Name:    %s\n", (*pet_ptr).name);
    printf("Species: %s\n", (*pet_ptr).species);
    printf("Age:     %d years old\n", (*pet_ptr).age);
    printf("Weight:  %.2lf kg\n", (*pet_ptr).weight);

    return 0;
}
#include <stdio.h>

struct swap {
    int item1;
    int item2;
};

struct swap swapsies(struct swap to_swap) {
    int temp = to_swap.item1;
    to_swap.item1 = to_swap.item2;
    to_swap.item2 = temp;
    return to_swap;
}

int main(void) {
    struct swap to_swap;
    to_swap.item1 = 12;
    to_swap.item2 = 15;
    printf("Before swap\n");
    printf("item 1 %d, item 2 %d\n", to_swap.item1, to_swap.item2);
    to_swap = swapsies(to_swap);
    printf("After swap\n");
    printf("item 1 %d, item 2 %d\n", to_swap.item1, to_swap.item2);
    return 0;
}
#include <stdio.h>

void swapsies(int *num1, int *num2) {
    int temp = *num1;
    *num1 = *num2;
    *num2 = temp;
}

int main(void) {
    int num1 = 12;
    int num2 = 6;
    printf("Before swap\n");
    printf("num1 is %d num2 is %d\n", num1, num2);
    swapsies(&num1, &num2);
    printf("After swap\n");
    printf("num1 is %d num2 is %d\n", num1, num2);
    return 0;
}
#include <stdio.h>

// malloc() and free() are functions in the <stdlib.h> library

#include <stdlib.h>

void read_array(int *numbers, int size);
void reverse_array(int *numbers, int size);

int main (void) {
    int size;
    printf("How many numbers would you like to scan: ");
    scanf("%d", &size);

    // Allocate some memory space for my array and return a pointer
    // to the first element
    int *numbers = malloc(size * sizeof(int));

    // Check if there is actually enough space to allocate
    // memory, exit the program if there is not enough memory
    // to allocate.

    if (numbers == NULL) {
        printf("Malloc failed, not enough space to allocate memomry\n");
        return 1;
    }

    // Perform some functions here
    read_array(numbers, size);
    reverse_array(numbers, size);

    // Free the allocated memory
    // In this case, it would happen on program exit anyway
    free(numbers);

    return 0;
}

void read_array(int *numbers, int size) {
    for (int i = 0; i < size; i++) {
        printf("Enter number %d: ", i + 1);
        scanf("%d", &numbers[i]);
    }
}

void reverse_array(int *numbers, int size) {
    int start = 0;
    int end = size - 1;

    while (start < end) {
        int temp = numbers[start];
        numbers[start] = numbers[end];
        numbers[end] = temp;
        start++;
        end--;
    }

    printf("Reversed array: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", numbers[i]);
    }
    printf("\n");
}