public World(char[,] field) { arrayWall = new bool[field.GetLength(0), field.GetLength(1)]; for (int x = 0; x < arrayWall.GetLength(0); x++) { for (int y = 0; y < arrayWall.GetLength(1); y++) { if (field[x, y] == 'W') { arrayWall[x, y] = true; } else { arrayWall[x, y] = false; } } } arrayBox = new bool[field.GetLength(0), field.GetLength(1)]; for (int x = 0; x < arrayBox.GetLength(0); x++) { for (int y = 0; y < arrayBox.GetLength(1); y++) { if (field[x, y] == 'B') { arrayBox[x, y] = true; } else { arrayBox[x, y] = false; } } } for (int x = 0; x < field.GetLength(0); x++) { for (int y = 0; y < field.GetLength(1); y++) { if (field[x, y] == 'P') { player = new ItemXY(x, y); } } } arrayMark = new bool[field.GetLength(0), field.GetLength(1)]; for (int x = 0; x < arrayMark.GetLength(0); x++) { for (int y = 0; y < arrayMark.GetLength(1); y++) { if (field[x, y] == 'M') { arrayMark[x, y] = true; } else { arrayMark[x, y] = false; } } } }
public bool move(Direction dir) { ItemXY newPositionRelative; switch (dir) { case Direction.Up: newPositionRelative = new ItemXY(0, 1); break; case Direction.Right: newPositionRelative = new ItemXY(1, 0); break; case Direction.Down: newPositionRelative = new ItemXY(0, -1); break; case Direction.Left: newPositionRelative = new ItemXY(-1, 0); break; default: newPositionRelative = new ItemXY(0, 0); break; } if (!arrayBox[player.x + newPositionRelative.x, player.y + newPositionRelative.y] && !arrayWall[player.x + newPositionRelative.x, player.y + newPositionRelative.y]) { player.x += newPositionRelative.x; player.y += newPositionRelative.y; paintXY(player.x - newPositionRelative.x, player.y - newPositionRelative.y); //Старое местоположение игрока paintXY(player.x, player.y); //Новое местоположение игрока } else if (arrayBox[player.x + newPositionRelative.x, player.y + newPositionRelative.y] && !(arrayBox[player.x + newPositionRelative.x * 2, player.y + newPositionRelative.y * 2] || arrayWall[player.x + newPositionRelative.x * 2, player.y + newPositionRelative.y * 2])) { arrayBox[player.x + newPositionRelative.x, player.y + newPositionRelative.y] = false; arrayBox[player.x + newPositionRelative.x * 2, player.y + newPositionRelative.y * 2] = true; player.x += newPositionRelative.x; player.y += newPositionRelative.y; paintXY(player.x - newPositionRelative.x, player.y - newPositionRelative.y); //Старое местоположение игрока paintXY(player.x, player.y); //Новое местоположение игрока paintXY(player.x + newPositionRelative.x, player.y + newPositionRelative.y); //Новое местоположение ящика } bool win = true; for (int y = 0; y < arrayMark.GetLength(1); y++) { for (int x = 0; x < arrayMark.GetLength(0); x++) { if (arrayMark[x, y] && !arrayBox[x, y]) { win = false; } } } return(win); }