r/ProgrammingPrompts • • Mar 08 '14

Monopoly Dice

22 Upvotes

Very simple prompt, and good for beginners to practice working with loops and if statements.

Write a code that takes a list of players as input (or hard code the names into the function if needed..) and returns three numbers (die 1, die 2, and the sum both) for each player's "turn". I wrote this in Python (beginner myself :p ) but this can be done in other langs.

Tip: We want the loop to be infinite (since the game doesn't stop after each player rolls once). Also, remember the rules about rolling doubles in monopoly.

This can actually be useful if you've managed to lose your dice for any board game or just want to speed up play time.

Have fun!


r/ProgrammingPrompts • • Mar 08 '14

Create a square that bounces off the edges of your screen! [javascript]

24 Upvotes

This may be difficult for beginners but for those that are able to hack around on codepen.io or the like could find this quite fun!

Please share your solutions - Be creative!

Edit: this doesn't just have to be in javascript! If you know how to do this in ANY language, you should share it too!


r/ProgrammingPrompts • • Dec 04 '16

Three amazing C projects to hone your skills with!

Thumbnail
github.com
30 Upvotes

r/ProgrammingPrompts • • Feb 03 '26

No AI Prompts

12 Upvotes

This subreddit is not for Prompt Engineering, Vibe Coding, AI Prompting.

This is a place for little programming challenges or simple project ideas.


r/ProgrammingPrompts • • Mar 18 '15

[Easy]Mathematical simulation: Shooting PI

12 Upvotes

In honor of the PI Day of the century (but a bit late), I want to present an unorthodox, but mathematically correct version of estimating PI.

PI is the most commonly known constant. It is the ratio between the circumference and the radius of a circle.

PI is also an irrational number. It has (theoretically) infinite digits.


Let's get shooting.

Imagine a square with a constant length. Inside this square a quarter of a circle is drawn with the center of the circle at the bottom left corner and the radius equal to the length of the square.

See this Wikimedia image

The mathematical definition of a circle is:

A circle is the set of all points in a plane that are at a given distance (r) from a given point, the centre.

Using the above we can create our approximation:

  • We fire a number of shots on the rectangle. (Assume that all shots hit the rectangle, no misses).

  • Each shot has a random x and a random y coordinate in the range 0 to length inclusive.

  • Calculate the distance from the bottom left corner (x = 0, y = 0) by using the Pythagorean theorem: d = square root of (x^2 + y^2)

  • Check if the calculated distance d is less than or equal to the length of the square.

    • If the distance is less or equal (the shot is inside the quarter-circle), increment a counter.

  • Once all shots are fired, the ratio of hits to total shots equals to PI/4

  • Output the approximation by printing (hits/total shots) * 4.

This approximation uses a mathematical model called Monte Carlo Method.

The program should:

  • Ask for the length of the square

  • Ask for the number of shots to fire

  • Perform the calculation as outlined above

  • Output the result

  • Ask if another simulation should be run. If so, start from the beginning.

All programming languages are allowed.

Have fun shooting


Challenges

  • Display the results graphically, as in this image (same as linked above).

  • Allow the user to run a series of tests with varying lengths and numbers of shots.


r/ProgrammingPrompts • • Nov 16 '15

[Easy] Music Playlist using links with Counter

4 Upvotes

Make playlist either in browser[JavaScript] or using some other language so that a list of music(links) can be played by adding links.

Also Add a countdown timer on to the program for 5 minutes so that a new song can be played once the old one is done.

(It would be good if the new song was not played in a new tab so that by the end there aren't 100 tabs or kill each tab after the 5 minute countdown)


r/ProgrammingPrompts • • Oct 05 '17

Replicate this Demo - Random Chance Activity

3 Upvotes

I made this demo using the PHP based Hashtag Framework: Random Chance Activity

I'm looking for volunteers to provide example solutions to replicate the demo using any other framework or language, preferably on a live server.

Just to be clear about what i'm looking for... assume these requirements for a Web App:

  • Every 3 reloads of the page by the same end-user, a record is created in a list seen by all other users reloading the same page.

  • For every reload, each record has a 10% chance of being linked to a new attribute that brings with it a chance of the record being marked as "lost" on later reloads, excluding it from the main list.

  • There are 3 unique chance of loss attributes, that each apply an independent chance per reload of the record being marked as "lost".

That's it... so, given those requirements, how would you build a Web App that would solve those requirements?


r/ProgrammingPrompts • • May 26 '14

Wa-Tor - Population Dynamics Simulation

15 Upvotes

WA-TOR##

Alexander K. Dewdney and David Wiseman developed a simple population dynamics simulation that was published in the December 1984 issue of Scientific American.

The introduction to Wa-Tor by Alexander K. Dewdney:####

Somewhere, in a dimension that can only be called recreational at a distance limited only by one's programming prowess, the planet Wa-Tor swims among the stars. It is shaped like a torus, or dougnut, and is entirely covered with water.

The two dominant denizens of Wa-Tor are sharks and fish, so called because these are the terrestrial creatures they mist closely resemble. The sharks of Wa-Tor eat the fish and the fish of Wa-Tor seem always to be in plentiful supply.

Wa-Tor is a time-discrete simulation. Each step is a "tick" in time. The program should calculate the next step and display it after the calculation is completed.

For simplicity, the planet is shaped like a torus, or doughnut. Basically, it can be simulated by a two-dimensional matrix (or array) where the top and bottom ends, and also the left and right ends wrap around (Hint: modulo operation).

References####

The simulation needs a few parameters to initialize the original population:#####
  • Initial number of fish in the ocean

  • Initial number of sharks in the ocean

  • Age at which fish breed (Once a fish breeds, the age of the breeding fish and of the offspring are set to 0)

  • Age at which sharks breed (same rule as for fish)

  • Time after which a shark that hasn't found food dies of starvation

There are a few rules for the fish and sharks:#####
  • At the beginning, the sharks and fish are randomly distributed across the grid.

  • The initial age of each shark and fish is randomly chosen between 0 and breeding age.

  • There can be only one animal (fish or shark) in single grid cell after each tick (during a tick it is possible that a shark and a fish are in the same cell because the shark eats the fish. After the tick, only the shark will be left in the cell)

  • Both, fish and sharks, move only in the cardinal directions (North, South, West, and East)

  • Fish swim in random directions at each tick

  • Sharks hunt - if there are fish in the surrounding cells sharks will pick a cell with a fish and eat it. If there are no fish surrounding the shark, the shark will move like a fish in a random direction.

  • If an animal reaches the breeding age, the animal first moves according to the movement rules, then a new animal of the same type is spawned in the cell from which the old animal moved, and the age of both, the parent and the offspring is reset to 0.

  • If a shark does not find any food before it reaches the starvation age, it dies and disappears from the grid. If it finds food, the starvation timer is reset to 0.

Expected Output at the End of each Tick####

  • Number of fish and difference between previous and current population

  • Number of sharks and difference between previous and current population

  • A simple graphical representation of the ocean (in the original program, a blank (space) was used for an empty grid position, a period (.) was used for a fish, and a zero (0) was used for a shark.

Additional Info####

  • There should be a way to select whether to manually advance the generations, or to automatically advance the generations (up to a certain limit) with a display interval (user configurable).

  • In the original program, the ocean was 32 cells wide and 14 cells high. For modern computers this setting is too small, so the ocean should be 80 cells wide and 40 cells high (or user configurable).

Optional Extensions####

  • Allow for diagonal movement for both, fish and sharks

  • Make fish more intelligent: let them sense sharks (with a configurable probability) in a configurable distance (up to 3 cells) and if the sense sharks, let them take evasive action

  • Let sharks detect fish in a configurable range (up to 3 cells)


r/ProgrammingPrompts • • Mar 18 '15

[Easy]Mathematical Simulation - Breaking a Stick to form a Triangle

15 Upvotes

Another Monte Carlo simulation.

A stick of a given length is twice broken at random places along it's length.

Calculate the probability that a triangle can be formed with the pieces.


Definition:

It is possible to form a triangle if none of the pieces are larger than half the length of the stick.


Assignment:

Create a program in a language of your choice that:

  • Asks the user for the length of the stick

  • Asks the user how many tries should be carried out

  • Simulate the breaking of the stick (each try has a new stick of length)

  • Count the successful tries

  • Print the probability as a percentage

Hint: The expected result should be around 23%

Have fun!


r/ProgrammingPrompts • • Jan 14 '15

[Easy] Letter Counter

17 Upvotes

Any language permitted.

Problem: Create a program where you input a string of characters, and output the number of each letter. Use vowels by default.

For example: asbfiusadfliabdluifalsiudbf -> a: 4 e: 0 i: 4 o: 0 u: 3

Bonus points: Make it command-line executable, with an optional mode to specify which letters to print.


r/ProgrammingPrompts • • Jan 11 '15

[Game Component] - Card Game Components

12 Upvotes

This will probably be my last prompt for a couple of weeks as my job requires me to travel and I don't know if I will have an open internet (or any internet at all) at my travel destination/job site.


This time it's again about re-usable game components.

Part 1: Playing Card

Create a single playing card for card games.

  • Standard Playing card:
    Each playing card has a

    • Suit (Hearts, Clubs, Diamonds, Spades)

    • Name (2,3,4,5,6,7,8,9,Jack,Queen,King,Ace)

    • Numeric Value (number cards have their numeric value equal to their name, Jack, Queen, King have each a value of 10, Aces can be either 10 or 1 depending on the setting of acesHigh)

    • acesHigh value that determines if the Aces count as 1 (acesHigh = false) or as 10 (acesHigh = true)

    • sequential value (from 0 to 12 where the "2" card has sequential value 0 and the Ace has sequential value 12)

    • optional a graphical representation of the card

    • optional a Unicode representation of the card in the form <suite symbol><card name> (the unicode values for the suits are: Spade: U+2660 (hex), Heart: U+2665 (hex), Diamond: U+2666 (hex), and Club: U+2663 (hex), the values can also be found at Wikipedia - Playing Cards in Unicode

    • textual representation in the form "<name> of <suite>" (e.g. "2 of Hearts", "Ace of Spades")

  • optional Joker:

    • By default, the Joker has no suit and no value. It only gets the suit and value assigned when the card is played.

  • Custom Playing card:
    Custom Playing cards can have:

    • a Color (as text) - similar to suite

    • a Name

    • a numeric value

    • a sequential value

    • optional a graphical representation of the card

    • textual representation in the form "<name> of <suite>" (e.g. "2 of Hearts", "Ace of Spades")


Part 2: Deck (general Storage for a number of cards)

Create a deck (discard pile, player hand) of playing cards for card games

  • A deck can hold an arbitrary number of Cards (either Standard cards, or Custom cards).

  • The deck should provide methods for adding to and removing cards from the top of the deck.

  • The deck should provide a method for shuffeling

  • The deck should provide medhods for direct card access (based on index) - adding and removing

  • The deck should provide a method to randomly pull any card from the deck


Optional - Part 3: Shoe (storage for a number of decks)

In Casino games, like Blackjack, usually multiple decks of cards are used.

The Shoe should be able:

  • To hold any number of identical decks

  • To remove the to card of the shoe

  • To shuffle the whole shoe


If anybody has further ideas, please comment and I'll add them.

Happy coding!


r/ProgrammingPrompts • • Mar 08 '14

The Mega Project List

Thumbnail
github.com
23 Upvotes

r/ProgrammingPrompts • • Sep 26 '15

[Easy/Medium] Tic-Tac-Toe

15 Upvotes

Write a program that will allow two players to play Tic-Tac-Toe.

The program should accomplish the following:

  • Allow the player to choose their symbol between X and O

  • Allow the player to choose who goes first

  • Display an empty board once the game starts

  • Display the board after each move, updated with that move

  • Display the board and end the game when the win condition is met, or there is a draw

The program must also ensure that:

  • Players cannot overwrite each other's moves

Deciding how the player will input their move is up to the programmer.

Additionally, write an algorithm for an AI to use in single-player mode.

Along with the abovementioned points, the AI should ideally:

  • Identify its best play, and

  • Block the player if they are going to win

Bonus challenge: Create a similar program for 3D Tic-Tac-Toe. This is played on three 3x3 grids, one on top of the other. Players win by forming squares that line up vertically, horizontally or diagonally, on a single grid or evenly across all grids. For example, Grid 1 [0,0], Grid 2 [0,1], Grid 3 [0,2] forms a horizontal line down the grids, and is therefore a win. The game must identify this win condition.

The program should ideally represent this visually, on console or otherwise. If creating an AI, it should ideally follow the points above as well.


r/ProgrammingPrompts • • Mar 07 '14

Virtual Decoder Ring!

20 Upvotes

Write a code that encrypts strings the same way a decoder pin works (by shifting two identical alphabets i.e. a shift of 3 would cause 'a' to encrypt to 'd'). It's also known as a Caesar cipher because it was used by Julius Caesar.

See if you can have the user determine the number of shifts!


r/ProgrammingPrompts • • Mar 08 '14

[Html, CSS] Create an Analog Clock Using the Canvas

Thumbnail
kirupa.com
10 Upvotes

r/ProgrammingPrompts • • Mar 08 '14

MOD: removal of link submissions

8 Upvotes

I have removed link submissions because I was looking forward to this subreddit focusing on conversationally creating and comparing projects and I feel that links to already created lists of projects may take away from this. However, I am aware that it is still possible to link to these pre-made lists by way of an in-text link. Please share your opinions on this decision! Thanks!


r/ProgrammingPrompts • • Mar 08 '14

Decimal to Roman numeral

12 Upvotes

A program that will output a roman numeral from a decimal.


r/ProgrammingPrompts • • Mar 08 '14

Reverse Checkers AI

12 Upvotes

The rules of reverse checkers are:

if a player can make a jump, he must make that jump. If there is a double jump, he must make the double jump.

Try and create the game. Now try adding an AI to play the game. If you're not familiar with building AI's, this would be a great chance for you to get some experience, but it'll take some reading. The go to solution algorithm is the minimax algorithm, which looks at your best moves and your opponents best moves. http://en.wikipedia.org/wiki/Minimax

To increase efficiency implement alpha beta pruning. http://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning

Have fun. It was a fun project for me.


r/ProgrammingPrompts • • Mar 07 '14

Collatz Conjecture!

21 Upvotes

Create a program which performs and prints to the screen the Collatz Conjecture of any number input by the user.

(the Collatz Conjecture is a conjecture by which a number is taken and if it is even, the number is halved, and if it is odd, the number is tripled and added to by one.)


r/ProgrammingPrompts • • Jan 07 '15

[EASY][Beginner] UAGS (Universal Acronym Generating System)

14 Upvotes

This is an extremely simple prompt, just for fun.

It is suitable for beginners as it only uses basic input/output and string manipulation.

UAGS (Universal Acronym Generating System)

Acronyms are currently all the hype in all forms of communication.

Your task is to program an Acronym Generator.

  • The user inputs a few words for which the Acronym should be generated

  • The computer takes the first letter for each word

  • The first letter of each word is then capitalized

  • All first letters are then joined together to form the Acronym

  • The Acronym should then be printed

  • Ask the user if they need another acronym generated

Have fun coding!


r/ProgrammingPrompts • • Mar 02 '15

[Easy] Calculate the day of the week for any given date

24 Upvotes

This time, it's going to be an easy prompt.

The task is to calculate the day of the week for any given date in the range March 1900 and February 2100.

Use a programming language of your choice.

The user inputs any date in the above range and the program should calculate the day of the week.

I leave the choice of input format for the date up to you.

The calculation works as follows (I'll use the names year, month, day for the given date):

  • First, subtract 1900 from the given year

  • Multiply this value with 365

  • Add the missing leap days (year - 1900) / 4

  • If the year is a leap year, subtract 1 if the month is < 3. (Edit thanks to /u/Philboyd_Studge)

  • add the days of the months up to but not including month where February always has 28 days. (Leap years are already accounted for.)

  • Add the day

  • Take the resulting number modulo 7

  • The result is the day of the week (0 = Sunday, 1 = Monday, etc.)

Output the date and the day of the week in textual form.

Have fun coding!


r/ProgrammingPrompts • • Apr 06 '14

Programming ideas on HN. Some are more advanced than others. A lot of interesting ideas in here!

9 Upvotes

r/ProgrammingPrompts • • Dec 19 '14

[Christmas Prompt] - The Twelve Days of Christmas

10 Upvotes

This time, it will not be a game, but two small challenges revolving around the Song The Twelve Days of Christmas.

The song is a well-known Christmas carol.

In general, the structure is:

  • Verse 1:

    • On the first day of Christmas

    • my true love sent to me

    • a Partridge in a Pear Tree

  • Verse 2:

    • On the second day of Christmas

    • my true love sent to me

    • Two Turtle Doves

    • and

    • a Partridge in a Pear Tree

  • Verse 3:

    • On the second day of Christmas

    • my true love sent to me

    • Three French Hens,

    • Two Turtle Doves

    • and

    • a Partridge in a Pear Tree

And so on.

The remaining days are:

  • 4 Calling Birds

  • 5 Gold Rings

  • 6 Geese-a-Laying

  • 7 Swans-a-Swimming

  • 8 Maids-a-Milking

  • 9 Ladies Dancing

  • 10 Lords-a-Leaping

  • 11 Pipers Piping

  • 12 Drummers Drumming


And now for the puzzles:

  1. Write a program that displays the verse for any given day in the range 1 to 12 (both inclusive), or for all days. Challenge: The program should be as short as possible, the code should be as efficient (in terms of calculation) as possible.

  2. Write a program that calculates the total number of presents received at any given day in the range 1 to 2 (both inclusive), or the total number of presents received up to and including any given day, or the total number of presents in the whole song.

Have fun coding!


Merry Christmas everyone!


r/ProgrammingPrompts • • Aug 29 '15

[Easy/Medium] Write a program that translates verbal numbers (one hundred and six) into integers (106.)

18 Upvotes

Bonus points for negative numbers, or decimals (three point one four.)

I think this is kind of an easy concept to think about, but might take a while to type out. I'll try it at some point and post results, if I can even get it to work.

Bonus bonus points - make it work the other way round, by converting integers into words.