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

#1 frostyraver  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • 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;
is ok, it's still readable!
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 + "):");
                try{ Guess = Convert.ToInt32(Console.ReadLine());
                    if (Guess < CorrectNumber){LowNumber = Guess;
                        Console.WriteLine("Number is too low!");
                        Console.ReadLine();
                        Console.Clear();}
                    else if (Guess > CorrectNumber){ HighNumber = Guess;
                        Console.WriteLine("Number is too high!");
                        Console.ReadLine();
                        Console.Clear();}
                    else{ Console.WriteLine("You guessed the number!");
                        Console.WriteLine("Did you want to play again? (Y/N)");
                        choice = Console.ReadLine();
                        HighNumber = 99;
                        LowNumber = 1;
                        CorrectNumber = RNG.Next(LowNumber, HighNumber + 1);}}
	            catch (Exception){ Console.WriteLine("Error! Use a number!");
                    Console.ReadLine();
                    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  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13954
  • View blog
  • Posts: 55,699
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 frostyraver  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 frostyraver  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: 30 Line code challenge!

Posted 13 August 2011 - 10:49 PM

View Posteclipsed4utoo, 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.
Was This Post Helpful? 0
  • +
  • -

#6 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • 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))
                using (System.Xml.XmlReader r = System.Xml.XmlReader.Create(filename))
                    movieCatalog = sr.Deserialize(r) as System.Collections.Generic.List<Movie>;
            if ((menuInput == "1" || menuInput == "3") && movieCatalog.Count == 0)
                System.Console.WriteLine("No movies");
            else if (menuInput == "1" || menuInput == "3") {
                for (int i = 0; i < movieCatalog.Count; i++)
                    System.Console.WriteLine((i + 1).ToString() + ". " + movieCatalog[i].Name + ", " + movieCatalog[i].Year);
                if (menuInput == "3") {
                    int movieIndex;
                    if (int.TryParse(GetInput(""), out movieIndex) && movieIndex - 1 > 0 && movieIndex <= movieCatalog.Count)
                        movieCatalog.RemoveAt(movieIndex - 1);
                    else
                        System.Console.WriteLine("Invalid value."); } }
            else if (menuInput == "2")
                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);
        return System.Console.ReadLine(); }}
[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 :P
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

Was This Post Helpful? 0
  • +
  • -

#7 frostyraver  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: 30 Line code challenge!

Posted 17 August 2011 - 05:07 AM

View Postjanne_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))
                using (System.Xml.XmlReader r = System.Xml.XmlReader.Create(filename))
                    movieCatalog = sr.Deserialize(r) as System.Collections.Generic.List<Movie>;
            if ((menuInput == "1" || menuInput == "3") && movieCatalog.Count == 0)
                System.Console.WriteLine("No movies");
            else if (menuInput == "1" || menuInput == "3") {
                for (int i = 0; i < movieCatalog.Count; i++)
                    System.Console.WriteLine((i + 1).ToString() + ". " + movieCatalog[i].Name + ", " + movieCatalog[i].Year);
                if (menuInput == "3") {
                    int movieIndex;
                    if (int.TryParse(GetInput(""), out movieIndex) && movieIndex - 1 > 0 && movieIndex <= movieCatalog.Count)
                        movieCatalog.RemoveAt(movieIndex - 1);
                    else
                        System.Console.WriteLine("Invalid value."); } }
            else if (menuInput == "2")
                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);
        return System.Console.ReadLine(); }}
[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 :P
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.
Was This Post Helpful? 0
  • +
  • -

#8 Tearjerker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1