Saturday, December 22, 2012

Memory Test Game in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;


class MemoryTestGame
{
    static void Main(string[] args)
    {
        Console.WriteLine("On each turn you will be given a new number (1 - 20) that you have to remember");
        Console.WriteLine("All previous number will be displayed as well");
        Console.WriteLine("You have to memorize the new number and type it together with the old ones" +
            " (on seperate lines)");
        Console.Write("Press Enter to start:");
        Console.ReadLine();

        bool inputMatch = true;
        int[] list = new int[100];
        Random randGenerator = new Random();
        int numbersCount = 0;

        while (inputMatch)
        {
            int newNumber = randGenerator.Next(1, 20);       //generate new number
            list[numbersCount] = newNumber;

            for (int i = 0; i <= numbersCount; i++)         //print number sequence
            {
                Console.Write(list[i] + " ");
            }
            Thread.Sleep(2500);
            Console.Clear();                                //clear content

            Console.WriteLine("Type the numbers on new lines:");
            for (int i = 0; i <= numbersCount; i++)
            {
                int input = int.Parse(Console.ReadLine());
                if (input != list[i])
                {
                    inputMatch = false;
                    break;
                }
            }

            numbersCount++;
        }
        Console.WriteLine("You managed to guess {0} numbers", numbersCount-1);

    }
  
}

Better Brute Force in C#

using System;
using System.Threading;

namespace Brute
{
    class Infos
    {
        public static char[] val { get; set; }
        public static uint tries { get; set; }
        public static int max { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Infos.val = new char[5000];
            for (int i = 0; i < Infos.val.Length; i++)
            {
                Infos.val[i] = (char)64;
            }
            Thread otherWorker = new Thread(new ThreadStart(Work));
            otherWorker.IsBackground = true;
            otherWorker.Priority = ThreadPriority.Highest;
            otherWorker.Start();
            Thread.CurrentThread.Priority = ThreadPriority.Lowest;
            uint latestRez;
            while (true)
            {
                latestRez = Infos.tries;
                Thread.Sleep(2000);
                Console.Clear();
                Console.WriteLine((Infos.tries - latestRez) / 2);
                for (int i = 0; i <= Infos.max; i++)
                {
                    Console.Write(Infos.val[i]);
                }
            }
        }
        private static void Work()
        {
            Infos.max = 1;
            bool addOne = false;
            while (true)
            {
                while (Infos.val[0] < (char)122)
                {
                    Infos.val[0]++;
                    Infos.tries++;
                }
                Infos.val[0] = (char)65;
                Infos.val[1]++;
                Infos.tries++;
                for (int i = 1; i <= Infos.max; i++)
                {
                    if (Infos.val[i] == (char)122)
                    {
                        Infos.val[i] = (char)65;
                        Infos.val[i + 1]++;
                        Infos.tries++;
                        if (i == Infos.max)
                        {
                            addOne = true;
                        }
                    }
                }
                if (addOne == true)
                {
                    Infos.max++;
                    addOne = false;
                }
            }
        }
    }
}