-
i need a c expert..
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
void start_box(void);
void show_box(void);
unsigned int get_random_exponent(void);
char who_first(void);
int play_again(void);
char player_first(void);
char computer_first(void);
void player_move(void);
void player_side(void);
void computer_move(void);
void computer_think_move(void);
char check(void);
char box[3][3], inputChar[10];
int exponent[9], n_integer[9], m_integer[9], sum[9], score[2] = {0, 0};
char winner, answer;
int i, j, a, b, input, again;
int main(void)
{
int y;
again = 'y';
printf("\n\nlet's play tic-tac-toe\n\n");
start_box();
get_random_exponent();
for (i=0 ; i<9 ; i++)
n_integer[i] = (int) pow(2, exponent[i]);
for(i=0 ; i<9 ; i++)
exponent[i] = 0;
get_random_exponent();
for (i=0 ; i<9 ; i++)
m_integer[i] = (int) pow(2, exponent[i]);
for (i=0 ; i<9 ; i++)
sum[i] = n_integer[i] + m_integer[i];
answer = who_first();
if (answer=='r'||answer=='R')
{ score[0] = 0;
score[1] = 0;
main();
}
else if (answer=='d'||answer=='D') main();
else if (answer=='y'||answer=='Y') player_first();
else if (answer=='n'||answer=='N') computer_first();
else
{ printf("\ni guess that's a no.\n\n");
printf("%d", answer);
computer_first();
}
if (winner=='O')
{ score[1]++;
show_box();
printf("\nTHANK YOU LOSER.\n"); }
else if (winner=='X')
{ score[0]++;
show_box();
printf("\nYOU WIN!\n");
}
else if (winner=='/')
{ show_box();
printf("\ndraw. main\n"); }
again = play_again();
do {
main();
} while (again=='y'||again=='Y'||again=='d'||again=='D');
do {
score[0] = 0;
score[1] = 0;
main();
} while (again=='r'||again=='R');
}
void start_box(void)
{
for (i=0 ; i<3 ; i++)
for(j=0 ; j<3 ; j++)
box[i][j] = ' ';
}
void show_box(void)
{
printf("\nyour score:%d\tcomputer's score:%d\n\n\n", score[0], score[1]);
printf("\t#########################\n");
printf("\t# # # #\n");
printf("\t# %c # %c # %c #\n", box[0][0], box[0][1], box[0][2]);
printf("\t# # # #\n");
printf("\t#########################\n");
printf("\t# # # #\tyou = X\n");
printf("\t# %c # %c # %c #\n", box[1][0], box[1][1], box[1][2]);
printf("\t# # # #\tcomputer = O\n");
printf("\t#########################\n");
printf("\t# # # #\n");
printf("\t# %c # %c # %c #\n", box[2][0], box[2][1], box[2][2]);
printf("\t# # # #\n");
printf("\t#########################\n\n\n");
printf("x , y\n");
printf("1 , 1 = %d + %d\n", n_integer[0],m_integer[0]);
printf("1 , 2 = %d + %d\n", n_integer[1],m_integer[1]);
printf("1 , 3 = %d + %d\n", n_integer[2],m_integer[2]);
printf("2 , 1 = %d + %d\n", n_integer[3],m_integer[3]);
printf("2 , 2 = %d + %d\n", n_integer[4],m_integer[4]);
printf("2 , 3 = %d + %d\n", n_integer[5],m_integer[5]);
printf("3 , 1 = %d + %d\n", n_integer[6],m_integer[6]);
printf("3 , 2 = %d + %d\n", n_integer[7],m_integer[7]);
printf("3 , 3 = %d + %d\n", n_integer[8],m_integer[8]);
printf("\n");
for (i=0 ; i<9 ; i++)
printf("%d\n", sum[i]);
}
unsigned int get_random_exponent(void)
{
int a, b, i;
exponent[0] = rand() % 9;
for (i=1 ; i<9 ; i++)
{ a = rand() % 9;
for (b=0 ; b<i ; b++)
{ if (a == exponent[b])
{ do
{ a = rand() % 9;
for (b=0 ; b<i ; b++)
{ if (a == exponent[b])
{ do
{ a = rand() % 9;
for (b=0 ; b<i ; b++)
{ if (a == exponent[b])
{ do
{ a = rand() % 9;
for (b=0 ; b<i ; b++)
{ if (a == exponent[b])
{ do
{ a = rand() % 9;
for (b=0 ; b<i ; b++)
{ if (a == exponent[b])
{ do
{ a = rand() % 9;
for (b=0 ; b<i ; b++)
{ if (a == exponent[b])
{ do
{ a = rand() % 9;
for (b=0 ; b<i ; b++)
{ if (a == exponent[b])
{ do
{ a = rand() % 9;
for (b=0 ; b<i ; b++)
{ if (a == exponent[b])
{ do
{ a = rand() % 9;
for (b=0 ; b<i ; b++)
{ if (a == exponent[b])
{ do
{ a = rand() % 9;
for (b=0 ; b<i ; b++)
{ if (a == exponent[b])
{ do
{ a = rand() % 9;
for (b=0 ; b<i ; b++)
{ if (a == exponent[b])
{ do
{ a = rand() % 9;
} while (a == exponent[b]);
}
}
} while (a == exponent[b]);
}
}
} while (a == exponent[b]);
}
}
} while (a == exponent[b]);
}
}
} while (a == exponent[b]);
}
}
} while (a == exponent[b]);
}
}
} while (a == exponent[b]);
}
}
} while (a == exponent[b]);
}
}
} while (a == exponent[b]);
}
}
} while (a == exponent[b]);
}
}
} while (a == exponent[b]);
}
}
exponent[i] = a;
}
}
char who_first(void)
{
printf("\nplay first (y/n)? ");
scanf("%c", &answer);
if (answer=='q'||answer=='Q') exit(0);
return answer;
}
int play_again(void)
{
printf("\nplay again (y/n)? ");
scanf("%c", &again);
if (again=='q'||again=='Q') exit(0);
return again;
}
char player_first(void)
{
winner = ' ';
do {
show_box();
player_move(); //player 1
show_box();
winner = check();
if (winner != ' ') return winner;
computer_think_move(); //comp 2
winner = check();
} while (winner == ' ');
return winner;
}
char computer_first(void)
{
winner = ' ';
computer_move(); //comp 1
show_box();
player_move();
if (input==sum[1]||input==sum[3]||input==sum[5]||input==sum[7])
{
if (input==sum[1])
{ i = rand() % 3;
do {
i = rand() % 3;
} while (i==1);
box[0][i] = 'O'; //comp 2
}
else if (input==sum[7])
{ i = rand() % 3;
do {
i = rand() % 3;
} while (i==1);
box[2][i] = 'O';
}
else if (input==sum[3])
{ i = rand() % 3;
do {
i = rand() % 3;
} while (i==1);
box[i][0] = 'O';
}
else
{ i = rand() % 3;
do {
i = rand() % 3;
} while (i==1);
box[i][2] = 'O';
}
show_box();
player_move(); //player 2
player_side(); //computer move 3
show_box();
player_move(); //player 3
}
do {
computer_think_move();
winner = check();
if (winner != ' ') return winner;
show_box();
player_move();
winner = check();
} while (winner == ' ');
return winner;
}
void player_move(void)
{ int r = 114, R = 82, d = 100, D = 68, q = 113 , Q = 81;
printf("input the sum of the integers assigned in the square you want to take: ");
scanf("%d", &input);
if (input==sum[0] && box[0][0]==' ')
{ box[0][0] = 'X';
return; }
else if (input==sum[1] && box[0][1]==' ')
{ box[0][1] = 'X';
return; }
else if (input==sum[2] && box[0][2]==' ')
{ box[0][2] = 'X';
return; }
else if (input==sum[3] && box[1][0]==' ')
{ box[1][0] = 'X';
return; }
else if (input==sum[4] && box[1][1]==' ')
{ box[1][1] = 'X';
return; }
else if (input==sum[5] && box[1][2]==' ')
{ box[1][2] = 'X';
return; }
else if (input==sum[6] && box[2][0]==' ')
{ box[2][0] = 'X';
return; }
else if (input==sum[7] && box[2][1]==' ')
{ box[2][1] = 'X';
return; }
else if (input==sum[8] && box[2][2]==' ')
{ box[2][2] = 'X';
return; }
else
{ printf("wrong input, try again.\n");
player_move();
}
if (input==r|input==R)
{ score[0] = 0;
score[1] = 0;
main();
}
if (input==D||input==d) main();
if (input==Q||input==q) exit(0);
return;
}
void player_side(void)
{
if (box[0][0]=='O' && box[0][1]=='X' && box[2][0]==' ') box[2][0] = 'O';
else if (box[0][0]=='O' && box[1][0]=='X' && box[0][2]==' ') box[0][2] = 'O';
else if (box[0][2]=='O' && box[0][1]=='X' && box[2][2]==' ') box[2][2] = 'O';
else if (box[0][2]=='O' && box[1][2]=='X' && box[0][0]==' ') box[0][0] = 'O';
else if (box[2][0]=='O' && box[2][1]=='X' && box[0][0]==' ') box[0][0] = 'O';
else if (box[2][0]=='O' && box[1][0]=='X' && box[2][2]==' ') box[2][2] = 'O';
else if (box[2][2]=='O' && box[2][1]=='X' && box[0][2]==' ') box[0][2] = 'O';
else if (box[2][2]=='O' && box[1][2]=='X' && box[2][0]==' ') box[2][0] = 'O';
else computer_think_move();
}
void computer_think_move(void)
{
for (i=0; i<3 ; i++)
{ if (box[i][0]==box[i][1] && box[i][2]==' ' && box[i][0]=='O')
{ box[i][2] = 'O';
return; }
else if (box[i][0]==box[i][2] && box[i][1]==' ' && box[i][0]=='O')
{ box[i][1] = 'O';
return; }
else if (box[i][1]==box[i][2] && box[i][0]==' ' && box[i][1]=='O')
{ box[i][0] = 'O';
return; }
else if (box[0][i]==box[1][i] && box[2][i]==' ' && box[0][i]=='O')
{ box[2][i] = 'O';
return; }
else if (box[1][i]==box[2][i] && box[0][i]==' ' && box[1][i]=='O')
{ box[0][i] = 'O';
return; }
else if (box[0][i]==box[2][i] && box[1][i]==' ' && box[0][i]=='O')
{ box[1][i] = 'O';
return; }
}
if (box[0][0]==box[1][1] && box[2][2]==' ' && box[0][0]=='O')
{ box[2][2] = 'O';
return; }
if (box[1][1]==box[2][2] && box[0][0]==' ' && box[1][1]=='O')
{ box[0][0] = 'O';
return; }
if (box[0][2]==box[1][1] && box[2][0]==' ' && box[0][2]=='O')
{ box[2][0] = 'O';
return; }
if (box[1][1]==box[2][0] && box[0][2]==' ' && box[1][1]=='O')
{ box[0][2] = 'O';
return; }
computer_move();
}
void computer_move (void)
{
srand((unsigned int)time((time_t *)NULL));
i = rand() % 3;
j = rand() % 3;
if (box[1][1]==' ') box[1][1] = 'O';
else if (box[0][0]==' '||box[0][2]==' '||box[2][0]==' '||box[2][2]==' ')
{
do {
do {
i = rand() % 3;
} while (i==1);
do {
j = rand() % 3;
} while (j==1);
if (i==0 && (box[0][0]==' '||box[0][2]==' '))
{ do {
j = rand() % 3;
} while (j==1);
}
else if (i==2 && (box[2][0]==' '||box[2][2]==' '))
{ do {
j = rand() % 3;
} while (j==1);
}
} while (box[i][j]!=' ');
box[i][j] = 'O';
return;
}
else if (box[0][1]==' '||box[1][0]==' '||box[1][2]==' '||box[2][1]==' ')
{ do {
i = rand() % 3;
j = rand() % 3;
if (i==0) j = 1;
else if (i==2) j = 1;
else
do {
j = rand() % 3;
} while (j==1);
} while (box[i][j]!=' ');
box[i][j] = 'O';
return;
}
}
char check(void)
{ int empty = 0;
for (i=0 ; i<3 ; i++)
if (box[i][0]==box[i][1] && box[i][1]==box[i][2]) return box[i][0];
for (i=0 ; i<3 ; i++)
if (box[0][i]==box[1][i] && box[1][i]==box[2][i]) return box[0][i];
if (box[0][0]==box[1][1] && box[1][1]==box[2][2]) return box[0][0];
if (box[0][2]==box[1][1] && box[1][1]==box[2][0]) return box[0][2];
for (i=0 ; i<3 ; i++)
for (j=0 ; j<3 ; j++)
if (box[i][j]==' ') empty++;
if (empty==0) return '/';
return ' ';
}
in my code, when the player inputs 'r' or 'R' the game and score resets; 'q' or 'Q' quits the game; and 'd' or 'D' resets the game but not the score.
but when i enter those letters it becomes an infinite loop. i tried fixing it but i think it didn't work. the code in red is where my problem enters.
i need help pls pls pls..i'm just a starter in c =/
-
Since you're just a starter, I won't mention that this program is a Rube Goldberg! :)
I believe you have some serious aptitude for coding, but this design is an OMG.
I'll look at it.
<< NO! I guess I won't >>
Congrats Rube Goldberg, Jr. Your program runs Turbo C completely out of memory in your get_random_exponent() function.
I run whole chess programs in Turbo C, with several hundred lines of code.
-
You need to learn a little indentation, too:
http://cpwiki.sf.net/Indentation
-
Oh. my. god. So many nested loops!
Usually, if I need more than about 2 or 3 nested loops, I write some more functions.
Generally, you should try to avoid global variables and avoid calling main().
Other than that, check your syntax. chars requre single parenthesis around the character 'a' and logical OR is two pipes ||
This is what I mean:
if (input==r|input==R)
-
Calling main() from within a function is bad, very bad. You should read about State Machines. What you want to do is to change the state of the machine, not begin another instance of it.
-
Code:
for (b=0 ; b<i ; b++)
{ if (a == exponent[b])
{ do
{ a = rand() % 9;
for (b=0 ; b<i ; b++)
Not only do you have a hideous amount of loops inside each other, but you also use the same loop variable for ALL of them - you should definitely not do that, as when the inner-most loop finish, it will automotically make all the other loops go over the limit too.
[I'm sure the problem you are trying to solve can be done with MANY fewer loops]
--
Mats
-
awww.. i guess there's no expert in here after all. noone can answer the question i am asking. is there someone who called himself/herself an master? too bad.
-
You're rude.
Too bad.
*thread closed*
EDIT:
If you want to get help, be more polite and take the advice already given. Otherwise, you are not welcome here. Read our forum guidelines for more information.