@King25, For all those considering a response it was good that you included a way to get to the entire code, but I have to admit it is not easy to figure out what exactly you need help with.
There is one function named check, is that where you require help?
This is the check function from your code:
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
|
/*Win/Lose Check*/
bool check(int a, int b)
{
int vertical = 1;
int horizontal = 1;
int diagonal1 = 1;
int diagonal2 = 1;
char player = place[a][b];
/*Vertical*/
int i;
/*Horizontal*/
int ii;
/*Vertical Check (|)*/
for (i = a + 1; place[i][b] == player && i <= 5; i++, vertical++);//Check Down
for (i = a - 1; place[i][b] == player && i >= 0; i--, vertical++);//Check UP
if (vertical >= 4)return true;
/*Horizontal Check (-)*/
for (ii = b - 1; place[a][ii] == player && ii >= 0; ii--, horizontal++);//Check Left
for (ii = b + 1; place[a][ii] == player && ii <= 6; ii++, horizontal++);//Check Right
if (horizontal >= 4) return true;
/*Diagonal Check 1 (/)*/
for (i = a - 1, ii = b - 1; place[i][ii] == player && i >= 0 && ii >= 0; diagonal1++, i--, ii--);//Up And Left
for (i = a + 1, ii = b + 1; place[i][ii] == player && i <= 5 && ii <= 6; diagonal1++, i++, ii++);//Down And Left
if (diagonal1 >= 4) return true;
/*Diagonal Check 2 (\)*/
for (i = a - 1, ii = b + 1; place[i][ii] == player && i >= 0 && ii <= 6; diagonal2++, i--, ii++);//Up And Right
for (i = a + 1, ii = b - 1; place[i][ii] == player && i <= 5 && ii >= 0; diagonal2++, i++, ii--);//Up And Left
if (diagonal2 >= 4) return true;
return false;
}
| |
The comment for "int drop(int)" indicates it' a "drop check", so perhaps you're including that as part of your request. It is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
/*Drop Check*/
int drop(int b)
{
int count = 5;
while (place[count][nowplace - 1] != 32 && count > -1) count--;
if (count == -1) {
return -1;
}
else {
place[count][nowplace - 1] = player;
return player;
}
//return 0;
}
| |
Am I on the right track figuring out what you're asking?
Ok, not yet figuring out what the plan is, here (which appears to be central to your question, you're not sure how to get this working?)...
I can begin with a few points to consider.
This appears to be C for the most part, but you do include <string> and <iostream> (and use cout) which indicate you expect a C++ compiler. Is the adherence to "C style" of code design and usage intentional, and is it really required?
Though I don't usually adhere to Linus Torvalds (the curator of the Linux operating system, and the person for which it's "named"), he does have one "coding standard" offered as a quip, which I paraphrase here for uncertainty of the exact quote:
"If you have more than 3 nested brackets, you need to fix your code".
Torvalds works all but exclusively in C, and infamously berated C++ as a language in the 90's. Even in C++, though, take Torvald's point to heart. You have a great many nested brackets in your code, which is in the context of very long functions. It is a widely agreed upon coding standard that such code promotes bugs and destroys clarity. The "start_game" function is an example of this observation.
In check (above), I notice something which also strikes me as a style to avoid. I repeat an example here:
|
for (i = a - 1, ii = b + 1; place[i][ii] == player && i >= 0 && ii <= 6; diagonal2++, i--, ii++);//Up And Right
| |
I'm not claiming this is wrong, but I can't really tell what the plan is here. It would seem that place is the 2D array representing the board.
What I'm talking about here is clarity in style. There are programmers coming to C and C++ from other backgrounds for which this style was, once, considered an advantage. A clearer implementation will be larger (more text), and some think that compacting tests and initialization like this is good work, but frankly it is confusing.
Many coding standards start out on the subject of for loops declaring (as an instruction to the programmer) to make the for loop itself simple. The initialization should be local (this is frequently optional), it should be simple (usually only that thing being looped, the index or count or whatever). The increment should also be kept simple (again limited to the index or count). The test should be only that which controls the loop, not more generalized logic which is the purpose of the loop.
Put another way, most coding standards do not accept for loops that have no bracketed code, but end in a ';'.
The reason is, among many other points, we can't quickly and immediately see what the loop is doing, separated from how it is controlled. In this style the two notions are mixed together, and we have to engage in more mental gymnastics just to read it that would be required if what the loop is doing is clearly isolated from how the loop is controlled.
I acknowledge that to those who prefer the style you're using here complain that it makes the code larger, but the overriding objective is clarity. We know from experience that clarity leads to fewer bugs, lets other members of a team understand the code, improves their productivity in working on the code, and all of that even applies to the original author when, much later, those assumed details have left memory and you will be reading the code as if you aren't the author (your future self).
Decades ago, as a child, I learned BASIC (the old, interpreted, line oriented BASIC typical of personal computers of the late 70's). When I learned C, my BASIC experience motivated me to write this way. I learned a few hard and rather illuminating lessons on this point. While it is fortunate for me to be a memory from 40 years ago, I will never forget when it really hit me like a board in the face. Just unwrapping this kind of construction into a clearer (though larger) version, which clearly separated process from sequencing, let me see what I could not see before. That alone let me fix a "bug" and a failure of logic I could not recognize otherwise, and it was immediate simply after reorganizing that code for clarity.
I'm not really sure what "check" is doing, or the theory of operation here. I suspect these loops should actually be functions...but then I'm not even really sure (beyond a shallow recognition) of the game itself.
I think the objective is for this function, check, to return true if 4 objects belonging to the player form a line. What I'm not really certain of, relative to both the game itself and the context of this function, if that is a test of the entire board every time, or if that test should begin with that player's most recent move.
I suspect efficiency may be found using that last phrase as a starting point, but I'm not sure yet.
We have to "talk" first, and foremost in that conversation is to inquire about whether or not you are willing to refactor this code into a C++ design, and why not if so.