User Tag List

Results 1 to 3 of 3

Thread: Tic Tac Toe v2.0 (C++)

  1. #1
    Tic Tac Toe v2.0 (C++)

    User Info Menu

    Tic Tac Toe v2.0 (C++)

    Program:

    Code:
    http://www.sendspace.com/file/lhrfy6
    Source Code: (Kinda sloppy)

    Code:
    #include <iostream>
    #include <windows.h>
    #include <time.h>
    #include <string>
    
    int main(void)
    {
        using namespace std;
        int selection1 = 0, playerTurn = 1, move = 0, winner = 0;
        bool gameOver = false;
        char s[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        srand(time(NULL));
        system("title TheEliteOne's Tic Tac Toe Game v2");
        system("cls");
        cout << "Select:" << endl << endl;
        cout << "1 - Player vs Computer" << endl;
        cout << "2 - Player vs Player" << endl;
        cout << "3 - Report a bug" << endl << endl;
        cin >> selection1;
        system("cls");
        if (selection1 == 1)
        {
    		int computer;
    		do
    		{
                // Show play mat
                system("cls");
                cout << "| " << s[1] << " | " << s[2] << " | " << s[3] << " |" << endl << "-------------" << endl;
                cout << "| " << s[4] << " | " << s[5] << " | " << s[6] << " |" << endl << "-------------" << endl;
                cout << "| " << s[7] << " | " << s[8] << " | " << s[9] << " |" << endl << "-------------" << endl << endl;
    
                // Get move
                if (playerTurn == 1)
                {
    				cout << "Player 1's turn: ";
    				cin >> move;
                    playerTurn++;
                    if (move == 1)
                    {
                        if (s[1] == '1')
                        {
                            s[1] = 'x';
                        }
                        else if (s[1] == 'x' || s[1] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 2)
                    {
                        if (s[2] == '2')
                        {
                            s[2] = 'x';
                        }
                        else if (s[2] == 'x' || s[2] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 3)
                    {
                        if (s[3] == '3')
                        {
                            s[3] = 'x';
                        }
                        else if (s[3] == 'x' || s[3] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 4)
                    {
                        if (s[4] == '4')
                        {
                            s[4] = 'x';
                        }
                        else if (s[4] == 'x' || s[4] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 5)
                    {
                        if (s[5] == '5')
                        {
                            s[5] = 'x';
                        }
                        else if (s[5] == 'x' || s[5] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 6)
                    {
                        if (s[6] == '6')
                        {
                            s[6] = 'x';
                        }
                        else if (s[6] == 'x' || s[6] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 7)
                    {
                        if (s[7] == '7')
                        {
                            s[7] = 'x';
                        }
                        else if (s[7] == 'x' || s[7] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 8)
                    {
                        if (s[8] == '8')
                        {
                            s[8] = 'x';
                        }
                        else if (s[8] == 'x' || s[8] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 9)
                    {
                        if (s[9] == '9')
                        {
                            s[9] = 'x';
                        }
                        else if (s[9] == 'x' || s[9] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                }
                else if (playerTurn == 2)
                {
    				computer = rand() % 9 + 1;
    				cout << "Computer's turn: " << computer;
    				playerTurn = 1;
                    if (computer == 1)
                    {
                        if (s[1] == '1')
                        {
                            s[1] = 'o';
                        }
                        else if (s[1] == 'x' || s[1] == 'o')
                        {
                            playerTurn = 1;
                        }
                    }
                    else if (computer == 2)
                    {
                        if (s[2] == '2')
                        {
                            s[2] = 'o';
                        }
                        else if (s[2] == 'x' || s[2] == 'o')
                        {
                            playerTurn = 1;
                        }
                    }
                    else if (computer == 3)
                    {
                        if (s[3] == '3')
                        {
                            s[3] = 'o';
                        }
                        else if (s[3] == 'x' || s[3] == 'o')
                        {
                            playerTurn = 1;
                        }
                    }
                    else if (computer == 4)
                    {
                        if (s[4] == '4')
                        {
                            s[4] = 'o';
                        }
                        else if (s[4] == 'x' || s[4] == 'o')
                        {
                            playerTurn = 1;
                        }
                    }
                    else if (computer == 5)
                    {
                        if (s[5] == '5')
                        {
                            s[5] = 'o';
                        }
                        else if (s[5] == 'x' || s[5] == 'o')
                        {
                            playerTurn = 1;
                        }
                    }
                    else if (computer == 6)
                    {
                        if (s[6] == '6')
                        {
                            s[6] = 'o';
                        }
                        else if (s[6] == 'x' || s[6] == 'o')
                        {
                            playerTurn = 1;
                        }
                    }
                    else if (computer == 7)
                    {
                        if (s[7] == '7')
                        {
                            s[7] = 'o';
                        }
                        else if (s[7] == 'x' || s[7] == 'o')
                        {
                            playerTurn = 1;
                        }
                    }
                    else if (computer == 8)
                    {
                        if (s[8] == '8')
                        {
                            s[8] = 'o';
                        }
                        else if (s[8] == 'x' || s[8] == 'o')
                        {
                            playerTurn = 1;
                        }
                    }
                    else if (computer == 9)
                    {
                        if (s[9] == '9')
                        {
                            s[9] = 'o';
                        }
                        else if (s[9] == 'x' || s[9] == 'o')
                        {
                            playerTurn = 1;
                        }
                    }
    				else
    				{
    					playerTurn = 1;
    				}
                }
    
    			// Check if someone has won
    			// Player one check
    			if (s[1] == 'x' && s[2] == 'x' && s[3] == 'x')
    			{
    				gameOver = true;
    				winner = 1;
    			}
    			if (s[1] == 'x' && s[5] == 'x' && s[9] == 'x')
    			{
    			    gameOver = true;
    			    winner = 1;
    			}
    			if (s[1] == 'x' && s[4] == 'x' && s[7] == 'x')
    			{
    			    gameOver = true;
    			    winner = 1;
    			}
    			if (s[2] == 'x' && s[5] == 'x' && s[8] == 'x')
    			{
    			    gameOver = true;
    			    winner = 1;
    			}
    			if (s[3] == 'x' && s[5] == 'x' && s[7] == 'x')
    			{
    			    gameOver = true;
    			    winner = 1;
    			}
    			if (s[3] == 'x' && s[6] == 'x' && s[9] == 'x')
    			{
    			    gameOver = true;
    			    winner = 1;
    			}
    			if (s[4] == 'x' && s[5] == 'x' && s[6] == 'x')
    			{
    			    gameOver = true;
    			    winner = 1;
    			}
    			if (s[7] == 'x' && s[8] == 'x' && s[9] == 'x')
    			{
    			    gameOver = true;
    			    winner = 1;
    			}
    			// Player two check
    			if (s[1] == 'o' && s[2] == 'o' && s[3] == 'o')
    			{
    				gameOver = true;
    				winner = 2;
    			}
    			if (s[1] == 'o' && s[5] == 'o' && s[9] == 'o')
    			{
    			    gameOver = true;
    			    winner = 2;
    			}
    			if (s[1] == 'o' && s[4] == 'o' && s[7] == 'o')
    			{
    			    gameOver = true;
    			    winner = 2;
    			}
    			if (s[2] == 'o' && s[5] == 'o' && s[8] == 'o')
    			{
    			    gameOver = true;
    			    winner = 2;
    			}
    			if (s[3] == 'o' && s[5] == 'o' && s[7] == 'o')
    			{
    			    gameOver = true;
    			    winner = 2;
    			}
    			if (s[3] == 'o' && s[6] == 'o' && s[9] == 'o')
    			{
    			    gameOver = true;
    			    winner = 2;
    			}
    			if (s[4] == 'o' && s[5] == 'o' && s[6] == 'o')
    			{
    			    gameOver = true;
    			    winner = 2;
    			}
    			if (s[7] == 'o' && s[8] == 'o' && s[9] == 'o')
    			{
    			    gameOver = true;
    			    winner = 2;
    			}
    			// Tie Check
    			if (s[1] != '1' && s[2] != '2' && s[3] != '3' && s[4] != '4' && s[5] != '5' && s[6] != '6' && s[7] != '7' && s[8] != '8' && s[9] != '9')
    			{
    			    gameOver = true;
    			}
    		} while (gameOver == false);
    
    		// Display winner
    		system("cls");
    		cout << "Game Over! ";
    		if (winner == 0)
    		{
    			cout << "The game is a tie!";
    		}
    		else if (winner == 1)
    		{
    			cout << "Winner is player 1!";
    		}
    		else if (winner == 2)
    		{
    			cout << "Winner is the computer!";
    		}
    		cout << endl;
    		system("pause");
    		return main();
        }
        else if (selection1 == 2)
        {
    		// Player vs Player
            do
            {
                // Show play mat
                system("cls");
                cout << "| " << s[1] << " | " << s[2] << " | " << s[3] << " |" << endl << "-------------" << endl;
                cout << "| " << s[4] << " | " << s[5] << " | " << s[6] << " |" << endl << "-------------" << endl;
                cout << "| " << s[7] << " | " << s[8] << " | " << s[9] << " |" << endl << "-------------" << endl << endl;
    
                // Get move
                cout << "Player " << playerTurn << "'s turn: ";
                cin >> move;
                if (playerTurn == 1)
                {
                    playerTurn++;
                    if (move == 1)
                    {
                        if (s[1] == '1')
                        {
                            s[1] = 'x';
                        }
                        else if (s[1] == 'x' || s[1] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 2)
                    {
                        if (s[2] == '2')
                        {
                            s[2] = 'x';
                        }
                        else if (s[2] == 'x' || s[2] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 3)
                    {
                        if (s[3] == '3')
                        {
                            s[3] = 'x';
                        }
                        else if (s[3] == 'x' || s[3] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 4)
                    {
                        if (s[4] == '4')
                        {
                            s[4] = 'x';
                        }
                        else if (s[4] == 'x' || s[4] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 5)
                    {
                        if (s[5] == '5')
                        {
                            s[5] = 'x';
                        }
                        else if (s[5] == 'x' || s[5] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 6)
                    {
                        if (s[6] == '6')
                        {
                            s[6] = 'x';
                        }
                        else if (s[6] == 'x' || s[6] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 7)
                    {
                        if (s[7] == '7')
                        {
                            s[7] = 'x';
                        }
                        else if (s[7] == 'x' || s[7] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 8)
                    {
                        if (s[8] == '8')
                        {
                            s[8] = 'x';
                        }
                        else if (s[8] == 'x' || s[8] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                    else if (move == 9)
                    {
                        if (s[9] == '9')
                        {
                            s[9] = 'x';
                        }
                        else if (s[9] == 'x' || s[9] == 'o')
                        {
                            playerTurn--;
                        }
                    }
                }
                else if (playerTurn == 2)
                {
                    playerTurn = 1;
                    if (move == 1)
                    {
                        if (s[1] == '1')
                        {
                            s[1] = 'o';
                        }
                        else if (s[1] == 'x' || s[1] == 'o')
                        {
                            playerTurn++;
                        }
                    }
                    else if (move == 2)
                    {
                        if (s[2] == '2')
                        {
                            s[2] = 'o';
                        }
                        else if (s[2] == 'x' || s[2] == 'o')
                        {
                            playerTurn++;
                        }
                    }
                    else if (move == 3)
                    {
                        if (s[3] == '3')
                        {
                            s[3] = 'o';
                        }
                        else if (s[3] == 'x' || s[3] == 'o')
                        {
                            playerTurn++;
                        }
                    }
                    else if (move == 4)
                    {
                        if (s[4] == '4')
                        {
                            s[4] = 'o';
                        }
                        else if (s[4] == 'x' || s[4] == 'o')
                        {
                            playerTurn++;
                        }
                    }
                    else if (move == 5)
                    {
                        if (s[5] == '5')
                        {
                            s[5] = 'o';
                        }
                        else if (s[5] == 'x' || s[5] == 'o')
                        {
                            playerTurn++;
                        }
                    }
                    else if (move == 6)
                    {
                        if (s[6] == '6')
                        {
                            s[6] = 'o';
                        }
                        else if (s[6] == 'x' || s[6] == 'o')
                        {
                            playerTurn++;
                        }
                    }
                    else if (move == 7)
                    {
                        if (s[7] == '7')
                        {
                            s[7] = 'o';
                        }
                        else if (s[7] == 'x' || s[7] == 'o')
                        {
                            playerTurn++;
                        }
                    }
                    else if (move == 8)
                    {
                        if (s[8] == '8')
                        {
                            s[8] = 'o';
                        }
                        else if (s[8] == 'x' || s[8] == 'o')
                        {
                            playerTurn++;
                        }
                    }
                    else if (move == 9)
                    {
                        if (s[9] == '9')
                        {
                            s[9] = 'o';
                        }
                        else if (s[9] == 'x' || s[9] == 'o')
                        {
                            playerTurn++;
                        }
                    }
                }
    
    			// Check if someone has won
    			// Player one check
    			if (s[1] == 'x' && s[2] == 'x' && s[3] == 'x')
    			{
    				gameOver = true;
    				winner = 1;
    			}
    			if (s[1] == 'x' && s[5] == 'x' && s[9] == 'x')
    			{
    			    gameOver = true;
    			    winner = 1;
    			}
    			if (s[1] == 'x' && s[4] == 'x' && s[7] == 'x')
    			{
    			    gameOver = true;
    			    winner = 1;
    			}
    			if (s[2] == 'x' && s[5] == 'x' && s[8] == 'x')
    			{
    			    gameOver = true;
    			    winner = 1;
    			}
    			if (s[3] == 'x' && s[5] == 'x' && s[7] == 'x')
    			{
    			    gameOver = true;
    			    winner = 1;
    			}
    			if (s[3] == 'x' && s[6] == 'x' && s[9] == 'x')
    			{
    			    gameOver = true;
    			    winner = 1;
    			}
    			if (s[4] == 'x' && s[5] == 'x' && s[6] == 'x')
    			{
    			    gameOver = true;
    			    winner = 1;
    			}
    			if (s[7] == 'x' && s[8] == 'x' && s[9] == 'x')
    			{
    			    gameOver = true;
    			    winner = 1;
    			}
    			// Player two check
    			if (s[1] == 'o' && s[2] == 'o' && s[3] == 'o')
    			{
    				gameOver = true;
    				winner = 2;
    			}
    			if (s[1] == 'o' && s[5] == 'o' && s[9] == 'o')
    			{
    			    gameOver = true;
    			    winner = 2;
    			}
    			if (s[1] == 'o' && s[4] == 'o' && s[7] == 'o')
    			{
    			    gameOver = true;
    			    winner = 2;
    			}
    			if (s[2] == 'o' && s[5] == 'o' && s[8] == 'o')
    			{
    			    gameOver = true;
    			    winner = 2;
    			}
    			if (s[3] == 'o' && s[5] == 'o' && s[7] == 'o')
    			{
    			    gameOver = true;
    			    winner = 2;
    			}
    			if (s[3] == 'o' && s[6] == 'o' && s[9] == 'o')
    			{
    			    gameOver = true;
    			    winner = 2;
    			}
    			if (s[4] == 'o' && s[5] == 'o' && s[6] == 'o')
    			{
    			    gameOver = true;
    			    winner = 2;
    			}
    			if (s[7] == 'o' && s[8] == 'o' && s[9] == 'o')
    			{
    			    gameOver = true;
    			    winner = 2;
    			}
    			// Tie Check
    			if (s[1] != '1' && s[2] != '2' && s[3] != '3' && s[4] != '4' && s[5] != '5' && s[6] != '6' && s[7] != '7' && s[8] != '8' && s[9] != '9')
    			{
    			    gameOver = true;
    			}
            } while (gameOver == false);
    
    		// Display winner
    		system("cls");
    		cout << "Game Over! ";
    		if (winner == 0)
    		{
    			cout << "The game is a tie!";
    		}
    		else if (winner == 1)
    		{
    			cout << "Winner is player 1!";
    		}
    		else if (winner == 2)
    		{
    			cout << "Winner is player 2!";
    		}
    		cout << endl;
    		system("pause");
    		return main();
        }
        else if (selection1 == 3)
        {
    		// Report bug
            ShellExecute(NULL, "open", "http://onehitgamer.com/forum/private.php?do=newpm&u=89470", NULL, NULL, SW_SHOWNORMAL);
            return main();
        }
        else
        {
    		// If user entered a invalid number
            cout << "Invalid Selection" << endl;
            system("pause");
            return main();
        }
    }
    People where saying that they wanted to play the computer, well the computer's move is random, so it is very easy, but you can still play it xD

    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: Tic Tac Toe v2.0 (C++)

    Nice job. Nice update to the original. Props.

  3. #3
    Tic Tac Toe v2.0 (C++)

    User Info Menu

    Re: Tic Tac Toe v2.0 (C++)

    Quote Originally Posted by j2eco7V View Post
    Nice job. Nice update to the original. Props.
    Thanks :)

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

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
  •