![]() |
|
#1
|
|||
|
|||
Pls help in this coding.This is the first time i am using this forum. I hope someone can help me out in the following coding.
I need to create a tic tac toe game where the user enters the row and column. I did the coding in Visual Studio but i got a lot of errors, but the coding is for c++ language i dont know what to do. [c] #include<iostream> using namespace std; int main(); //void main() { char p1[10], p2[10]; int game[3][3],x,z,r,c,s=0,rpos,cpos; cout<<"TIC TAC TOE"<<endl; cout<<"ENTER PLAYER 1 NAME : "; cin>>p1; cout<<"Enter any Number (1-9)"; cin>>x; cout<<"ENTER PLAYER 2 NAME : "; cin>>p2; cout<<"Enter any Number (1-9)"; cin>>z; for(r=0;r>3;r++) { for(c=0;c>3;c++) game[r][c]=0; } for(r=0;r>3;r++) { for(c=0;c>3;c++) cout<<game[r][c]=0; cout<<endl; } do { s=s+1; if(s>4) { cout<<"THE GAME IS DRAW "; break; } cout<<p1<<"Enter Row(1-3) & Column(1-3) : "; cin>>rpos>>cpos; game[rpos-1][cpos-1]=x; for(r=0;r>3;r++) { for(c=0;c>3;c++) cout<<game[r][c]<<" "; cout<<endl; } if((game[0][0]==x && game[0][1]==x && game[0][2]==x) || game[1][0]==x && game[1][1]==x && game[1][2]==x) || game[2][0]==x && game[2][1]==x && game[2][2]==x) || game[0][0]==x && game[1][0]==x && game[2][0]==x) || game[0][1]==x && game[1][1]==x && game[2][1]==x) || game[0][2]==x && game[1][2]==x && game[2][2]==x) || game[0][0]==x && game[1][1]==x && game[2][2]==x) || game[0][0]==x && game[1][1]==x && game[2][0]==x)) { cout<<p1<<"WINS THE GAME "; break; } cout<<p2<<"Enter Row(1-3) & Column(1-3) :"; cin>>rpos>>cpos; game[rpos-1][cpos-1]=z; for(r=0;r>3;r++) { for(c=0;c>3;c++) cout<<game[r] CPP / C++ / C Code:
Last edited by dsmith : 07-Apr-2004 at 08:44.
Reason: Added c syntax highlighting with [c] & [/c]
|
|
#2
|
||||
|
||||
|
Hello Harsha. Welcome to GIDForums. You will notice that I editted your post to include syntax highlighting. It makes it much easier to read
.Anyway, lets get your code to compile first and then we can see if it produces the correct results
CPP / C++ / C Code:
CPP / C++ / C Code:
Fix these items and then see where you are at. Good luck! d |
|
#3
|
|||
|
|||
Thank YouI have done the changes u have ask me to do. It works now. But i did.t know that I can use C++ coding in Visual Studio cause this the first time I am doing in it. When I use Visual I always put void main. Anyway thank you for correcting my small mistakes. U really help me a lot.
I want to ask another question can I use characters to fill in the tic tac toe instate of numbers. I have modify the coding as follow: [c] #include<iostream> using namespace std; int main() { char p1[10], p2[10],x,z; int game[3][3],r,c,s=0,rpos,cpos; cout<<"TIC TAC TOE"<<endl; cout<<"ENTER PLAYER 1 NAME : "; cin>>p1; cout<<p1[0]<<" This is your intial "<<endl; x=p1[0]; //cout<<"Enter any Number (1-9)"; //cin>>x; cout<<"ENTER PLAYER 2 NAME : "; cin>>p2; cout<<p2[0]<<" This is your intial "<<endl; z=p2[0]; //cout<<"Enter any Number (1-9)"; //cin>>z; for(r=0;r<3;r++) { for(c=0;c<3;c++) game[r][c]=0; } for(r=0;r<3;r++) { for(c=0;c<3;c++) cout<<game[r][c]<<"\t"; cout<<endl; } do { s=s+1; if(s>4) { cout<<"THE GAME IS DRAW "; break; } cout<<p1<<" Enter Row(1-3): "; cin>>rpos; cout<<" Column(1-3): "; cin>>cpos; game[rpos-1][cpos-1]=x; for(r=0;r<3;r++) { for(c=0;c<3;c++) cout<<game[r][c]<<"\t"; cout<<endl; } if((game[0][0]==x && game[0][1]==x && game[0][2]==x) || (game[1][0]==x && game[1][1]==x && game[1][2]==x) || (game[2][0]==x && game[2][1]==x && game[2][2]==x) || (game[0][0]==x && game[1][0]==x && game[2][0]==x) || (game[0][1]==x && game[1][1]==x && game[2][1]==x) || (game[0][2]==x && game[1][2]==x && game[2][2]==x) || (game[0][0]==x && game[1][1]==x && game[2][2]==x) || (game[0][0]==x && game[1][1]==x && game[2][0]==x)) { cout<<p1<<"WINS THE GAME "; break; } cout<<p2<<" Enter Row(1-3): "; cin>>rpos; cout<<" Column(1-3): "; cin>>cpos; game[rpos-1][cpos-1]=z; for(r=0;r<3;r++) { for(c=0;c<3;c++) cout<<game[r] CPP / C++ / C Code:
Last edited by dsmith : 07-Apr-2004 at 11:56.
Reason: Please use [c] & [/c] for syntax highlighting.
|
|
#4
|
||||
|
||||
|
Hi harsha. Don't forget to use the [c] & [/c] tags around your c syntax for highlighting. Isn't it prettier that way?
![]() void main() used to be okay, but I believe ANSI C++ dictates that main must return an int. So I always use it no matter what compiler I am using. You can definetely use the first charecter of the name (which is also a pretty good idea.) You do need to change a few things though. First of all define game[][] as a char instead of an int. This is confusing cout when you print. You can typecast this, but it makes more sense to use what the charecter really is. CPP / C++ / C Code:
Next when you initialize your game array, initalize using '0' instead of just 0. Otherwise when you print your empty tic-tac-toe board you will get nothing. [c] game[r] CPP / C++ / C Code:
One more thing. Your program is quite easy to break. If you want to make it more hearty, check the input that you are getting from the users. For instance, If I enter a move of 0, you should try to trap that. Overall this is a pretty good little program. Good job! |
|
#5
|
|||
|
|||
|
good job, dsmith, maybe u can help me with my chess program: it has 1,209 errors! j.k.
|
|
#6
|
||||
|
||||
|
Haha nice one. If that happened for real, I'd have to break your keyboard :-P
__________________
-Aaron |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| coding a sentinel controlled loop | tommy69 | C++ Forum | 2 | 10-Mar-2004 14:52 |
| coding a word with a givin factor | funnyf | C++ Forum | 2 | 13-Jan-2004 08:32 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The