Off Topic > Off Topic

Programming Megathread

Pages: << < (131/241) > >>

McJob:


--- Quote from: SetGaming on May 07, 2016, 12:28:03 PM ---When you're pulling the xPos and yPos values from cin, does this make them a string? If so, the compiler could be interpreting the xPos and yPos by their strict string/char value, instead of a number. That'd be my guess, at least.

--- End quote ---
xPos and yPos are both char types, which is actually an integeral type (aka it only holds whole numbers; signed char specifically has a range of 0 - 255). cin itself attempts to store a user's input into the appropriate variables in the order you list them. When I do the following lines;


--- Code: ---yPos -= 'A';
yPos -= 'a';
--
xPos - '0'

--- End code ---

I'm essentially saying "take the character the user input and covert it back into normal number"; 'A', 'a' and '0' are the first characters for the Upper-case, Lower-case and Number range of characters, so removing them from the input character will convert them into a number I can use for my array.


--- Quote from: devildogelite on May 07, 2016, 11:59:58 AM ---Have you checked to see what values are in the vector to make sure it's actually passing through and isn't just null.

--- End quote ---
That was a great try, but it turned out that wasn't the problem. Firstly, let's have a look at what Visual Studio's debug tools tell us:


--- Code: ---When using the Array Version:

xPos: 2
yPos: 0
boardArray[xPos][yPos] does exist, and is currently set to the value of ' '.

When using the Vector Version:

xPos: 2
yPos: 0
boardVector has a size of '0' and boardVector[xPos][yPos] doesn't exist.
--- End code ---

Yup. Turns out my problem is that I made an assumption that vector types would be Pass-by-Reference like the array, when they actually work as Pass-by-Value. The VERY easy fix (pointed out by one awesome dude not on these forums) was simply to change all the (applicable) function arguments into references, which I also did for the isPlayerOne bool to keep it consistent.

Thanks for the attempts guys, back to coding :) Multi-dimensional arrays/vectors are awful memory-wise, and so I'm going to be converting the vector into a single dimension and simply have a Setter/Getter function that abstracts away getting the appropriate values (another great idea by the same dude).

ZSNO:

Btw, when you create the board, you can just do something like

--- Code: ---vector<vector<char>> boardVector(BOARD_SIZE, vector<char>(BOARD_SIZE, ' '));
--- End code ---
and it will create a 2D vector of BOARD_SIZExBOARD_SIZE filled with ' 's

McJob:


--- Quote from: ZSNO on May 07, 2016, 04:35:02 PM ---Btw, when you create the board, you can just do something like

--- Code: ---vector<vector<char>> boardVector(BOARD_SIZE, vector<char>(BOARD_SIZE, ' '));
--- End code ---
and it will create a 2D vector of BOARD_SIZExBOARD_SIZE filled with ' 's

--- End quote ---
Thanks. My idea I'm going for here is that the user can specify what size of board they want, and they can restart at any time which means I need to reinitialise the board anyway, so putting all of that in a reusable function/method is going to be a little bit easier in the long run.

My next big tweaks will include turning the whole game into a class (so I don't have to use references), turning the multi-dimensional vector into a single vector and using a Setter/Getter to abstract away the math involved (which is apparently extremely simple) and adding the ability to play with an AI bot instead of a second player.

EDIT: If the idea of classes works well, I might start adding some more basic games (like Hangman) in as different classes, and the user can select which game they'd like to play. I'm doing this really just to get a feel for the language and its differences to C# and try practice some of the concepts I've learned.

devildogelite:

Yeah when I was doing hill-ciphers I found it a lot easier to just use a normal array and then use modulus and what not.

Ravencroft·:

wtf is wrong with this code for finding min value of 10 scores?(c++)



the highlighted number is apparently the minimum even though none of the numbers are that small, or even negative...
it actually should be the last inputted number there, 21.



Pages: << < (131/241) > >>

Go to full version