Click here to Skip to main content
15,890,506 members
Home / Discussions / C#
   

C#

 
GeneralRe: Find Insert Position In An ObservableCollection Pin
Kevin Marois2-Oct-17 7:21
professionalKevin Marois2-Oct-17 7:21 
GeneralRe: Find Insert Position In An ObservableCollection Pin
Dave Kreskowiak2-Oct-17 7:45
mveDave Kreskowiak2-Oct-17 7:45 
GeneralRe: Find Insert Position In An ObservableCollection Pin
Kevin Marois2-Oct-17 7:52
professionalKevin Marois2-Oct-17 7:52 
GeneralOT: bad link in your sig. Pin
OriginalGriff2-Oct-17 21:56
mveOriginalGriff2-Oct-17 21:56 
GeneralRe: OT: bad link in your sig. Pin
Dave Kreskowiak3-Oct-17 1:52
mveDave Kreskowiak3-Oct-17 1:52 
GeneralRe: OT: bad link in your sig. Pin
OriginalGriff3-Oct-17 2:20
mveOriginalGriff3-Oct-17 2:20 
AnswerRe: Find Insert Position In An ObservableCollection Pin
Alaa Ben Fatma3-Oct-17 10:27
professionalAlaa Ben Fatma3-Oct-17 10:27 
SuggestionUse design patterns in the 15 puzzle game and ensure a rollback Pin
Member 1276922230-Sep-17 5:22
Member 1276922230-Sep-17 5:22 
Use design patterns in the game and ensure a rollback

Send a console implementation of the game patches. Ensure that the size of the field is selected and the level of difficulty (how much the board differs from the state of the "victory"), make it possible for the game to become more complicated when a certain amount of random moves takes place instead of the user's turn. Realize the possibility of canceling moves.
 The patterns are expected: Singleton, Factory method, Team, Memento.


C#
<pre>
//Game.cs
using System;
namespace 15puzzle
{
    class Points
    {
        public readonly int x, y;
        public Points(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
    class Game
    {
        public int Length=0;
        const int nw = 4, height = 4;
        int[,] field = new int[nw, height];
        Points[] FieldValue = new Points[16];
        

        public Game(int[] point)
        { 

            int r = 0;
            string[] file = new string[4];
            Length = 16;
          
            mixer(point);

            for (int j = 0; j < height; j++)
            {
                for (int i = 0; i < nw; i++)
                {
                    field[j, i] = point[r];
                    FieldValue[point[r]] = new Points(j, i);
                    r++;

                }
            }
          
        }

        public void mixer(int[] p)
        {

            int tmp = 0;

            Random rnd = new Random();

            for (int i = 0; i < 16; i++)
            {
                bool isExist = false;
                do
                {
                    isExist = false;
                    tmp = rnd.Next(0, 16);
                    for (int j = 0; j < i; j++)
                    {
                        if (tmp == p[j]) { isExist = true; }
                    }
                }
                while (isExist);
                p[i] = tmp;
            }
        }

        private Points GetLocation(int value)
        {

            return FieldValue[value];
        }


        public void drawField()
        {

            Console.WriteLine("----------------------------");
            for (int i = 0; i < nw; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    Console.Write(field[i, j] + "\t");

                }
                Console.WriteLine();

            }

            Console.WriteLine("----------------------------");

        }

        public bool repeat(double Length,int[] point)
        {

            for (int i = 0; i < Length; ++i)
            {
                for (int y = i + 1; y < Length; ++y)
                {
                    if (point[i] == point[y])
                    {
                        Console.WriteLine(point[i] + " ==" + point[y]);
                        throw new ArgumentException("Numbers should not be repeated");
                    }


                }
            }
            return true;
        }

        public Boolean finish()
        {
            bool temp = false;
            int value = 1;
            for (int i = 0; i < nw; ++i)
            {
                for (int j = 0; j < height; ++j)
                {
                    if (field[i, j] == value)
                    {

                        temp = true;
                        ++value;
                        if (value == Length)
                        {
                            value = 0;
                        }
                    }
                    else
                    {
                        return false;
                    }

                }

            }      
            return temp;

        }

        public void Move(int value)
        {

            try
            {
            Console.WriteLine(value);
            if (value > 15 || value < 0)
            {
                throw new ArgumentException();
            }

            int x = GetLocation(0).x;
            int y = GetLocation(0).y;

            int ValueX = GetLocation(value).x;
            int ValueY = GetLocation(value).y;

                if ((ValueX == x && (ValueY == y - 1 || ValueY == y + 1))||(ValueY == y && (ValueX == x - 1 || ValueX == x + 1)))
                {

                    field[x, y] = value;
                    field[ValueX, ValueY] = 0;

                    var vere = FieldValue[0];
                    FieldValue[0] = FieldValue[value];
                    FieldValue[value] = vere;
                }

            }

            catch (ArgumentException)
            {
                Console.WriteLine("There is no such number, try again: ");
            }
            catch (Exception)
            {
                Console.WriteLine("Next to this number is not 0, try again: ");
            }
            
        }
      
    }





    //--------------------------------------------------------------------------------------------------------------------------------------------------------


    class Points2
    {
        public readonly int x, y;
        public Points2(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
    class Game2
    {
        public int Length = 0;
        const int nw = 3, height = 3;
        int[,] field = new int[nw, height];
        Points[] FieldValue = new Points[9];


        public Game2(int[] point)
        {

            int r = 0;
            string[] file = new string[3];
            Length = 9;

            mixer(point);

            for (int j = 0; j < height; j++)
            {
                for (int i = 0; i < nw; i++)
                {
                    field[j, i] = point[r];
                    FieldValue[point[r]] = new Points(j, i);
                    r++;

                }
            }

        }

        public void mixer(int[] p)
        {

            int tmp = 0;

            Random rnd = new Random();

            for (int i = 0; i < 9; i++)
            {
                bool isExist = false;
                do
                {
                    isExist = false;
                    tmp = rnd.Next(0, 9);
                    for (int j = 0; j < i; j++)
                    {
                        if (tmp == p[j]) { isExist = true; }
                    }
                }
                while (isExist);
                p[i] = tmp;
            }
        }

        private Points GetLocation(int value)
        {

            return FieldValue[value];
        }


        public void drawField()
        {

            Console.WriteLine("----------------------------");
            for (int i = 0; i < nw; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    Console.Write(field[i, j] + "\t");

                }
                Console.WriteLine();

            }

            Console.WriteLine("----------------------------");

        }

        public bool repeat(double Length, int[] point)
        {

            for (int i = 0; i < Length; ++i)
            {
                for (int y = i + 1; y < Length; ++y)
                {
                    if (point[i] == point[y])
                    {
                        Console.WriteLine(point[i] + " ==" + point[y]);
                        throw new ArgumentException("Numbers should not be repeated");
                    }


                }
            }
            return true;
        }

        public Boolean finish()
        {
            bool temp = false;
            int value = 1;
            for (int i = 0; i < nw; ++i)
            {
                for (int j = 0; j < height; ++j)
                {
                    if (field[i, j] == value)
                    {

                        temp = true;
                        ++value;
                        if (value == Length)
                        {
                            value = 0;
                        }
                    }
                    else
                    {
                        return false;
                    }

                }

            }
            return temp;

        }

        public void Move(int value)
        {

            try
            {
                Console.WriteLine(value);
                if (value > 15 || value < 0)
                {
                    throw new ArgumentException();
                }

                int x = GetLocation(0).x;
                int y = GetLocation(0).y;

                int ValueX = GetLocation(value).x;
                int ValueY = GetLocation(value).y;

                if ((ValueX == x && (ValueY == y - 1 || ValueY == y + 1)) || (ValueY == y && (ValueX == x - 1 || ValueX == x + 1)))
                {

                    field[x, y] = value;
                    field[ValueX, ValueY] = 0;

                    var vere = FieldValue[0];
                    FieldValue[0] = FieldValue[value];
                    FieldValue[value] = vere;
                }

            }

            catch (ArgumentException)
            {
                Console.WriteLine("There is no such number, try again: ");
            }
            catch (Exception)
            {
                Console.WriteLine("Next to this number is not 0, try again: ");
            }

        }

    }
}


<pre>using System;
using System.Collections.Generic;


namespace ConsoleApplication9
{


    class program
    {
        static void Main()
        {

            int i;
            int[] p = new int[16];

            for (i = 0; i < 16; i++)
            {
                p[i] = i + 1;
            }

            p[15] = 0;

            int n;
            Console.WriteLine("Input integer number size: 4-4x4, 3-3x3 ");
            n = int.Parse(Console.ReadLine());
            if(n == 4)
            {
         
                puzzle4.game1();

            }
            if(n == 3)
            {
                puzzle3.game2();
            }
        }
    }

    class puzzle3
    {
        public static void game2()
        {

    
            
            int i;
            int[] p = new int[9];

            for (i = 0; i < 9; i++)
            {
                p[i] = i + 1;
            }

            p[8] = 0;

            Console.WriteLine("15 Puzzle");

            Game2 MyGame = new Game2(p);
            int score = 0;

            MyGame.mixer(p);

            for (;;)
            {
                MyGame.drawField();

                int a;
                Console.WriteLine("Change number: ");

                string input = Console.ReadLine();
                if (Int32.TryParse(input, out a))
                {

                    MyGame.Move(a);
                }
                else
                {
                    Console.WriteLine("Error input");
                }

                if (MyGame.finish())
                {
                    MyGame.drawField();
                    Console.WriteLine("Winner");
                    Console.WriteLine("Game end");
                    break;
                }
                score++;

                Console.WriteLine("Number of moves: " + score);


            }
        }


    }



    //--------------------------------------------------------------------------------------------------------------------------------


    class puzzle4
    {
        public static void game1()
        {

            
            int i;
            int[] p = new int[16];

            for (i = 0; i < 16; i++)
            {
                p[i] = i + 1;
            }

            p[15] = 0;



            Console.WriteLine("15 Puzzle");



            Game MyGame = new Game(p);
            int score = 0;

            MyGame.mixer(p);

            for (;;)
            {
                MyGame.drawField();         
                int a;
                Console.WriteLine("Change number: ");

                string input = Console.ReadLine();
                if (Int32.TryParse(input, out a))
                {

                    MyGame.Move(a);
                }
                else
                {
                    Console.WriteLine("Error input");
                }

                if (MyGame.finish())
                {
                    MyGame.drawField();
                    Console.WriteLine("Winner");
                    Console.WriteLine("Game end");
                    break;
                }
                score++;

                Console.WriteLine("Number of moves: " + score);


            }
        }


    }

// use patern memento
    class GameHistory
    {
        public Stack<Game> History { get; private set; }
        public GameHistory()
        {
            History = new Stack<Game>();
        }
    }

   
}





GeneralRe: Use design patterns in the 15 puzzle game and ensure a rollback Pin
OriginalGriff30-Sep-17 5:40
mveOriginalGriff30-Sep-17 5:40 
GeneralRe: Use design patterns in the 15 puzzle game and ensure a rollback Pin
Nathan Minier2-Oct-17 1:35
professionalNathan Minier2-Oct-17 1:35 
Answeruse of design patterns in 15 puzzle Pin
Member 1276922230-Sep-17 0:23
Member 1276922230-Sep-17 0:23 
GeneralRe: use of design patterns in 15 puzzle Pin
OriginalGriff30-Sep-17 0:43
mveOriginalGriff30-Sep-17 0:43 
QuestionCreating Custom Message using data from Excel Pin
MRMOSES28-Sep-17 19:57
MRMOSES28-Sep-17 19:57 
AnswerRe: Creating Custom Message using data from Excel Pin
OriginalGriff28-Sep-17 20:10
mveOriginalGriff28-Sep-17 20:10 
GeneralRe: Creating Custom Message using data from Excel Pin
MRMOSES28-Sep-17 20:23
MRMOSES28-Sep-17 20:23 
AnswerRe: Creating Custom Message using data from Excel Pin
OriginalGriff28-Sep-17 20:26
mveOriginalGriff28-Sep-17 20:26 
GeneralRe: Creating Custom Message using data from Excel Pin
MRMOSES28-Sep-17 21:41
MRMOSES28-Sep-17 21:41 
GeneralRe: Creating Custom Message using data from Excel Pin
OriginalGriff28-Sep-17 22:10
mveOriginalGriff28-Sep-17 22:10 
QuestionSending a Url and querystring XML and getting a response, USPS Rate Request. Pin
jkirkerx28-Sep-17 7:28
professionaljkirkerx28-Sep-17 7:28 
AnswerRe: Sending a Url and querystring XML and getting a response, USPS Rate Request. Pin
jkirkerx28-Sep-17 7:42
professionaljkirkerx28-Sep-17 7:42 
GeneralRe: Sending a Url and querystring XML and getting a response, USPS Rate Request. Pin
jkirkerx28-Sep-17 8:40
professionaljkirkerx28-Sep-17 8:40 
GeneralI got this now Pin
jkirkerx28-Sep-17 10:07
professionaljkirkerx28-Sep-17 10:07 
QuestionHow can I add a value for each pixel Pin
WI54M27-Sep-17 19:18
WI54M27-Sep-17 19:18 
AnswerRe: How can I add a value for each pixel Pin
OriginalGriff27-Sep-17 19:41
mveOriginalGriff27-Sep-17 19:41 
QuestionExporting the placeholder and its graph to excel sheet Pin
Atul Naik27-Sep-17 8:46
Atul Naik27-Sep-17 8:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.