PDA

View Full Version : C++ Program help, please.



Macro_FTW
02-12-2008, 01:17 PM
#include <iostream>
using std::cin;
using std::cout;

bool SpacesFilled(bool []);
bool Winner(int []);
int SpaceToChange();


bool SpacesFilled(bool A[])
{
int C=0;
while(C<9)
{
if(!A[C])
return false;
C++;
}
return true;
}

bool Winner(int A[])
{
int C=0;
int v[3]={0,1,2};
while(C<7)
{
if((A[v[0]]==1 && A[v[1]]==1 && A[v[2]]==1)||(A[v[0]]==2 && A[v[1]]==2 && A[v[2]]==2))
return true;
else {
switch(v[0])
{
case 0: v[0]=1;v[1]=4;v[2]=7;break;
case 1: v[0]=2;v[1]=5;v[2]=8;break;
case 2: v[0]=3;v[1]=0;v[2]=6;break;
case 3: v[0]=4;v[1]=3;v[2]=5;break;
case 4: v[0]=6;v[1]=4;v[2]=2;break;
case 6: v[0]=8;v[2]=0;break;
}
}
C++;
}
return false;
}
int SpaceToChange()
{
int var;

cout << "Player, choose your space.\n";
cin >> var;
return var;
}

void main ()
{
int C=0;
int Temp;
char Shown[9];
int Player[9];
bool Taken[9];
int PlayerTurn=1;

while(C<9)
{
Taken[C]=false;
Player[C]=-1;
Shown[C]=C;
C++;
}
C=0;

while(!SpacesFilled(Taken) && !Winner(Player))
{
TopOfLoop:
cout << "Player " << PlayerTurn << ", please choose a square.\n\n";
while(C<9) //Showing tic-tac-toe board
{
cout << Shown[C];
if(C==0||C==1||C==3||C==4||C==6||C==7)
cout << " | ";
else {
cout << "\n---------\n";
}
C++;
}
C=0;
cin >> Temp;
Temp=Temp-1;
if(Taken[Temp])
goto TopOfLoop;
else {
Taken[Temp]=true;
Player[Temp]=PlayerTurn;
switch(PlayerTurn)
{
case 1: Shown[Temp]='O';break;
case 2: Shown[Temp]='X';break;
}
}
switch(PlayerTurn)
{
case 1: PlayerTurn=2;break;
case 2: PlayerTurn=1;break;
}
}
C=0;
if(!Winner(Player))
cout << "No winner.";
else {
cout << "There's a winner!\n";
while(C<9)
{
cout << Shown[C];
if(C==0||C==1||C==3||C==4||C==6||C==7)
cout << " | ";
else {
cout << "\n---------\n";
}
C++;
}
}
}

It always makes an annoying "BEEP!" when it shows the board. (It cout's the board after TopOfLoop:) All help is appreciated :)

~Macro

Korbman
02-15-2008, 07:28 PM
Ok, well before I give you any solid help I need to find out some information first...what program are you using to develop the C++ code? Is it Visual Studio 6? 2005? Dev C++?
I'm pretty good with VS 2005, and I remember a little bit of VS 6...I'm not that good with Dev C++, but I can make it work =P.
Just let me know and I'll make an edit on this post with some fixes =)
For now I'll try working with it in VS 2005

EDIT: Alright, I admit this is pretty weird. I can't find the source of the beep. Nice job on the code btw :)!
Anyway, I'm not sure if you've noticed this, but I found that if you start at a higher number, such as inputing 8 or 9 first, the beeps go away. If you try to start with anything before that the beeps continue until one of those numbers are pressed.
I can't, for the life of me, figure out why the computer thinks that it needs to beep if the number is less than 8. I'll try working with it for a little while longer, and I'll add some more edits to this post of I fix it.

Weirdest non-syntax error ever :p

ShowerThoughts
02-15-2008, 08:24 PM
tvXxX is good at that he makes an game ask him

Macro_FTW
02-19-2008, 12:52 PM
Ok, well before I give you any solid help I need to find out some information first...what program are you using to develop the C++ code? Is it Visual Studio 6? 2005? Dev C++?
I'm pretty good with VS 2005, and I remember a little bit of VS 6...I'm not that good with Dev C++, but I can make it work =P.
Just let me know and I'll make an edit on this post with some fixes =)
For now I'll try working with it in VS 2005

EDIT: Alright, I admit this is pretty weird. I can't find the source of the beep. Nice job on the code btw :)!
Anyway, I'm not sure if you've noticed this, but I found that if you start at a higher number, such as inputing 8 or 9 first, the beeps go away. If you try to start with anything before that the beeps continue until one of those numbers are pressed.
I can't, for the life of me, figure out why the computer thinks that it needs to beep if the number is less than 8. I'll try working with it for a little while longer, and I'll add some more edits to this post of I fix it.

Weirdest non-syntax error ever :p

Microsoft Visual C++ 2005 Express Version. Thanks for checking it out for me. :D

Yes, it is the wierdest nonsyntax error ever. XD

solarwind
02-23-2008, 09:18 PM
Just a tip: get rid of the using std:: crap at the top there. You don't need it. Not a good practice to use namespaces either. Just call your cin/cout functions like this:

std::cout << "lol" << std::endl;

Easy.

Second thing, don't use explicit arrays. Use the std::vector template container class. You can access it like an array but it's WAYYY better. It can be used in a dynamic way. Seriously. Example: to declare an int array:

std::vector<int> myArray;

Or a boolean array:

std::vector<bool> anotherArray;

Yet another tip: don't use char arrays. Use the string class std::string.

R0b0t1
02-24-2008, 02:14 AM
Uhm... Its back? OMFG! ITS BAAAAACCCCCKKKKK!!!

But it taught me something... something I will not forget ;)




But if I didn't need all of the extra... things ...that vectors have, why use one?

solarwind
02-24-2008, 02:27 AM
Uhm... Its back? OMFG! ITS BAAAAACCCCCKKKKK!!!

But it taught me something... something I will not forget ;)




But if I didn't need all of the extra... things ...that vectors have, why use one?
1. What's back?
2. An array in C++ is very messy. It's just an array of pointers. That's why just use vectors. Trust me, they'll come in handy. That's the C++ way of doing things.

bullzeye95
02-24-2008, 03:28 AM
1. What's back?

Err, unfortunately, I think he's talking about you. :redface:

solarwind
02-24-2008, 04:53 AM
Err, unfortunately, I think he's talking about you. :redface:
Lol.

Anyway, best site for C++ stuff: http://www.cplusplus.com/doc/tutorial/

And here's a simple program I made to demonstrate the use of the std::vector class exactly like an array: http://www.blog.solarwind.metafy.org/2008/02/21/c-random-number-guesser-alpha/