- Home /
Problem with random chess piece moveplate generator
I'm making chess where chess pieces have random abilities (like queen moves like pawn, knight moves like rook etc.), but every time when i click to chess piece it's abilities changes(at first pawn moves like king after second click it starts going like knight), how to make it gets it's abilities at start and saves it until game finishes without changes.
public void RandomMove() { System.Random randomizer = new System.Random(); for (int i = 0; i < 1; i++) { float funcToChoose = randomizer.Next(6);
switch (funcToChoose)
{
case 0:
King_Move();
break;
case 1:
Queen_Move();
break;
case 2:
Knight_Move();
break;
case 3:
Bishop_Move();
break;
case 4:
Rook_Move();
break;
case 5:
WhitePawn_Move();
break;
case 6:
BlackPawn_Move();
break;
}
}
}
public void InitiateMovePlates()
{
switch (this.name)
{
case "White_queen":
RandomMove();
break;
case "Black_queen":
RandomMove();
break;
case "White_knight":
RandomMove();
break;
case "Black_knight":
RandomMove();
break;
case "White_bishop":
RandomMove();
break;
case "Black_bishop":
RandomMove();
break;
case "Black_king":
RandomMove();
break;
case "White_rook":
RandomMove();
break;
case "Black_rook":
RandomMove();
break;
case "White_pawn":
RandomMove();
break;
case "Black_pawn":
RandomMove();
break;
}
}
Show some code so people can actually help, nobody is going to write the entire code for you.
Answer by HellsHand · Apr 23, 2021 at 12:44 PM
I'd say your best bet is create something like bool firstMove = true;
and an int moveIndex = 0;
then after RandomMove()
set firstMove = false;
. After that add moveIndex = functToChoose;
before your switch
after the movement. Finally wrap System.Random randomizer = new System.Random(); for (int i = 0; i < 1; i++) { float funcToChoose = randomizer.Next(6);
in an if
that only runs if(firstMove == true;
The only problem is your for loop
. Is there a reason you are using a loop for a single iteration?
I did like you said but after click moveplates didn't spawn, i moved firstMove = false; after if(firstMove == true) after this moveplates started spawning only 1 times, after 2 click it doesn't spawn and you can't move this chess piece again in next turns, about loop i don't remember, it was few month ago, if it's a root of all problem how can i solve it?
public void RandomMove() {
if(firstMove == true) {
System.Random randomizer = new System.Random();
for (int i = 0; i < 1; i++)
{
int funcToChoose = randomizer.Next(6);
moveIndex = funcToChoose;
switch (funcToChoose)
{
case 0:
King_Move();
break;
case 1:
Queen_Move();
break;
case 2:
Knight_Move();
break;
case 3:
Bishop_Move();
break;
case 4:
Rook_Move();
break;
case 5:
WhitePawn_Move();
break;
case 6:
BlackPawn_Move();
break;
}
}
}
firstMove = false;
}
Here this is a little cleaner:
float funcToChoose;
bool firstMove = true;
public void RandomMove()
{
if (firstMove == true)
{
System.Random randomizer = new System.Random();
funcToChoose = randomizer.Next(6);
firstMove = false;
}
switch (funcToChoose)
{
case 0:
King_Move();
break;
case 1:
Queen_Move();
break;
case 2:
Knight_Move();
break;
case 3:
Bishop_Move();
break;
case 4:
Rook_Move();
break;
case 5:
WhitePawn_Move();
break;
case 6:
BlackPawn_Move();
break;
}
}
Answer by Kitare_T · Apr 23, 2021 at 05:11 PM
IT WORKED, Thank you very much! I wish you all the best!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Problem with sound 0 Answers
2D Platformer Bug 0 Answers
Multiple Cars not working 1 Answer
Help in solving bug in my code :) 1 Answer