1 Replies - 853 Views - Last Post: 01 January 2017 - 04:44 PM

Poll: C# 'Magic Trick' console program (2 member(s) have cast votes)

Feedback(Positive and negative welcome)

  1. Interesting! (1 votes [50.00%] - View)

    Percentage of vote: 50.00%

  2. MOAR! (0 votes [0.00%])

    Percentage of vote: 0.00%

  3. Not great (1 votes [50.00%] - View)

    Percentage of vote: 50.00%

Vote Guests cannot vote

#1 TKXP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-September 15

C# 'Magic Trick' console program

Posted 22 December 2016 - 12:24 PM

Greetings all,
This is something I cooked up in a free period. Originally got the idea from here

I believe this will increase your love for magic read maths and coding. :lol:/>

The "magic trick" entails:
1. A person thinks of a number between 1 and 60.

2. The person is given 6 cards one-by-one and they are asked if their number appears on said card.

3. All cards where 'yes' was answered is put aside.

4. Summing the top-left value of each card on the pile from #3 will give you the guessed number.


A few screenshots of the program are also attached.
Attached Image
Attached Image
Attached Image

An executable file can be downloaded from here
(Note: the .exe file has been scanned with Avast DeepScreen, and it safe)

Here follows the code:

using System;

public class CProgram
{
	public static void Main()
	{
		do
		{
			Heading();
			char cFirst = Grid1();
			char cSecond = Grid2();
			char cThird = Grid3();
			char cFourth = Grid4();
			char cFifth = Grid5();
			char cSixth = Grid6();
			Console.Clear();
			int i = Output(cFirst, cSecond, cThird, cFourth, cFifth, cSixth);
			Console.WriteLine("Voilą, your number is " + i.ToString() + "\n\nPress any key to continue...");
			Console.ReadKey();
		}
		while (IsValid());
		Exit();
	}
	
	private static void Heading()
	{
		Console.WriteLine("Welcome to the Magic Show!\n\nPlease guess a number between 1 and 60\nPlease remember it :)/>/> \n\n\nPress any key to continue...");
		Console.ReadKey();
	}
	
	private static char Grid1()
	{
		Console.Clear();
		Console.WriteLine("Is your number in this grid? #1 of #6");
		Console.WriteLine("\n\t1\t11\t21\t31\t41\t51\n\t3\t13\t23\t33\t43\t53\n\t5\t15\t25\t35\t45\t55\n\t7\t17\t27\t37\t47\t57\n\t9\t19\t29\t39\t49\t59\n");
		Console.Write("Press 'y' for Yes and any other key for No ");
		char cInput = Console.ReadKey().KeyChar;
		return cInput;
	}
	
	private static char Grid2()
	{
		Console.Clear();
		Console.WriteLine("Is your number in this grid? #2 of #6");
		Console.WriteLine("\n\t2\t11\t22\t31\t42\t51\n\t3\t14\t23\t34\t43\t54\n\t6\t15\t26\t35\t46\t55\n\t7\t18\t27\t38\t47\t58\n\t10\t19\t30\t39\t50\t59\n");
		Console.Write("Press 'y' for Yes and any other key for No ");
		char cInput = Console.ReadKey().KeyChar;
		return cInput;
	}
	
	private static char Grid3()
	{
		Console.Clear();
		Console.WriteLine("Is your number in this grid? #3 of #6");
		Console.WriteLine("\n\t4\t13\t22\t31\t44\t53\n\t5\t14\t23\t36\t45\t54\n\t6\t15\t28\t37\t46\t55\n\t7\t20\t29\t38\t47\t60\n\t12\t21\t30\t39\t52\n");
		Console.Write("Press 'y' for Yes and any other key for No ");
		char cInput = Console.ReadKey().KeyChar;
		return cInput;
	}
	
	private static char Grid4()
	{
		Console.Clear();
		Console.WriteLine("Is your number in this grid? #4 of #6");
		Console.WriteLine("\n\t8\t13\t26\t31\t44\t57\n\t9\t14\t27\t40\t45\t58\n\t10\t15\t28\t41\t46\t59\n\t11\t24\t29\t42\t47\t60\n\t12\t25\t30\t43\t56\n");
		Console.Write("Press 'y' for Yes and any other key for No ");
		char cInput = Console.ReadKey().KeyChar;
		return cInput;
	}
	
	private static char Grid5()
	{
		Console.Clear();
		Console.WriteLine("Is your number in this grid? #5 of #6");
		Console.WriteLine("\n\t16\t21\t26\t31\t52\t57\n\t17\t22\t27\t48\t53\t58\n\t18\t23\t28\t49\t54\t59\n\t19\t24\t29\t50\t55\t60\n\t20\t25\t30\t51\t56\n");
		Console.Write("Press 'y' for Yes and any other key for No ");
		char cInput = Console.ReadKey().KeyChar;
		return cInput;
	}
	
	private static char Grid6()
	{
		Console.Clear();
		Console.WriteLine("Is your number in this grid? #6 of #6");
		Console.WriteLine("\n\t32\t37\t42\t47\t52\t57\n\t33\t38\t43\t48\t53\t58\n\t34\t39\t44\t49\t54\t59\n\t35\t40\t45\t50\t55\t60\n\t36\t41\t46\t51\t56\n");
		Console.Write("Press 'y' for Yes and any other key for No ");
		char cInput = Console.ReadKey().KeyChar;
		return cInput;
	}
	
	private static int Output(char _c1, char _c2, char _c3, char _c4, char _c5, char _c6)
	{
		int iOut = 0;
		if (_c1 == 'y')
		iOut += 1;
		else
		iOut += 0;
		if (_c2 == 'y')
		iOut += 2;
		else
		iOut += 0;
		if (_c3 == 'y')
		iOut += 4;
		else
		iOut += 0;
		if (_c4 == 'y')
		iOut += 8;
		else
		iOut += 0;
		if (_c5 == 'y')
		iOut += 16;
		else
		iOut += 0;
		if (_c6 == 'y')
		iOut += 32;
		else
		iOut += 0;
		
		return iOut;
	}
	
	private static void Exit()
	{
		Console.WriteLine("Thank you for playing!!\nPress any key to exit...");
		Console.ReadKey();
	}
	
	private static bool IsValid()
	{
		Console.Clear();
		bool bIsValid;
		Console.Write("Do you wish to play again?\nPress 'y' for Yes and any other key for No ");
		char cInput = Console.ReadKey().KeyChar;
		Console.Clear();
		if (cInput == 'y')
		bIsValid = true;
		else
		bIsValid = false;
		return bIsValid;
		
	}
}



Go on now, and show this to your friends. Tell them its an AI which can get into their minds. :scooter:/> :rockon:/>

Is This A Good Question/Topic? 0
  • +

Replies To: C# 'Magic Trick' console program

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5078
  • View blog
  • Posts: 13,705
  • Joined: 18-April 07

Re: C# 'Magic Trick' console program

Posted 01 January 2017 - 04:44 PM

The first time I read this I didn't actually realize that each card actually has the block of numbers on it. Only after looking at your link did I realize that. I thought you had said that if you give them 6 cards, each with a single number. Now knowing the trick in its entirety does it make it more magical. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1