private void moveFun(PictureBox piece, Point startPoint, Point endPoint, LockMoving lockMoving) { int speed = 3; while (1 == 1) { int xVector = (endPoint.X - startPoint.X) > 0 ? 1 : -1; int yVector = (endPoint.Y - startPoint.Y) > 0 ? 1 : -1; piece.Location = new Point(piece.Location.X + xVector, piece.Location.Y + yVector); if (piece.Location.Equals(endPoint)) { lockMoving.LockV = false; break; } Thread.Sleep(speed); } }
private void movePieceAsyn(List <string> actionMove, char[,] layout, char role, char result) { LockMoving lockv = new LockMoving(false); new Thread(new ThreadStart(() => { foreach (string s in actionMove) { Console.WriteLine(s); } foreach (string s in actionMove) { Thread.Sleep(10); string[] posFormat = Regex.Split(s, "-"); int startPointX = Convert.ToInt32(posFormat[0].Split(',')[0]); int startPointY = Convert.ToInt32(posFormat[0].Split(',')[1]); int endPointX = Convert.ToInt32(posFormat[1].Split(',')[0]); int endPointY = Convert.ToInt32(posFormat[1].Split(',')[1]); Point startPos = GetAbsoluteLocation(startPointX, startPointY); Point endPos = GetAbsoluteLocation(endPointX, endPointY); Point victimPos = new Point(0, 0); if (Math.Abs(startPointX - endPointX) > 1) { victimPos = GetAbsoluteLocation((startPointX + endPointX) / 2, (endPointY + startPointY) / 2); } lockv.LockV = true; if (role == 'a') { foreach (PictureBox pb in blackItems) { if (pb.Location.Equals(startPos)) { new Thread(new ThreadStart(() => { moveFun(pb, startPos, endPos, lockv); if (endPointX == 7) { pb.ImageLocation = System.IO.Path.Combine(Application.StartupPath + @"\..\..\res\blackKing.png"); } foreach (PictureBox pbv in redItems) { if (pbv.Location.Equals(victimPos)) { pbv.Location = new Point(50, 20); } } })).Start(); //pb.Location = endPos; //if (endPointX == 7) //{ // pb.ImageLocation = System.IO.Path.Combine(Application.StartupPath + @"\..\..\res\blackKing.png"); //} //foreach (PictureBox pbv in redItems) //{ // if (pbv.Location.Equals(victimPos)) // { // pbv.Location = new Point(50, 20); // } //} } } } else { foreach (PictureBox pb in redItems) { if (pb.Location.Equals(startPos)) { new Thread(new ThreadStart(() => { moveFun(pb, startPos, endPos, lockv); if (endPointX == 0) { pb.ImageLocation = System.IO.Path.Combine(Application.StartupPath + @"\..\..\res\redKing.png"); } foreach (PictureBox pbv in blackItems) { if (pbv.Location.Equals(victimPos)) { pbv.Location = new Point(50, 740); } } })).Start(); //pb.Location = endPos; //if (endPointX == 0) //{ // pb.ImageLocation = System.IO.Path.Combine(Application.StartupPath + @"\..\..\res\redKing.png"); //} //foreach (PictureBox pbv in blackItems) //{ // if (pbv.Location.Equals(victimPos)) // { // pbv.Location = new Point(50, 740); // } //} } } } while (lockv.LockV == true) { Thread.Sleep(1); } } })).Start(); }