Thank You,
There is definitely a lot to learn when it comes to good practices. I corrected some of the things You mentioned. Some of things i do not understand, maybe because i am still beginner. I am posting changed code and, if You could be kind enough, for some additional tips.
1. I am still keeping this code in one file just because it is easier for now to operate on it. I know how to split it into different files, i will do it for final version. This is a tip i see very often.
2. Deleted namespace std.
Don't have public data member variables. Instead make an interface of functions to interact with the data. This doesn't necessarily mean blindly having a get and set function for each member. |
Could you explain in a little more detail? I understand that this is about seed_x,seed_y variables in public. How can i properly change it?
To print out a 2d array, have a nested for loop like you have in the ClearBoard function. Also have a const or constexpr variable which is the size of the grid. |
Somehow i still have a problem with that. I tried using nested loop like in ClearBoard function, it does not work. Character @ does not want to move above 10x10 or it simply does not print at all on grid. Another option is that i get only vertical grid, but not horizontal, or @ is printed only in first column. I will not post code of all of my different tries because it takes to much of a space. Could i ask for some example on how can i generate grid of, let's say 100x100 cells?
Functions which don't change the state of the class (don't change the values of any members) should be marked const before the opening brace. Note that 2 functions that differ only in const or not, are 2 different functions. This is one example of const correctness and it a good thing because the compiler can help you. Use const or constexpr as much as you can, including function arguments / parameters that shouldn't change. |
That is very useful tip. I tried changing function definition to const here, however then i get a lot of pedantic errors (without this, code does not produce any errors on my side). So for the time being i've kept it as is.
Consider using unsigned types, there shouldn't ever be a possibility for negative values especially for array subscripts. |
Could You show me an example?
I've stopped generation of numbers above 10 or below 0 by changing my if statements inside of a movingPositionX(or Y) function so it would be called again if x or y will not meet this criteria. Somehow i feel that this is a dirty hack because code needs to call itself with unwanted variable as long as it will not find the correct one. But, it works and my code stopped to crash now.
To summarize, unsolved for me is still:
1. How can i make grid 100x100 in PrintBoard(). (I feel stupid because of it, but i totally does not understand why i fail here)
2. @ character jumping somehow from i.e 10,10 to 10,1. Imagine if this random walk would be a population of Africa. After reaching edges of African continent suddenly they would appear on the far edge of North America. This is wrong.
Could You have a look now?
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
class Grid{
public:
Grid();
void MoveCharacter();
int counter = 0;
int seed_x, seed_y;
private:
char board [10][10];
void ClearBoard();
void PrintBoard();
int movingPositionX();
int movingPositionY();
int randomCopy();
void SimulateCharacter();
};
Grid::Grid()
{
ClearBoard();
}
void Grid::ClearBoard()
{
for (int i = 0; i < 10; i++) //rows
{
for (int j = 0; j < 10; j++) //columns
{
board[i][j] = ' ';
}
}
}
void Grid::PrintBoard() // How can i efficiently print more elements, for example 100x100?
{
std::cout << " 1 2 3 4 5 6 7 8 9 10\n";
for (int i = 0; i < 10; i++)
{
++counter;
std::cout << "-----------------------\n";
std::cout << i+1 << board[0][i] << "|" << board[1][i] << "|" << board[2][i]
<< "|" << board[3][i] << "|" << board[4][i] << "|"
<< board[5][i] << "|" << board[6][i] << "|" << board[7][i] << "|"
<< board[8][i] << "|" << board[9][i] << "|\n";
}
std::cout << "-----------------------\n";
}
void Grid::MoveCharacter()
{
char player = '@';
bool exec = false;
int x,y;
while (exec == false){
PrintBoard();
std::cout << "\nEnter seed value separated by newline\n";
std::cout << "X: ";
std::cin >> x;
std::cout << "Y: ";
std::cin >> y;
x = x - 1;
y = y - 1;
seed_x = x;
seed_y = y;
board[x][y] = player;
exec = true;
}
SimulateCharacter();
}
void Grid::SimulateCharacter()
{
bool walk = false;
char player = '@';
int walk_x,walk_y;
while (walk == false){
for (int i = 0; i < 3; i++){
PrintBoard();
walk_x = movingPositionX();
walk_y = movingPositionY();
board[walk_x][walk_y] = player;
if (i == 2){
walk = true;
std::string restart;
std::cout << "Next Generation(y)?\n";
std::cout << walk_x << " " << walk_y << "\n"; // Just for reference to know printed values
std::cin >> restart;
if (restart == "y"){
walk = false;
} else {
std::cout << "Thank You";
walk = true;
}
}
}
}
}
int Grid::movingPositionX()
{
if (counter == 0) {
randomCopy();
}
int x = rand() % 10;
++counter;
int walk_x;
if (x < 5){
++seed_x;
walk_x = seed_x;
if (walk_x >= 10 || walk_x < 1){ // Here we check if generated walk value does not exceed.
movingPositionX();
}
return walk_x;
} else if (x > 5 || x == 5) {
--seed_x;
walk_x = seed_x;
if (walk_x >= 10 || walk_x < 1){ // Here we check if generated walk value does not exceed.
movingPositionX();
}
return walk_x;
}
return 0;
}
int Grid::movingPositionY()
{
if (counter == 0) {
randomCopy();
}
int y = rand() % 10;
++counter;
int walk_y;
if (y < 5){
++seed_y;
walk_y = seed_y;
if (walk_y >= 10 || walk_y < 1){ // Here we check if generated walk value does not exceed.
movingPositionY();
}
return walk_y;
} else if (y > 5 || y == 5) {
--seed_y;
walk_y = seed_y;
if (walk_y >= 10 || walk_y < 1){ // Here we check if generated walk value does not exceed.
movingPositionY();
}
return walk_y;
}
return 0;
}
int Grid::randomCopy()
{
srand(time(nullptr));
return 0;
}
int main()
{
Grid ObjectGrid;
ObjectGrid.MoveCharacter();
return 0;
}
|