Informatika gyűjtemény

NézetNyomtat

kb_sakk.cs (Vissza)
Az alábbi letöltési lehetőségek közül választhatsz: (segítség)
Karakterkódolás:
Sortörés:
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: us-ascii
Méret: 2 KB
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace sakk
{
    class Coords
    {
        public int x, y;

        public Coords(int _x, int _y)
        {
            x = _x;
            y = _y;
        }
    }

    class sakk
    {
        private int x, y;
        private int[,] table = new int[8, 8];
        private Queue<Coords> q = new Queue<Coords>();

        public sakk()
        {
            StreamReader ins = new StreamReader("sakk.be");
            string[] line = ins.ReadLine().Split(' ');
            x = int.Parse(line[1]) - 1;
            y = int.Parse(line[0]) - 1;

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    table[i, j] = -1;
                }
            }

            table[x, y] = 0;
            q.Enqueue( new Coords( x, y ) );

            jump();
            printOut();
        }

        private void printOut()
        {
            StreamWriter outs = new StreamWriter("sakk.ki");
            for (int j = 0; j < 8; j++)
            {
                for (int i = 0; i < 8; i++)
                {
                    if (!= 0) outs.Write(" ");
                    outs.Write( table[i, j] );
                }
                outs.WriteLine();
            }
            outs.Close();
        }

        private void jump()
        {
            Coords c;
            while (q.Count != 0)
            {
                c = q.Dequeue();
                checkPos(c, +2, +1);
                checkPos(c, +2, -1);
                checkPos(c, -2, +1);
                checkPos(c, -2, -1);
                checkPos(c, +1, +2);
                checkPos(c, +1, -2);
                checkPos(c, -1, +2);
                checkPos(c, -1, -2);
            }
        }

        private void checkPos(Coords c, int dx, int dy)
        {
            if (c.+ dx >= 0 && c.+ dx < 8 && c.+ dy >= 0 && c.+ dy < 8)
            {
                // ide ugorhatunk.
                if (table[c.+ dx, c.+ dy] == -1)
                {
                    table[c.+ dx, c.+ dy] = table[c.x, c.y] + 1;
                    q.Enqueue( new Coords( c.+ dx, c.+ dy ) );
                }
            }
        }

        static void Main(string[] args)
        {
            new sakk();
        }
    }
}
(Vissza)