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");
}