Ok, in TrueBasic, i made a tictactoe program. It was awsome and worked perfect. Now, im trying to convert the program to c++.
PLEASE, take a sec and look at the following attachments. The first is the working program in TrueBasic, the second is my attempt at converting it. I get mega errors in the c++ one. I dont know what im doing wrong, and im confused on how to make subroutines and pass variables so i can use the same variables in different subroutines. I dont have to use variable passing in the truebasic version, but i must in c++. Please help me! These are both word documents. Ok nevermind, here is the c++ attempt and there is no attachment. Just say if you want to see it in truebasic (this is really cool). *Keep in mind, I probably dont have the 'void' and 'variable passing' syntax correct, so help me on that please*:
#include "iostream.h"
#include "conio.h"
#include "stdlib.h"
void getmove(int y, int z, char playername, char playerup)
{
do
{
cout<<playername<<", you are the "<<playerup;
cout<<".Enter the x coordinate for you move: ";
cin>>y>>endl;
check_input(y, z);
cout<<"playername<<", you are the "<<playerup;
cin>>z>>endl;
check_input(y, z);
if (board[y][z]!=="-")
{
cout<<"You're more stupid than my grandma! That spot's taken!"<<endl;
}
}while (board[y][z]!=="-");
board[y][z]=playerup;
}
void showboard(char board[3][3])
{
cout<<"CURRENT BOARD";<<endl;
cout<<"--------------------------"<<endl;
cout<<board[1][1]<<""<<board[1][2]<<""<<board[1][3]<<endl;
cout<<board[2][1]<<""<<board[2][2]<<""<<board[2][3]<<endl;
cout<<board[3][1]<<""<<board[3][2]<<""<<board[3][3]<<endl;
cout<<"--------------------------"<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
}
void key()
{
cout<<"This is the key(x coordinate and y coordinate)"<<endl;
cout<<"----------------------------------------------"<<endl;
cout<<"(1,1) (1,2) (1,3)"<<endl;
cout<<""<<endl;
cout<<"(2,1) (2,2) (2,3)"<<endl;
cout<<""<<endl;
cout<<"(3,1) (3,2) (3,3)"<<endl;
cout<<"----------------------------------------------"<<endl;
cout<<""<<endl;
cout<<""<<endl;
}
void check_input(int y, int z)
{
while (y!==1 && y!==2 && y!==3)
{
cout<<"You scum-bucket scallywag! Re-enter your number!"<<endl;
cin>>y
}
}
void checkforwin(int y, int z)
{
for (int qr=1; qr<3; qr++);
{
for (int g=1; g<4; g++);
{
if (board[1][g]=="X" && board[2][g]=="X" && board[3][g]=="X")
{
win=1
}
}
for (int t=1; t<4; t++);
{
if (board[t][1]=="X" && board[t][2]=="X" && board[t][3]=="X")
{
win=1
}
}
if (board[1][1]=="X" && board[2][2]=="X" && board[3][3]=="X")
{
win=1
}
if (board[1][3]=="X" && board[2][2]=="X" && board[3][1]=="X")
{
win=1
}
}
}
void computermove(int y, int z,int d)
{
y=0;
z=0;
int d;
randomize();
for (int count=1; count<3; count++)
{
if (count==1)
{
d="X";
else if (count==2)
{
d="O";
}
}
for (int a=1; a<4; a++)
{
if (board[a][1]=d && board[a][2]=d && board[a][3]="-")
{
y=a;
z=3;
}
else if (board[a][1]=d && board[a][2]="-" && board[a][3]=d)
{
y=a;
z=2;
}
else if (board[a][1]="-" && board[a][2]=d && board[a][3]=d)
{
y=a;
z=1;
}
else if (board[1][a]=d && board[2][a]=d && board[3][a]="-")
{
y=3;
z=a;
}
else if (board[1][a]=d && board[2][a]="-" && board[3][a]=d)
{
y=2;
z=a;
}
else if (board[1][a]="-" && board[2][a]=d && board[3][a]=d)
{
y=1;
z=a;
}
else if (board[1][1]=d && board[2][2]=d && board[3][3]="-")
{
y=3;
z=3;
}
else if (board[1][1]=d && board[2][2]="-" && board[3][3]=d)
{
y=2;
z=2;
}
else if (board[1][1]="-" && board[2][2]=d && board[3][3]=d)
{
y=1;
z=1;
}
else if (board[1][3]=d && board[2][2]=d && board[3][1]="-")
{
y=3;
z=1;
}
else if (board[1][3]=d && board[2][2]="-" && board[3][1]=d)
{
y=2;
z=2;
}
else if (board[1][3]="-" && board[2][2]=d && board[3][1]=d)
{
y=1;
z=3;
}
else if (y=0 && z=0)
{
do
{
y=1+rand() % 3;
z=1+rand() % 3;
}while(board[y][z]!=="-"
}
}
}
board[y][z]="O"
}
int main()
{
char board[3][3];
int win;
int e;
int r;
int moves;
char choose;
char name1;
char name2;
char playername;
char playerup;
char choose1;
int x;
int y;
do
{
win=0;
moves=0;
cout<<"Do you want to play with a human (H) or the computer (C)?";
cin>>choose;
if choose=="H" || choose=="h"
{
//************************MAIN LOOP************************************
cout<<"Player 1, enter your name: ";
cin>>name1;
cout<<"Player 2, enter your name: ";
cin>>name2;
system("CLS");
for (int x=1; x<4; x++);
{
for (int y=1; y<4; y++);
{
board[x][y]="-"
}
}
do
{
showboard(board[3][3]);
key();
playerup="X";
playername=name1;
getmove(y,z,playername,playerup);
moves=moves+1;
system("CLS");
checkforwin(y, z);
if (win==0 && moves<9)
{
showboard(board[3][3]);
key();
playerup="O"
playername=name2
getmove(y,z,playername,playerup);
moves=moves+1;
checkforwin(y, z);
}
system("CLS");
if (moves==9 && win==0)
{
win=3
}
}while(win!==1 && win!==2 && win!==3);
showboard(board[3][3]);
if (win==1)
{
cout<<name1<<", you won!"<<endl;
}
else if (win==2)
{
cout<<name3<<", you won!"<<endl;
}
else if (win==3)
{
cout<<"Neither of you won, it's a tie!"<<flush<<endl;
}
_getch();
system("CLS");
cout<<"Do you want to play again? (Enter 'n' for no): ";
cin>>choose1;
}
else if (choose="c" || choose="C")
{
for (int x=1; x<4; x++);
{
for (int y=1; y<4; y++);
{
board[x][y]="-"
}
}
cout<<"Player 1, enter your name: ";
cin>>name1
do
{
showboard(board[3][3]);
key();
playerup="X";
playername=name1;
getmove(y,z,playername,playerup);
moves=moves+1;
system("CLS");
checkforwin(y, z);
if (win==0 && moves<9)
{
showboard(board[3][3]);
key();
computermove(y,z, d);
moves=moves+1;
checkforwin(y, z);
}
system("CLS")
if (moves==9 && win==0)
{
win=3
}
}while(win!==1 && win!==2 && win!==3);
showboard(board[3][3]);
if (win==1)
{
cout<<name1<<", you won!"<<endl;
}
else if (win==2)
{
cout<<name3<<"GAME OVER YOU SUCK!"<<endl;
}
else if (win==3)
{
cout<<"Neither of you won, it's a tie!"<<flush<<endl;
}
_getch();
system("CLS");
cout<<"Do you want to play again? (Enter 'n' for no): ";
cin>>choose1;
}
}while(choose1!=="n" || choose1!=="N");
return 0;
}