patterncMinor
Tic Tac Toe dynamically changing board position and score board position
Viewed 0 times
dynamicallytoeticpositionscoretacandchangingboard
Problem
Here is my game logic:
```
#include
#include
#include
enum key
{
UP,
DOWN,
LEFT,
RIGHT,
INVALID,
ENTER,
NONE
};
typedef enum key key;
key get_key();
enum LOCATION
{
MATRIX=0,
SCORE
};
typedef enum LOCATION LOCATION;
void gotoxy(int x, int y);
void MoveMatrix(int (locations)[2],char (matrix)[3]);
void MoveScore(int score, int(locations)[2],char names[2][30]);
void PrintMatrix(int(locations)[2],char (*matrix)[3]);
void IntitiateMatrix(char(*matrix)[3]);
int PlayGame(char(matrix)[3], int(locations)[2],char names[2][30],int score[2]);
void PickBox(char(matrix)[3], int(locations)[2], int *box);
void gotoBox(int box, int(*locations)[2]);
void updateScreen(char(matrix)[3], int(locations)[2], char names[2][30], int score[2], int box);
char CheckWin(char(*matrix)[3], char symbol);
void PrintScore(int(locations)[2], int score, char names[2][30]);
void main()
{
char names[2][30], matrix[3][3],YorN;
int locations[2][2] = { {0,0},{9,0} };
int score[2] = { 0 };
int i = 0, started = 0, won;
//Just to intitiate the names array and the matrix, incase the player chose to change their location using Move Matrix and MoveScore functions, without them firstly inttiated.
IntitiateMatrix(matrix);
strcpy(names[0], "Player A");
strcpy(names[1], "Player B");
//
while (i != 5)
{
system("cls");
printf("1. New game\n");
printf("2. Reset settings\n");
printf("3. Change Matrix Location\n");
printf("4. Change Scores bar location\n");
printf("5. Exit\n");
scanf("%d", &i);
switch (i)
{
case 1:
- Tic Tac Toe - player Vs player
- Player A (first player to enter his name) is getting 'O' and is starting
- Players choose the desired spot to put their sign(X or O) using the arrows keys and pressing enter
- Possible to change the board location via a menu
- Possible to change the score area location via a menu
```
#include
#include
#include
enum key
{
UP,
DOWN,
LEFT,
RIGHT,
INVALID,
ENTER,
NONE
};
typedef enum key key;
key get_key();
enum LOCATION
{
MATRIX=0,
SCORE
};
typedef enum LOCATION LOCATION;
void gotoxy(int x, int y);
void MoveMatrix(int (locations)[2],char (matrix)[3]);
void MoveScore(int score, int(locations)[2],char names[2][30]);
void PrintMatrix(int(locations)[2],char (*matrix)[3]);
void IntitiateMatrix(char(*matrix)[3]);
int PlayGame(char(matrix)[3], int(locations)[2],char names[2][30],int score[2]);
void PickBox(char(matrix)[3], int(locations)[2], int *box);
void gotoBox(int box, int(*locations)[2]);
void updateScreen(char(matrix)[3], int(locations)[2], char names[2][30], int score[2], int box);
char CheckWin(char(*matrix)[3], char symbol);
void PrintScore(int(locations)[2], int score, char names[2][30]);
void main()
{
char names[2][30], matrix[3][3],YorN;
int locations[2][2] = { {0,0},{9,0} };
int score[2] = { 0 };
int i = 0, started = 0, won;
//Just to intitiate the names array and the matrix, incase the player chose to change their location using Move Matrix and MoveScore functions, without them firstly inttiated.
IntitiateMatrix(matrix);
strcpy(names[0], "Player A");
strcpy(names[1], "Player B");
//
while (i != 5)
{
system("cls");
printf("1. New game\n");
printf("2. Reset settings\n");
printf("3. Change Matrix Location\n");
printf("4. Change Scores bar location\n");
printf("5. Exit\n");
scanf("%d", &i);
switch (i)
{
case 1:
Solution
Inclusions
You include
You add
Next, you
The same applies to
Coding Style
Your coding style should be consistent and readable to be easy to follow and understand later on. For a consistent coding style, I highly recommend using a tool like indent.
Variable naming
You name an enum "LOCATION". All upper-case names are by practice used only for macros.
If you rely on
Function names are not consistent. Some of them are all lower case, some are lower camel case, some are upper camel case, and some contain underscores. See naming conventions
In Windows programming, the convention is to use upper camel case since it is used exclusively all Windows APIs.
Bracing Style
You use the K&R style, but your employment of braces seems to be random.
For example:
You would either put braces after every one line loop, or you would omit it. You put it in one case and left it in the other. In case you are confused,
is valid C, because the first
Variable declaration placement
Declaring variables at the start of a block is so C89/Ansi that its not a rule anymore.
Program design
You use
I also noticed that you're not using any of C99 or C11 features, so you are basically coding like in the 80's.
For example ..
You can reduce the four line code to one, while still being readable, like this:
This is an example of designated initializers.
Also, since this is a simple function that gets called a lot, it is encouraged to mark it as
You could have used the enhanced for-loop scope, like this:
which also has the advantage of narrowing down the scope of the variables used in the loop, which is always a good thing.
Summary
The code is functional, overall well-designed but unnecessarily long and seemingly outdated.
You include
Windows.h but that would generate an error, since the file is called windows.h. That isn't a real problem on Windows since it uses a case-insensitive filesystem anyway, but that's something to point out.enumsYou add
NONE at the last of the enum making it the largest value in the enum. That's not a problem, but usually NONE-type enum values are zero (thus false as a boolean) for convenience.Next, you
typedef the enum right after you declare it. A shorthand would be to typedef it on declaration:typedef enum { ... } key;The same applies to
enum Location.Coding Style
Your coding style should be consistent and readable to be easy to follow and understand later on. For a consistent coding style, I highly recommend using a tool like indent.
Variable naming
You name an enum "LOCATION". All upper-case names are by practice used only for macros.
If you rely on
SCORE to be 1, then explicitly write so, either for readability or if you plan to port the code to other non-POSIX compliant systems/compilers.Function names are not consistent. Some of them are all lower case, some are lower camel case, some are upper camel case, and some contain underscores. See naming conventions
In Windows programming, the convention is to use upper camel case since it is used exclusively all Windows APIs.
Bracing Style
You use the K&R style, but your employment of braces seems to be random.
For example:
// InitiateMatrix()
...
for (...) {
for (...)
...
}You would either put braces after every one line loop, or you would omit it. You put it in one case and left it in the other. In case you are confused,
for (...)
for (...)
...is valid C, because the first
for is technically one line because it encloses a block of code (the other loop).Variable declaration placement
Declaring variables at the start of a block is so C89/Ansi that its not a rule anymore.
Program design
You use
enum LOCATION values quite a lot in the program. However, MATRIX and SCORE are unrelated, so it would make more sense to #define them instead of using an enum since you're not using any of the benefits of an enum anyway.I also noticed that you're not using any of C99 or C11 features, so you are basically coding like in the 80's.
For example ..
gotoxy()You can reduce the four line code to one, while still being readable, like this:
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { .x = x, .y = y });This is an example of designated initializers.
Also, since this is a simple function that gets called a lot, it is encouraged to mark it as
inline so that the compiler can optimize it in any context it finds fit. This applies to other functions as well.InitiateMatrix()You could have used the enhanced for-loop scope, like this:
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
matrix[i][j] = ' ';which also has the advantage of narrowing down the scope of the variables used in the loop, which is always a good thing.
Summary
The code is functional, overall well-designed but unnecessarily long and seemingly outdated.
Code Snippets
typedef enum { ... } key;// InitiateMatrix()
...
for (...) {
for (...)
...
}for (...)
for (...)
...SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), { .x = x, .y = y });for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
matrix[i][j] = ' ';Context
StackExchange Code Review Q#98824, answer score: 2
Revisions (0)
No revisions yet.