User Tag List

Results 1 to 6 of 6

Thread: C++ Tic Tac Toe Game

  1. #1
    C++ Tic Tac Toe Game

    User Info Menu

    C++ Tic Tac Toe Game

    Game: http://www.sendspace.com/file/7w67du

    Source Code:

    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    int main(void)
    {
        system("title TheEliteOne's Tic Tac Toe Game v1");
        char s1 = '1', s2 = '2', s3 = '3', s4 = '4', s5 = '5', s6 = '6', s7 = '7', s8 = '8', s9 = '9', again;
        int one, game = 0, move = 1, p1, p2;
        do
        {
    
            /* Display the game board */
    
            system("CLS");
            cout << "| " << s1 << " | " << s2 << " |" << s3 << "\n-----------\n";
            cout << "| " << s4 << " | " << s5 << " |" << s6 << "\n-----------\n";
            cout << "| " << s7 << " | " << s8 << " |" << s9 << "\n-----------\n";
    
            if (move == 1) {cout << "Player one's move: ";}
            else if (move == 2) {cout << "Player two's move: ";}
            cin >> one;
    
            /*  Player 1 move */
    
            if (move == 1)
            {
                if (one == 1)
                {
                    if (s1 == '1')
                    {
                        s1 = 'x';
                    }
                }
                else if (one == 2)
                {
                    if (s2 == '2')
                    {
                        s2 = 'x';
                    }
                }
                else if (one == 3)
                {
                    if (s3 == '3')
                    {
                        s3 = 'x';
                    }
                }
                else if (one == 4)
                {
                    if (s4 == '4')
                    {
                        s4 = 'x';
                    }
                }
                else if (one == 5)
                {
                    if (s5 == '5')
                    {
                        s5 = 'x';
                    }
                }
                else if (one == 6)
                {
                    if (s6 == '6')
                    {
                        s6 = 'x';
                    }
                }
                else if (one == 7)
                {
                    if (s7 == '7')
                    {
                        s7 = 'x';
                    }
                }
                else if (one == 8)
                {
                    if (s8 == '8')
                    {
                        s8 = 'x';
                    }
                }
                else if (one == 9)
                {
                    if (s9 == '9')
                    {
                        s9 = 'x';
                    }
                }
                else
                {
                    cout << "\n\nInvalid Move";
                }
                move++;
            }
    
            /* Player 2 move */
    
            else if (move == 2)
            {
                if (one == 1)
                {
                    if (s1 == '1')
                    {
                        s1 = 'o';
                    }
                }
                else if (one == 2)
                {
                    if (s2 == '2')
                    {
                        s2 = 'o';
                    }
                }
                else if (one == 3)
                {
                    if (s3 == '3')
                    {
                        s3 = 'o';
                    }
                }
                else if (one == 4)
                {
                    if (s4 == '4')
                    {
                        s4 = 'o';
                    }
                }
                else if (one == 5)
                {
                    if (s5 == '5')
                    {
                        s5 = 'o';
                    }
                }
                else if (one == 6)
                {
                    if (s6 == '6')
                    {
                        s6 = 'o';
                    }
                }
                else if (one == 7)
                {
                    if (s7 == '7')
                    {
                        s7 = 'o';
                    }
                }
                else if (one == 8)
                {
                    if (s8 == '8')
                    {
                        s8 = 'o';
                    }
                }
                else if (one == 9)
                {
                    if (s9 == '9')
                    {
                        s9 = 'o';
                    }
                }
                else
                {
                    cout << "\n\nInvalid Move";
                }
                move = 1;
            }
    
    /* Check if player 1 has one the game */
    
      if (s1 == 'x' && s2 == 'x' && s3 == 'x')
      {
          game = 1;
          p1 = 1;
      }
      else if (s1 == 'x' && s4 == 'x' && s7 == 'x')
      {
          game = 1;
          p1 = 1;
      }
      else if (s1 == 'x' && s5 == 'x' && s9 == 'x')
      {
          game = 1;
          p1 = 1;
      }
      else if (s2 == 'x' && s5 == 'x' && s8 == 'x')
      {
          game = 1;
          p1 = 1;
      }
      else if (s3 == 'x' && s6 == 'x' && s9 == 'x')
      {
          game = 1;
          p1 = 1;
      }
      else if (s3 == 'x' && s5 == 'x' && s7 == 'x')
      {
          game = 1;
          p1 = 1;
      }
      else if (s4 == 'x' && s5 == 'x' && s6 == 'x')
      {
          game = 1;
          p1 = 1;
      }
      else if (s7 == 'x' && s8 == 'x' && s9 == 'x')
      {
          game = 1;
          p1 = 1;
      }
    
      /* Check if player 2 has one the game */
    
        if (s1 == 'o' && s2 == 'o' && s3 == 'o')
      {
          game = 1;
          p2 = 1;
      }
      else if (s1 == 'o' && s4 == 'o' && s7 == 'o')
      {
          game = 1;
          p2 = 1;
      }
      else if (s1 == 'o' && s5 == 'o' && s9 == 'o')
      {
          game = 1;
          p2 = 1;
      }
      else if (s2 == 'o' && s5 == 'o' && s8 == 'o')
      {
          game = 1;
          p2 = 1;
      }
      else if (s3 == 'o' && s6 == 'o' && s9 == 'o')
      {
          game = 1;
          p2 = 1;
      }
      else if (s3 == 'o' && s5 == 'o' && s7 == 'o')
      {
          game = 1;
          p2 = 1;
      }
      else if (s4 == 'o' && s5 == 'o' && s6 == 'o')
      {
          game = 1;
          p2 = 1;
      }
      else if (s7 == 'o' && s8 == 'o' && s9 == 'o')
      {
          game = 1;
          p2 = 1;
      }
    
      if (s1 != '1' && s2 != '2' && s3 != '3' && s4 != '4' && s5 != '5' && s6 != '6' && s7 != '7' && s8 != '8' && s9 != '9')
      {
          goto lol;
      }
    
        } while (game == 0);
    
        /* Display the wining player */
    
    system("CLS");
    cout << "| " << s1 << " | " << s2 << " |" << s3 << "\n-----------\n";
    cout << "| " << s4 << " | " << s5 << " |" << s6 << "\n-----------\n";
    cout << "| " << s7 << " | " << s8 << " |" << s9 << "\n-----------\n\n";
    
    
        if (p1 == 1)
        {
            cout << "Player 1 wins!\n\n";
        }
        else if (p2 == 1)
        {
            cout << "Player 2 wins!\n\n";
        }
    
        goto go;
    
        lol:
    
        system("CLS");
        cout << "| " << s1 << " | " << s2 << " |" << s3 << "\n-----------\n";
        cout << "| " << s4 << " | " << s5 << " |" << s6 << "\n-----------\n";
        cout << "| " << s7 << " | " << s8 << " |" << s9 << "\n-----------\n\n";
    
        cout << "The game is a tie!\n\n";
    
        go:
    
        system("PAUSE");
        system("CLS");
    
        /* Ask to play again */
    
        cout << "Play again? (y/n)\t";
        cin >> again;
        if (again == 'y' || again == 'Y')
        {
            p1 = 0, p2 = 0, game = 0;
            return main();
        }
    }

    If you have an idea on a PSP related program that is ran on Windows PM me with some information on your idea please.

  2. #2
    Life is never fair

    User Info Menu

    Re: C++ Tic Tac Toe Game

    It looks okay to me. I didn't test it out yet, but if I get around to it, I will. Good job though.

  3. #3
    C++ Tic Tac Toe Game

    User Info Menu

    Re: C++ Tic Tac Toe Game

    I really think that could be optimized. That's a freaking mess of if/then statements!

  4. #4
    Life is never fair

    User Info Menu

    Re: C++ Tic Tac Toe Game

    Yeah, I agree, but if I remember correctly, he's still learning all this stuff, so he really don't know how to change it and optimize it.

  5. #5
    C++ Tic Tac Toe Game

    User Info Menu

    Re: C++ Tic Tac Toe Game

    Quote Originally Posted by j2eco7V View Post
    It looks okay to me. I didn't test it out yet, but if I get around to it, I will. Good job though.
    Thanks.

    Quote Originally Posted by B1G_BR0TH3R View Post
    I really think that could be optimized. That's a freaking mess of if/then statements!
    Quote Originally Posted by j2eco7V View Post
    Yeah, I agree, but if I remember correctly, he's still learning all this stuff, so he really don't know how to change it and optimize it.
    =/ I could of made it better, but it is just a simple game so didn't go "all out" with it.

    If you have an idea on a PSP related program that is ran on Windows PM me with some information on your idea please.

  6. #6
    Life is never fair

    User Info Menu

    Re: C++ Tic Tac Toe Game

    You'll learn. It is just a simple game, I understand. Nothing wrong with the way you did it.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •