Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- void wait(int sec)
- {
- clock_t endwait;
- endwait = clock() + sec * CLOCKS_PER_SEC;
- while (clock() < endwait) {}
- }
- int main()
- {
- int grid[10][10] =
- {
- { 0, 0, 0, 0, 0, 0, 0, 0, 0},
- { 0, 0, 0, 0, 0, 0, 0, 0, 0},
- { 0, 0, 0, 0, 0, 0, 0, 0, 0},
- { 0, 0, 0, 0, 0, 0, 0, 0, 0},
- { 0, 0, 0, 0, 0, 0, 0, 0, 0},
- { 0, 0, 0, 0, 0, 0, 0, 0, 0},
- { 0, 0, 0, 0, 0, 0, 0, 0, 0},
- { 0, 0, 0, 0, 0, 0, 0, 0, 0},
- { 0, 0, 0, 0, 0, 0, 0, 0, 0},
- { 0, 0, 0, 0, 0, 0, 0, 0, 0}
- };
- int currentX = 0;
- int currentY = 0;
- int goalX = 5;
- int goalY = 5;
- int deltaX = goalX - currentX;
- int deltaY = goalY - currentY;
- grid[goalX][goalY] = 2;
- grid[currentX][currentY] = 1;
- while(true)
- {
- int directionX;
- int directionY;
- if(deltaX != 0)
- {
- directionX = deltaX / abs(deltaX);
- }
- if (deltaY != 0)
- {
- directionY = deltaY / abs(deltaY);
- }
- grid[currentX][currentY] = 0;
- currentX += directionX;
- currentY += directionY;
- grid[currentX][currentY] = 1;
- wait(1);
- system("clear");
- for (int i = 0; i < 9; i++)
- {
- for(int h = 0; h < 9; h++)
- {
- std::cout << grid[i][h];
- }
- std::cout << std::endl;
- }
- if((goalX == currentX + 1 || goalX == currentX - 1 && goalY == currentY + 1 || goalY == currentY - 1)) break;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment