My First c++ program : TIC TAC TOE !

This is a discussion on My First c++ program : TIC TAC TOE ! within the C++ Programming forums, part of the General Programming Boards category; Hi all, just completed my first c++ program! After asking some questions on these and other boards, i was able ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    11

    My First c++ program : TIC TAC TOE !

    Hi all, just completed my first c++ program! After asking some questions on these and other boards, i was able to complete it.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    using namespace std;
    	
    
    int playerturn;
    string boardarray[4][4];
    bool boardarray2[4][4];
    bool winstatus;
    
    
    
    struct drawmenu(){
    
    	cout << "O----------------------O " << endl;
    	cout << "|  TIC-TAC-TOE         | " << endl;
    	cout << "|    by: Andy Hin      | " << endl;
    	cout << "|                      | " << endl;
    	cout << "|  [1] Begin Game      | " << endl;
    	cout << "|                      | " << endl;
    	cout << "|  [2] Quit Game       | " << endl;
    	cout << "|                      | " << endl;
    	cout << "O----------------------O " << endl;
    	
    
    	
    }
    
    struct drawboard(){
    
    	
    	cout << "      1     2     3   " << endl;
    	cout << "				       " << endl;
    	cout << "1       "<<boardarray[1][1]<<" | "<<boardarray[1][2]<<"     | "<<boardarray[1][3]<< endl;
    	cout << "          |       |    " << endl;
    	cout << "      -----------------" << endl;
    	cout << "2       "<<boardarray[2][1]<<" | "<<boardarray[2][2]<<"     | "<<boardarray[2][3]<< endl;
    	cout << "          |       |    " << endl;
    	cout << "      -----------------" << endl;
    	cout << "3       "<<boardarray[3][1]<<" | "<<boardarray[3][2]<<"     | "<<boardarray[3][3]<< endl;
    	cout << "          |       |    " << endl;
    
    	
    }
    
    
    
    
    struct winchecko(){
    	
    	if (boardarray[1][1]=="O" && boardarray[1][2]=="O" && boardarray[1][3]=="O"){
    		cout << "O wins, top row!" << endl;
    		winstatus=true;
    	}else if (boardarray[2][1]=="O" && boardarray[2][2]=="O" && boardarray[2][3]=="O"){
    		cout << "O wins, middle row!" << endl;
    		winstatus=true;
    	}else if (boardarray[3][1]=="O" && boardarray[3][2]=="O" && boardarray[3][3]=="O"){
    		cout << "O wins, bottom row!" << endl;
    		winstatus=true;
    	}else if (boardarray[1][1]=="O" && boardarray[2][1]=="O" && boardarray[3][1]=="O"){
    		cout << "O wins, first collumn!" << endl;
    		winstatus=true;
    	}else if (boardarray[1][2]=="O" && boardarray[2][2]=="O" && boardarray[3][2]=="O"){
    		cout << "O wins, second collumn!" << endl;
    		winstatus=true;
    	}else if (boardarray[1][3]=="O" && boardarray[2][3]=="O" && boardarray[3][3]=="O"){
    		cout << "O wins, third collumn!" << endl;
    		winstatus=true;
    	}else if (boardarray[1][1]=="O" && boardarray[2][2]=="O" && boardarray[3][3]=="O"){
    		cout << "O wins, diagonally!" << endl;
    		winstatus=true;
    	}else if (boardarray[3][1]=="O" && boardarray[2][2]=="O" && boardarray[1][3]=="O"){
    		cout << "O wins, diagonally!" << endl;
    		winstatus=true;
    	}
    }
    
    struct wincheckx(){
    	
    	if (boardarray[1][1]=="X" && boardarray[1][2]=="X" && boardarray[1][3]=="X"){
    		cout << "X wins, top row!" << endl;
    		winstatus=true;
    	}else if (boardarray[2][1]=="X" && boardarray[2][2]=="X" && boardarray[2][3]=="X"){
    		cout << "X wins, middle row!" << endl;
    		winstatus=true;
    	}else if (boardarray[3][1]=="X" && boardarray[3][2]=="X" && boardarray[3][3]=="X"){
    		cout << "X wins, bottom row!" << endl;
    		winstatus=true;
    	}else if (boardarray[1][1]=="X" && boardarray[2][1]=="X" && boardarray[3][1]=="X"){
    		cout << "X wins, first collumn!" << endl;
    		winstatus=true;
    	}else if (boardarray[1][2]=="X" && boardarray[2][2]=="X" && boardarray[3][2]=="X"){
    		cout << "X wins, second collumn!" << endl;
    		winstatus=true;
    	}else if (boardarray[1][3]=="X" && boardarray[2][3]=="X" && boardarray[3][3]=="X"){
    		cout << "X wins, third collumn!" << endl;
    		winstatus=true;
    	}else if (boardarray[1][1]=="X" && boardarray[2][2]=="X" && boardarray[3][3]=="X"){
    		cout << "X wins, diagonally!" << endl;
    		winstatus=true;
    	}else if (boardarray[3][1]=="X" && boardarray[2][2]=="X" && boardarray[1][3]=="X"){
    		cout << "X wins, diagonally!" << endl;
    		winstatus=true;
    	}
    	
    }
    
    struct drawcheck(){
    	if (playerturn==9 && winstatus==false){
    		cout << "Cats Game!" << endl;
    	}
    }
    
    
    
    int main(){
    
    	boardarray[1][1]=" ";
    	boardarray[1][2]=" ";
    	boardarray[1][3]=" ";
    	boardarray[2][1]=" ";
    	boardarray[2][2]=" ";
    	boardarray[2][3]=" ";
    	boardarray[3][1]=" ";
    	boardarray[3][2]=" ";
    	boardarray[3][3]=" ";
    
    
    	int menuselect=0;
    	int moverow;
    	int movecollumn;
    
    	drawmenu();
    	
    	cin >> menuselect;
        if (menuselect=1){
    		cout << "Being Game..." << endl;
    		drawboard();
    		}
    		
    	do{
    
    	cout << " Please Make a move"<<endl;
    	cout << " Row:";
    	cin >>  moverow;
    	cout << " Collumn: ";
    	cin >> movecollumn;
    	
    
    	
    	if (playerturn%2==1 && boardarray2[moverow][movecollumn]==false){ 
    		boardarray[moverow][movecollumn]= "X";
    		boardarray2[moverow][movecollumn]=true;
    		playerturn++;
    	
    	} else if (playerturn%2==0 && boardarray2[moverow][movecollumn]==false){
    		boardarray[moverow][movecollumn]= "O";
    		boardarray2[moverow][movecollumn]=true;
    		playerturn++;
    	
    	}
    	
    	wincheckx();
    	winchecko();
    	drawcheck();
    	drawboard();
    
    	
    
    	}while (winstatus==false);
    
    
    return 0;
    }
    lol, its definitely not the cleanest code, and i was just programming as i went, adding more code to fix problems - which isn't the best way. Let me know your suggestions, and i will work on to improve in my next project.

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    i havent run it yet..
    Qould like to check it out later..

    but why do people say they are coding in C++ when they dont use any of its concepts mainly Classes.. (Object oriented programming)..

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    Looks good. One thing you can change is so that the user doesnt have to type the row and column to place the next marker in, and I suggest you make the "board" smaller, since x and o are still the same size I think a good size of the board helps.

    As I said this is good for someone who is so new to C++, when you get a little more experienced you might want to go back and code an AI for the game.

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    >>but why do people say they are coding in C++ when they dont use any of its concepts mainly Classes

    maybe because they haven't learned classes yet

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>struct drawmenu()
    That's completely wrong, along with the other times you do that. You should declare it as void drawmenu( )
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    Originally posted by vasanth
    i havent run it yet..
    Qould like to check it out later..

    but why do people say they are coding in C++ when they dont use any of its concepts mainly Classes.. (Object oriented programming)..
    I totally agree with you vasanth because what is the difference between C and C++ except in Object Oriented Programming!!

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by M_Ghani
    I totally agree with you vasanth because what is the difference between C and C++ except in Object Oriented Programming!!
    Templates.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    the OP does use classes, just not any user defined classes. In fact OP used three different classes. cin is an object of the istream class defined in iostream, and cout is an object of the ostream class defined in iostream. neither would be allowed in a C program. Likewise, boardarray is an object of the STL string class, which would never fly in C. Furthermore, the OP used a namespace, which wouldn't be allowed in C. The OP program was definitely in C++. In fact, OP probably should have used cstdlib rather than stdlib.h, which also wouldn't be allowed in C.

    Could OP have been written the program in C since no user defined classes were used? Sure. Should they have? That's debateable, and is a debate which no one could ever win.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe!
    By Sinensis in forum C Programming
    Replies: 2
    Last Post: 10-21-2008, 04:40 PM
  2. Check tic tac toe winner
    By cjmdjm in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2005, 11:41 AM
  3. Tic Tac Toe AI help please...
    By Rune Hunter in forum Game Programming
    Replies: 12
    Last Post: 11-05-2004, 03:24 PM
  4. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM
  5. Need help with Tic Tac Toe
    By Zerostatic in forum C++ Programming
    Replies: 19
    Last Post: 07-19-2002, 07:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21