r/ProgrammingPrompts • • Jul 07 '16

Give new life to an old game

50 Upvotes

Hello everybody!

It's been a fairly long time, but here is another challenge for you.

The rules are:

  • Any language (except the original BASIC or PC-Basic) allowed

  • Result should match the original as closely as possible

And here is the challenge:

Take the source-code from the game "The Oregon Trail" and convert it into a language of your choice.

The source code that acts as reference for this project is here.

The purpose of this Programming Prompt is to learn how to read and work with legacy code (in a language you probably don't know) and to convert this code into a more modern language.

Good luck & have fun!


r/ProgrammingPrompts • • Dec 04 '16

Three amazing C projects to hone your skills with!

Thumbnail
github.com
30 Upvotes

r/ProgrammingPrompts • • Jan 31 '15

[C/C#/C++/VB] [Easy] [Hard] Generating music with the internal speaker and writing a simple musical notation processor.

29 Upvotes

Here is an interesting project for those of you programming in Windows. The goal is to create a simple musical notation processor which you can use to amuse or annoy your friends. I've written this for the benefit of beginners who are tired of manipulating characters.

Your computer has an internal speaker that can only emit a beep, the loudness of which is independent of the volume setting. However, by specifying its pitch and length, we can use it to generate music. For example,

This project will require the use of two functions that are native to Windows—beep and sleep. If you are not using Microsoft Visual Studio, you will need to write,

#include <windows.h>

beep takes two arguments—frequency and duration.
sleep takes one argument—duration.

Frequency is measured in hertz and must be an integer from 37 to 32,767. Other values will result in a crash.
Duration is measured in milliseconds.

For example,

#include <windows.h>

int main(void)
{    beep(261, 500);
     sleep(1500);
}

This program will beep at 261 Hz for 500 ms, pause for 1,500 ms, then terminate.


Practice

Create a generative music program by using random numbers as frequencies and durations. Make sure to keep the durations short or else you will have to endure a lengthy cacophony.


A basic understanding of musical notation is recommended. Here are some important references.

  1. Beats per minute - Tempo calculator

  2. Note value

  3. Piano key frequencies

It would be tedious to write the exact frequency and duration of every note in hertz and milliseconds. Fortunately, there exists a formula which converts the position of a note on a standard piano into its frequency.

f(n) = 2^((n - 49) / 12) * 440 Hz

Problem 1

Implement a function that converts the position of a note into its corresponding frequency. For example, 40 corresponds to 261 Hz.


Although we could just hard code the music, our program will be far more usable if it can read the music from a file. The next problems will require you to design a notation for this purpose.


Problem 2

Implement a function that converts the name of a note into its corresponding position. For example, C4 corresponds to 40, C4♯ and D4♭ correspond to 41, D4 corresponds to 42, et cetera.


Problem 3

Implement a function that converts a note value into its corresponding duration. For example, given a tempo of 120 beats per minute and a beat unit of 4, the quarter note corresponds to 500 ms, the half note corresponds to 1000 ms, the whole note corresponds to 2000 ms, et cetera. The tempo and beat unit should be variable.


Problem 4

Implement a parser that can read your notation from a file.


Done? Test it out by transcribing Flight of the Bumblebee and playing it. Adjust the tempo.


Intermediate Challenges

  • Reduce redundant notation. If a note is of the same duration or octave as the note before it, then its duration or octave can probably be omitted.

  • Implement key signature to indicate that certain notes are to be automatically flattened or sharpened. This will also require implementing the [natural accidental](http://en.wikipedia.org/wiki/Natural_(music)).

  • Implement arpeggio.

  • Implement glissando.

  • Implement repetition.


Advanced Challenges


r/ProgrammingPrompts • • Feb 27 '17

Challenge: Program a text based RPG in Java using objects and libraries.

28 Upvotes

If Java isn't your cup of tea, feel free to use any other object oriented language.

The goal is to make an RPG that continues playing, spawning battles and item drops randomly (enemies increase gradually in difficulty.) You should have the following attributes to your character: Name, attack, defense, HP, mana, stamina, and xp.

Challenges: Come up with a way to increase character level, each level up increasing an attribute and increasing xp needed for next level up.

Enemies should be objects too. They should get more difficult as the game progresses.

Items should drop randomly from enemies.

Have any more challenges? Comment below. I am working on this program myself and would like to hear suggestions for challenges!


r/ProgrammingPrompts • • Feb 14 '18

Valentines day programming puzzle I wrote for my wife -- give it a try. Requires some experience with Java and involves the Ethereum blockchain.

26 Upvotes

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 • • 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 • • Mar 08 '14

The Mega Project List

Thumbnail
github.com
23 Upvotes

r/ProgrammingPrompts • • Jun 07 '21

Write a network server that serves a SQLite database in your dominant language. Write a client in a different language.

22 Upvotes

r/ProgrammingPrompts • • Nov 04 '14

Write a game of Battleships (aka Sea Battle) - to revive the subreddit

22 Upvotes

Battleships

Nearly everybody knows the game of battleships.

Wikipedia Battleships rules

The game board is a 10 by 10 matrix for each player with a second 10 by 10 matrix to track the player's shots. The matrix is numbered (0 to 9) across the vertical axis and uses letters across the horizontal axis (A to J).

Each player places a number of ships on the game board. The ships can be placed vertically or horizontally, but not diagonally.

Optional rule: Ships must not be placed adjacent to other ships. At least a single square must be empty between the ships. This rule applies also to corners.

The ships in detail are:

  • 4 submarines (1 square each)

  • 3 destroyers (2 squares each)

  • 2 cruisers (3 squares each)

  • 1 battleship (4 squares)

The players take turns shooting at squares.

Shots are taken in the form of Strings consisting of 2 characters. First character represents the Column (letters A to J) and second character is the row number (0 to 9).

The other player then answers with "hit" or "miss".

In case of a "hit", the current player gets another shot.

In case of a "miss", the other player gets to shoot.

A player can shoot until he misses.

If a ship is destroyed, the reporting player reports "sunken" in addition to the "hit" report.

If all ships of a player are destroyed, this player loses the game.


The programming task is to create a battleship game.

As a basic goal it should:

Single player vs computer - single computer strategy

  • provide and display the two matrixes for the player

  • handle setting up of the ships (optionally including the optional rule from above)

  • create a computer player that competes against the human

  • handle the game loop (turns)

  • decide winning conditions

  • display a simple help screen with the basic rules

  • provide a GUI for input (can be textual, or graphical, but when the GUI is graphical, it should also respond to mouse clicks for shots).

Optional extensions####

Single player vs computer with different computer strategies

  • create different computer strategies to place the shots

Computer vs Computer with different strategies

  • Allow for a computer against computer game and display each round from the perspective of one of the computers

Multiplayer over LAN

  • allow two players to compete against each other over LAN connection

  • either player only sees their boards

Game recording

  • create a system to record the games (board setup for both players, moves of the players)

Game saving

  • Allow interruption of a human vs computer game at any point and save the game state to resume later.

Different ship types and numbers

  • Allow to alter the composition of the fleet (the number, naming, and sizes of the ships). The fleet must be identical for both players.

Other optional rules as described in the Wikipedia article linked on top

Final statement####

Use a programming language of your choice. It's also allowed to create a browser / server based version of the game.

Most important:

Have fun with the challenge##


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 • • Jun 14 '20

Determine the least number of coins possible to make change

21 Upvotes

This is a somewhat simple one inspired by my working at a grocery store. Write a program capable of telling you the smallest number of coins you can make change with for a given amount. Examples, 25¢ should return 1, 35¢ should return 2, and 58¢ should return 6. I’m working off of American currency here but whatever you use should work just fine too as long as you adjust for it.

Bonus features for a extra work:

  1. Make the program also tell you what coins you received

  2. Write this as a recursive method

  3. Add a feature that allows the program to make change if you’re out of a certain coin. Example: 20¢ can be made with two dimes but if you’re out of dimes you’d need 4 nickels. You can take this further by making it able to work with only a certain number of coins. For example, having one dime, you can make change for 20¢ with a dime and two nickels.

  4. Combine ideas 2 and 3

  5. Create a register class that keeps track of how many of each coin you have and totals up how much is in the register, this is well combined with idea 3.


r/ProgrammingPrompts • • Nov 13 '14

Write Your Own Shell!

21 Upvotes

Write your own shell thread!

Inspired by the common second/third year university assignment, designed to teach you about processes, IPC and handling user input.

A shell is a command line interpreter, it takes commands from the user and executes them. A common example is Command Prompt on Windows or Bash often found on *NIX systems.

Checkout Wikipedia for more information on a shell (though if you're unfamiliar with what a shell is this may be too difficult for you).

Foreground and Background execution (&) Unnamed pipes ( | ) Input/output redirection (< and >) Comments (#) Built in commands (cd, exit, ?)

I did this assignment in third year and it was a blast. I recommend using C or C++ for this programming prompt. It could easily be implemented in many other languages too, though. There are plenty of resources on the web like this: http://linuxgazette.net/111/ramankutty.html

Will update when not on mobile. Sorry for not including more of a spec.

If you're an up and coming programmer who is interested in this post below and I'll write up a how to and my solution


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 • • Dec 02 '15

[META] Looking for a daily coding challenge during Advent? - Try Advent Of Code - Daily small code challenge.

Thumbnail adventofcode.com
20 Upvotes

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 • • Jun 11 '19

Make a tool that scrapes the change-logs of a wikimedia image to make an animated gif

19 Upvotes

I’m looking around the web for an animated gif of the maps of LGBTQ rights around the world, found here:

https://commons.m.wikimedia.org/wiki/File:World_laws_pertaining_to_homosexual_relationships_and_expression.svg#mw-jump-to-license

Disappointed that such a thing doesn’t seem to exist, I thought it would be great to have a software tool to automatically create this. And if one were making such a tool, it might as well work for any Wikimedia image with a change history.

(And I guarantee that this particular LGBTQ rights map animated gif would be front page material on /r/dataisbeautiful or /r/MapPorn.)

If anyone does decide to make this tool, please let me know by tagging my username when you release it!


r/ProgrammingPrompts • • Jul 23 '17

AI to generate ShowerThoughts

19 Upvotes

1.) Acquire large data set of relatively factual information (all of Wikipedia?)

2.) Exclude sentences that aren't declarations

3.) Remove sentences with uncertain context

4.) Feed these cleaned declarative statements to a deep learning algorithm

5.) Wait some time

6.) Ask it to spew out some content


r/ProgrammingPrompts • • Jul 06 '15

[Easy to Hard] A more interesting challenge - a minimal version control system.

19 Upvotes

Ever use Git, Mercurial, CVS, or Subversion? Of course you have. For this project, I think it would be fun to do a very basic version control system (from command line). It can be as simple as 'copy the entire folder' to as complex as you want. Let's keep it minimal - maybe bonus points if it's under 500 characters?


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.


r/ProgrammingPrompts • • Nov 10 '14

Game component: Die and Dicebag

18 Upvotes

This Programming Prompt is only part of a bigger idea.

In the near future, I am planning to post a series of dice games as programming prompts.

To make the development of the future dice games easier, I ask that you make a library, class, template, module, framework (whatever you want to call it) to handle dice.

Concept and Requirements

  • Use a programming language of your choice


Single Die
  • Start by simulating a single n-sided die

    • The constructor (or equivalent in the language of your choice) of the die should take the number of sides as a parameter. A no-parameter constructor should default to a 6-sided die

    • A die can be rolled and returns the result in the range from 1 to n inclusive (of course, the returned values are whole numbers, no decimals)

    • A die can be held so that it always returns the result of the last roll before the die was held.

    • It should be possible to query the hold state of a die.

    • It should be possible to always query the result of the last roll without re-rolling.

  • Use the random feature of the language of your choice, no need to invent your own random algorithm (but you can do so, if you insist).

  • There should be a nice, textual presentation of the roll result.

  • Once the single die simulation is complete, create a dicebag consisting of several dice.

Optional######
  • Allow for non-numeric die sides (a two-sided die could have "Head" and "Tail" on the sides, a Fate die has "+", "-", and " " (blank) on it's sides, etc.)

  • Allow for a die-color (in text only "Red", "Black", etc.)


Dicebag
  • A default dicebag does not contain any dice but can hold any number of dice.

  • It should be possible to add and remove dice at any time from the dicebag.

  • Allow adding of multiple dice with the same sides, or with different sides.

  • Allow adding of previously created "Die" objects.

  • the dicebag can hold m dice which all can have different sides (RPG-dice)

    • To achieve this, use the standard notation for dice which is mdn where m is the number of dice and n represents the sides of the dice. (5d6 would mean 5 6-sided dice) - The more advanced form including arithmetic operators is not necessary. (for example 5d6 + 3d4)

  • It should be possible to roll all dice (roll result as an array) or roll any specific die(roll result as an integer)

  • It should be possible to get the last-roll results for all dice or for any specific die (see above)

  • It should be possible to query the sum of the last-roll

  • It should be possible to set each die on hold, or to release each die.

  • It should be possible to query the hold state of all dice or of any single die.

  • There should be a nice, textual presentation of the roll results (I envisioned something like "1-4-6" (to display a 3d6 roll)

  • There should also be a way to retrieve the amount of dice in the dicebag.


As a final result, you should have a re-usable piece of code for many different dice games.

If anybody has reasonable suggestions, please state them in the comments and I will be happy to add them to this post.

Please post your solutions here so that others willing to participate in the following prompts may use your libraries

Edit: The goal of this post is to have standard die and dicebag libraries for as many languages as possible.

Edit #2:

Code can be uploaded either directly in the comment (but this is only for very short code - less than 50 lines or so), or, commonly pastebin.com is used for code that fits in a single file, for code in multiple files, gist.github.com is commonly used.

Here is a guide on code posting in reddit - it's for Java, but applies to all languages

Edit #3:

Languages covered so far:


r/ProgrammingPrompts • • Nov 09 '14

Anybody up for a game of Hangman?

18 Upvotes

Anybody up for a game of Hangman?##

I know, it's one of the most common programming tasks given (and one ofthe most implemented programs), but let's mention it here again.

Write an implementation of the Hangman game in the language of your choice.

	  +----------+
	  |  /       |
	  | /      (-_-)
	  |/         |
	  |         /|\
	  |        / | \
	  |          |
	  |         / \
	  |        /   \
	  |
------+-----------------

I've chosen the above shape to be the display. (I kept it deliberately simple without Unicode characters.) This gives a total of 10 wrong letters before the game is over.

The User interface can be simple terminal or a GUI of you like.


Minimum requirements:

  • Use a wordlist file (some links supplied at the end of this post)

  • Allow the user to select certain minimum and maximum word lengths between 7 and 15 characters and use only words of this length

  • Handle the Game loop and display the results

  • Display "_" (underscores) for each obscured (not yet guessed) letter

  • Track already guessed letters and warn the user about duplicate guesses (don't add them to fail/right again)

  • If the user guesses a correct character, all instances of the character in the word should be opened


Optional features:

  • Allow the user to set the wordlist file

  • Allow the user to edit the (or create their own) wordlist file

  • Allow user vs. user play (one user enters a word which is compared against the wordlist), the other user guesses. Next round, the roles are reversed.


The steps for each failure

The drawing starts with the bottom only:

------+-----------------

Then the vertical column should be displayed:

	  +
	  |
	  |
	  |
	  |
	  |
	  |
	  |
	  |
	  |
------+-----------------

Then the horizontal hoist beam:

	  +----------+
	  |
	  |
	  |
	  |
	  |
	  |
	  |
	  |
	  |
------+-----------------

The reinforcement follows:

	  +----------+
	  |  /
	  | / 
	  |/  
	  |   
	  |   
	  |   
	  |   
	  |   
	  |
------+-----------------

The head on the rope:

	  +----------+
	  |  /       |
	  | /      (-_-)
	  |/
	  | 
	  | 
	  | 
	  | 
	  | 
	  |
------+-----------------

The body dangling:

	  +----------+
	  |  /       |
	  | /      (-_-)
	  |/         |
	  |          |
	  |          |
	  |          |
	  |        
	  |        
	  |
------+-----------------

With one hand:

	  +----------+
	  |  /       |
	  | /      (-_-)
	  |/         |
	  |         /|
	  |        / |
	  |          |
	  |       
	  |       
	  |
------+-----------------

With both hands:

	  +----------+
	  |  /       |
	  | /      (-_-)
	  |/         |
	  |         /|\
	  |        / | \
	  |          |
	  |        
	  |        
	  |
------+-----------------

With one leg:

	  +----------+
	  |  /       |
	  | /      (-_-)
	  |/         |
	  |         /|\
	  |        / | \
	  |          |
	  |         / 
	  |        /  
	  |
------+-----------------

Game over:

	  +----------+
	  |  /       |
	  | /      (-_-)
	  |/         |
	  |         /|\
	  |        / | \
	  |          |
	  |         / \
	  |        /   \
	  |
------+-----------------

Wordlist links

Have Fun!###


r/ProgrammingPrompts • • Mar 08 '14

Very simple procedurally generated map

18 Upvotes

I think this could be a good exercise/project for anyone!

What is it?


In this project the programmer will create a "map" which will be printed out to the console. This map can be any size the programmer wants, however it needs to be different every single run.

Rules (optional)


There should be multiple different tiles with their own rules and symbolized by a unique character (chosen by the programmer):

Ground - The base of the map, no special rules

Water - Should form in "lake(s)"

House - Should only form on ground tiles

Boat - Should only form on water, if one of the surrounding tiles are a house tile

Example outputs


In this case, tiles are represented as:

Ground - "#"

Water - "~"

House - "■"

Boat - "U"

10x5 Map:

##########
##~■#■~###
#~~~■~~~##
##U■~~~###
#####~####

20x10 Map:

########~###########
#######~~~##########
########~###########
####################
#####~##############
####~~~■############
##■##~##############
###■################
###############■###■
####################

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.