Answer the question
In order to leave comments, you need to log in
How to move diagonally and in all directions [chess]?
Good afternoon!
There is a problem with a chessboard.
It is necessary to create an array of all possible moves of certain pieces and write them down.
With the rook, everything is quite simple, but with the rest it is harder.
Tell me how this can be implemented or what function you need to pay attention to using.
And in order to add a new figure, I create another sheet - List BishopMoves (int x, int y)?
using System;
using System.Collections.Generic;
namespace ConsoleApp_HW2
{
class Program
{
List<Coords>[][,] moves; //список ходов для любого типа фигур (фигура и ее координаты)
static void Main(string[] args) //выход статичного метода
{
Program program = new Program();
program.Start();
}
void Start()
{
int count = Enum.GetNames(typeof(Figure)).Length; //запрос из списка Enum
moves = new List<Coords>[count][,]; //фигуры с массивом списков координат для фигур
moves[(int)Figure.Rook] = new List<Coords>[8, 8]; //список координат для Ладьи
for (int x = 0; x <= 7; x++)
for (int y = 0; y <= 7; y++)
moves[(int)Figure.Rook][x, y] = RookMoves(x, y);
}
List<Coords> RookMoves (int x, int y) //передвижение Ладьи
{
List<Coords>list = new List<Coords>(14); //фигура имеет 14 ходов из любой точки доски
for (int sx = x - 1; sx >= 0; sx--) //идем влево пока равно нулю или больше и уменьшаем
list.Add(new Coords(sx, y)); //добавляем координату x, y без изменений
for (int sx = x + 1; sx <=7; sx++) //идем вправо пока меньше или равен 7 и прибавляем
list.Add (new Coords(sx, y)); //добавляем координату x, y без изменений
for (int sy = y - 1; sy >= 0; sy--) //идем вниз пока равно нулю или больше и уменьшаем
list.Add(new Coords(x, sy)); //добавляем координату y, x без изменений
for (int sy = y + 1; sy <= 7; sy++) //идем вниз пока меньше или равен 7 и прибавляем
list.Add(new Coords(x, sy)); //добавляем координату y, x без изменений
return list; //вернуть список
}
struct Coords //структура
{
public int x;
public int y;
public Coords (int x, int y)
{
this.x = x;
this.y = y;
}
public override string ToString() //запись координат в строчку
=> $"{x}, {y}";
}
enum Figure //перечисление фигур
{
Rook, // Ладья
Knight, //Конь
Bishop, //Слон
Queen, // Ферзь
King //Король
}
}
}
Answer the question
In order to leave comments, you need to log in
1. Store only data about the coordinates of each piece in an 8x8 matrix, where 0 is empty, 1 is a white rook, 2 is a white knight, and so on. 101 - black rook, 102 - black knight... (better with a mask, for example 64 = 0x40)
2. For each piece, additionally keep a map of possible moves with a possible displacement of the pieces. For example, for a knight there will be 8 of them: [-1, -2], [-1, 2], [2, 1], [2, -1], [1, 2] ... and so on.
For an elephant There are only 4 of them: [-1, -1], [-1, 1], [1, 1], [1, -1]
PS By the way, don't forget about castling - this is also a move, but you need to register it separately. And there will be two of them on each side of the board.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question