# 30 Line code challenge!

Page 1 of 1

## 7 Replies - 2701 Views - Last Post: 03 September 2011 - 07:39 PM

### #1 frostyraver

Reputation: 13
• Posts: 195
• Joined: 21-April 09

# 30 Line code challenge!

Posted 13 August 2011 - 09:22 AM

Java is doing one, so why can't we!

Rules with this though:
1. Don't put everything on 1 line
2. Doing
```if (Guess > CorrectNumber){HighNumber = Guess;
```
3. No prize, just for fun! So challenge yourself! (You're only cheating yourself)
4. Condensing the Braces is fine.
5. It can be ANY thing you like, as long as it runs!
6. Just make sure you tell us what you have made!

Here's what I made, 30 line guessing game with error checking (For console)! It's also a great example to get started.

```using System;
namespace RandomAp{
class Program{
static void Main(string[] args){
int Guess = 0;
Random RNG = new Random();
int HighNumber = 99;
int LowNumber = 1;
int CorrectNumber = RNG.Next(1, 100);
string choice = "Y";
while (choice == "Y" || choice == "Yes" || choice == "y" || choice == "yes"){
Console.Write("Guess the number (" + LowNumber + "-" + HighNumber + "):");
if (Guess < CorrectNumber){LowNumber = Guess;
Console.WriteLine("Number is too low!");
Console.Clear();}
else if (Guess > CorrectNumber){ HighNumber = Guess;
Console.WriteLine("Number is too high!");
Console.Clear();}
else{ Console.WriteLine("You guessed the number!");
Console.WriteLine("Did you want to play again? (Y/N)");
HighNumber = 99;
LowNumber = 1;
CorrectNumber = RNG.Next(LowNumber, HighNumber + 1);}}
catch (Exception){ Console.WriteLine("Error! Use a number!");
Guess = 0;}}}}}

```

I'm interested to see what people make.

EDIT: Forgot all about the randomizer how it's not actually 100 but 99, no idea how I forgot, fixed all of that now.

This post has been edited by frostyraver: 13 August 2011 - 10:48 PM

Is This A Good Question/Topic? 1

## Replies To: 30 Line code challenge!

### #2 modi123_1

• Suitor #2

Reputation: 14151
• Posts: 56,726
• Joined: 12-June 08

## Re: 30 Line code challenge!

Posted 13 August 2011 - 09:38 AM

So what's the deal? Any program under thirty lines?

### #3 eclipsed4utoo

Reputation: 1533
• Posts: 5,972
• Joined: 21-March 08

## Re: 30 Line code challenge!

Posted 13 August 2011 - 01:40 PM

Moving this to the C# Programmers forum since it's not really a question.

Also, state what the application is suppose to do. Or is this just any application?

### #4 frostyraver

Reputation: 13
• Posts: 195
• Joined: 21-April 09

## Re: 30 Line code challenge!

Posted 13 August 2011 - 10:43 PM

Just any program, a game, or something that gets a letter from a text.

### #5 frostyraver

Reputation: 13
• Posts: 195
• Joined: 21-April 09

## Re: 30 Line code challenge!

Posted 13 August 2011 - 10:49 PM

eclipsed4utoo, on 13 August 2011 - 01:40 PM, said:

Moving this to the C# Programmers forum since it's not really a question.

Sorry about that, thought I was at the right forum oddly.

### #6 janne_panne

• WinRT Dev

Reputation: 428
• Posts: 1,047
• Joined: 09-June 09

## Re: 30 Line code challenge!

Posted 15 August 2011 - 07:43 AM

Just a quick movie "database":

```class Program{
static void Main(string[] args) {
System.Collections.Generic.List<Movie> movieCatalog = new System.Collections.Generic.List<Movie>();
System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(typeof(System.Collections.Generic.List<Movie>));
for (string filename = GetInput("Enter Movie database filepath:"), menuInput = GetInput("Select action: \n1) Print movies \n2) Add movies \n3) Remove movie \n4) Quit"); menuInput != "4"; menuInput = GetInput("Select action: \n1) Print movies \n2) Add movies \n3) Remove movie \n4) Quit")) {
if (System.IO.File.Exists(filename))
movieCatalog = sr.Deserialize(r) as System.Collections.Generic.List<Movie>;
if ((menuInput == "1" || menuInput == "3") && movieCatalog.Count == 0)
System.Console.WriteLine("No movies");
for (int i = 0; i < movieCatalog.Count; i++)
System.Console.WriteLine((i + 1).ToString() + ". " + movieCatalog[i].Name + ", " + movieCatalog[i].Year);
int movieIndex;
if (int.TryParse(GetInput(""), out movieIndex) && movieIndex - 1 > 0 && movieIndex <= movieCatalog.Count)
movieCatalog.RemoveAt(movieIndex - 1);
else
System.Console.WriteLine("Invalid value."); } }
for (string movieInput = "y"; movieInput.ToLower() == "y"; movieInput = GetInput("Enter another movie? (Y/N)"))
movieCatalog.Add(new Movie { Name = GetInput("Enter movie name:"), Year = GetInput("Enter movie year:") });
using (System.Xml.XmlWriter w = System.Xml.XmlWriter.Create(filename))
sr.Serialize(w, movieCatalog); } }
private static string GetInput(string message) {
System.Console.WriteLine(message);
[System.Serializable()]public class Movie{
public string Name { get; set; }
public string Year { get; set; }}

```

Features:
- Has a menu with 4 choices (print movies, add movies, remove movie, quit).
- Serializes movies to xml file and reads the file.

Edit:
I wonder if my for loops are very readable
But since this isn't that uncommon: for (int x = 0, y = 0; x < 100; x++, y++) I thought using few string there in initialization section wouldn't be that bad. Readers/writers don't get closed because it seems using statement's disposing is enough but normally I would flush and close them too in Using statement.

If someone still sees a line which could be squeezed out, let me know

Btw. nice challenge, I like trying to create short piece of code out of something that normally would take much more lines.

This post has been edited by janne_panne: 15 August 2011 - 07:56 AM

### #7 frostyraver

Reputation: 13
• Posts: 195
• Joined: 21-April 09

## Re: 30 Line code challenge!

Posted 17 August 2011 - 05:07 AM

janne_panne, on 15 August 2011 - 07:43 AM, said:

Just a quick movie "database":

```class Program{
static void Main(string[] args) {
System.Collections.Generic.List<Movie> movieCatalog = new System.Collections.Generic.List<Movie>();
System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(typeof(System.Collections.Generic.List<Movie>));
for (string filename = GetInput("Enter Movie database filepath:"), menuInput = GetInput("Select action: \n1) Print movies \n2) Add movies \n3) Remove movie \n4) Quit"); menuInput != "4"; menuInput = GetInput("Select action: \n1) Print movies \n2) Add movies \n3) Remove movie \n4) Quit")) {
if (System.IO.File.Exists(filename))
movieCatalog = sr.Deserialize(r) as System.Collections.Generic.List<Movie>;
if ((menuInput == "1" || menuInput == "3") && movieCatalog.Count == 0)
System.Console.WriteLine("No movies");
for (int i = 0; i < movieCatalog.Count; i++)
System.Console.WriteLine((i + 1).ToString() + ". " + movieCatalog[i].Name + ", " + movieCatalog[i].Year);
int movieIndex;
if (int.TryParse(GetInput(""), out movieIndex) && movieIndex - 1 > 0 && movieIndex <= movieCatalog.Count)
movieCatalog.RemoveAt(movieIndex - 1);
else
System.Console.WriteLine("Invalid value."); } }
for (string movieInput = "y"; movieInput.ToLower() == "y"; movieInput = GetInput("Enter another movie? (Y/N)"))
movieCatalog.Add(new Movie { Name = GetInput("Enter movie name:"), Year = GetInput("Enter movie year:") });
using (System.Xml.XmlWriter w = System.Xml.XmlWriter.Create(filename))
sr.Serialize(w, movieCatalog); } }
private static string GetInput(string message) {
System.Console.WriteLine(message);
[System.Serializable()]public class Movie{
public string Name { get; set; }
public string Year { get; set; }}

```

Features:
- Has a menu with 4 choices (print movies, add movies, remove movie, quit).
- Serializes movies to xml file and reads the file.

Edit:
I wonder if my for loops are very readable
But since this isn't that uncommon: for (int x = 0, y = 0; x < 100; x++, y++) I thought using few string there in initialization section wouldn't be that bad. Readers/writers don't get closed because it seems using statement's disposing is enough but normally I would flush and close them too in Using statement.

If someone still sees a line which could be squeezed out, let me know

Btw. nice challenge, I like trying to create short piece of code out of something that normally would take much more lines.

Oh wow nice! Yeah it's why I made this post, it's nice to see one who can give what advice.

### #8 Tearjerker

Reputation: 0
• Posts: 22
• Joined: 02-December 10

## Re: 30 Line code challenge!

Posted 03 September 2011 - 07:39 PM

Nevermind.

Nice jobs guys, gotta try to rework my stuff and repost it. sry.

This post has been edited by Tearjerker: 03 September 2011 - 07:47 PM