Hi,
I just wrote the classic tictactoe game, everything works fine, and now i want to use timers that can be toogled on/off before the game.
1 2 3
|
void stimer() //start/set the timer
{now = clock();
deadline = now + 5*CLOCKS_PER_SEC;}
|
1 2 3 4
|
bool ftimer()//finish the timer
{now = clock();
if (deadline>now){return TRUE;}//if player was in time its true, else false
else {return FALSE;}}
|
1 2 3 4
|
bool iftimer()
{char ans;ans=getch();//this would be the toogle funct
if (ans=='y'||ans=='Y'){return TRUE;}
else return (FALSE);}
|
1 2
|
void clastmove()//if timer is on it cancels the last move if it was out of time
{if(iftimer()==TRUE){table[x-1][y-1]=' ';}}
|
1 2
|
void ctimer() //and finally the check if player is out of time, so it deletes the mark.
{if (ftimer()==FALSE){cout << "Time out! Next player.\n";clastmove();}}
|
Well, maybe there are much better ideas for this timer problem, but hell ya.
I place the functions like this:
1 2 3 4
|
stimer(); //set the timer
cin >> x_coord; cin >> y_coord; ftimer(); //after user input check if user stepped in time or not
*draws the new 'X' or 'O' in table*
ctimer();//if user was not in time it cancels his move.
|
So, when the time is out, clastmove() is executed, it checks if the timer is on, if on it cancels the last move. But when clastmove() is executed - if(iftimer()==TRUE) - the iftimer is also, and it asks again if i want to turn on the timer.
How can i do that an "if(***)" only reads the return value of the function, nad not executes it again, and i only have to toogle it on at the start of program, and then the other functions only read the value "true" if i answered 'y', or "false" if i said 'n'.
I'm only learning and practicing c++, so my programs are not well organized, but they do what they have to.
ps:sorry for my english.
EDIT1:if the pc is unbelievably slow that it can only happen in nightmares, so the call of ctimer also calls the ftimer(i think) in the if statement, and it replaces the value of the ftimer called instantly after user input or not?
EDIT2:i know the solution of placing ans=getch() in the main and the iftimer() only reads it, so if there arent any other solutions let me know and i close this stupid topic:P