c++ – How to get the number 1 in an array of zeroes to move to another place in the array when the player presses 1, 2, 3 or 4 using for loops?

I’m a very new programmer who is trying to make a game where the player as represented by a 1 is randomly placed in a 20×20 grid with ten enemies, represented by 2’s, that are also randomly placed. I’m trying to get the player character to move and then have the action the player just made print out at the bottom telling them which direction they moved. The player is not moving and the action isn’t being printed out though and I don’t know why. I hope it’s something obvious…

edit: sorry for the previously vague title, I’m very tired!

Code:

#include <iostream>
#include <fstream>
#include "Enemy.h"
#include "Player.h"
#include <windows.h>
using namespace std;
// I know about the inefficiency of the random spawn for the //enemies I just want the movement code figured out.  
void main()
{

    Player p1;
    Enemy e1, e2, e3, e4, e5, e6, e7, e8, e9, e10;

    int PlayerMovement, PlayerRow, PlayerCol, EnemyRow, EnemyCol, EnemyRand, EnemyRand2, EnemyRand3, EnemyRand4, EnemyRand5, EnemyRand6, EnemyRand7, EnemyRand8, EnemyRand9, EnemyRand10;
    const int PLAYER = 1, CLEAR = 0, ENEMY = 2, NUMBER_OF_ENEMIES = 11; // A constant makes the variable unable to be altered during the programs run-time.

    srand((unsigned)time(0)); //Generates psudo-randomness so the player and enemies can be placed randomly around the array 
    //"time (0)" returns the number of seconds from the system clock, which is zero.

    int array2D[20][20]; // sets up the arry to be 20 Rows by 20 Collums.

    for (int Row = 0; Row < 20; Row++)
    {
        for (int Col = 0; Col < 20; Col++)
        {
            array2D[Row][Col] = 0;
            cout << array2D[Row][Col];
        }
        cout << "n";
    }

    PlayerRow = (rand() % 20);
    p1.setPlayerRow(PlayerRow);
    cout << p1.getPlayerRow() << "."; // Chooses Row at random.

    PlayerCol = (rand() % 20);
    p1.setPlayerCol(PlayerCol);
    cout << p1.getPlayerCol() << "n";
    array2D[p1.getPlayerRow()][p1.getPlayerCol()] = PLAYER; // Chooses Collum at random for the player.

    EnemyRow = (rand() % 20);
    e1.setEnemyRow(EnemyRow);
    cout << e1.getEnemyRow() << ".";

    EnemyCol = (rand() % 20);
    e1.setEnemyCol(EnemyCol);
    cout << e1.getEnemyCol() << "n";
    array2D[e1.getEnemyRow()][e1.getEnemyCol()] = ENEMY;

    EnemyRow = (rand() % 20);
    e2.setEnemyRow(EnemyRow);
    cout << e2.getEnemyRow() << ".";

    EnemyCol = (rand() % 20);
    e2.setEnemyCol(EnemyCol);
    cout << e2.getEnemyCol() << "n";
    array2D[e2.getEnemyRow()][e2.getEnemyCol()] = ENEMY;

    EnemyRow = (rand() % 20);
    e3.setEnemyRow(EnemyRow);
    cout << e3.getEnemyRow() << ".";

    EnemyCol = (rand() % 20);
    e3.setEnemyCol(EnemyCol);
    cout << e3.getEnemyCol() << "n";
    array2D[e3.getEnemyRow()][e3.getEnemyCol()] = ENEMY;

    EnemyRow = (rand() % 20);
    e4.setEnemyRow(EnemyRow);
    cout << e4.getEnemyRow() << ".";

    EnemyCol = (rand() % 20);
    e4.setEnemyCol(EnemyCol);
    cout << e4.getEnemyCol() << "n";
    array2D[e4.getEnemyRow()][e4.getEnemyCol()] = ENEMY;

    EnemyRow = (rand() % 20);
    e5.setEnemyRow(EnemyRow);
    cout << e5.getEnemyRow() << ".";

    EnemyCol = (rand() % 20);
    e5.setEnemyCol(EnemyCol);
    cout << e5.getEnemyCol() << "n";
    array2D[e5.getEnemyRow()][e5.getEnemyCol()] = ENEMY;

    EnemyRow = (rand() % 20);
    e6.setEnemyRow(EnemyRow);
    cout << e6.getEnemyRow() << ".";

    EnemyCol = (rand() % 20);
    e6.setEnemyCol(EnemyCol);
    cout << e6.getEnemyCol() << "n";
    array2D[e6.getEnemyRow()][e6.getEnemyCol()] = ENEMY;

    EnemyRow = (rand() % 20);
    e7.setEnemyRow(EnemyRow);
    cout << e7.getEnemyRow() << ".";

    EnemyCol = (rand() % 20);
    e7.setEnemyCol(EnemyCol);
    cout << e7.getEnemyCol() << "n";
    array2D[e7.getEnemyRow()][e7.getEnemyCol()] = ENEMY;

    EnemyRow = (rand() % 20);
    e8.setEnemyRow(EnemyRow);
    cout << e8.getEnemyRow() << ".";

    EnemyCol = (rand() % 20);
    e8.setEnemyCol(EnemyCol);
    cout << e8.getEnemyCol() << "n";
    array2D[e8.getEnemyRow()][e8.getEnemyCol()] = ENEMY;

    EnemyRow = (rand() % 20);
    e9.setEnemyRow(EnemyRow);
    cout << e9.getEnemyRow() << ".";

    EnemyCol = (rand() % 20);
    e9.setEnemyCol(EnemyCol);
    cout << e9.getEnemyCol() << "n";
    array2D[e9.getEnemyRow()][e9.getEnemyCol()] = ENEMY;

    EnemyRow = (rand() % 20); // uses the same principle as the players random spawn code
    e10.setEnemyRow(EnemyRow);
    cout << e10.getEnemyRow() << ".";

    EnemyCol = (rand() % 20);
    e10.setEnemyCol(EnemyCol);
    cout << e10.getEnemyCol() << "n";
    array2D[e10.getEnemyRow()][e10.getEnemyCol()] = ENEMY;

    cout << "n2D Array Output:n";
    for (int row = 0; row < 20; row++)
    {
        for (int col = 0; col < 20; col++)
        {
            cout << " " << array2D[row][col];
        }
        cout << "n"; //handles showing the resulting array after the player and enemies have been randomly placed.
    }
cout << "n Player turn (1=North 2=South 3=East 4=Westn ";
    cin >> PlayerMovement;
    if (PlayerMovement < 0 || PlayerMovement >= 1)
    {

    }
    else if (PlayerMovement == 1)
    {
        cout << "Moved Northn";

        array2D[p1.getPlayerRow()][p1.getPlayerCol()] = CLEAR;
        PlayerRow = PlayerRow - 1;
        p1.setPlayerRow(PlayerRow);

    }
    else if (PlayerMovement == 2)
    {
        cout << "Moved southn";
        array2D[p1.getPlayerRow()][p1.getPlayerCol()] = CLEAR;
        PlayerRow = PlayerRow + 1;
        p1.setPlayerRow(PlayerRow);

    }
    else if (PlayerMovement == 3)
    {
        cout << "Moved eastn";
        array2D[p1.getPlayerRow()][p1.getPlayerCol()] = CLEAR;
        PlayerCol = PlayerCol + 1;
        p1.setPlayerCol(PlayerCol);

    }
    else if (PlayerMovement == 4)
    {
        cout << "Moved westn";
        array2D[p1.getPlayerRow()][p1.getPlayerCol()] = CLEAR;
        PlayerCol = PlayerCol - 1;
        p1.setPlayerCol(PlayerCol);
    }

        array2D[p1.getPlayerRow()][p1.getPlayerCol()] = PLAYER;
        system("pause");
        }

Leave a Comment