Thread: Oh My Gosh, I Need Help With This!!!

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    Oh My Gosh, I Need Help With This!!!

    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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's hard to know where to start - there are a number of problems just looking at it, never mind compiling it.

    All over the place, you have
    > board[y][z]!=="-"
    When you should have
    &nbsp; board[y][z]!=='-'
    Not only that, !== is not an operator, you mean !=

    > void check_input(int y, int z)
    You make no check for z.
    In addition, the scope and parameter rules are such that changing y in this function will have NO effect on the caller's copy of y (or z)

    > if (board[1][g]=="X" && board[2][g]=="X" && board[3][g]=="X")
    Use of "" instead of '' as already mentioned.
    And an array overstep problem because you've declared the board as 3x3, but have forgotten that C arrays start at index 0, not 1

    > d="X";
    Yet more "" when you should be ''

    > if choose=="H" || choose=="h"
    I think you know about the "" now
    But where are the ( ) ?

    > showboard(board[3][3]);
    Nope this is not how you pass an array to a function. In this case, you need
    &nbsp; showboard(board);

    If ever there was a good example of how not to do something, this is it.

    This has so obviously been written in one go without ever trying to compile it. Of course now, all the mistakes which you should have spotted have been propagated all over the place, and you now have " I get mega errors in the c++ one. "

    Personally, I would do these things
    1. delete this file (or hide it until later)
    2. get a pencil / paper and sketch out the functions you require, plan the interfaces between them and choose how to represent the main data structure (the board).
    3. WRITE and TEST each function in turn - do not make a mad stampede for the finish line like you have done this time, otherwise we'll be having this conversation again.

    > and im confused on how to make subroutines and pass variables so i can use the same variables in different subroutines.
    Then practice on a simpler problem first.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. knight's tour!
    By Jasin14 in forum C Programming
    Replies: 13
    Last Post: 12-22-2010, 04:30 PM
  2. Splitting NTFS Partitions...
    By Grayson_Peddie in forum Tech Board
    Replies: 12
    Last Post: 02-20-2004, 12:24 AM
  3. Gosh darn it I hate my job
    By face_master in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 01-04-2003, 02:11 PM
  4. oh my gosh this is horrible...it has to be stopped
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-13-2002, 02:32 PM