Click here to Skip to main content
15,889,803 members
Home / Discussions / C#
   

C#

 
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 
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 
There is an implementation of the game, you need to roll back the previous move, choose the level of complexity (the size of the playing field 3x3, 4x4), using the Singleton patterns, Factory method, Command, Memento, help.


C#
<pre>using System;

//Game.cs
namespace puzzle15
{
    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
    {
       ..............................................................................................
    }
    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++;

                }
            }

        }

       .................................................................................................

    }

}


//Program.cs
using System;
using System.Collections.Generic;


namespace puzzle15
...............................................................................................

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


}


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 
Questionhow to change each pixel color Pin
WI54M27-Sep-17 1:46
WI54M27-Sep-17 1:46 
AnswerRe: how to change each pixel color Pin
Dave Kreskowiak27-Sep-17 1:47
mveDave Kreskowiak27-Sep-17 1:47 
GeneralRe: how to change each pixel color Pin
WI54M27-Sep-17 18:56
WI54M27-Sep-17 18:56 

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.