Weekly Challenge #12 – Basic Nim

Nim is a mathematical game of strategy in which objects (Such as matchsticks) are organised into a series of rows. Two players take it in turns to remove one or more objects from a single row. The player who removes the final object is the loser of the game.

In this challenge, you will be creating a basic, playable version of Nim. 

Bronze

Write a subroutine which outputs the state of the Nim board at the start of the game. By default, the board should have four rows, each containing 1, 3, 5 and 7 objects.

|
| | |
| | | | |
| | | | | | |
      |
    | | |
  | | | | | 
| | | | | | |

Silver

Add functionality to your game so that users are able to take turns removing objects from rows. The game should end once the final object has been removed, and the program should output which player has won.

|
| | |
| | | | |
| | | | | | |
Player One: Enter a row: 2
Player One: How many objects should be removed? 2

|
|
| | | | |
| | | | | | |
Player Two: Enter a row:

Gold

Extend your program so that it now allows the user the option to play against the computer. The computer should randomly decide which row to remove objects from, and should remove a random number of objects from the selected row. 

Basic Nim Game
1) Play a Friend
2) Play vs Computer
Please enter your choice: 2

|
| | |
| | | | |
| | | | | | |
Player One: Enter a row: 2
Player One: How many objects should be removed? 2

|
|
| | | | |
| | | | | | |
Computer removes 4 objects from row 3

|
| | |
|
| | | | | | |
Player One: Enter a row:

Diamond

Modify your program further so that rather than playing every game with a fixed layout of 4 rows (Containing 1, 3, 5, 7 objects), the program randomly generates the number of rows and the number of objects contained within each row. 

Ensure your program contains sufficient validation throughout to prevent users entering invalid options (Such as removing too many objects, or selecting a row which doesn’t exist)

Weekly Challenge #11 – Hexadecimal Quiz

(It turns out that my definition of “weekly” differs from most…)

This week’s challenge follows on from Week #10, except this time we will be testing the user’s knowledge of hexadecimal numbers. I highly recommend attempting Week #10 before beginning this one, as many of the techniques covered in that challenge are also applicable here.

Continue reading “Weekly Challenge #11 – Hexadecimal Quiz”

Weekly Challenge #8 – A Trio of Ciphers

Not one, not two but three ciphers for you to program. For each cipher, write a program which asks the user to enter a plaintext message. Your program should then encrypt the message according to the rules shown in each task. Finally your program should output the encrypted version of the message.

One slight hurdle though – it’s up to you to work out what each cipher is doing!

Continue reading “Weekly Challenge #8 – A Trio of Ciphers”

Weekly Challenge #6 – Ranking System

We’re back in action after a lovely half-term holiday.

This is an implementation of a system for ranking players in a particular game. This is based on the ELO system utilised in various one-vs-one games such as chess. Fun fact, this was also found in the 2010 film The Social Network.

It’s a very maths-y challenge, and definitely the hardest one yet. Good luck!

Bronze

Create a program which asks the user to enter the names of two players. The program should then repeatedly ask which user won each match. After the result of each match has been entered, each player’s win percentage should be output.

Continue reading “Weekly Challenge #6 – Ranking System”

Weekly Challenge #5 – Packing Parcels

This is a (very) simplified version of the bin-packing problem. Feel free to explore this in more detail if you’re interested in learning how to optimise this algorithm.

Bronze

Laura is sending a parcel to her partner abroad. She wants to put in a variety of items, but can only send a maximum of 20kg. 

Write a program which asks the user to enter a the weight of each item. This should be repeated until a negative weight is entered.

Finally, output the total weight of the parcel. If the parcel is over 20kg, then an error message should be displayed.

Continue reading “Weekly Challenge #5 – Packing Parcels”

Weekly Challenge #4 – Time for Change

This challenge is a basic first step into the change-making problem, which is an interesting topic of research for mathematicians and programmers alike. 

For the purpose of this challenge, we will only be dealing with pence. If you want to make this more challenging, you can use pound coins and notes too.

Bronze

Write a program which asks the user to enter the price of a customer’s shopping bill (In pence). It should then also ask the user to enter the amount of cash the customer has handed over (In pence). The program will then calculate and output the correct amount of change to give the customer.

Continue reading “Weekly Challenge #4 – Time for Change”

Weekly Challenge #3 – Sentence Repetition

To complete these challenges, you should become familiar with splitting strings. These help you split a string into a list/array of individual words. For the gold challenge, you may also want to view my tutorial on selecting random items from a list in Python.

Bronze

Write a program which asks the user to enter a sentence. The program should then output the sentence, but with the first word of the sentence repeated at the end of the sentence.

For example: “Hello Word” would become “Hello World Hello”

Continue reading “Weekly Challenge #3 – Sentence Repetition”