Friday, April 3, 2015

Quick Match Cards game in c#

QuickMatch

Rules of QuickMatch

QuickMatch can be played as per the following rules:

n  It is a multi-player game. There can be 2 to 4 players.

n  Each player is given a set of five cards. The remaining cards are kept in a pile at the center.

n  The player can either draw a card from the pile of cards or pick a card, which is thrown by 
another player.

n  The first player to collect five cards of the same suite wins the game.



Instant play,download the game : click here


Design Specifications

The design of the game should be as per the following specifications:

n  It is a multi-player game. The number of players that can play the game can be 2 to 4.

n  The name of each player is accepted from the user before starting the game.

n  The cards are distributed to the players randomly. The remaining cards are kept in a pile at th
center.

n  The player whose name is entered first gets the first chance.

n  Other players get the chance in the sequence in which their names were entered.

n  The first player needs to pick the topmost card from the pile.

n  The card picked by the player gets added to his/her card collection. When a new card is added in the players card collection, the total number of cards in the collection exceeds five. Therefore, the player needs to discard a card from his/her collection and place it upturned so that the other players can see the card.

n  After the first player discards the card, the next player gets the chance to pick a card.
All players except the first player can either pick a card from the pile or pick the card that the previous player has discarded.


n  After a player discards a card, the players collection of cards should be checked to determine whether all cards belong to the same suite.









C# Code:


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

namespace ConsoleApplication9
{
    class user
    {
        public string name;
        public int[] cards=new int[5];
    }

   
    
    
    class Program
    {
        static string[] cardspack = new string[52];
        static bool[] flag = new bool[52];
    
        
        
        
        
        static int getcard()
        {
            int nn;
            Random r = new Random();
            while(true)
            {
            nn = (r.Next() % 52);
            if (flag[nn] == false)
                break;
            }
            flag[nn] = true;
             return nn;       
               
        }
        
        
        
        
        static void init()
        {
            for (int i = 0, j = 2; i < 52; i++)
            {
                flag[i] = false;
                string msg = "The ";
                if (i % 13 == 0)
                {
                    msg += "king ";
                    j = 2;
                }
                else if ((i - 1) % 13 == 0)
                    msg += "queen ";
                else if ((i - 2) % 13 == 0)
                    msg += "jack ";
                else if ((i - 3) % 13 == 0)
                    msg += "ace ";
                else
                {
                    msg += j.ToString() + " ";
                    j++;
                }
                if (i < 13)
                    msg += "spades ";
                else if (i < 26)
                    msg += "hearts ";
                else if (i < 39)
                    msg += "clubs ";
                else
                    msg += "diamonds ";
                cardspack[i] = msg;
                //Console.WriteLine(cardspack[i]+"  "+flag[i]);
            }
            
        }

        static void wait()
        {
            
            
            Console.ForegroundColor = ConsoleColor.Black;
            for (long i = 0; i < 1000; i++)
            {
                for (long ii = 0; ii < 10; ii++)
                {
                    Console.BackgroundColor = ConsoleColor.White;
                    Console.Write("10101000010000");
                } 
                for (long ii = 0; ii < 10; ii++)
                {
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.Write("10101000010000");
                } 
                }
            Console.Clear();
        }
        
        
        
        static int check(user u)
        {
            if (u.cards[0] < 13 && u.cards[1] < 13 && u.cards[2] < 13 && u.cards[3] < 13 && u.cards[4] < 13)
            {
                Console.BackgroundColor = ConsoleColor.Green;
                Console.Clear();
                Console.WriteLine("\n\n\n\n\t\t\t"+u.name+" WINS!!!!!!!!!!!!");
                return 1;
            }
            else if (u.cards[0] > 12 && u.cards[1] > 12 && u.cards[2] > 12 && u.cards[3] > 12 && u.cards[4] > 12 && u.cards[0] < 26 && u.cards[1] < 26 && u.cards[2] < 26 && u.cards[3] < 26 && u.cards[4] < 26)
            {
                Console.BackgroundColor = ConsoleColor.Green;
                Console.Clear();
                Console.WriteLine("\n\n\n\n\t\t\t"+u.name+" WINS!!!!!!!!!!!!");
                return 1;
            }
            else if (u.cards[0] > 25 && u.cards[1] > 25 && u.cards[2] > 25 && u.cards[3] > 25 && u.cards[4] > 25 && u.cards[0] < 39 && u.cards[1] < 39 && u.cards[2] < 39 && u.cards[3] < 39 && u.cards[4] < 39)
            {
                Console.BackgroundColor = ConsoleColor.Green;
                Console.Clear();
                Console.WriteLine("\n\n\n\n\t\t\t"+u.name+" WINS!!!!!!!!!!!!");
                return 1;
            }
            else if (u.cards[0] > 38 && u.cards[1] > 38 && u.cards[2] > 38 && u.cards[3] > 38 && u.cards[4] > 38)
            { 
                Console.BackgroundColor = ConsoleColor.Green;
                Console.Clear();
                Console.WriteLine("\n\n\n\n\t\t\t"+u.name+" WINS!!!!!!!!!!!!");
                return 1;
            }
        else
                return 0;
        }

        
        static void Main(string[] args)
        {
            Console.Title = "Vadivelan Udayakumar";
            wait();
            Console.CursorVisible = false;
            Console.BackgroundColor = ConsoleColor.Gray;
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("\n\n\n\n\n\n\n\n\n\t\t\t\tWelcome!!!!!!!!!\t\t\t\t");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("\n\n\t\t\t     Quick Match-Cards Game\t\t\t\t\t");
            Console.WriteLine("\n\n\t\t\t\t  Press any key\t\t\t\t\t");

           
            Console.ReadLine();
            wait();
            init();
            Console.BackgroundColor = ConsoleColor.White;
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Black;
            Console.WriteLine("\n\n\t\t\tQuick Match:::\n\n\tAbout game:\n\n\t * It is a multi-player game, there can be 2 to 4 players.\n\n\t * Each player is given a set of five cards.\n\n\t * The remaining cards are kept in a pile at the center.\n\n\t * The player can either draw a card from the pile of cards or pick a \n\t   card, which is thrown by another player.\n\n\t * The first player to collect five cards of the same suite wins the \n\t   game.\n\n\n\n\t\tPRESS ANY KEY TO START.....");
            Console.ReadLine();
            Console.BackgroundColor = ConsoleColor.Yellow;
            Console.Clear();
            Console.Title = "..............Quick Match...............By, Vadivelan.U and Parthiban.S";
            Console.CursorVisible = true;
            Console.Write("\n\n\tEnter the Number of players (Integer value):\n\n\t");
            int n = Convert.ToInt16(Console.ReadLine());
            user[] u = new user[n];

            Console.Clear();

            for (int i = 0; i < n; i++)
            {
                u[i] = new user();
                Console.Write("\n\n\t\t\tEnter player" + (i + 1) + "'s name (String Value): \n\n\t\t\t");
                u[i].name=Console.ReadLine();
                Console.Write("\n\n\n\t\t\tYour cards are:\n");
                for (int j = 0; j < 5;)
                {
                        int nn=getcard();               
                        
                        u[i].cards[j] = nn;
                        j++;
                        Console.Write("\n\n\t\t\t" + cardspack[nn]);
                   
                }
                Console.WriteLine("\n\n\t\t\tPress any key..");
                Console.ReadKey();
                Console.Clear();
            }


            wait();
            
            
            Console.WriteLine("Lets Start the game:");
            int left=-1;
            for (int i = 0; ;i++)
            {
                
                if (i % 2 == 0)
                {
                    Console.BackgroundColor = ConsoleColor.White;
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Black;
                }
                else
                    
                    {
                        Console.BackgroundColor = ConsoleColor.Blue;
                        Console.Clear();
                        Console.ForegroundColor = ConsoleColor.Black;
                    }
                Console.Title = "\n\t\tPlay : " + u[i].name;
                Console.WriteLine("\n\n\t\t" + u[i].name + "'s turn\n\n\t\tYour Cards are:");
                for (int j = 0; j < 5; j++)
                    Console.WriteLine("\t\t"+cardspack[u[i].cards[j]]);
                if(left!=-1)
                    Console.WriteLine("\nEnter n to take a new card from pile (or) m to take the dropped card(" + cardspack[left] + ") : ");
                else
                    Console.WriteLine("\n\t\tEnter n to take a new card from pile"); 
                char c = Convert.ToChar(Console.ReadLine());
                int newc=0;
                switch (c)
                {
                    case 'm':
                                   newc = left;
                                   Console.WriteLine("\n\n\t\tCard taken:" + cardspack[newc]);
                        
                        break;
        
                    case 'n':
                        newc = getcard();
                        Console.WriteLine("\n\n\t\tNew card:" + cardspack[newc]);
                            break;
                }


                Console.WriteLine("\n\t\tEnter the card to drop(1-5):");
                int k = Convert.ToInt16(Console.ReadLine());
                left=u[i].cards[(k-1)];
                flag[left]=false;
                u[i].cards[(k-1)]=newc;

                Console.WriteLine("\n\t\tYour new cards are:\n");

                for (int j = 0; j < 5; j++)
                    Console.WriteLine("\n\t\t"+cardspack[u[i].cards[j]]);

                int x=check(u[i]);
                if (x == 1)
                    break;
                Console.ReadLine();

                if ((i + 1) == n)
                        i = -1;
                Console.Clear();
            
            }





                Console.Read();
        }
    }
}







Instant play,download the game : click here