Reputation: 39
I am making a tic tac toe game for my Computer Programming class in university. I have a few lines that i need to print 3 times in the program. I made a function for this, i called it and did not get any errors but the problem is that function is not running. It has only a few lines to print but it is not doing so. Please see the code by reading the source file: C source: https://dl.dropboxusercontent.com/u/62524851/Untitled1.c Exe file for the program: https://dl.dropboxusercontent.com/u/62524851/Untitled1.exe
#include <stdio.h>
#include <stdlib.h>
void text(char layout[9], int guide[9])
{
system("cls");
printf("\n\n\t\t ----------Tic tac toe----------");
printf("\n\nEnter number of the column you want to make your move in.\nNumbers of columns are shown below:\nPress 10 to quit.\n\n");
printf("\t\t\t%d\t%d\t%d", guide[0], guide[1], guide[2]);
printf("\n\n\t\t\t%d\t%d\t%d", guide[3], guide[4], guide[5]);
printf("\n\n\t\t\t%d\t%d\t%d\n\n", guide[6], guide[7], guide[8]);
printf("\t\t\t%c\t%c\t%c", layout[0], layout[1], layout[2]);
printf("\n\n\t\t\t%c\t%c\t%c", layout[3], layout[4], layout[5]);
printf("\n\n\t\t\t%c\t%c\t%c\n\n", layout[6], layout[7], layout[8]);
}
void main(void)
{
int guide[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, user, user2, x, i;
char layout[9] = {'.', '.', '.', '.', '.', '.', '.', '.', '.'}, c, name2[8], a[8];
do
{
system("cls");
printf("\n\n\t\t ----------Tic tac toe----------");
printf("\n\t\t By Muhammad Saad Masroor. 23507");
printf("\n\n\n\n\n\n\n\n\n\n\n\nPress c to continue");
c = getch();
} while (c != 'c');
system("cls");
printf("\n\n\t\t ----------Tic tac toe----------\n\n");
printf("With whome do you want to play?\n\nA. Computer\nB. Friend\n");
c = getch();
puts("Enter your name: ");
gets(a);
if (c == 'a')
{
system("cls");
printf("\n\n\n\n\n\n\n\n\t\t\t Lets play!");
printf("\n\n\t\t Between %s and Computer", a);
getch();
}
else if (c == 'b')
{
puts("Enter your friend's name: ");
gets(name2);
system("cls");
printf("\n\n\n\n\n\n\n\n\t\t\t Lets play!");
printf("\n\n\t\t Between %s and %s", a, name2);
getch();
}
while (x != 10)
{
void text(char layout[9], int guide[9]);
printf("%s's turn", a);
scanf("%d", &user);
if (user == user2)
{
printf("Invalid input: %d, enter another number", user);
scanf("%d", &user);
}
switch (user)
{
case (1):
layout[0] = 'X';
break;
case (2):
layout[1] = 'X';
break;
case (3):
layout[2] = 'X';
break;
case (4):
layout[3] = 'X';
break;
case (5):
layout[4] = 'X';
break;
case (6):
layout[5] = 'X';
break;
case (7):
layout[6] = 'X';
break;
case (8):
layout[7] = 'X';
break;
case (9):
layout[8] = 'X';
break;
case (10):
x = 10;
printf("You quit");
break;
default:
printf("Invalid input");
getch();
break;
}
void text(char layout[9], int guide[9]);
// User wins
if (layout[0] == 'X' && layout[1] == 'X' && layout[2] == 'X' ||
layout[3] == 'X' && layout[4] == 'X' && layout[5] == 'X' ||
layout[6] == 'X' && layout[7] == 'X' && layout[8] == 'X' ||
layout[0] == 'X' && layout[3] == 'X' && layout[6] == 'X' ||
layout[1] == 'X' && layout[4] == 'X' && layout[7] == 'X' ||
layout[2] == 'X' && layout[5] == 'X' && layout[8] == 'X' ||
layout[0] == 'X' && layout[4] == 'X' && layout[8] == 'X' ||
layout[2] == 'X' && layout[4] == 'X' && layout[6] == 'X')
{
printf("%s wins!\n", a);
x = 10;
break;
}
else if (user == 10)
{
break;
}
else if (c == 'b')
{
printf("%s's turn", name2);
scanf("%d", &user2);
}
if (user2 == user)
{
printf("Invalid input: %d, enter another number", user);
scanf("%d", &user2);
}
if (user2 == 10)
{
printf("You quit");
getch();
x = 10;
}
else if (c == 'a')
{
user2 = rand()%(9);
if (user2 == user)
{
user2 = rand()%(9);
}
printf("Computer's turn: %d", user2);
}
switch (user2)
{
case (1):
layout[0] = 'O';
getch();
break;
case (2):
layout[1] = 'O';
break;
case (3):
layout[2] = 'O';
break;
case (4):
layout[3] = 'O';
break;
case (5):
layout[4] = 'O';
break;
case (6):
layout[5] = 'O';
break;
case (7):
layout[6] = 'O';
break;
case (8):
layout[7] = 'O';
break;
case (9):
layout[8] = 'O';
break;
default:
printf("Invalid input");
break;
}
void text(char layout[9], int guide[9]);
// Computer/2nd user wins
if (layout[0] == 'O' && layout[1] == 'O' && layout[2] == 'O' ||
layout[3] == 'O' && layout[4] == 'O' && layout[5] == 'O' ||
layout[6] == 'O' && layout[7] == 'O' && layout[8] == 'O' ||
layout[0] == 'O' && layout[3] == 'O' && layout[6] == 'O' ||
layout[1] == 'O' && layout[4] == 'O' && layout[7] == 'O' ||
layout[2] == 'O' && layout[5] == 'O' && layout[8] == 'O' ||
layout[0] == 'O' && layout[4] == 'O' && layout[8] == 'O' ||
layout[2] == 'O' && layout[4] == 'O' && layout[6] == 'O')
{
if (c == 'a')
{
printf("Computer wins!\n");
getch();
x = 10;
break;
}
else if (c == 'b')
{
printf("%s wins!", name2);
getch();
x = 10;
break;
}
}
}
}
Code reindented and somewhat reformatted compared with original.
Upvotes: 0
Views: 122
Reputation: 753515
Before you go any further, this code:
switch (user)
{
case (1):
layout[0]='X';
break;
...
should be replaced by:
layout[user-1] = 'X';
You would need to validate that the move is valid before making that assignment:
if (user == 10)
...report quit and break loop...
else if (user <= 0 || user > 10)
...report error and collect new number...
And that meme is repeated, using 40-odd lines where 5 are sufficient. There are other striking repeats in the code that would be best avoided.
As your real problem, the trouble is that you've not called the function text()
anywhere. You've just declared it multiple times.
So, for example, in main()
, you have:
while (x != 10)
{
void text(char layout[9], int guide[9]);
printf("%s's turn", a);
scanf("%d", &user);
This declares text()
again, but doesn't call it. You need something like:
while (x != 10)
{
text(layout, guide);
printf("%s's turn", a);
scanf("%d", &user);
Upvotes: 1