C++ TIC TAC TOE game help please (OXO GAME)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Arnas Lasys
    New Member
    • Nov 2011
    • 1

    C++ TIC TAC TOE game help please (OXO GAME)

    Hello, so i made this tic tac toe game, its playable between 2 people. but i cant figure out how to make it between me and the computer... any idea how to do that with the use of % operator and srand and rand.... with the ctime library?

    THIS IS how far i got, and it doesnt do anything new...

    /Include the libraries
    #include <iostream>
    #include <string>
    #include <ctime>
    //Using the standard namespace
    using namespace std;

    // Declare global variables
    char Board[9];

    // Declare functions
    void showBoard ( );
    bool moveIsValid (int m);
    int whoWon ( ); // Returns 0 if no one has won, 1 of player 1 has won, and 2 if player 2 has won

    void main ( )
    {
    srand (time (NULL) );

    //Declare local variable
    string Player_1_Name;
    int Whose_Turn = 1; // 1 means it's player 1's turn, 2 means it's player 2's turn
    int Move; // Stores where the player wants to move
    int Total_Moves = 1;
    int Computer = rand ( ) % 8 + 1;

    //Assing values to the playing board
    Board[0] = '0';
    Board[1] = '1';
    Board[2] = '2';
    Board[3] = '3';
    Board[4] = '4';
    Board[5] = '5';
    Board[6] = '6';
    Board[7] = '7';
    Board[8] = '8';

    // Get player names
    cout <<"Player 1: Please enter your name." << endl;
    cin >> Player_1_Name;



    while (whoWon ( ) == 0 && Total_Moves <9)
    {

    // Do this until the player chooses a valid move
    do
    {

    // Show the board
    showBoard ( );

    // Tell which player to move
    if (Whose_Turn == 1)
    {
    cout << Player_1_Name << ": It's your turn." << endl;
    }
    else
    {
    cout << Computer << ": It's the computer's turn." << endl;
    }
    // Get the move
    cout << " Enter the number of the spot where you'd like to move." << endl;
    cin >> Move;
    } while (moveIsValid (Move) != true);

    //Add 1 to Total_Moves
    Total_Moves++;

    // Change whose turn it is
    switch (Whose_Turn)
    {
    case (1):
    {
    Board[Move] = 'x';
    Whose_Turn = 2;
    break;
    }
    case (2):
    {
    Board[Move] = 'o';
    Whose_Turn = 1;
    }
    }
    }
    // Show the board
    showBoard ( );

    if (whoWon ( ) == 1)
    {
    cout << Player_1_Name << " has won the game!" << endl;
    }
    else if (whoWon ( ) == 2)
    {
    cout << Computer << "has won the game!" << endl;
    }
    else
    {
    cout << " It's a tie game!" << endl;
    }

    system ("PAUSE");
    }
    void showBoard ( )
    {
    cout << endl;
    cout << Board[0] << " | " << Board[1] << " | " << Board[2] << endl;
    cout <<"--+---+--" << endl;
    cout << Board[3] << " | " << Board[4] << " | " << Board[5] << endl;
    cout <<"--+---+--" << endl;
    cout << Board[6] << " | " << Board[7] << " | " << Board[8] << endl;
    cout << endl;

    }

    bool moveIsValid (int m )
    {
    if(Board[m] !='x' && Board[m] != 'o')
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    int whoWon ( )
    {
    if (Board[0] == Board[1] && Board[1] == Board[2])
    {
    if(Board[0] == 'x')
    {
    return 1;
    }
    else
    {
    return 2;
    }

    }
    if (Board[3] == Board[4]&& Board[4] == Board[5])
    {
    if(Board[3] == 'x')
    {
    return 1;
    }
    else
    {
    return 2;
    }

    }
    if (Board[6] == Board[7] && Board[7] == Board[8])
    {
    if(Board[6] == 'x')
    {
    return 1;
    }
    else
    {
    return 2;
    }

    }
    if (Board[0] == Board[3] && Board[3] == Board[6])
    {
    if(Board[0] == 'x')
    {
    return 1;
    }
    else
    {
    return 2;
    }

    }
    if (Board[1] == Board[4] && Board[4] == Board[7])
    {
    if(Board[1] == 'x')
    {
    return 1;
    }
    else
    {
    return 2;
    }

    }
    if (Board[2] == Board[5] && Board[5] == Board[8])
    {
    if(Board[2] == 'x')
    {
    return 1;
    }
    else
    {
    return 2;
    }

    }
    if (Board[0] == Board[4] && Board[4] == Board[8])
    {
    if(Board[0] == 'x')
    {
    return 1;
    }
    else
    {
    return 2;
    }

    }
    if (Board[2] == Board[4] && Board[4] == Board[6])
    {
    if(Board[2] == 'x')
    {
    return 1;
    }
    else
    {
    return 2;
    }

    }
    return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Go back to your game using 2 people that works.

    Just replace the 2nd player keyboard inout with a call to a function that you will write that makes the move.

    I would remove the code for th 2nd player and put it inside this function that will now be called from main(). Build the game and verify that i still works. You are now the computer.

    Now replace the contents of the function with code to produce a real computer move. As a first cut have the comuter move to any square that is available. Build and verify the game still works.

    Now augment the move so that the computer blocks where the other user has two squares. Buid and verify the game still works.

    Note that by doing this incrementally and testing each time, your game becomes increasingly effective and at each step it always works. This is a lot diffrent from writing a ton of code and then doing a giant debug only to find out most of your code needs to be rewritten.

    Comment

    Working...