PHP代写 | WD 2020S1 Assignment-2

Introduction

For this assignment, we are asking you to implement a mini Pokedex in C. The task is split into 4 sections; each section is not weighted the same.

What is a Pokemon? What is a Pokedex?

Hello there! Welcome to the world of Pokemon! My name is Oak! People call me the Pokemon Prof! This world is inhabited by creatures called Pokemon! For some people, Pokemon are pets. Others use them for fights. Myself … I study Pokemon as a profession.— Professor Oak

Pokemon are fictional creatures from the Pokemon franchise, most famously from the Pokemon games. The game revolves around the (questionably ethical) capturing of these creatures. Within the fiction of this universe, a device called the Pokedex is used to catalogue all the creatures a player finds and captures.

Where can we learn more about Pokedexes?

You can play the one of the original Pokemon games here. Quite early in the game you get introduced to the Pokedex.

There’s some more information on the Pokemon Wikia.

In addition, Google will be a great resource, as the topic has been extensively written up about.

Supplied Code

This zip file contains the files that you need to get started with the assignment. It contains the following files:

pokedex.h
contains declarations for all functions you need to implement for this assignment. Don’t change pokedex.h
pokedex.c
contains stubs for all the functions you need to implement for this assignment. Put all your Pokedex code in pokedex.c.
pokemon.h
contains declarations for all functions you must call to create and manipulate Pokemon. Don’t change pokemon.h
pokemon.c
contains the functions you must call to create and manipulate Pokemon. Don’t change pokemon.c
main.c
contains a main function and other functions that allow you to interactively test the functions you implement in pokedex.c Don’t change main.c
test_pokedex.c
contains a main function and other functions which are your starting point for a test framework to automatically test the functions you implement in pokedex.c. As you implement functions in pokedex.c you should add tests to test_pokedex.c. Only put testing code in test_pokedex.c.

Reference implementation

A reference implementation reference_implementation is available to help you understand the assignment.

~dp1091/bin/reference_implementation
===========================[ Pokedex ]==========================
            Welcome to the Pokedex!  How can I help?
================================================================
Enter command: ?
  a [pokemon_id] [name] [height] [weight] [type1] [type2]
    Add a Pokemon to the Pokedex
  p
    Print all of the Pokemon in the Pokedex (in the order they were added)
  d
    Display details of the currently selected Pokemon
  >
    Move the cursor to the next Pokemon in the Pokedex
  <
    Move the cursor to the previous Pokemon in the Pokedex
  m [pokemon_id]
    Move the cursor to the Pokemon with the specified pokemon_id
  r
    Remove the current Pokemon from the Pokedex
  f
    Set the current Pokemon to be found
  c
    Print out the count of Pokemon who have been found
  t
    Print out the total count of Pokemon in the Pokedex
  e [pokemon_A] [pokemon_B]
    Add an evolution from Pokemon A to Pokemon B
  s
    Show evolutions of the currently selected Pokemon
  q
    Quit
  ?
    Show help
Enter command: a 1 Bulbasaur 0.7 6.9 poison grass
Added Bulbasaur to the Pokedex!

Your pokedex.c (compiled with the supplied pokemon.c and main.c) should match the behaviour of the reference implementation.

Provision of a reference implementation is a common method to provide an operational specification, and it’s something you will likely need to do outside UNSW.

If you discover what you believe to be a bug in the reference implementation, report it in the class forum. We may fix the bug or indicate that you do not need to match the reference implementation’s behaviour in this case.

Stage 1: Adding and Printing

When you compile and run the given code, you will get the following message if you try to add a Pokemon:

dcc -o pokedex main.c pokedex.c pokemon.c
./pokedex
===========================[ Pokedex ]==========================
            Welcome to the Pokedex!  How can I help?
================================================================
Enter command: a 1 Bulbasaur 0.7 6.9 poison grass
exiting because you have not implemented the add_pokemon function in pokedex.c

When you run the a command, this function in pokedex.c is called:

void add_pokemon(Pokedex pokedex, Pokemon pokemon) {
    fprintf(stderr, "exiting because you have not implemented the add_pokemon function in pokedex.c\n");
    exit(1);
}

Your first task is to implement add_pokemon. All the functions you have to implement in pokedex.c have descriptions in pokedex.h: NOTE: SHOULD BE JUST add_pokemon prototype

//
// DO NOT CHANGE THIS FILE
//
// You do not submit this file. This file is not marked.
// If you think you need to change this file you have
// misunderstood the assignment - ask in the course forum.
//
// Assignment 2 19T2 CP1511: Pokedex
// pokedex.h
//
// You must not change this file.
//
// Version 1.0.0: Assignment released.
// Version 1.0.1: Minor clarifications to `get_current_pokemon`.
// Version 1.0.2: Minor clarifications to `add_pokemon`.
// Version 1.0.3: Added examples about Pokemon order in a Pokedex.
// Version 1.0.4: Renamed "pokemon_id" to "id" in change_current_pokemon.
// Version 1.0.5: Clarify "encountering" Pokemon in go_exploring.
// Version 1.0.6: Add info about not copying evolution info + fix typos.
// Version 1.0.7: Clarifications about valid types in get_pokemon_of_type.
#include "pokemon.h"
#ifndef _POKEDEX_H_
#define _POKEDEX_H_
typedef struct pokedex *Pokedex;
#define DOES_NOT_EVOLVE (-42)
// Create a new Pokedex and return a pointer to it.
// The pointer is to a malloced piece of memory, and it is the caller's
// responsibility to call 'destroy_pokedex' to free that memory.
Pokedex new_pokedex(void);
////////////////////////////////////////////////////////////////////////
//                         Stage 1 Functions                          //
////////////////////////////////////////////////////////////////////////
// Add a new Pokemon to the Pokedex.
// Note: just adding the Pokemon to the Pokedex does not mean it has
// been 'found'.
//
// The new Pokemon should be added to the end of the Pokedex (i.e.
// directly after the Pokemon that was added when add_pokemon was last
// called).
//
// When the first Pokemon is added to the Pokédex, the currently
// selected Pokemon becomes this Pokemon.
//
// The currently selected Pokemon remains the first Pokemon that was
// inserted into the Pokedex, until the `change_current_pokemon`,
// `next_pokemon`, or `prev_pokemon` functions are called.
//
// If there is already a Pokemon in the Pokedex with the same pokemon_id
// as this Pokemon, the function should print an appropriate error
// message and exit the program.
//
// Pokedex Order:
// --------------
// The Pokemon in the Pokedex are stored in the order in which they were
// added, i.e. the first Pokemon in the Pokedex will be the first
// Pokemon that was added, the second Pokemon in the Pokedex will be the
// second Pokemon that was added etc, and the last Pokemon in the
// Pokedex will be the Pokemon that was added most recently.
//
// For example, if Pikachu (#025) was added into an empty Pokedex, the
// Pokedex would now contain Pikachu at the start of the list, and
// Pikachu would be the currently selected Pokemon.
//
// [Start of Pokedex]
// #025: Pikachu <-- currently selected Pokemon
// [End of Pokedex]
//
// If Squirtle (#007) was then added, it would be the second Pokemon in
// the Pokedex, after Pikachu. The currently selected Pokemon would
// remain Pikachu.
//
// [Start of Pokedex]
// #025: Pikachu <-- currently selected Pokemon
// #003: Squirtle
// [End of Pokedex]
//
// If Diglett (#050) was then added, it would be the third Pokemon in the
// Pokedex, after Squirtle.
//
// [Start of Pokedex]
// #025: Pikachu <-- currently selected Pokemon
// #003: Squirtle
// #050: Diglett
// [End of Pokedex]
void add_pokemon(Pokedex pokedex, Pokemon pokemon);
// Print out the details of the currently selected Pokemon in the form:
//   Id: 007
//   Name: Squirtle
//   Height: 0.51m
//   Weight: 9.0kg
//   Type: Water
//
// If the Pokemon has not been found, the output should be:
//   Id: 007
//   Name: ********
//   Height: --
//   Weight: --
//   Type: --
//
// If the Pokemon has multiple types, they should be separated by a space:
//   Id: 006
//   Name: Charizard
//   Height: 1.7m
//   Weight: 90.5kg
//   Type: Fire Flying
void detail_pokemon(Pokedex pokedex);
// Return the currently selected Pokemon (i.e. the pointer to the
// pokemon struct, which was originally passed in to new_pokemon).
//
// The currently selected Pokemon starts out as the first Pokemon that
// was added to the Pokedex, and can be changed by calling the functions
// you will implement in Stage 2 (e.g. `next_pokemon`).
//
// See the `test_next_pokemon` function in test_pokedex.c for a concrete
// example of this function being used.
//
// If the Pokedex is empty, this function should print an appropriate
// error message and exit the program.
Pokemon get_current_pokemon(Pokedex pokedex);
// Sets the currently selected Pokemon to be 'found'.
//
// (see the assignment spec for more details on what being 'found' means)
//
// If there are no Pokemon in the Pokedex, this function does nothing.
void find_current_pokemon(Pokedex pokedex);
// Print out all of the Pokemon in the Pokedex, in the order in which
// they are stored in the Pokedex.
//
// See the comments in add_pokemon for more details about the ordering
// of Pokemon.
//
// The output should match the following form
//
// --> #001: Bulbasaur
//     #004: **********
//     #007: ********
//     #010: Caterpie
//     #013: Weedle
//     #016: Pidgey
//
// An arrow should be displayed next to the currently selected Pokemon.
//
// If a pokemon_id has less than three digits, it should be printed with
// leading '0's.
//
// If it has more than three digits, it should be printed as-is.
//
// --> #001: Bulbasaur
//     #004: **********
//     #007: ********
//     #010: Caterpie
//     #013: Weedle
//     #1263: Jabberwocky
//
// Any Pokemon not yet 'found' should have its name replaced with
// asterisks.
//
// The Pokemon should be printed in the order that they were inserted
// into the Pokedex.
void print_pokemon(Pokedex pokedex);
////////////////////////////////////////////////////////////////////////
//                         Stage 2 Functions                          //
////////////////////////////////////////////////////////////////////////
// Change the currently selected Pokemon to be the Pokemon after the
// currently selected Pokemon in the Pokedex, based on the order in
// which the Pokemon are stored in the Pokedex.
//
// See the comments above the add_pokemon function for more details
// about the ordering of Pokemon.
//
// For example, if we had the following Pokedex:
//
// [Start of Pokedex]
// #025: Pikachu <-- currently selected Pokemon
// #003: Squirtle
// #050: Diglett
// [End of Pokedex]
//
// Calling the next_pokemon function would change the currently selected
// Pokemon to be Squirtle:
//
// [Start of Pokedex]
// #025: Pikachu
// #003: Squirtle <-- currently selected Pokemon
// #050: Diglett
// [End of Pokedex]
//
// If there is no next Pokemon, the current Pokemon is unchanged.
//
// If there are no Pokemon in the Pokedex, this function does nothing.
void next_pokemon(Pokedex pokedex);
// Change the currently selected Pokemon to be the Pokemon before the
// currently selected Pokemon in the Pokedex, based on the order in
// which the Pokemon are stored in the Pokedex.
//
// For example, if we had the following Pokedex:
// [Start of Pokedex]
// #025: Pikachu
// #003: Squirtle <-- currently selected Pokemon
// #050: Diglett
// [End of Pokedex]
//
// Calling the prev_pokemon function would change the currently selected
// Pokemon to be Pikachu:
//
// [Start of Pokedex]
// #025: Pikachu <-- currently selected Pokemon
// #003: Squirtle
// #050: Diglett
// [End of Pokedex]
//
// If there is no previous Pokemon, the current Pokemon is unchanged.
//
// If there are no Pokemon in the Pokedex, this function does nothing.
void prev_pokemon(Pokedex pokedex);
// Change the currently selected Pokemon in the Pokedex to be the
// Pokemon with the ID `id`.
//
// If there is no Pokemon with the ID `id`, this function does
// nothing.
void change_current_pokemon(Pokedex pokedex, int id);
// Remove the currently selected Pokemon from the Pokedex.
//
// When the currently selected Pokemon is removed, the new currently
// selected Pokemon should become the Pokemon that was after the removed
// Pokemon.
//
// For example, if we had the following Pokedex:
// [Start of Pokedex]
// #025: Pikachu
// #003: Squirtle <-- currently selected Pokemon
// #050: Diglett
// [End of Pokedex]
//
// Calling the remove_pokemon function would remove Squirtle, leaving us
// with the following Pokedex:
//
// [Start of Pokedex]
// #025: Pikachu
// #050: Diglett <-- currently selected Pokemon
// [End of Pokedex]
//
// If the removed Pokemon was at the end of the Pokedex (i.e. it had no
// Pokemon after it), the new currently selected Pokemon should instead
// become the Pokemon that was before the removed Pokemon.
//
// If the removed Pokemon was the only Pokemon in the Pokedex, the
// currently selected Pokemon should become NULL.
//
// If there are no Pokemon in the Pokedex, this function does nothing.
void remove_pokemon(Pokedex pokedex);
// Destroy a given Pokedex and free all associated memory.
void destroy_pokedex(Pokedex pokedex);
// Return the number of Pokemon in the Pokedex that have been found.
int count_found_pokemon(Pokedex pokedex);
// Return the total number of Pokemon in the Pokedex, whether or not
// they have been found.
int count_total_pokemon(Pokedex pokedex);
////////////////////////////////////////////////////////////////////////
//                         Stage 3 Functions                          //
////////////////////////////////////////////////////////////////////////
// Add the information that the Pokemon with the ID `from_id` can
// evolve into the Pokemon with the ID `to_id`.
//
// See the evolution chart at https://pokemondb.net/pokedex/squirtle for
// more details
//
// For this assignment you can assume that every Pokemon can only evolve
// into one other Pokemon. If the function is called again with the same
// `from_id`, the new `to_id` will override the previous
// evolution information.
//
// For example, if the function was called three times:
//
// evolve_pokemon(pokedex, 0, 1)
// evolve_pokemon(pokedex, 0, 2)
// evolve_pokemon(pokedex, 0, 3)
//
// The end result of the these three function calls would be that
// Pokemon 0 evolves into Pokemon 3 (rather than 1 or 2).
//
// If there is no Pokemon with the ID `from_id` or `to_id`,
// or if the provided `from_id` and `to_id` are the same,
// this function should print an appropriate error message and exit the
// program.
void add_pokemon_evolution(Pokedex pokedex, int from_id, int to_id);
// Show the evolutions of the currently selected Pokemon.
// It should include the Pokemon it evolves into (if any), as well as
// any evolutions that its evolved state can evolve into, and so on.
//
// For example:
//
// #004 Charmander [Fire] --> #005 Charmeleon [Fire] --> #006 Charizard [Fire, Flying]
//
// If any of the Pokemon have not yet been 'found', their types and names
// should be replaced with "????".
//
// For example, if the Pokedex has found Charmander but not Charmeleon
// or Charizard, the output should be:
//
// #004 Charmander [Fire] --> #005 ???? [????] --> #006 ???? [????]
void show_evolutions(Pokedex pokedex);
// Return the pokemon_id of the Pokemon that the currently selected
// Pokemon evolves into.
//
// If the currently selected Pokemon does not evolve, this function
// should return DOES_NOT_EVOLVE.
//
// If the Pokedex is empty, this function should print an appropriate
// error message and exit the program.
int get_next_evolution(Pokedex pokedex);
////////////////////////////////////////////////////////////////////////
//                         Stage 4 Functions                          //
////////////////////////////////////////////////////////////////////////
// Create a new Pokedex which contains only the Pokemon of a specified
// type from the original Pokedex.
//
// If a Pokemon has more than one type, only one of its types has to
// match the given type to be included in the Pokedex.
//
// If the specified type is NONE_TYPE, INVALID_TYPE, or MAX_TYPE, this
// function should print an appropriate error message and exit the
// program.
//
// The Pokemon should appear in this Pokedex in the same order as they
// appeared in the original Pokedex.
//
// The new Pokedex's currently selected Pokemon should be the first
// Pokemon in that Pokedex.
//
// For example, if we had the following Pokedex:
//
// [Start of Pokedex]
// #129: Staryu (Water)
// #025: Pikachu (Electric)
// #072: Slowpoke (Psychic Water) <-- currently selected Pokemon
// #003: Squirtle (Water)
// #050: Diglett (Ground)
// [End of Pokedex]
//
// Calling get_pokemon_of_type(pokedex, WATER_TYPE) would result in a
// new Pokedex containing the following Pokemon in the following order:
//
// [Start of Pokedex]
// #129: Staryu (Water) <-- currently selected Pokemon
// #072: Slowpoke (Psychic Water)
// #003: Squirtle (Water)
// [End of Pokedex]
//
// If a Pokemon has not yet been found, it should not be added to the
// new Pokedex.
//
// All attributes should be copied except evolution information: the
// copied Pokemon should behave as if they have no evolutions.
//
// If there are no matching Pokemon, this function should return an
// empty Pokedex.
//
// This function should not alter the original Pokedex. It should create
// copies of any matching Pokemon in the original Pokedex, and insert
// those copies into the new Pokedex.
Pokedex get_pokemon_of_type(Pokedex pokedex, pokemon_type type);
// Create a new Pokedex which contains only the Pokemon that have
// previously been 'found' from the original Pokedex.
//
// The Pokemon should appear in this Pokedex in ascending order of
// pokemon_id (regardless of the order that they appeared in the
// original Pokedex).
//
// The new Pokedex's currently selected Pokemon should be the first
// Pokemon in that Pokedex (i.e. the Pokemon with the lowest pokemon_id).
//
// For example, if we had the following Pokedex:
//
// [Start of Pokedex]
// #129: ******
// #025: Pikachu
// #072: Slowpoke <-- currently selected Pokemon
// #003: Squirtle
// #050: *******
// [End of Pokedex]
//
// Calling get_found_pokemon would result in a new Pokedex containing
// the following Pokemon in the following order:
//
// [Start of Pokedex]
// #003: Squirtle <-- currently selected Pokemon
// #025: Pikachu
// #072: Slowpoke
// [End of Pokedex]
//
// All attributes should be copied except evolution information: the
// copied Pokemon should behave as if they have no evolutions.
//
// If there are no matching Pokemon, this function should return an
// empty Pokedex.
//
// This function should not alter the original Pokedex.  It should
// create copies of any Pokemon which are in the original Pokedex, and
// insert those copies into the new Pokedex.
Pokedex get_found_pokemon(Pokedex pokedex);
// Create a new Pokedex containing only the Pokemon from the original
// Pokedex which have the given string appearing in its name.
// (e.g. if the text provided is 'basau' then 'Bulbasaur' should be one
// of the Pokemon in the returned Pokedex)
//
// The text provided is not case sensitive,  e.g. if the text provided
// is 'bulb', then 'Bulbasaur' should be one of the Pokemon in the
// returned Pokedex.
//
// If a Pokemon has not yet been found, it should not be added to the
// new Pokedex.
//
// The new Pokedex's currently selected Pokemon should be the first
// Pokemon in that Pokedex.
//
// The Pokemon should appear in this Pokedex in the same order as they
// appeared in the original Pokedex.
//
// For example, if we had the following Pokedex:
//
// [Start of Pokedex]
// #129: Staryu
// #060: Polywag
// #025: Pikachu
// #072: Slowpoke <-- currently selected Pokemon
// #003: Squirtle
// #011: Metapod
// #050: Diglett
// #124: Vaporeon
// [End of Pokedex]
//
// Calling search_pokemon(pokedex, "po") would result in a new Pokedex
// containing the following Pokemon in the following order:
//
// [Start of Pokedex]
// #060: Polywag <-- currently selected Pokemon
// #072: Slowpoke
// #011: Metapod
// #124: Vaporeon
// [End of Pokedex]
//
// All attributes should be copied except evolution information: the
// copied Pokemon should behave as if they have no evolutions.
//
// If there are no matching Pokemon, this function should return an
// empty Pokedex.
//
// This function should not alter the original Pokedex.  It should
// create copies of any Pokemon which are in the original Pokedex, and
// insert those copies into the new Pokedex.
//
// !! You must not call any functions from string.h in this function !!
Pokedex search_pokemon(Pokedex pokedex, char *text);
#endif //  _POKEDEX_H_

You need to put code in the add_pokemon function to store the pokemon it is given into the pokedex it is given.

You don’t need to know any details about what the Pokemon type is (i.e. the type of the Pokemon pokemon argument passed to add_pokemon) to implement add_pokemon or any other function in pokedex.c.

Pokemon Tip #0: Pokemon IDs

In the Pokemon game franchise, Pokemon IDs start at 1 and go up to 809 (at the time of writing this). The first Pokemon is Bulbasaur, who has a Pokemon ID of 1. This assignment, however, allows any Pokemon ID from 0 upwards (because programmers love counting from 0).

Throughout this spec we’ll be making use of Bulbasaur (as well as some of the other official Pokemon) in our examples, but do note that you can make your own Pokemon with any ID from 0 to the maximum number that can be stored in an integer.

Hint: your code should also operate under the assumption that a pokemon_id of 0 is valid.