CP1401/CP5639 Assignment 2 – Blue v Red
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
CP1401/CP5639 Assignment 2 - Blue v Red
Task:
For this assessment, you are to plan and then code a medium-sized console-based program in Python 3. This assignment is designed to help you build skills using:
• As in assignment 1: Input, Processing and Output; Decision structures; Repetition structures
• New in assignment 2: Functions; random; Lists; Files
The assignment includes a Developer's Journal – a document you complete as you plan and code your program to help you focus on and improve your process and become a better developer.
Incredibly Important: This assignment must not be where you first learn these skills. The subject is designed to systematically teach you these principles through the lectures (first), seminars, then the practicals. You should have practised each programming construct many times before you start using it in your assignment. If you are not sure how to do something in this assignment, go back and learn from the subject teaching, not from the Internet. Remember:
100% of what you need to know to complete this assignment is taught in the subject.
Problem Description:
Blue v Red is a game of two teams – the user, Blue, and the computer, Red. For each game, the user will choose a number of players and enter their player names. The computer will choose its player names from a text file. The user then chooses which player to compete against the computer team. Each game proceeds until one team is the winner. The program stores statistics for the number of games won and lost. Sample Output below shows exactly how the program should work. The program starts with a welcome and a main menu with the following options:
1. Play Game | 2. Show Stats | 3. Quit.
• 1. Play Game
o Pressing P (or p) plays a game. First, you choose the number of players. You then enter each player’s name for your team. Each team must be stored as a list of strings. Notice the error checking looping on both the number of players and each name. Teams must have between 1 and 10 players, and player names cannot be the empty string "".
o The computer then loads player names from the file names.txt to form its team of the same size. The text file should contain one name per line with at least 10 names.
o When both teams are created, you are then asked which player (by number) you want to compete. The computer chooses an opponent randomly from its team list. Losing players are removed from their team. The winner is determined randomly based on the following formula, which is easier to explain in code than prose:
random.random() < len(user) / (len(user) + len(computer))
o The game proceeds until one team is defeated – their list is empty.
• 2. Show Stats
o This displays a report of the results from the games played so far. Notice in the sample output how the values are formatted to line up neatly.
• 3. Quit
o Choosing to quit will end the main menu and then display a farewell message based on the overall results. The farewell message should be either:
“Goodbye and better luck next time.” if the user does not have a winning percentage, or “Farewell champion!” if the user has more wins than losses.
Make sure you understand how the program should work (that's the "analysis" step in program development) before you plan it ("design" step), then code it ("implementation"). Don't forget to test your program thoroughly, comparing it to these requirements.
Coding Requirements and Expectations:
1. Make use of named constants as appropriate, e.g., for things that would otherwise be “magic numbers” . Remember the guidelines for constants:
https://github.com/CP1404/Starter/wiki/Programming-Patterns#constants
2. You are expected to include two kinds of useful comments in each of your program:
a. Every function must have a """docstring""".
b. Use # block comments for things that might reasonably need a comment.
Do not include unnecessary comments as these are just "noise" and make your program harder to read. See the subject teaching for how to write good comments:
https://github.com/CP1404/Starter/wiki/Styles-and-Conventions#commenting
3. Follow the teaching and patterns guide for things like error checking, menus, etc.:
https://github.com/CP1404/Starter/wiki/Programming-Patterns
You may also notice that we have written useful functions for getting valid inputs before, e.g.,
https://github.com/CP1401/Practicals/tree/master/prac_06#example
You have been taught how to write menus and you know that quit should not be a separate option within the menu, but rather the stopping condition for the loop with final actions coded outside the main menu loop:https://github.com/CP1404/Starter/wiki/Programming-Patterns#menus
Notice in the marking rubric that any use of while True will result in very low marks. We have taught you why this is. Follow what you've been taught, and you can be confident that you are on the right track!
4. Functions should be used for sections of the program and repeated tasks as you have been taught. Follow the DRY (Don't Repeat Yourself) principle and consider turning repeated code into functions. Functions should be designed according to good principles like SRP (Single Responsibility Principle). Do not use global variables. Any use of global variables will result in a very low mark for the functions criterion. Here are some suitable possibilities for functions:
• playing a game can be done with a function that returns a Boolean for if the user won
• competing between two selected players can be done by passing in the two player names and returning the string “Blue” or “Red” for the winner
• displaying the stats report
• loading the computer team
• printing the teams
• the main menu and one-off program behaviour (like the start and end) should all be part of the main function – like our examples and teaching
5. Sample output from the program is provided. Follow this to meet requirements, but you are
allowed to be creative and customise the interface as you wish. You can change small details that don't break the requirements, but if you change it substantially you may miss some of the required aspects and lose marks. E.g., you could display different outputs for wins and losses, but you could not add or remove a menu option, you could not decide to have a different way of competing, and you should ensure your stats line up neatly.
Please ask if you are unsure about this customisation.
6. Do not use constructs that have not been taught in the subject, including, but not limited to, exceptions, list comprehensions, dictionaries, classes, etc.
You are welcome to use the remove list method and the random.choice, random.random functions. Please study the marking rubric and notice there are heavy penalties for doing things we have taught you not to do, like using while True or global variables!
7. Follow the style guide:https://github.com/CP1404/Starter/wiki/Style-Guide
Work incrementally on this task: focus on completing small parts rather than trying to get everything working at once. This is called "iterative development" and is a common way of working for professional developers. Here is a suggested approach to this:
1. Start your assignment with the first part of the journal, as described below.
2. Begin your development with planning and writing pseudocode – this is important for your process as well as your journal.
3. Start coding with the main function and creating a working menu using the standard pattern. For each menu item, just print something (like " … play …").
4. Choose one function or section at a time to implement. E.g., start with the function to play a turn and get this working, then call the function from your main menu.
5. When you do a more complex section, keep it simple first, then add complexity. E.g., when getting number of players or player names, ignore the error-checking to start with. Get it working properly, then add error-checking.
6. For loading the computer team from file, you could either:
a. write and test this function early, or
b. just use a list literal while developing the rest of the program, then write the function.
Journal:
A significant learning outcome for this subject is for you to develop solutions to problems systematically and thoughtfully. We are interested in your process and the lessons you have learned through this experience, not just in the final product. To encourage you in learning the systematic problem-solving process, you will record your work experiences and insights in a simple journal for this assignment. Submit this as a Word document or PDF file.
Use the template file you have been provided with.
There are three parts to your journal:
1. Assignment 1 Reflections and Lessons
2. Work Entries
3. Summary
Assignment 1 Reflections and Lessons:
Before you begin working on this assignment, take some time to reflect on what you learned about your process through assignment 1, including how you will make changes this time based on any feedback you received. Please consider your previous reflection exercise from the practicals and ensure you write personally:https://github.com/CP1401/Practicals/tree/master/prac_07#reflection
Work Entries:
Each time you work on the assignment, record an entry in your journal that includes:
• Date, start and end times that you worked
• What you worked on with simple details, enough that someone reading it would understand
• Any difficulties you faced and how you overcame them
Do not include multiple entries for short work sessions. If you did 7 minutes, took a break then came back and did 37.5 minutes … we don't need this level of detail – just include a single entry of about 45 minutes. The entry detail can be quite short, but make sure your focus is on your process, not your actual code.
Summary:
Summarise the lessons you learned about the problem-solving process (not about Python code) through doing this assignment. This is the most important part, where you reflect on your process and show what you have learned. Did anything you considered or changed based on assignment 1 reflection prove important or useful? How are you improving in terms of following a systematic development process? What would you differently next time?
Please note that the only reasonable way to write a journal is regularly, while you develop your solution. A journal that is completed at the end of the assignment after you've finished is not a journal and will not aid your learning experience much at all.
Here is a sample journal entry that shows a 'satisfactory' level:
13/04/2025, 8:30 - 9:30am
Work: Wrote pseudocode for main function; nearly completed start and menu section.
Challenges: It took a few goes to remember how to deal with lists and functions in pseudocode. I checked the "Pseudocode Guide" and followed the examples, which helped.
Sample Output:
It should be clear which parts below are user input (not printed but entered by the user).
Welcome to Blue v Red
1. Play Game | 2. Show Stats | 3. Quit
Enter your choice: Why?
Invalid choice.
1. Play Game | 2. Show Stats | 3. Quit
Enter your choice: 1
Enter the number of players in each team: 0
Invalid input. Please enter a number between 1 and 10.
Enter the number of players in each team: 100
Invalid input. Please enter a number between 1 and 10.
Enter the number of players in each team: 3
Enter name for player 1:
You can't have an empty player name.
Enter name for player 1: why not?
Enter name for player 2: Oh, I see now that's my name :)
Enter name for player 3:
You can't have an empty player name.
Enter name for player 3: Champion
Blue Team: why not? Oh, I see now that's my name :) Champion
Red Team: Alice the Adventurer Kiera the Knight Ingenious Ingrid
Who will compete for your team?
1. why not?
2. Oh, I see now that's my name :)
3. Champion
Choose your player number (1-3): 4
Invalid input. Please enter a number between 1 and 3.
Choose your player number (1-3): 3
Red chose Alice the Adventurer
Alice the Adventurer (Red) wins :(
Blue Team: why not? Oh, I see now that's my name :)
Red Team: Alice the Adventurer Kiera the Knight Ingenious Ingrid
Who will compete for your team?
1. why not?
2. Oh, I see now that's my name :)
Choose your player number (1-2): 1
Red chose Ingenious Ingrid
Ingenious Ingrid (Red) wins :(
Blue Team: Oh, I see now that's my name :)
Red Team: Alice the Adventurer Kiera the Knight Ingenious Ingrid
Who will compete for your team?
1. Oh, I see now that's my name :)
Choose your player number (1-1): 1
Red chose Alice the Adventurer
Alice the Adventurer (Red) wins :(
Blue Team:
Red Team: Alice the Adventurer Kiera the Knight Ingenious Ingrid
Red wins the game :(
1. Play Game | 2. Show Stats | 3. Quit
Enter your choice: 1
Enter the number of players in each team: 2
Enter name for player 1: This name is really long, so I hope I win
Enter name for player 2: Steve
Blue Team: This name is really long, so I hope I win Steve
Red Team: Kiera the Knight Great Gadfly
Who will compete for your team?
1. This name is really long, so I hope I win
2. Steve
Choose your player number (1-2): 1
Red chose Kiera the Knight
This name is really long, so I hope I win (Blue) wins!
Blue Team: This name is really long, so I hope I win Steve
Red Team: Great Gadfly
Who will compete for your team?
1. This name is really long, so I hope I win
2. Steve
Choose your player number (1-2): 1
Red chose Great Gadfly
This name is really long, so I hope I win (Blue) wins!
Blue Team: This name is really long, so I hope I win Steve
Red Team:
Blue wins the game :)
1. Play Game | 2. Show Stats | 3. Quit
Enter your choice: 2
Wins: 1
Losses: 1
Win %: 50.0
1. Play Game | 2. Show Stats | 3. Quit
Enter your choice: 1
Enter the number of players in each team: 1
Enter name for player 1: All for one and One for all!
Blue Team: All for one and One for all!
Red Team: Dave
Who will compete for your team?
1. All for one and One for all!
Choose your player number (1-1): 0
Invalid input. Please enter a number between 1 and 1.
Choose your player number (1-1): 1
Red chose Dave
All for one and One for all! (Blue) wins!
Blue Team: All for one and One for all!
Red Team:
Blue wins the game :)
1. Play Game | 2. Show Stats | 3. Quit
Enter your choice: 2
Wins: 2
Losses: 1
Win %: 66.7
1. Play Game | 2. Show Stats | 3. Quit
Enter your choice: 3
Farewell champion!
And here is a third run where the user just selected quit as the first menu choice.
Welcome to Blue v Red
1. Play Game | 2. Show Stats | 3. Quit
Enter your choice: 2
Wins: 0
Losses: 0
Win %: 0.0
1. Play Game | 2. Show Stats | 3. Quit
Enter your choice: 3
Goodbye and better luck next time.
Submission:
Write your pseudocode and code in a single Python file called a2_blue_v_red.py.
Note: You are only required to write pseudocode for your main function and the play game function. You are welcome and encouraged to do it for the whole program, but we will only mark pseudocode for these two functions. However, your main function must be substantial and appropriately designed (e.g., do not use a "menu" function). Remember, "main should look like the whole program" , with the details in the functions:
https://github.com/CP1404/Starter/wiki/Programming-Patterns#main-program-structure
Copy the module docstring below and update this with your own details and your pseudocode, then your solution code below.
"""
CP1401 Assignment 2
Blue v Red
Student Name: XX
Date started: XX
Pseudocode:
"""
You must use the provided template file for your journal.
DO NOT zip or compress your files together.
Submit your two files, by attaching them in one LearnJCU submission by the date and time specified on LearnJCU. Submissions received after this date will incur late penalties as described in the subject outline. Note that the assignment allows multiple submissions, but we will only look at and assess the most recent submission. If you need to resubmit, then you must submit both files again.
Integrity:
The work you submit for this assignment must be your own. Submissions that are detected to be too similar to that of another student or other work (e.g., code found online or generated with AI) will be dealt with according to JCU procedures for handling plagiarism and may result in serious penalties.
The goals of this assignment include helping you gain understanding of fundamental programming concepts and skills, and future subjects will build on this learning. Therefore, it is important that you develop these skills to a high level by completing the work and gaining the understanding yourself. You may discuss the assignment with other students and get assistance from your peers, but you may not do any part of anyone else’s work for them and you may not get anyone else to do any part of your work. Note that this means you must never give a copy of your work to anyone or accept a copy of anyone else’s work, including looking at another student's work or having a classmate look at your work. If you require assistance with the assignment, please ask general questions in the subject discussion forum, or get specific assistance with your own work by talking with subject staff.
The subject teaching (lectures, seminars, practicals, textbook and guides provided in the subject) contain all the information you need for this assignment. You should not use online resources (e.g. , Google, Stack Overflow, ChatGPT, Copilot, other AI, etc.) to find resources or assistance because this would limit your learning and would mean that you would not achieve the goals of the assignment - mastering fundamental programming concepts and skills.
2025-04-18