patterncMajor
Tic-Tac-Toe program in about 250 lines
Viewed 0 times
toeticprogram250tacaboutlines
Problem
I'm new to C programming and I recently challenged myself to writing a Tic-Tac-Toe program. I found that my code becomes lengthy very quickly with so many
```
#include
#include
typedef int bool;
#define true 1
#define false 0
int main()
{
playGame();
return 0;
}
int playGame()
{
char scanned[3];
printf("Do you wish to play tick-tack-toe?\n");
scanf("%s", scanned);
if(strcasecmp(scanned,"yes")==0)
startGame();
else
{
if (strcmp(scanned,"no")==0 || strcmp(scanned,"nah")==0 || strcmp(scanned,"naw")==0)
{
printf("That's too bad!/nThis program will now end.");
return 1;
}
printf("Not valid input!/nThis program will now end.");
return 1;
}
}
int startGame()
{
//Sets up board for values 1-9;
char board[3][3] =
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
bool winner = false;
printf("\n\nHere is your playing board. Player One is Os and Player Two is Xs\n");
printf("Entering a number 1-9 (then pushing enter) as shown below will use\nthe current Player's turn in that location.\n");
printf("\n\n");
printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
printf("-----------\n");
printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
printf("-----------\n");
printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
int x=0;
int j=0;
int turn[9];
while (j<sizeof(turn)/sizeof(turn[0]) && winner == false)
{
scanf("%d",&turn[j]);
//turn[j] = x;
if (j%2==0)
{
if (turn[j]==1)
{
board[0][0] = 'O';
printf("\n\n");
printf(" %c | %c | %c\n", board[0][0], board[0][
if statements. This code works as is (and I still wish to add more features). I just think there's probably a more neat/efficient way to program this. Any ideas on how to trim this?```
#include
#include
typedef int bool;
#define true 1
#define false 0
int main()
{
playGame();
return 0;
}
int playGame()
{
char scanned[3];
printf("Do you wish to play tick-tack-toe?\n");
scanf("%s", scanned);
if(strcasecmp(scanned,"yes")==0)
startGame();
else
{
if (strcmp(scanned,"no")==0 || strcmp(scanned,"nah")==0 || strcmp(scanned,"naw")==0)
{
printf("That's too bad!/nThis program will now end.");
return 1;
}
printf("Not valid input!/nThis program will now end.");
return 1;
}
}
int startGame()
{
//Sets up board for values 1-9;
char board[3][3] =
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
bool winner = false;
printf("\n\nHere is your playing board. Player One is Os and Player Two is Xs\n");
printf("Entering a number 1-9 (then pushing enter) as shown below will use\nthe current Player's turn in that location.\n");
printf("\n\n");
printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
printf("-----------\n");
printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
printf("-----------\n");
printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
int x=0;
int j=0;
int turn[9];
while (j<sizeof(turn)/sizeof(turn[0]) && winner == false)
{
scanf("%d",&turn[j]);
//turn[j] = x;
if (j%2==0)
{
if (turn[j]==1)
{
board[0][0] = 'O';
printf("\n\n");
printf(" %c | %c | %c\n", board[0][0], board[0][
Solution
Boolean values
C99 has Boolean values:
Variable names
Repetition
I would set up a variable name to reduce all those
Also, nested
Edit:
In clarification to the comments, the ?: operator is a ternary conditional operator. It works as follows:
In this example, I used it in combination with an assignment operation.
C99 is informal terminology to mean ISO/IEC 9899:1999, which was finalized in 1999. The latest version of C is C11, which was finalized in 2011, and which should also support Boolean variables.
C99 has Boolean values:
#include .Variable names
int x=0;
int j=0;x and j don't describe what they do. Is j the turn number? Also, I don't see x used anyway except this commented out line: //turn[j] = x;.Repetition
I would set up a variable name to reduce all those
ifs. Maybe something like:char token = (j % 2 == 0) ? 'O' : 'X';
if (turn[j]==1)
{
board[0][0] = token;
printf("\n\n");
printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
printf("-----------\n");
printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
printf("-----------\n");
printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
}
// ...Also, nested
for loops may be useful here to reduce that list of ifs to one value.Edit:
In clarification to the comments, the ?: operator is a ternary conditional operator. It works as follows:
condition ? (run this statement if condition evaluated to true) : (run this statement if condition evaluated to false);In this example, I used it in combination with an assignment operation.
C99 is informal terminology to mean ISO/IEC 9899:1999, which was finalized in 1999. The latest version of C is C11, which was finalized in 2011, and which should also support Boolean variables.
Code Snippets
int x=0;
int j=0;char token = (j % 2 == 0) ? 'O' : 'X';
if (turn[j]==1)
{
board[0][0] = token;
printf("\n\n");
printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
printf("-----------\n");
printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
printf("-----------\n");
printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
}
// ...condition ? (run this statement if condition evaluated to true) : (run this statement if condition evaluated to false);Context
StackExchange Code Review Q#94854, answer score: 24
Revisions (0)
No revisions yet.