The Game of Days-of-the-Year

The game Days-of-the-Year is played by two players who take turns naming a date of the year starting from January 1st. On any move, a player may increase the month or the day but not both. Thus, for example, the first player will start the game by naming any day in January (apart from the 1st), or the 1st of any month of the year (apart from January). The player who names December 31st wins - this is the aim of the game. To summarise:

  • The game is for two players
  • The game starts with the day: 1st January
  • Each player takes it in turn to either increase the day or the month of the date
  • They obviously cannot call a non-existent date (i.e. 30th of February)
  • The player who names December 31st wins

To be 100% clear: this game uses the standard Georgian Calendar (Links to an external site.) year on a non-leap year. That means that February has 28 days only. Point 4 is VERY IMPORTANT! Dates such as the 31st of November do not exist!

Example Gameplay

The game starts on January 1st, play starts:

  • Player 1 chooses to increase the day to the 5th giving a date of 5th January
  • Player 2 chooses to increase the month to February giving a date of 5th February
  • Player 1 then chooses to increase the month to December giving a date of 5th December (Player 1 is obviously an idiot)
  • Player 2 now increases the day of the 31st giving a date of 31st December
  • Player 2 is declared the winner

Your task is to write an implementation for two human players who share the same computer and keyboard and take turns to play - there is no 'AI' in this.

Because of the varying level of programming expertise amongst you, there are three levels of implementation.

Level 1 - Minimum Acceptably Correct - Assumes all Input is Correct

If you attempt this version you do not need to do any checking of input data - you can assume that the users will always enter correct data. That is:

  • You can assume users only ever type in the sort of values expected i.e. string (in the correct case too) or integer (based on the prompts of the program).
  • You can assume the integers they type will always be within the expected range i.e. from 27th June, if changing the day they would only provide: 28, 29, 30; and if changing the month they would only provide: 7, 8, ..., 12. Likewise, the string they enter will either be precisely day or month.

It is acceptable that this version crashes (or otherwise does not work properly) if the users does not enter the expected values, or if they enter integers outside the allowed ranges.

Recommendation: You should all start here to get the bare bones of this game working. Once you've got this figured out, you can leave it at that or move onto level 2.

Level 2 - Checks All Input

The second level checks to ensure that the user has entered only expected values, that the integers are in the permitted range, the rules of the game are not broken and it does not crash WHATEVER the user enters. You need to pay close attention to the rules of the game here ... do not allow your code to deadlock! Instead of crashing or deadlocking, it asks the user to re-enter data (so they can put in a correct value if it was a genuine typo or mistake). The error messages must conform to the 'Critical Messages' to achieve the marks (see below).

You do not need to worry if the user force quits the program (i.e.using CTRL+C) I do not want/expect you to handle this.

Level 3 - Additional Function

The final version requires you to work independently to learn how to use features of the language we have not studied at the point this coursework is released. It is intended to reward independent leaning and so I will not answer specific questions related to how to do it, but the information you need is in the course notes. (I will, however, be happy to answer questions clarifying what you are intended to achieve). To meet this level, you need the following capability:

To reach the maximum marks available you must implement the following additional functionality. If adding these functions breaks previous parts specified in level 1 or 2, then you will (potentially) lose more marks than it is possible to gain. The functions are:

  • It should be possible to override the starting date of the game when the program is run (not compiled).
  • When overriding the start date it should also output the day of the date as an ordinal numeral (Links to an external site.) (i.e. 1 of January should be 1st of January, 2 of January should be 2nd of January, and so on).

That is, if you run the program 'normally', then the program should work with the default start date (1st January) and output. But without making any changes to your code or recompiling it, it should be possible to override the default start date and provide ordinal-based output. It also needs to meet the requirements for Level 2.

To allow for these functions to be marked, I need to tell you how the program will be run to access this feature. It will be run by calling:

java DaysOfTheYear DAY MONTH

where DAY will be an integer from 1-31 and MONTH an integer between 1-12. To avoid repeated code you can assume the input is always in range and a valid date and will never be 31st December (that is, there will always be at least one move possible). For example, if I run java DaysOfTheYear 5 2 the program will start on 5th February.

Critical Messages - Essential for Correct Operation

Messages that your program prints must conform exactly to a specific pattern - because the marking will compare the output of your code with the output they expect, and only exact matches will count.

The messages and the way you print them are chosen so they make sense when you play the game manually. Take note of the case!

These messages are:

  • "The current date is: X of Y". Where X is the day and Y is the month. This message is printed before it announces whose turn it is. X should be an integer between the values of 1 to 31 and Y should be a string representing the month i.e. one of January, February, March, ..., December. There is a newline character at the end of this output.
  • "It is Player X's Turn!". Where X is the player numbers. X should be an integer either 1 or 2. There is a newline character at the end of this output.
  • "Do you want to increase the day or the month? (day or month): ". There is a space after the first ':' character. There should be no newline character at the end of this output.
  • "Which month do you want to pick: ". There is a space after the first ':' character. There should be no newline character at the end of this output.
  • "Which day do you want to pick: ". There is a space after the first ':' character. There should be no newline character at the end of this output.
  • "Player X is the winner of the game!". Where X is the player numbers. X should be an integer either 1 or 2. There is a newline character at the end of this output.
  • "Input invalid, please try again!". This statement is printed any time the user enters any invalid data. There is a newline character at the end of this output.

Note that you should not print the " (quote) characters in the strings above.

NOTE: these are the ONLY messages your program should output if the input from the user is correct. Any additional 'welcome' messages, or splash screens or anything else should not be included.

Example Dialogue (Level 1)

Here is an example of the message sequences when playing a very simple game. Messages in bold are entered by the player.

The current date is: 1 of January

It is Player 1's Turn!

Do you want to increase the day or the month? (day or month): day

Which day do you want to pick: 5

The current date is: 5 of January

It is Player 2's Turn!

Do you want to increase the day or the month? (day or month): month

Which month do you want to pick: 2

The current date is: 5 of February

It is Player 1's Turn!

Do you want to increase the day or the month? (day or month): month

Which day do you want to pick: 12

The current date is: 5 of December

It is Player 2's Turn!

Do you want to increase the day or the month? (day or month): day

Which month do you want to pick: 31

The current date is: 31 of December

Player 2 is the winner of the game!

Example Dialogue (Level 2 and 3)

Here is an example of the message sequences when playing a game where incorrect input is given. Messages in bold are entered by the player.

The current date is: 1 of January

It is Player 1's Turn!

Do you want to increase the day or the month? (day or month): asdf

Input invalid, please try again!

day

Which day do you want to pick: 31

The current date is: 31 of January

It is Player 2's Turn!

Do you want to increase the day or the month? (day or month): month

Which month do you want to pick: ghjk

Input invalid, please try again!

12

The current date is: 31 of December

Player 2 is the winner of the game!
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.