Hello this is my first question on this page because neither my teacher nor ChatGPT could help me, both said that they dont find an mistake.
I want to make an program that tests if the knight can run over every square on the chessboard without using a sinle square twice.
Now i have the Problem that my IDE (Visual Code Community) gives me the (NET7.0)
Error: "CS1612"
which says: "The return value of "Figur.Standort" is not a variable and therefore cannot be changed."
(The Error is in the Method "Bewegen")
class program
{
static void Main()
{
//Playfield chessboard
bool[,] Spielfeld = new bool[8, 8]; ;
//Mögliche Spielzüge des Springers
Pos[] Spielzüge = new Pos[8];
Spielzüge[0] = new Pos(1, 2);
Spielzüge[1] = new Pos(1, -2);
Spielzüge[2] = new Pos(2, 1);
Spielzüge[3] = new Pos(2, -1);
Spielzüge[4] = new Pos(-1, 2);
Spielzüge[5] = new Pos(-1, -2);
Spielzüge[6] = new Pos(-2, 1);
Spielzüge[7] = new Pos(-2, -1);
//Startposition of the knight on the playfield
Pos Position = new Pos(1, 0);
//Knight
Figur Springer = new Figur(Spielzüge, Position);
//Mainloop
while (true)
{
}
}
}
class Figur
{
public Pos[] Möglichkeiten { get; set; }
public Pos Standort { get; set; }
public Figur(Pos[] möglichkeiten, Pos standort)
{
Möglichkeiten = möglichkeiten;
Standort = standort;
}
public void Bewegen(int zugIndex)
{
Standort.X += Möglichkeiten[zugIndex].X;
Standort.Y += Möglichkeiten[zugIndex].Y;
}
}
struct Pos
{
public int X { get; set; }
public int Y { get; set; }
public Pos(int x, int y)
{
X = x;
Y = y;
}
}
i Expectet that the Method "Bewegen" uses the in Möglichkeiten[Index] saved Pos to calculate the new "Standort"
Pos
is a value type (struct), therefore whenever you access the property Standort
, you will get a copy and thus are not able to modify the X/Y properties of the original.
Option 1: change to a reference type by replacing struct Pos
by class Pos
.
Option 2: assign a new Pos to Standort:
Standort = new Pos(Standort.X + Möglichkeiten[zugIndex].X, Standort.Y + Möglichkeiten[zugIndex].Y);