[ back to toc ]

Game

Date: 2002/05/04 13:45

Q:
I have this program code (C Program), im having problems running them, can
u help, n point out y there not workin. Im using telnet, dunno if this is
causin the problem, but can u compile n check if it works?? thx, its a
noughts n crooses game. I havent got access to a unix station till next
week, so, if it works can u tell me, and help wit n errors? all help is
appreciated.

/*Program Noughts & Crosses*/
/*To play: Enter row co-ordinate, then 'SPACE' then column co-ordinate
'ENTER'. ( row & column co-ordinates should be numbers 1,2,3, as row and
column 0 are not in use) i.e. 2 'space' 2 'ENTER'*/

#include <stdio.h>
#include <string.h> /*sets up 2 strings so names can be used*/
char oxo[4][4];
void display_grid();
int main(void)

{
int r1,c1,r0,c0,rx,cx,count;
int name1[20],name2[20]; u/*defines name1 & name2 are variables */

for(r1=1;r1<=3;r1++)

{

for(c1=1;c1<=3;c1++)

{

oxo[r1][c1]='.';
oxo[0][1]='1'; /**/
oxo[0][2]='2'; /*prints column numbers*/
oxo[0][3]='3'; /**/
oxo[1][0]='1'; /**/
oxo[2][0]='2'; /*prints row numbers*/
oxo[3][0]='3'; /**/

}

}

printf("\n Player 1 what is your name?\n" ); /*asks player 1 1s name*/

scanf("%s",name1); /*reads name*/

printf("\nHello %s you place the '0's & you go first! \n",name1); /*print
out */

printf("\n Player 2 what is your name?\n"); /*asks player 1 2s name*/

scanf("%s",name2); /*reads name*/

printf("\nHello %s you place the 'X's & you go second! \n",name2); /*print
out */

display_grid();

count=0;

while(count<9)

{

if(count<9)

{

do{

printf("%s enter you next move, '0's",name1); /*asks for m move by name*/

scanf("%d%d",&r0,&c0);

}

while((oxo[r0][c0]!='.')||(oxo[r0][c0]=='X')||(oxo[r0][c0]=='0'));

{

oxo[r0][c0]='0';

}

count++;

} display_grid();

/*These lines look to see if there is a row of 0s in any of the 8 ways
possible program looks to see if 0s have won*/

if ((oxo[1][1]=='0'&&oxo[1][2]=='0'&&oxo[1][3]=='0')||

(oxo[2][1]=='0'&&oxo[2][2]=='0'&&oxo[2][3]=='0')||
(oxo[3][1]=='0'&&oxo[3][2]=='0'&&oxo[3][3]=='0')||
(oxo[1][1]=='0'&&oxo[2][1]=='0'&&oxo[3][1]=='0')||
(oxo[1][2]=='0'&&oxo[2][2]=='0'&&oxo[3][2]=='0')||
(oxo[1][3]=='0'&&oxo[2][3]=='0'&&oxo[3][3]=='0')||
(oxo[1][1]=='0'&&oxo[2][2]=='0'&&oxo[3][3]=='0')||
(oxo[3][1]=='0'&&oxo[2][2]=='0'&&oxo[1][3]=='0'))

{

printf("\n GAME OVER...%s WINS.... GAME OVER\n",name1); / /*if 0 wins
print this*/

return; /*if '0' wins end game*/

}

else /*if 0 not won program continues*/

count<9;

if (count<9)

{

do{

printf("%s enter you next move, 'X's",name2 ; /*asks for m move by
name*/

scanf("%d%d",&rx,&cx);

}

while((oxo[rx][cx]!='.')||(oxo[rx][cx]=='X')||(oxo[rx][cx]=='0'));

{

oxo[rx][cx]='X';
}

count++;

}

display_grid();

/*These lines look to see if there is a row of Xs in any of the 8 ways
possible program looks to see if Xs have won*/

if ((oxo[1][1]=='X'&&oxo[1][2]=='X'&&oxo[1][3]=='X')||

(oxo[2][1]=='X'&&oxo[2][2]=='X'&&oxo[2][3]=='X')||
(oxo[3][1]=='X'&&oxo[3][2]=='X'&&oxo[3][3]=='X')||
(oxo[1][1]=='X'&&oxo[2][1]=='X'&&oxo[3][1]=='X')||
(oxo[1][2]=='X'&&oxo[2][2]=='X'&&oxo[3][2]=='X')||
(oxo[1][3]=='X'&&oxo[2][3]=='X'&&oxo[3][3]=='X')||
(oxo[1][1]=='X'&&oxo[2][2]=='X'&&oxo[3][3]=='X')||
(oxo[3][1]=='X'&&oxo[2][2]=='X'&&oxo[1][3]=='X'))

{

printf("\nGAME OVER...%s WINS!!... GAME OVER\n",name2);
/ /*if X wins print this*/

return; /*if X wins end game*/

}

else /*if 0 not won program continues*/

count<9;

}

if (count=9) /*when 9 moves have been made with no winner game stops*/

printf("\nGAME OVER...NO WINNER!!...GAME OVER\n"); /*print o u out*/

}

void display_grid()

{

int row, col;

for(row=1;row<=3;row++)

{

for(col=1;col<=3;col++)

{

printf(" %c |",oxo[row][col]); /*prints & spaces vertical g grid
lines*/

}

printf("\n");

if(row<=3)

{

printf(" ---------------\n"); /*prints & spaces horizontal g grid
lines*/

}

}

return;

}
A:
Dear *NAME-DELETED*,

The program you wrote works fine. There are some minor syntax errors that
you have to correct. Other than that there is no problem with the program,
and in case the program was written you I congratulate, because you wrote
functionally perfect code without debugging. To fix the syntax errors on
the other hand should not be an issue for you.

Please respect others to write English and not slang. Especially when you
talk to someone that is not native English as I am. It really caused me a
small hedeache to understand your sentences. I made me actually learn some
american slang, but that is not the purpose.

regards,
Peter
Q:
Sorry about the slang, the problem i am having is when I cut and paste the
program into the telnet window, when i compile the program i get errors,
and cant run the program, but when i run the program in linux it works
fine, do you know why this might be happening? Have you got access to
telnet so you can try? AGAIN, im am very sorry about the slang used in my
last post. (",)

A:
Dear Mark,

as I wrote in my last letter there are (were) some syntax errors in the
program that way you sent it to me. They do NOT seem to be simple
copy/paste errors, but rather typing errors. It would be helpful if you
could not only say that the compiler gives error but could verbatim copy
here the error messages, thus I could have some clue on the reason.

Regards,
Peter
Q:
Yes, I sorted out the Syntax errors and the program works fine, I got a
few questions on how I could improve my program. First thing I want to do
is-at the moment when a co-ordinate is put in where a "X" or "O" has
already been placed it just keeps asking until a valid set of co-ordinates
is entered(no error message) - how would I go about putting a error
message in telling the user that at the co-ordinates that they have
entered that there is not a free place.

Also, when the user is asked to enter co-ordinates, if a letter is put in
the program crashes, i need to validate this so a error message is
displayed - I have NO clue at all of how to do this, if you could help it
would be very much appreciated.

Thx
A:
Dear Mark,

I am certain that this is a homework for you and using this service you
try to min8imize the effort you invest in it. My humble opinion is that
you should not. You have to starve for the result otherwise you loose your
time and do not lern anything. This was the very reason why I did not tell
you where the syntax errors were.

I am certain that you did NOT write this program, not a line of it, but
you need a working program to submit as a homework.

I apologize if this is not the case, but I am certain.

OK. Here are some answers to your questions, that will really help you to
solve the issue, but will not solve it instead of you:

1. In the code you can easily find where the coordinate reading is
handled. The code now is:

do{

printf("%s enter you next move, '0's",name1); /*asks for m move by name*/

scanf("%d%d",&r0,&c0);

}

while((oxo[r0][c0]!='.')||(oxo[r0][c0]=='X')||(oxo[r0][c0]=='0'));

What you can do is to set r0 and c0 to some value out of range (-1 for
example) before the 'do' loop and print an error message before the
'scanf' if r0 and c0 are not -1:

if( r0 != -1 && c0 != -1 )
printf("The place is already occupied\n");

2. If the user enters some letter then the function 'scanf' puts zero into
the variable. You have to extend the error condition in loop tail 'while':

while(
(r0<1)||(r0>3)||(c0<1)||(c0>3)||
(oxo[r0][c0]!='.')||(oxo[r0][c0]=='X')||(oxo[r0][c0]=='0'));

The program crashes, because the array oxo is indexed below (or if the
number is too big then above) the index limits. Due to short circuit
evaluation of the logical expression the error will be eliminated this
way. (You may need to debug the code, may not be perfect, but the main
solution is this.)

Regards,
Peter

[ back to toc ]