Thread: Tic Tac Toe -- Can you guys rate this please?

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    Post Tic Tac Toe -- Can you guys rate this please?

    Hey all, can you guys give me some input on how effecient this code is? It seems rather effecient to me, the main game loop only calls three functions, and those functions are re-used for each player as well as very small within themselves. I also believe there are no unneccesary variables to prevent effeciency.

    Can you guys give some input please?

    Thanks

    // Tac Tac Toe
    #include <iostream.h>

    // Functions
    void DrawBoard( void );
    void PlayerTakeTurn( void );
    void CheckWinner( void );

    // Variables
    int Board[3][3]; // Game Board
    int Row, Column; // Input Controls
    int TurnAmount; // Amount of Turns Taken, used for draws.
    int PlayerTurn; // Who's turn is it?
    bool ValidTurn; // Valid Turn?
    int winner; // What player won?
    int x,y; // Misc.

    // Main Function - Defines and Main Loop
    int main()
    {
    // Make all objects in the grid = 0;
    for(x = 0; x < 3; x++)
    {
    for(y = 0; y < 3; y++)
    {
    Board[x][y] = 0;
    }
    }

    winner = 0; // No winne yet
    PlayerTurn = 1; // Player 1 starts
    TurnAmount = 0; // Max 9 turns before cats game

    // Main Loop
    while(winner == 0)
    {
    ValidTurn = false; // Reset turn validity each turn
    TurnAmount++; // Another turn closer to cats game
    DrawBoard(); // Draw the game board
    PlayerTakeTurn(); // Takes the players turn
    if (TurnAmount > 8) // Cats game, end the game now.
    break;
    }

    // Who won?
    switch (winner)
    {
    case 1:
    cout << "Player 1 emerges victorious!" << endl;
    break;
    case 2:
    cout << "Player 2 emerges victorious!" << endl;
    break;
    default:
    cout << "Draw! No one wins! You both suck." << endl;
    break;

    // Quit
    return 0;
    }

    // Draws the playing grid, complete with X's and O's
    void DrawBoard( void )
    {
    for(x = 0; x < 3; x++)
    {
    for(y = 0; y < 3; y++)
    {
    if (y > 0)
    cout << "|";
    switch(Board[x][y])
    {
    case 1:
    cout << "X";
    break;
    case 2:
    cout << "O";
    break;
    default:
    cout << " ";
    break;
    }
    }
    cout << endl;
    if (x < 2)
    cout << "-----" << endl;
    }
    return;
    }

    // This is where the player takes their turn.
    void PlayerTakeTurn( void )
    {
    // Repeat until a turn is valid.
    while(ValidTurn == false)
    {
    cout << "Player " << PlayerTurn <<" your turn" << endl;
    cout << "Row: ";
    cin >> Row;
    Row--;
    cout << "Column: ";
    cin >> Column;
    Column--;
    if (Board[Row][Column] == 0)
    {
    // Checks for valid turn
    Board[Row][Column] = PlayerTurn;
    ValidTurn = true;
    CheckWinner();
    if (PlayerTurn == 1)
    {
    PlayerTurn = 2;
    }
    else
    {
    PlayerTurn = 1;
    }
    }
    else
    {
    cout << "Invalid Turn, try again." << endl;
    ValidTurn = false;
    }
    }
    return;
    }

    // Checks for a winner of the game.
    void CheckWinner( void )
    {
    y = 0;
    x = 0;
    // Is this the best way to do this? I can't think of a better one.
    // Top Row
    if (Board[0][0] == PlayerTurn && Board[0][1] == PlayerTurn && Board[0][2] == PlayerTurn)
    winner = PlayerTurn;
    // Middle Row
    else if (Board[1][0] == PlayerTurn && Board[1][1] == PlayerTurn && Board[1][2] == PlayerTurn)
    winner = PlayerTurn;
    // Bottom Row
    else if (Board[2][0] == PlayerTurn && Board[2][1] == PlayerTurn && Board[2][2] == PlayerTurn)
    winner = PlayerTurn;
    // Left Column
    else if (Board[0][0] == PlayerTurn && Board[1][0] == PlayerTurn && Board[2][0] == PlayerTurn)
    winner = PlayerTurn;
    // Middle Column
    else if (Board[0][1] == PlayerTurn && Board[1][1] == PlayerTurn && Board[2][1] == PlayerTurn)
    winner = PlayerTurn;
    // Right Column
    else if (Board[0][2] == PlayerTurn && Board[1][2] == PlayerTurn && Board[2][2] == PlayerTurn)
    winner = PlayerTurn;
    // Diagonal, Top Left - Bottom Right
    else if (Board[0][0] == PlayerTurn && Board[1][1] == PlayerTurn && Board[2][2] == PlayerTurn)
    winner = PlayerTurn;
    // Diagonal, Top Right - Bottom Left
    else if (Board[0][2] == PlayerTurn && Board[1][1] == PlayerTurn && Board[2][0] == PlayerTurn)
    winner = PlayerTurn;
    return;
    }
    "Where genius ends, madness begins."
    -Estauns

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Why don't you upload he code to the msg board...this is much better than just dumping it in a thread (just a sugestion)

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    I'm in a good mood

    In a good mood so here's the code...Works pretty good, had to make a couple of compiler specific changes but didn't really change anything else...(except i added one '}' but i marked where...
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Tic Tac Toe... so close...
    By SlayerBlade in forum C Programming
    Replies: 14
    Last Post: 10-10-2005, 08:58 PM
  4. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  5. my tic tac toe game, please try it
    By Leeman_s in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 05:16 PM