GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Apr-2004, 08:40
harsha harsha is offline
New Member
 
Join Date: Mar 2004
Location: Malaysia
Posts: 12
harsha is on a distinguished road

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:
<<" ";
			cout<<endl;
		} 

		if((game[0][0]==z && game[0][1]==z && game[0][2]==z) ||
			game[1][0]==z && game[1][1]==z && game[1][2]==z) ||
			game[2][0]==z && game[2][1]==z && game[2][2]==z) ||
			game[0][0]==z && game[1][0]==z && game[2][0]==z) ||
			game[0][1]==z && game[1][1]==z && game[2][1]==z) ||
			game[0][2]==z && game[1][2]==z && game[2][2]==z) ||
			game[0][0]==z && game[1][1]==z && game[2][2]==z) ||
			game[0][0]==z && game[1][1]==z && game[2][0]==z))
		{
			cout<<p2<<"WINS THE GAME ";
		break;
		}
	}while(s<=5);
	return 0;
}
Last edited by dsmith : 07-Apr-2004 at 08:44. Reason: Added c syntax highlighting with [c] & [/c]
  #2  
Old 07-Apr-2004, 09:00
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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
  1. Near the top, you have:
    CPP / C++ / C Code:
    
    int main();
    
    //void main()
    {
    

    I am assuming you did this, because the compiler growled at you about void main. This is even worse. Your main function should always return int and there is no need to pre-declare it. So this can be changed to:
    CPP / C++ / C Code:
    int main()
    {
    
  2. Next further down you have:

    [c]
    cout<<game[r]
    CPP / C++ / C Code:
    =0;	
    

    You are trying to do two things here and the compiler is not going to let you do this. You have already initialized each game array holder, all you want to do here is print it, so change this to:
    [c]
    cout<<game[r]
    CPP / C++ / C Code:
    ;
    
  3. Finally in two places, you have these big long comparison statements like:
    CPP / C++ / C Code:
    
    		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))
    		{
    

    In both cases, you are missing opening parenthesis on every line, this should look more like:
    CPP / C++ / C Code:
    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))
        {
    
Now, this should compile, but I don't think it is going to run properly. In your for statements you have:
CPP / C++ / C Code:
for(r=0;r>3;r++)
A for statement has three parameters
  1. An initialization (r=0)
  2. A comparison. The function will loop while this is true (r>3)
  3. An incrementation (r++).
The problem is your comparison is for the function to process while r is greater than three. This never happens. What you want instead is:
CPP / C++ / C Code:
for(r=0;r<3;r++)
Check all of your for statements for this. I believe most of them are incorrect.

Fix these items and then see where you are at.

Good luck!
d
  #3  
Old 07-Apr-2004, 10:50
harsha harsha is offline
New Member
 
Join Date: Mar 2004
Location: Malaysia
Posts: 12
harsha is on a distinguished road
Question

Thank You


I 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:
<<"\t";
      cout<<endl;
    } 

    if((game[0][0]==z && game[0][1]==z && game[0][2]==z) ||
      (game[1][0]==z && game[1][1]==z && game[1][2]==z) ||
      (game[2][0]==z && game[2][1]==z && game[2][2]==z) ||
      (game[0][0]==z && game[1][0]==z && game[2][0]==z) ||
      (game[0][1]==z && game[1][1]==z && game[2][1]==z) ||
      (game[0][2]==z && game[1][2]==z && game[2][2]==z) ||
      (game[0][0]==z && game[1][1]==z && game[2][2]==z) ||
      (game[0][0]==z && game[1][1]==z && game[2][0]==z))
    {
      cout<<p2<<"WINS THE GAME ";
    break;
    }
  }while(s<=5);
  return 0;
}
Last edited by dsmith : 07-Apr-2004 at 11:56. Reason: Please use [c] & [/c] for syntax highlighting.
  #4  
Old 07-Apr-2004, 12:13
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
char p1[10], p2[10],x,z, game[3][3];
  int r,c,s=0,rpos,cpos;

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:
='0';

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  
Old 08-Apr-2004, 19:54
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
good job, dsmith, maybe u can help me with my chess program: it has 1,209 errors! j.k.
  #6  
Old 08-Apr-2004, 20:48
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Haha nice one. If that happened for real, I'd have to break your keyboard :-P
__________________
-Aaron
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 18:18.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.