samedi 19 mai 2018

Replacing a single character of a string in an array

How do I effectively modify one character in a string located in an array? I have looked up answers before, but I am not getting the desired output.

My code:

#include<string>

const int winX = 25;
const int winY = 25;

// Where the player, @, is supposed to be
int x = 5; 
int y = 5; 

std::string map[winX][winY]; // This is the array for an ASCII box

void Console::preRend() {
    std::string line = "";
    std::string top = "";

    for (int i = 0; i <= winX - 1; i++) {


        top.append("#");

        if (i == 0 || i==winX - 1) {
            line.append("#");
        }
        else if (i != 0 || i != winX - 1) {
            line.append(" ");
        }
        for (int row = 0; row <= winY; row++) {
            if (row == 1 || row == winY) {
                map[1][row] = top;
            }
            else {
                map[1][row] = line;
            }
        }
    }
}

void Console::draw() {

    system("cls");

    for (int i = 1; i <= winY; i++) {
        if (i == y) {
            map[1][y][x - 1] = '@';
        }

        std::cout << map[1][i] << std::endl;

    }
}

I am expecting the player to render 5 characters to the left, and 5 characters downwards relative to the top left corner of my ASCII box, but it results in this output:

@########################
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#                       #
#########################

The character @ is not supposed to be there, and is acting as if int x and int y were both equal to 1. Any solutions?

Aucun commentaire:

Enregistrer un commentaire