![]() |
|
[C] Coding a game-board to play in C++ [Requesting Assistance] - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] Coding a game-board to play in C++ [Requesting Assistance] (/showthread.php?tid=5819680) |
Coding a game-board to play in C++ [Requesting Assistance] - Zink - Apr 2, 2018 I am coding a game-board challenge, but I am getting an error when I set the required number for the code to initialized the game-board. [img]http://#/laMtBd.png[/img] Code Here is the current code I have.RE: Coding a game-board to play in C++ [Requesting Assistance] - Annabelle Lee - Apr 2, 2018 The obvious error I see is: var+1; This is not going to do anything, you need to either increment with var++; or do var = var + 1; Both should work. Apart from this you don't seem to have correct formatting when printing out the board. I've taken the liberty to fix up your print function to display your intended board a bit better. You can go from here to add the column headers yourself. Overall your code has some other issues, which I would be happy to go over if you'd like me to. Here is an updated print function: Code void prntBoard(Pieces board[][COLUMNS],int& num_rows)RE: Coding a game-board to play in C++ [Requesting Assistance] - Zink - Apr 2, 2018 (Apr 2, 2018 02:23 PM)Annabelle Lee Wrote: The obvious error I see is: var+1; This is not going to do anything, you need to either increment with var++; or do var = var + 1; Both should work. Apart from this you don't seem to have correct formatting when printing out the board. I've taken the liberty to fix up your print function to display your intended board a bit better. You can go from here to add the column headers yourself. Overall your code has some other issues, which I would be happy to go over if you'd like me to. Ohhh okay I see what I did wrong. Yeah If you can that would be great, I still have a bit to do. RE: Coding a game-board to play in C++ [Requesting Assistance] - Annabelle Lee - Apr 2, 2018 Okay. Going to step through this code line by line and go over some of the issues I feel like are glaring. Please keep in mind that some of it might be nitpicking, so please bear with me on that. First issue overall I see is just the way the formatting, spacing, and consistency of the usage of caps and variable names should be a bit better. Now for the rest of it: Code #include <iostream>This is fine, nothing to see here. Code enum Pieces{SPACE = ' ', RED = 2, BLUE = 3}; // Enumeration for board pieces.Few issues here is the lack of consistency for upper/lowercase. (Nitpicking.) As well as the way you named your variables. You don't need a const int for COLUMNS, you an simply use a macro (#define) to make it a bit neater. Likewise if you have a COLUMNS, you should name your rows: ROWS, not num_rows. (It reads better. Obviously it is the number of rows.) So a slightly neater version is thus. Also, don't make spelling mistakes in function names for the sake of brevity. (prnt instead of print.) Lastly, you don't need an enum here, and in my opinion it simply makes the program more confusing. Instead simply use a 2D array of ints for the board pieces, and when iterating over board, replace with characters in the print function. For example a 0 on board is empty. A 1 is Red and a 2 is Blue. Code #define COLUMNS 6Code int main()Fixed up main a bit to look nicer and follow guidelines. Also note how the board is now a 2D array of integers, and is using 2 for loops to make sure all elements are zeroed. Code void header()Header and instruction functions are fine, just fix up a bit of the spacing/tabbing. Code void printBoard(int board[][COLUMNS], int ROWS)So the finished, neater code is like so: Code #include <iostream>Hopefully you can take it from there to implement some other functions. Remember to be careful about arrays starting at zero when grabbing user input. You'll need to subtract one from the column number the user gives and then access that array column. I think that covered everything I saw wrong with it. Good luck. |