i need this for my tictactoe game. pls help.
i have this kind of code:
---------------------------------------------------------------------
Code:
#include <stdio.h>
#include <stdlib.h>
void show_box(void);
char who_first(void);
unsigned int get_random_exponent(void);
unsigned int get_integer(int exp);
int get_sum(int a, int b);
int take_box(int input);
int player_move(void);
char box[9] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
int exponent[9], n_integer[9], m_integer[9], score[2];
char answer, input, value;
int i, a, b;
int main(void)
{
int playerMove, c;
int score[2] = {0, 0};
printf("let's play tic-tac-toe\n\n");
printf("your score:%d\tcomputer's score:%d\n\n\n", score[0], score[1]);
show_box();
get_random_exponent();
for (i=0 ; i<9 ; i++)
n_integer[i] = get_integer(exponent[i]);
for(i=0 ; i<9 ; i++)
exponent[i] = 0;
get_random_exponent();
for (i=0 ; i<9 ; i++)
m_integer[i] = get_integer(exponent[i]);
for(i=0; i<9 ; i++)
{ printf("%c = %d + %d\n\n", box[i], n_integer[i], m_integer[i]);
}
for (i=0 ; i<9 ; i++)
box[i] = get_sum(n_integer[i], m_integer[i]);
printf("sums:\n");
for(i=0 ; i<9 ; i++)
printf("%d\n", box[i]);
who_first();
if (answer == 'y'||answer == 'Y')
{ player_move();
}
else if (answer == 'n'||answer == 'N')
{
box[4] = 'X';
/* computer will take the middle box */
}
else
{ printf("i guess that's a no.");
/* computer takes middle box */
}
scanf("%d", &answer);
getchar();
return 0;
}
void show_box (void)
{
printf("\t###################\n");
printf("\t# # # #\n");
printf("\t# %c # %c # %c #\n", box[0], box[1], box[2]);
printf("\t# # # #\n");
printf("\t###################\n");
printf("\t# # # #\tyou = X\n");
printf("\t# %c # %c # %c #\n", box[3], box[4], box[5]);
printf("\t# # # #\tcomputer = O\n");
printf("\t###################\n");
printf("\t# # # #\n");
printf("\t# %c # %c # %c #\n", box[6], box[7], box[8]);
printf("\t# # # #\n");
printf("\t###################\n\n");
}
char who_first(void)
{
printf("play first (y/n)? ");
scanf("%c", &answer);
return answer;
}
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;
} 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;
}
}
unsigned int get_integer(int exp)
{
int value = 1;
while (exp > 0)
{ value *= 2;
exp--;
}
return value;
}
int get_sum(int a, int b)
{
value = a + b;
return value;
}
int player_move(void)
{
printf("input the sum of the integers assigned in the square you want to take: ");
scanf("%d", &input);
for (i=0 ; i<9 ; i++)
{ if (input == box[i])
{ if (box[i] == 'X'||box[i] == 'O')
{ printf("that box is already taken.\n");
player_move();
box[i] = 'X';
}
}
else
{ printf("wrong sum. try again.\n");
player_move();
}
}
printf("you entered a wrong sum.");
player_move();
}
-----------------------------------------------------
these are my problems:
1. why are the list of "sums" not sums? some are even difference! how can i fix this problem?
2. this code is not yet complete. when the player inputs the sum of a box the letter in the box should be replaced with an 'X'. How can i do that?
i badly need your help guys. thanks.
my apologies about the code
actually, this is the 3rd month since i was taught with c prog. if i'm soo dumb over this, i'm sorry. that's why i'm badly asking the help of those experts in this field. as i have said, the code is not yet finished. my apologies again. this is actually tic-tac-toe. it's just a little bit complicated.
i followed your advice about the sum and the get_random_exponent. thanks. now my only problem is how to display 'X' or 'O' in my board.
i got this simple tic-tac-toe code:
Code:
#include<stdio.h>
#include<stdlib.h>
char matrix[3] [3];
char check(void);
void init_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);
int y;
int main(void)
{
char done;
int y;
printf("This is the game of Tic Tac Toe.\n");
printf("You will be playing against me.\n");
done = ' ';
init_matrix();
do {
disp_matrix();
get_player_move();
done = check();
if(done!= ' ') break;
get_computer_move();
done = check();
} while(done == ' ');
if (done == 'X') printf("You won!\n");
else printf("I won!!!!\n");
disp_matrix();
scanf("%d", &y);
getchar();
return 0;
}
void init_matrix(void)
{
int i, j;
for(i=0; i<3; i++)
for(j=0; j<3; j++) matrix[i][j] = ' ';
}
void get_player_move (void)
{
int x,y;
printf("Enter X,Y coordinates for your move: ");
scanf("%d%*c%d", &x, &y);
x--; y--;
if(matrix[x] [y] != ' '){
printf("Invalid move, try again.\n");
get_player_move();
}
else matrix[x] [y] = 'X';
}
void get_computer_move(void)
{
int i, j;
for(i=0; i<3; i++){
for(j=0; j<3; j++)
if(matrix[i] [j]==' ') break;
if(matrix[i] [j]==' ') break;
}
if(i*j==9) {
printf("draw\n");
scanf("%d", &y);
exit(0);
}
else
matrix[i] [j] = 'O';
}
void disp_matrix(void)
{
int t;
for(t=0; t<3; t++){
printf(" %c | %c | %c ",matrix[t] [0], matrix[t] [1], matrix [t] [2]);
if(t!=2) printf("\n---|---|---\n");
}
printf("\n");
}
char check(void)
{
int i;
for(i=0; i<3; i++)
if(matrix[i][0]==matrix[i][1]&&matrix[i][0]==matrix[i][2])
return matrix[i][0];
for(i=0; i<3; i++)
if(matrix[0][i]==matrix[1][i]&&
matrix[0][i]==matrix[2][i]) return matrix[0][i];
if(matrix[0] [0]==matrix[1] [1] && matrix[1][1]== matrix[2][2])
return matrix[0][0];
if(matrix[0][2]==matrix[1] [1] && matrix[1][1]==matrix[2][0])
return matrix[0][2];
return ' ';
}
if i'm not mistaken, those in red are the ways that could make a 'X' or an 'O' appear in the board. i did that, but when i run the code it doesn't happen.
we are not yet allowed to use 2d arrays. does it mean that only works for 2d and not in 1d (if that's what it's called)?