0

Very new programmer here. I'm trying to create a simple game in c++. So far I've made the player (represented as a 1) and ten enemies (represented by a 2) spawn randomly in a 20x20 array of 0's. Now I'm trying to make the '1' move across the array when the player presses the 'wasd' keys and have the action be printed out. I've tried using a switch case to do this but I've realised I have no idea how it works. Can someone help me?

Code:

#include <iostream>
#include <fstream>
#include "Enemy.h"
#include "Player.h"

using namespace std;

// Comments explaining how the code works on a simple level is because I want to
// explain it to my mum later
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 = 10; // 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 array to be 20 rows by 20 columns

    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() % 21);
    p1.setPlayerRow(PlayerRow);
    cout << p1.getPlayerRow() << "."; // Chooses Row at random.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    EnemyCol = (rand() % 21);
    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 (w=North s=South a=East d=West\n ";
        cin >> PlayerMovement;
        if (PlayerMovement < 0 || PlayerMovement >= 1)
        {

        }
        switch (PlayerMovement) // switch allows the program to have programs or expressions changed during the programs running.
        {
        case 'w': // in the case that the player presses 'w' key.
        {
            cout << "Moved North\n";

            array2D[p1.getPlayerRow()][p1.getPlayerCol()] = CLEAR; // removes the 1 that represents the player 
            PlayerRow = PlayerRow - 1; // will add and subtract from Rows and Cols depending if they are moving North/West or South/East respectively.
            p1.setPlayerRow(PlayerRow); // sets the new player Row by adding the 1 in the new position.

        }
        case 's':
        {
            cout << "Moved south\n";
            array2D[p1.getPlayerRow()][p1.getPlayerCol()] = CLEAR;
            PlayerRow = PlayerRow + 1;
            p1.setPlayerRow(PlayerRow);

        }
        case 'd':
        {
            cout << "Moved east\n";
            array2D[p1.getPlayerRow()][p1.getPlayerCol()] = CLEAR;
            PlayerCol = PlayerCol + 1;
            p1.setPlayerCol(PlayerCol);

        }
        case 'a':
        {
            cout << "Moved west\n";
            array2D[p1.getPlayerRow()][p1.getPlayerCol()] = CLEAR;
            PlayerCol = PlayerCol - 1;
            p1.setPlayerCol(PlayerCol);

            array2D[p1.getPlayerRow()][p1.getPlayerCol()] = PLAYER;
        }
        }
    system("pause");
}
  • 1
    Instead of ten distinct `Enemy` variables, why not an array? – Some programmer dude May 19 '22 at 12:36
  • 1
    And a loop would probably be good to handle the input and print the output multiple times. – Some programmer dude May 19 '22 at 12:38
  • I don't get the `rand() % 21`... You know that an array of 20 elements has elements with indices 0 ... 19? [std::rand()](https://stackoverflow.com/q/32927722/7478597) is deprecated but ignoring this: Shouldn't it be `rand() % 20`? Btw. [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants) are bad -- better to use named constants. – Scheff's Cat May 19 '22 at 12:53
  • Thank you for the advice on the enemies random spawn code. I new something was wrong with it! I should have said I know about the loop issue before I posted, sorry, I just want to get the fundamentals down. Can you help me with the movement now? Scheff's Cat I implemented your changes, it removed an enemy, I fixed it by adding an 11th enemy and I'm keeping it that way because it takes less time than undoing it lol. – Pogramming May 19 '22 at 15:17
  • Hello? I should of mentioned that my mum was coming round soon to see it. I'd like something to show her. I suppose I should give you information on what I based my switch based movement on. It was based on this thread. http://m.cplusplus.com/forum/general/120664/ – Pogramming May 19 '22 at 16:53
  • Please provide the necessary content in Enemy.h and Player.h. See [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Minxin Yu - MSFT May 20 '22 at 01:43

1 Answers1

0

This is the test I did to move using wasd. Use double buffering to prevent screen flickering.

#include <iostream>
#include <fstream>
#include<conio.h>
#include <Windows.h>
using namespace std;
#define LENGTH 20
char array2D[LENGTH][LENGTH]; // sets up the array to be 20 rows by 20 columns

HANDLE hOutput, hOutBuf;
COORD coord = { 0,0 };
DWORD bytes = 0;

void show()
{
    int i=0, j=0;

    for (i = 0; i < LENGTH; i++)
    {
        coord.Y = i;
        WriteConsoleOutputCharacterA(hOutBuf, array2D[i], LENGTH, coord, &bytes);
    }
    //Set the new buffer as the active display buffer
    SetConsoleActiveScreenBuffer(hOutBuf);


    for (i = 0; i < LENGTH; i++)
    {
        coord.Y = i;
        WriteConsoleOutputCharacterA(hOutput, array2D[i], LENGTH, coord, &bytes);
    }
    //Set the new buffer as the active display buffer
    SetConsoleActiveScreenBuffer(hOutput);
 
}
class Player
{
public:
    Player()
    {
        X = 0;
        Y = 0;
    }
    void moveX(int x)
    {
      int temp=X;
      X+=x;
      if (X < 0 || X>19)
      {
        X = temp;
      }
   
    
    }
void moveY(int y)
{
    int temp = Y;
    Y += y;
    if (Y < 0 || Y>19)
    {
        Y = temp;
    }

}


    unsigned int X;
    unsigned int Y;
};

int main()
{
    Player p1;
    Player e1, e2, e3, e4, e5, e6, e7, e8, e9, e10;

   
    hOutBuf = CreateConsoleScreenBuffer(
        GENERIC_WRITE,
        FILE_SHARE_WRITE,
        NULL,
        CONSOLE_TEXTMODE_BUFFER,
        NULL
    );
    hOutput = CreateConsoleScreenBuffer(
        GENERIC_WRITE,
        FILE_SHARE_WRITE,
        NULL,
        CONSOLE_TEXTMODE_BUFFER,
        NULL
    );
   // hide cursor
    CONSOLE_CURSOR_INFO cci;
    cci.bVisible = 0;
    cci.dwSize = 1;
    SetConsoleCursorInfo(hOutput, &cci);
    SetConsoleCursorInfo(hOutBuf, &cci);
    


    char ch;

    for (int Row = 0; Row < 20; Row++)
    {
        for (int Col = 0; Col < 20; Col++)
        {
            array2D[Row][Col] = '*';

        }

    }

    array2D[p1.X][p1.Y] = '1';

    while (true)
    {
        show();
         ch=_getch();

        switch (ch)
        {
           
        case 'w':
            array2D[p1.X][p1.Y] = '*';
            p1.moveX(-1);

            break;
            
        case 'a':
            array2D[p1.X][p1.Y] = '*';
            p1.moveY(-1);
           
            break;
        case 's':
            array2D[p1.X][p1.Y] = '*';
            p1.moveX(1); 
            
            break;
        case 'd':
            array2D[p1.X][p1.Y] = '*';
            p1.moveY(1); 
           
            break;

        default:
            break;
        }

        array2D[p1.X][p1.Y] = '1';


    }
 
    system("pause");
}
Minxin Yu - MSFT
  • 2,234
  • 1
  • 3
  • 14