EDIT As pointed out by @TobySpeight calling
main()
from inside a c++ program results in Undefined Behavior. This means that it could do almost anything and none of what would do can be expected and is probably a bad thing. For example data within the program can be corrupted. Back when I started programming it could have shut down the computer by causing a Kernel Panic.
There is a perfectly good loop in
main()
for executing the game, to run another game put the game loop and the sleep statement into an outer loop.
EDIT As pointed out by @TobySpeight calling
main()
from inside a c++ program results in Undefined Behavior. This means that it could do almost anything and none of what would do can be expected and is probably a bad thing. For example data within the program can be corrupted. Back when I started programming it could have shut down the computer by causing a Kernel Panic.
There is a perfectly good loop in
main()
for executing the game, to run another game put the game loop and the sleep statement into an outer loop.
EDIT As pointed out by @TobySpeight calling
main()
from inside a c++ program results in Undefined Behavior. This means that it could do almost anything and none of what would do can be expected and is probably a bad thing. For example data within the program can be corrupted. Back when I started programming it could have shut down the computer by causing a Kernel Panic.There is a perfectly good loop in
main()
for executing the game, to run another game put the game loop and the sleep statement into an outer loop.
- 26.2k
- 13
- 47
- 113
EDIT As pointed out by @TobySpeight calling
main()
from inside a c++ program results in Undefined Behavior. This means that it could do almost anything and none of what would do can be expected and is probably a bad thing. For example data within the program can be corrupted. Back when I started programming it could have shut down the computer by causing a Kernel Panic.
There is a perfectly good loop in
main()
for executing the game, to run another game put the game loop and the sleep statement into an outer loop.
This is an example of main()
without calling itself, it compiles, I don't know if it works:
int main() {
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r); //stores the console's current dimensions
MoveWindow(console, r.left, r.top, 900, 900, TRUE); // 800 width, 100 height
string playAgain("no");
do {
setup();
while (!gameOver) {
changeP();
pick();
draw();
result(winCheck(pChar));
}
std::cout << "Enter yes to play again";
std::getline(std::cin, playAgain);
} while (playAgain.compare("yes") == 0);
}
EDIT As pointed out by @TobySpeight calling
main()
from inside a c++ program results in Undefined Behavior. This means that it could do almost anything and none of what would do can be expected and is probably a bad thing. For example data within the program can be corrupted. Back when I started programming it could have shut down the computer by causing a Kernel Panic.
There is a perfectly good loop in
main()
for executing the game, to run another game put the game loop and the sleep statement into an outer loop.
This is an example of main()
without calling itself, it compiles, I don't know if it works:
int main() {
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r); //stores the console's current dimensions
MoveWindow(console, r.left, r.top, 900, 900, TRUE); // 800 width, 100 height
string playAgain("no");
do {
setup();
while (!gameOver) {
changeP();
pick();
draw();
result(winCheck(pChar));
}
std::cout << "Enter yes to play again";
std::getline(std::cin, playAgain);
} while (playAgain.compare("yes") == 0);
}
Usage of main()
In C++ main()
should only be called by the operating system, never by the program itself, it is the entry point into the program from the operating system. The definition of main()
is integer so that it can return the status of the program to whatever is calling it.
Due to the recursive nature of the use of main()
in this program it is possible that a stack overflow will occur and that may cause security risks on the computer running this program. A stack overflow may have other undesired side affects as well.
using namespace std
The use of this statement may cause collisions of function names and variables, it would be much better to use std::cin
, std::cout
and std::string
rather than having this statement in the code. Having this statement in a header file can cause even more confusion. As you write more complex program that are object oriented you may find yourself defining cin
and cout
for your objects so that they can be input or output.
Global Variables
The use of global variables makes writing and debugging code more difficult. Global variables may be modified anywhere in the program and changes to a variable can be difficult to track down.
Magic Numbers
Numeric constants in code are sometimes referred to as Magic Numbers, since it isn't clear what they are or mean.
There are a number of numeric constants used in the code, such as 0, 1, 2, 4, 14, 40, and 219. This makes the code harder to read and understand. It isn't clear what pColor = 4
or pColor = 14
are doing and it really isn't clear what this statement is doing field[i][j] = 219;
You can use const int PLAYER_ONE = 1;
or const int FIELD_HEIGHT = 7; const int FIELD_WIDTH = 8;
to define symbolic constants rather than numbers. This will make the code easier to read, and if you need to change the size of field you only need to edit in one place rather than multiple places. It will also make easier to understand any for loops that move through the field
matrix.
String Versus C Style String
The code already includes the C++ string class, it might be better if p1, p2 and player were defined as string rather C style character arrays. The built in std::cin
and std::cout
already know how to handle the string class.
Use struct or class
There could be a struct or class that represents a player. It could have the fields int id;
, string name;
and int color;
. This would reduce the number of variables for each player.
std::cin >> p1.name;
std::cin >> p2.name;
or the struct or class could contain a function that gets the user name.