Click here to Skip to main content
15,036,891 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;

namespace ConsoleApplication15
{

    public partial class Shuffle
    {
        int[,] gridval = new int[3, 3];

        Random r;
        private int[] lable = new int[9];

        ArrayList genArr;
        int Num, num;
        static int x, y;
        static int z;
        public void display_Details()
        {

            Random rand = new Random();
            List<int> numbers = Enumerable.Range(0, 9).ToList();

            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("\t\t\t*****THE_SHUFFLE_GAME*****\n");
            Console.WriteLine("---It is a single player game\n");



            for (x = 0; x < 3; x++)
            {
                for (y = 0; y < 3; y++)
                {
                    int index = rand.Next(0, numbers.Count - 1);
                    gridval[x, y] = numbers[index];
                    numbers.RemoveAt(index);
                    Console.Write(" + " + gridval[x, y]);

                }
                Console.WriteLine(" + ");
            }

        }

        public void Initialize()
        {
            gridval = new int[3, 3]
        {
            {0,0,0},
            {0,0,0},
            {0,0,0},
        };
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if ((i == 3) && (j == 3))
                    {
                        x = i;
                        y = j;
                    }
                    else
                    {
                        gridval[i, j] = genRandomVal();
                    }
                }
            }
        }

        public int Random()
        {
            Num = r.Next(1, 9);
            for (int k = 0; k < genArr.Count; k++)
            {
                if (genArr[k].Equals(Num))
                {
                    Random();
                }
            }
            return Num;
        }

        public int genRandomVal()
        {
            num = Random();
            genArr.Add(num);
            return num;
        }




        private void swaping(int a, int b)
        {
            int tmp;
            tmp = gridval[x, y];
            gridval[x, y] = gridval[a, b];
            gridval[a, b] = tmp;
            Console.WriteLine(gridval[x, y]);
        }

        private void moveDown()
        {

            if ((x - 1) < 0) return;
            swaping(x - 1, y);
            x = x - 1;
            z = z - 3;
        }

        private void moveUp()
        {
            if ((x + 1) > 2) return;
            swaping(x + 1, y);
            x = x + 1;
            z = z + 3;
        }

        private void moveRight()
        {
            if ((y - 1) < 0) return;
            swaping(x, y - 1);
            y = y - 1;
            z = z - 1;
        }

        private void moveLeft()
        {
            if ((y + 1) > 2) return;
            swaping(x, y + 1);
            y = y + 1;
            z = z + 1;
        }

        public void Run_Game()
        {
            char ch;
            Console.WriteLine("\n---Enter L,R,U,D to swaping");
            ch = Convert.ToChar(Console.ReadLine());
            for (x = 0; x < 3; x++)
            {
                for (y = 0; y < 3; y++)
                {
                    if (gridval[x, y] == 0)
                    {
                        if (gridval[1, 1] == 0)
                        {
                            switch (ch)
                            {
                                case 'L': //swaping(gridval[1, 1], gridval[1, 0]);
                                    moveLeft();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                case 'R': //swaping(gridval[1, 1], gridval[1, 2]);
                                    moveRight();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                case 'U': //swaping(gridval[1, 1], gridval[0, 1]);
                                    moveUp();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                case 'D': //swaping(gridval[1, 1], gridval[2, 1]);
                                    moveDown();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                //default:
                            }
                        }
                        else if (gridval[0, 1] == 0)
                        {
                            switch (ch)
                            {
                                case 'L': //swaping(gridval[0, 1], gridval[0, 0]);
                                    moveLeft();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                case 'R': //swaping(gridval[0, 1], gridval[0, 2]);
                                    moveRight();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                case 'D': //swaping(gridval[0, 1], gridval[1, 1]);
                                    moveDown();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                //default:
                            }
                        }
                        else if (gridval[1, 2] == 0)
                        {
                            switch (ch)
                            {
                                case 'L': //swaping(gridval[1, 2], gridval[1, 1]);
                                    moveLeft();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                case 'U': //swaping(gridval[1, 2], gridval[0, 2]);
                                    moveUp();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                case 'D': //swaping(gridval[1, 2], gridval[2, 2]);
                                    moveDown();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                //default:
                            }
                        }
                        else if (gridval[2, 1] == 0)
                        {
                            switch (ch)
                            {
                                case 'L': //swaping(gridval[2, 1], gridval[2, 0]);
                                    moveLeft();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                case 'R': //swaping(gridval[2, 1], gridval[2, 2]);
                                    moveRight();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                case 'U': //swaping(gridval[2, 1], gridval[1, 1]);
                                    moveUp();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                //default:
                            }
                        }
                        else if (gridval[1, 0] == 0)
                        {
                            switch (ch)
                            {
                                case 'D': //swaping(gridval[1, 0], gridval[2, 0]);
                                    moveDown();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                case 'R': //swaping(gridval[1, 0], gridval[1, 1]);
                                    moveRight();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                case 'U': //swaping(gridval[1, 0], gridval[0, 0]);
                                    moveUp();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                //default:
                            }
                        }
                        else if (gridval[0, 0] == 0)
                        {
                            switch (ch)
                            {
                                case 'D': //swaping(gridval[0, 0], gridval[1, 0]);
                                    moveDown();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                case 'R': //swaping(gridval[0, 0], gridval[0, 1]);
                                    moveRight();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                //default:
                            }
                        }
                        else if (gridval[0, 2] == 0)
                        {
                            switch (ch)
                            {
                                case 'L': //swaping(gridval[0, 2], gridval[0, 1]);
                                    moveLeft();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                case 'D': //swaping(gridval[0, 2], gridval[1, 2]);
                                    moveDown();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                //default:
                            }
                        }
                        else if (gridval[2, 2] == 0)
                        {
                            switch (ch)
                            {
                                case 'L': //swaping(gridval[2, 2], gridval[2, 1]);
                                    moveLeft();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                case 'U': //swaping(gridval[2, 2], gridval[1, 2]);
                                    moveUp();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                //default:
                            }
                        }
                        else if (gridval[2, 0] == 0)
                        {
                            switch (ch)
                            {
                                case 'R': //swaping(gridval[2, 0], gridval[2, 1]);
                                    moveRight();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                case 'U': //swaping(gridval[2, 0], gridval[1, 0]);
                                    moveUp();
                                    Console.WriteLine(gridval[x, y]);
                                    break;
                                //default:
                            }
                        }
                    }
                }
            }
        }



        class Program
        {
            static void Main(string[] args)
            {
                Shuffle obj1 = new Shuffle();
                obj1.display_Details();
                obj1.Run_Game();
               // obj1.moveDown();
               // obj1.moveLeft();
               // obj1.moveRight();
               // obj1.moveUp();

               // obj1.Random();

                Console.ReadLine();
            }
        }
    }
}





after executing the code when i try to shuffle numbers i cant shuffle the output in the console
dont know whats wrong

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 24-Jun-14 4:39am
v3
Comments
OriginalGriff 24-Jun-14 10:32am
   
So, you decided to dump your code here and say "sort it out".
You didn't even bother to try and format it to make it readable for us, or give us a clue as to what part was giving problems.

Well, I formatted it for you, but I can't be bothered to read it.
So perhaps you want to think about your question a bit, and perhaps make it easier for us to work out what you expect to happen?
Use the "Improve question" widget to edit your question and provide better information.
[no name] 24-Jun-14 10:41am
   
actually when i executed the code i can shuffle 0 only once when i try to shuffle it again it exits without giving any exceptions or any thing

sorry for the bad format

1 solution

"actually when i executed the code i can shuffle 0 only once when i try to shuffle it again it exits without giving any exceptions or any thing"
Well yes - that is exactly what I would expect.
Look at your code - I'll rip out the irrelevant stuff for a moment:
C#
public void Run_Game()
{
    char ch;
    Console.WriteLine("\n---Enter L,R,U,D to swaping");
    ch = Convert.ToChar(Console.ReadLine());
    // Do something with ch in nested loops.
}



class Program
{
    static void Main(string[] args)
    {
        Shuffle obj1 = new Shuffle();
        obj1.display_Details();
        obj1.Run_Game();

        Console.ReadLine();
    }
}

So what is that going to do?
It creates the game, it reads an input from the user, it does something with it, then it's back to Main, read a line from the user - and exit the program.

You need to loop in the Run_Game method to get more user input until he gives up or wins!
   
Comments
[no name] 24-Jun-14 11:16am
   
thanx man
OriginalGriff 24-Jun-14 11:25am
   
You're welcome!
[no name] 24-Jun-14 12:51pm
   
can u show me how to do that??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900