Archived

This topic is now archived and is closed to further replies.

why won't these function work???

This topic is 5308 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have a few functions in my TTT game so that it will save me time and size when I have to repeat some code. Here what it looks like:
#include <stdio.h>
#include <windows.h>

int turn, move;
char name1[25], name2[25];
char board[9]; // The tic-tac-toe Board

void DrawBoard();
void DrawBoard() //* Draws the board *//

{

printf("\n\n");
printf(" %c | %c | %c \n", board[0], board[1], board[2]);
printf(" --------- \n");
printf(" %c | %c | %c \n", board[3], board[4], board[5]);
printf(" --------- \n");
printf(" %c | %c | %c \n", board[6], board[7], board[8]);
printf("\n\n");
}

void Playersturn();
void Playersturn() //Players turn

{

int turn=1;
int move=0;
if (turn == 1)
{printf("Please make a move %s : ", name1);
scanf("%d", &move);
turn=2;}
else
{printf("Please make a move %s : ", name2);
scanf("%d", &move);
turn=1;}
}
void CheckSpace();
void CheckSpace() // Checks for double usage and places X''s and O''s

{
}

int main()

{
BOOL StillPlaying=TRUE;

printf("Welcome to tic-tac-toe!!!\n\n");

//players names

printf("What is 1st player''s name: ");
scanf("%s", name1);
printf("\nWhat is 2nd player''s name: ");
scanf("%s", name2);

while (StillPlaying)
{
DrawBoard();

Playersturn();

{
move --;
if ((turn==1)&&(board[move]!=''X'')&&(board[move]!=''O''))
{board[move]=''X'';}
else if((board[move]!=''X'')&&(board[move]!=''O''))
{board[move]=''O'';}
else
{printf("Choose a different spot");}
Playersturn();
}

}//While Winner

return 0;

}// int main()


I even put int turn=1; and int move=0; inside one of the functions, still no good. How can i fix this or how can I keep the same player after it tells them to pick an empty space. Thanks

Share on other sites
Just a few notes, looking through your code :

Putting int turn=1 and int move=0 inside function Playersturn() will make new variables local to that function only, ie. they disappear when the function ends. Your program needs to alter the global turn and move variables declared at the start of the program, so remove the copies from Playersturn().

Secondly, if the slot on the board contains an X or an O already, you''re calling Playersturn() again. That''s good, but Playerturn() is also changing which player''s turn it is to go next. This is called a side-effect and is generally considered a bad thing. As the placing of the X or O was unsuccessful, you need to set the turn back to the same player and get their move again. It would be better to remove the changing of ''turn'' from Playersturn() and change it only if an O or X is successfully placed.

Additionally, you don''t really need to prototype every function as you go along. Writing "void Playersturn();" just says "there''s a function called Playersturn, it has no parameters, and I''ll tell you what it is later on". You can then refer to that function in any of the code that appears before the actual definition.

HTH

Share on other sites
Pootle is right your move checking code belongs with the code that gathers the players move.

Thats what CheckSpace is for huh??

Heres some puesdocode

playersturn()
{
// gather correct players input
//__while move input is invalid
//___report it to player and ask for valid input
// make correct players move
}

DrawBoard();
While(StillPlaying)
{
playersturn();
DrawBoard();

// check if current player has won
// check if the game is a draw

// Goto next players turn
//__by swapping player ids
}

sorry about all the editing I need sleep...

[edited by - CodeJunkie on July 8, 2003 3:58:08 AM]

Share on other sites
So is using functions in the project a good idea.

(friend) WHAT!!!!..... Are you crazy???
(me) Yes I am.

Share on other sites
I thought it was good to prototype functions. First, it makes debugging a hell of a lot easier, and second, the main function is in a more logical place. (The beginning of the code)

Share on other sites
Yes. Using functions is a Good Thing. Not only do they allow you to re-use sections of code, but they also break the program down into manageable sections that are easier to get your brain around, and make the whole thing a lot easier to read.

Share on other sites

Okay I took your advice and moved some code around. By what you guys are telling me I need to work on this code:
void Playersturn();void Playersturn() //Players turn{if (turn == 1)	{printf("Please make a move %s : ", name1); 	scanf("%d", &move);	turn=2;}else	{printf("Please make a move %s : ", name2);	scanf("%d", &move);	turn=1;}move --;if ((turn==1)&&(board[move]!=''X'')&&(board[move]!=''O''))	{board[move]=''X'';}else if((board[move]!=''X'')&&(board[move]!=''O''))	{board[move]=''O'';}else 	{printf("Choose a different spot");}}

Also are you telling me that I can get rid of all the
void(somename)(); ....
I thought you needed those... I guess not.

(friend) WHAT!!!!..... Are you crazy???
(me) Yes I am.

Share on other sites
Yes, you dont need these lines ... 'void Playersturn();'

[edited by - Tiffany Smith on July 8, 2003 1:29:32 PM]

Share on other sites
You don't need the prototypes in this case.

This bit of code won't compile without a prototype :

void MyFunction( void ){  MyOtherFunction();}void MyOtherFunction( void ){  printf("Hello world\n");}

..because when the compiler is building MyFunction() it has never heard of MyOtherFunction() before. Adding a prototype at the start of the file will fix this by telling the compiler about the inputs and output of the function, and reassuring it that the definition will be given later :

// Required prototypevoid MyOtherFunction( void );// Function definitionsvoid MyFunction( void ){  MyOtherFunction();}void MyOtherFunction( void ){  printf("Hello world\n");}

In your original code, having the prototype right next to the function definition didn't really serve any purpose!

[edited by - Pootle on July 8, 2003 12:57:52 PM]

Share on other sites
Well I am somewhat new to coding so I might keep it or I might not. My next project that I want to do is between a hangman game or a connect 4 game. Which one do you guys see less of

(friend) WHAT!!!!..... Are you crazy???
(me) Yes I am.

Share on other sites
Connect Four is fun. It would be easy to draw another Text-Based Board. It wouldn''t be much different from this Tic Tac Toe game.

Share on other sites
I have a question... how fast is system(cls); . Can you see the screen clear and print a new board to the screen or is it seamless from the last board to the new one that you would think it is the same board?

I just added it and it works beautifully

(friend) WHAT!!!!..... Are you crazy???
(me) Yes I am.

[edited by - librab103 on July 8, 2003 1:48:19 PM]