2dimensional array game problem

Ive created a program that reads a text file into a 2 dimensional array and lets two player move around, after the turn ends i want the player to shoot across the array with a letter. But im having probems with the shooting function.

MAIN.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <windows.h>
#include "Map.h"
#include "Player.h"
bool Game = true;
bool pOneTurn = true;
bool pTwoTurn = false;

int main()
{


    Map mObj(2);
    mObj.createmap();
    mObj.SSP();
    mObj.RefreshScreen();

    while(Game){ //while the game is running
        while(pOneTurn){ //player ones turn
            int moves = 0;
            while(moves <5){
                std::cout << "PLAYER ONE w(UP) s(DOWN) a(LEFT) d(RIGHT)\n"; //display controls and stats
                std::cout << "\n --- MOVES LEFT --- " << 5 - moves << std::endl;
                mObj.InputChoice(1); //run input function to see where to move
                mObj.RefreshScreen(); //rewrite the array

                moves++;
            }
            mObj.callFire(1);
            pTwoTurn = true; //set player twos turn to true
            pOneTurn = false; //set player ones turn to false
        }
        while(pTwoTurn){
            int moves=0;
            while(moves < 5){
                std::cout << "PLAYER TWO w(UP) s(DOWN) a(LEFT) d(RIGHT)\n";
                  std::cout << "\n --- MOVES LEFT --- " << 5-moves << std::endl;
                mObj.InputChoice(2);
                mObj.RefreshScreen();

                moves++;
            }
            mObj.callFire(2);
            pOneTurn = true;
            pTwoTurn = false;

        }
    }

}


PLAYER.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <windows.h>
#include "Map.h"
#include "Player.h"
Player::Player(int x, int y):
xp(x),
yp(y)
{

}
void Player::InputChoice(char c,char map[][20], char bgmap[][20])
{
        char x;
        std::cin >> x;
         switch(x){
                case 'w' :        //moves 'x' up one and replaces the coord before with what it originally had
                    if(yp <= 0){
                        yp = 0;
                    }else{yp-=1;}
                        map[yp + 1][xp] = bgmap[yp + 1][xp]; //replace last coord with the background map, which stays constant.
                    break;
                case 's':
                    if(yp >= 19){
                        yp = 19;
                    }else{yp+=1;}
                        map[yp - 1][xp] = bgmap[yp - 1][xp];
                    break;
                case 'a':
                    if(xp <= 0){
                        xp = 0;
                    }else{xp -=1;}
                        map[yp][xp + 1] = bgmap[yp][xp + 1];
                    break;
                case 'd':
                    if(xp >= 19){
                        xp = 19;
                    }else{xp +=1;}
                        map[yp][xp - 1] = bgmap[yp][xp - 1];
                    break;
            }
            map[yp][xp] = c; //set current position to 'x', later in this project i will be using a varible when i ask how to put in multiple players
            // and study vectors a little more(lots of learning to do!).
}

void Player::SetStartPos(char c,char map[][20])
{
    map[xp][yp] = c;
}
//----------------------------------------------------------------------------
void Player::FireG(char map[][20], Player &pobj) //FIRE function
{
    map[pobj.getx() + 1][pobj.gety()] = 'H'; //just setting the cord that is right 1 of the player to H, making the
    //H move should be fairly simple, so that is my last priority
}
//-----------------------------------------------------------------------------

void Player::cls() //windows h function to replace screen with nulls
{
  DWORD n;
  DWORD size;
  COORD coord = {0};
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  GetConsoleScreenBufferInfo ( h, &csbi );
  size = csbi.dwSize.X * csbi.dwSize.Y;
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
  SetConsoleCursorPosition ( h, coord );
}


PLAYER.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef PLAYER_H
#define PLAYER_H
#include "Map.h"
#include <iostream>
class Player //not much in this class, work in progress
{
    public:
        int getx(){return xp;};
        int gety(){return yp;};

        Player(int x, int y); //takes the players coords as parameters
        void InputChoice(char c,char map[][20], char bgmap[][20]); //uses the current map to move across
        void SetStartPos(char c,char map[][20]); //set positions at the start so they are visible
//----------------------------------------FIRE PROTOTYPE------------------
        void FireG(char map[][20], Player &pobj); //fires an X horizontally across the map, uses the map and current player
        //i used the adress of the object so it would effect the current object(correct me if im wrong), i want the current xcord and ycord
        //(should those be byref? or am i just on the wrong track?)
//----------------------------------------------------------------------------
        void cls();
    private:
    int xp;
    int yp;
};

#endif 


MAP.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "Map.h"
#include "Player.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <windows.h>
Map::Map(int pN):
pObj(5,5),
ptObj(15,15) //player TWO object
{


}
//------------------------------CALL FUNC------------------------------
void Map::callFire(int pN)
{
    switch(pN){
        case 1:
            pObj.FireG(map, pObj);
            break;
        case 2:
            ptObj.FireG(map,ptObj);
            break;
    }
}
//------------------------------------------------------------------------

void Map::SSP() //calls player function
{

            pObj.SetStartPos('B',map);
            ptObj.SetStartPos('O',map);

}

void Map::InputChoice(int pN) //player NUMBER
{
    /* i didnt know how to access the player class outside without making another object to player, so i just made a function that
    calls the object, i need this seperate because i need to call input, THEN cls(), then refresh, in that specific order. Comment if you think i should
    move the refresh function into map aswell to make some giant function that handles it. Problem is i feel like im stuffing to much stuff into Map.h and
    .cpp */
    switch(pN){
        case 1:
            pObj.InputChoice('B',map,bgmap); //calls player functions
            break;
        case 2:
            ptObj.InputChoice('O',map,bgmap);
            break;
    }

}
void Map::RefreshScreen() //reprint array
{
    cls();
    for( int nrow = 0; nrow < nrows; nrow++){
            for(int ncol = 0; ncol < ncols;ncol++)
                std::cout << map[nrow][ncol] << " ";
            std::cout << std::endl;
            }
}
void Map::createmap() { //read map in from "Map.txt" file
    using namespace std;
    ifstream MapStruct("Map.txt");
    while(!MapStruct.eof()){
        for( int nrow = 0; nrow < nrows; nrow++){
            for(int ncol = 0; ncol < ncols;ncol++){
                    MapStruct >> map[nrow][ncol];
                    bgmap[nrow][ncol] = map[nrow][ncol];
                }
            }
        }
MapStruct.close(); //close file to prevent leaks
}

void Map::cls() //windows h function to replace screen with nulls
{
  DWORD n;
  DWORD size;
  COORD coord = {0};
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  GetConsoleScreenBufferInfo ( h, &csbi );
  size = csbi.dwSize.X * csbi.dwSize.Y;
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
  SetConsoleCursorPosition ( h, coord );
}


MAP.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <vector>
class Map
{
    public:
        void createmap() ;
        void InputChoice(int pN); //pN(Player Number)
        void RefreshScreen(); //reprint array
        Map(int noP); //number of PLAYERS
        void SSP(); //SET START POSITION
        void callFire(int pN); //call the fire function using map
        void cls();
    private:
        static const int nrows = 20; //array bounds
        static const int ncols = 20;
        char map[nrows][ncols];
        char bgmap[nrows][ncols];
        Player pObj; //a map HAS a player, a player is NOT a map(thanks you guys for that logic, helped A LOT)
        Player ptObj;


};

#endif // MAP_H 


Currently when i run it an H randomly appears throughout the map, help please!
Rewrote
how is the fire function supposed to work?
Need4Sleep wrote:
a map HAS a player, a player is NOT a map(thanks you guys for that logic, helped A LOT)


No offense, but that's very funny... "That's when I realized, a banana is not a telephone" -MAD
That was just a little logic i used for an early topic, was trying to make the player a friend to map. Some kind fellow gave me the quote:
"That is wrong. Inheritance is for IS-A relationships. a Player IS-NOT-A map. That means a Player as a subclass of Map is a broken design."

But anyways, I was able to solve this after i was rested the next day.
Topic archived. No new replies allowed.