Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C++ Tic-tac-toe

tic-tac-toe first progam

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1 dbordzo

dbordzo

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 27 July 2013 - 02:44 AM

Hello, This is my first program. I will be glad if you could tell me what mistakes I'm making ; )
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int which_move = 1;
char tab[9] = { '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' };  //Main board
//***********************************************************************************
void board();       //Draw a board
void put_X(int);
void put_O(int);
int win_X();        //Checked win X
int win_O();        //Checked win O
void end_game();
//***********************************************************************************
int main()
{
    cout << "Hello in tic-tac-toe game!!!!!\n\n\n\n\n";
    int cross;
    int nought;
    
    //Draw a board
    board();
    
    //Main Loop
    while(which_move < 10)
    {
        // X or O
        if (which_move%2)
        {
            //Checked win, yes = break
            if(win_O())
                end_game();
                break;
        
            //Checked correct move
            do{
                cout << "\n\nWhere put cross: ";
                cin >> cross;}
            while(tab[cross - 1] == 'O' or tab[cross - 1] == 'X' or cross > 9);
            
            put_x(cross);
        }
        
        else
        {
            if(win_X())
                end_game();
                break;
            
            do{
            cout << "\n\nPodaj gdzie postawic nought: ";
            cin >> nought;}
            while(tab[nought - 1] == 'O' or tab[nought - 1] == 'X' or nought > 9);
            put_O(nought);

        }
    
    which_move++;
    }
}
//**********************************************************************************
void board()
{

    cout << "\n\n\n";
    
    //Loop do draw a board
    for (int i = 0 ; i <= 8 ; i++)
    {
        cout << tab[i];
        if(i == 2 or i == 5) //New line
        {
            cout << "\n";
        }
        else
        {
            if (i != 8) cout << " | ";
        }
    }
}
//*****************************************************************************
void put_x(int cross)
{
    tab[cross - 1] = 'X';
    system("cls");
    board();
}
//*****************************************************************************
void put_O(int nought)
{
    tab[nought - 1] = 'O';
    system("cls");
    board();
}
//*****************************************************************************
int win_X()
{
    //Checked X
    if ((tab[0] == 'X' and tab[1] == 'X' and tab[2] == 'X')
        or (tab[0] == 'X' and tab[3] == 'X' and tab[6] == 'X')
        or (tab[1] == 'X' and tab[4] == 'X' and tab[7] == 'X')
        or (tab[3] == 'X' and tab[5] == 'X' and tab[8] == 'X')
        or (tab[3] == 'X' and tab[4] == 'X' and tab[5] == 'X')
        or (tab[6] == 'X' and tab[7] == 'X' and tab[8] == 'X')){return 1;}
    else { return 0;}
}
//*******************************************************************************
int win_O()
{
    //Checked O
    if ((tab[0] == 'O' and tab[1] == 'O' and tab[2] == 'O')
        or (tab[0] == 'O' and tab[3] == 'O' and tab[6] == 'O')
        or (tab[1] == 'O' and tab[4] == 'O' and tab[7] == 'O')
        or (tab[3] == 'O' and tab[5] == 'O' and tab[8] == 'O')
        or (tab[3] == 'O' and tab[4] == 'O' and tab[5] == 'O')
        or (tab[6] == 'O' and tab[7] == 'O' and tab[8] == 'O')){return 1;}
    else { return 0;}
}
//*******************************************************************************
void end_game()
{
    cout << "\n\nGame is over, winner: ";
    string winner = which_move%2 ? "nought\n\n" : "cross\n\n";
    cout << winner;
}


Edited by Roger, 27 July 2013 - 10:40 PM.


#2 Scotty

Scotty

    CC Newcomer

  • Member
  • PipPip
  • 10 posts

Posted 27 July 2013 - 08:43 PM

Are you having runtime or compiler issues? 


Edited by Scotty, 27 July 2013 - 09:01 PM.


#3 dbordzo

dbordzo

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 28 July 2013 - 06:40 AM

Oh sorry I thought, that here we can write us code, to share with overs, and they, are saying what mistakes are we doing, or if everything is ok, they say this. Because sometimes, code works properly, but looks very bad. Sorry if this's wrong section ; ).



#4 Scotty

Scotty

    CC Newcomer

  • Member
  • PipPip
  • 10 posts

Posted 28 July 2013 - 09:07 AM

I think you've done a good job considering it's your first program :)

 

Some things you could work on are:

 

Commenting,

Better variable names... also try to avoid 'i' unless it's an arbitrary decision,

 

for (int i = 0 ; i <= 8 ; i++)

 

could be turned into

 

for (int panel = 0 ; panel <= 8 ; panel++)

 

This clearly shows you are looping through panels of the board.

 

 

Notice how you have many functions doing the same task, just modified slightly. An example would be:

 

 

  1. //*****************************************************************************
  2. void put_x(int cross)
  3. {
  4. tab[cross - 1] = 'X';
  5. system("cls");
  6. board();
  7. }
  8. //*****************************************************************************
  9. void put_O(int nought)
  10. {
  11. tab[nought - 1] = 'O';
  12. system("cls");
  13. board();
  14. }
  15. //*****************************************************************************

 

Both could be replaced with just one function:

 

  1. //*****************************************************************************
  2. void place_marker( int location , char marker ) {
  3. tab[location - 1] = marker;
  4. system("cls");
  5. board();
  6. }
  7. //*****************************************************************************

 

Have a look at your main while loop and other functions and guess where the 'double code' lies. (In main loop) See if you can find a way to combine that 'if' and 'else' together, avoiding the duplicate code.


Edited by Scotty, 28 July 2013 - 09:08 AM.


#5 dbordzo

dbordzo

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 29 July 2013 - 02:34 AM

Thank you very much Scotty! ; ) Now my code looks much better I think. I know that my name of variables and comments, are really primitives, but I m not good in English yet. Thank you one more!

 

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
 
int which_move = 1;
char tab[9] = { '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' };  //Main board
//***********************************************************************************
void board();       //Draw a board
void place_marker( int location, char marker );
int win();        //Checked win
void end_game();
//***********************************************************************************
int main()
{
    cout << "Hello in tic-tac-toe game!!!!!\n\n\n\n\n";

    int place;
    
    //Draw a board
    board();
    
    //Main Loop
    while(which_move < 10)
    {
        if (win())
		  break;
	   
	   // Where place
        do{
		  string figure = which_move%2 ? "cross" : "nought";
		  cout << "\n\nWhere put " << figure << ": "; 
		  cin >> place;
	   }while(tab[place - 1] == 'O' || tab[place - 1] == 'X' || place > 9);
    
    place_marker( place, (which_move % 2) ? 'X' : 'O' );
    which_move++;
    }
}
//**********************************************************************************
void board()
{
 
    cout << "\n\n\n";
    
    //Loop do draw a board
    for (int panel = 0 ; panel <= 8 ; panel++)
    {
        cout << tab[panel];
        if(panel == 2 || panel == 5) //New line
        {
            cout << "\n";
        }
        else
        {
            if (panel != 8) cout << " | ";
        }
    }
}
//*****************************************************************************
void place_marker( int location, char marker )
{
    tab[location - 1] = marker;
    system("cls");
    board();
}
//*****************************************************************************
int win()
{
    if((tab[0] == 'X' && tab[1] == 'X' && tab[2] == 'X')
        || (tab[0] == 'X' && tab[3] == 'X' && tab[6] == 'X')
        || (tab[1] == 'X' && tab[4] == 'X' && tab[7] == 'X')
	   || (tab[2] == 'X' && tab[5] == 'X' && tab[8] == 'X')
        || (tab[3] == 'X' && tab[5] == 'X' && tab[8] == 'X')
        || (tab[3] == 'X' && tab[4] == 'X' && tab[5] == 'X')
        || (tab[6] == 'X' && tab[7] == 'X' && tab[8] == 'X')
	   || (tab[0] == 'O' && tab[1] == 'O' && tab[2] == 'O')
        || (tab[0] == 'O' && tab[3] == 'O' && tab[6] == 'O')
        || (tab[1] == 'O' && tab[4] == 'O' && tab[7] == 'O')
        || (tab[3] == 'O' && tab[5] == 'O' && tab[8] == 'O')
        || (tab[3] == 'O' && tab[4] == 'O' && tab[5] == 'O')
        || (tab[6] == 'O' && tab[7] == 'O' && tab[8] == 'O')
	   || (tab[2] == 'O' && tab[5] == 'O' && tab[8] == 'O')) {return 1;}
    
    else { return 0;}
}
//*******************************************************************************
void end_game()
{
    cout << "\n\nGame is over, winner: ";
    string winner = which_move%2 ? "nought\n\n" : "cross\n\n";
    cout << winner;
}
 

Edited by dbordzo, 29 July 2013 - 02:36 AM.


#6 Scotty

Scotty

    CC Newcomer

  • Member
  • PipPip
  • 10 posts

Posted 29 July 2013 - 05:41 PM

Code is looking good. :)

 

I've managed to hunt around for a code convention pdf. It's based on java, though it will and should apply to C/C++ programming.

 

Hope you enjoy reading it, I did.

 

-Scott.C



#7 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts

Posted 11 August 2013 - 09:07 PM

Code is looking good. :)
 
I've managed to hunt around for a code convention pdf. It's based on java, though it will and should apply to C/C++ programming.
 
Hope you enjoy reading it, I did.
 
-Scott.C


Unfortunately I have to disagree here. There's still lots of improvements that could be made. If I don't forget, I'll post a few updates tomorrow after work...

Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download