Tic Tac Toe!

This is a discussion on Tic Tac Toe! within the C++ Programming forums, part of the General Programming Boards category; Alright, so I went to my school's CS tutoring center and that really helped me out. Now I'm not sure ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    22

    Tic Tac Toe!

    Alright, so I went to my school's CS tutoring center and that really helped me out. Now I'm not sure if this program is set up correctly and what not, but I think I got the basic idea. This is a tic tac toe game for two players (no AI). It won't compile and I'm wondering what I should do from here.

    This is the link to the assignment, with an example of what it should look like when it is complete:

    http://www.cs.uwm.edu/classes/cs201/.../06/06/06.html


    Here are the errors I recieve:

    Code:
     prog12.cc: In function `int main()':
    prog12.cc:8: error: too few arguments to function `bool checkWin(char*, char)'
    prog12.cc:38: error: at this point in file
    prog12.cc: In function `bool playGame()':
    prog12.cc:68: error: `selection' undeclared (first use this function)
    prog12.cc:68: error: (Each undeclared identifier is reported only once for each function it appears in.)
    Whenever I try to add "int selection;" into the "bool playGame" function, the compiler freaks out and gives me a bunch of random errors.

    Here is the code I've wrote so far:

    Code:
    See new code below!
    Thanks in advance!
    Last edited by the_lumin8or; 04-03-2006 at 08:39 PM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    Do not do this:

    cin >> selection >> endl;
    Remove the endl the rest of the bugs should be easy enough to fix after that...

    Hint line 38 is missing something. Look closely!

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    22
    Thanks 0x90! That really helped! So the program compiles, but I am getting a bunch of garbage when I run the program....I'm really stuck here.

    Here is some of the results I'm getting when I run it...
    Code:
    grid3.cs: a.out
    
    Welcome to Tic-Tac-Toe
    Three in a row wins!
    
    1). Play a game
    2). Quit
    
    Please make a selection: 1
      |   |
    ---------
      |   |
    ---------
      |   |
    Enter location (1-9) = 9
    Enter location (1-9) = 4
    x Wins!
    xCat's game!
    
    Welcome to Tic-Tac-Toe
    Three in a row wins!
    
    1). Play a game
    2). Quit
    
    Please make a selection: 5
      |   |
    ---------
      |   |
    ---------
      |   |
    Enter location (1-9) = clear
    Enter location (1-9) = x Wins!
    xCat's game!
    
    Welcome to Tic-Tac-Toe
    Three in a row wins!
    
    1). Play a game
    2). Quit
    
    Please make a selection:   |   |
    ---------
      |   |
    ---------
      |   |
    Segmentation Fault (core dumped)
    Heh, something is obviously wrong in my code...I just don't know what it is. If you guys have any suggestions, please let me know!!

    Thanks!


    Here's my new code:
    Code:
    //Tic Tac Toe
    //Written by Jerad 
    
    //Function prototypes.
    
    void drawBoard(char board[]);
    int getMove(char board[], char turn);
    bool checkWin(char board[], char turn);
    bool playGame(void);
    
    #include<iostream>
    using namespace std;
    
    //Main function.
    
    int main()
    {
    char turn;
    int numTurns;
    int move;
    int x_wins;
    int o_wins;
    int no_wins;
    
    //Main loop, calls the menu function.
    while(playGame())
    {
      //Initialze board.
      char board[] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
      bool win = false;
      turn = 'x';
    
      for (numTurns = 1; numTurns < 10; numTurns++)
      {
        drawBoard(board); //Display board.
        move = getMove(board, turn);
        board[move] = turn;
        win = checkWin(board, turn); //Check for win.
        if (win) break;
      }
    
      if (turn == 'x'){
        x_wins++;
        cout << turn << " Wins!" << endl;
        }
      else if (turn == 'o'){
        o_wins++;
        cout << turn << " Wins!" << endl;
        }
      else
        no_wins = 0;
        cout << turn << "Cat's game!" << endl;
      }
    
      cout << "X wins: " << x_wins << endl;
      cout << "O wins: " << o_wins << endl;
      cout << "Non-wins: " << no_wins << endl;
    }
    
    //Menu Function.
    bool playGame(void)
    {
      int selection;
      cout << endl;
      cout << "Welcome to Tic-Tac-Toe" << endl;
      cout << "Three in a row wins!" << endl;
      cout << endl;
      cout << "1). Play a game" << endl;
      cout << "2). Quit" << endl;
      cout << endl;
      cout << "Please make a selection: ";
      cin >> selection;
      if (selection == 1);
       return true;
    }
    
    
    //Display Board Function.
    void drawBoard(char board[])
    {
      cout << board[1] << " | " << board[2] << " | " << board[3] << endl;
      cout << "---------" << endl;
      cout << board[4] << " | " << board[5] << " | " << board[6] << endl;
      cout << "---------" << endl;
      cout << board[7] << " | " << board[8] << " | " << board[9] << endl;
    }
    
    //Get position from player.
    int getMove(char board[], char turn)
    {
      int move;
        cout << "Enter location (1-9) = ";
        cin >> move;
      do{
        cout << "Enter location (1-9) = ";
        cin >> move;
        }
        while ((move < 1) || (move > 9) && (board[move] != ' '));
      do{
        return move;
        }
        while ((move > 1) || (move < 9) && (board[move] == ' '));
    }
    
    //Check for a win.
    bool checkWin(char board[], char turn)
    {
      if (((board[1] == board[2]) && (board[2] == board[3])) && (board[3] == turn));
      else if (((board[4] == board[5]) && (board[5] == board[6])) && (board[6] == turn));
      else if (((board[7] == board[8]) && (board[8] == board[9])) && (board[9] == turn));
      else if (((board[1] == board[5]) && (board[5] == board[9])) && (board[9] == turn));
      else if (((board[3] == board[5]) && (board[5] == board[7])) && (board[7] == turn));
        return true;
    }
    Last edited by the_lumin8or; 04-04-2006 at 11:08 AM.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Let's look at your input problem:
    Code:
      int move;
        cout << "Enter location (1-9) = ";
        cin >> move;
    You enter clear. What int value did you expect to read?
    cin does not process invalid values well so you either have to not enter bad values (your program will croak) or make the input safe -- which is a lot harder. If you really want to do this, look into getline() which reads whatever was entered as a character string, which you then check for a valid value then convert to your int.

    For anything else wrong it's best to describe exactly what the code did wrong, what the code should have done, and where you think the problem is. That's better than letting us try to analyze your output and try to figure out what you already know.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    22
    Alright guys, I have a few questions about this chunk of code.

    1) When I try to increment the "x" or "o" variable and display the winner at the end of each game, it displays "Stalemate" regardless of who wins. There must be something wrong with how that loop is set up, but I'm not quite sure...

    2) I want to display the last lines of code in this function (how many times each person won 'x_wins' and 'o_wins') when the user quits the program(when 'playGame()' is false). Any suggestions?

    Code:
    //Main function.
    
    int main()
    {
    char turn;
    int numTurns;
    int move;
    int x_wins;
    int o_wins;
    int no_wins;
    
    
    while(playGame())
    {
      //Initialze board.
      char board[] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
      bool win = false;
      turn = 'x';
    
      for (numTurns = 1; numTurns < 10; numTurns++)
      {
        move = getMove(board, turn);
        board[move] = turn;
        drawBoard(board);
        win = checkWin(board, turn); //Check for win.
        if (win) break;
        if (turn=='x')
          turn='o';
        else
          turn='x';
        cout<< "-----------------------------"<<endl;
        cout<< "Thank you, it is now "<< turn << "'s turn."<<endl;
        cout<<endl;
      }
    
      if (turn == 'x'){
        x_wins++;
        cout << endl;
        cout << turn << " Wins!" << endl;
        }
      else if (turn == 'o'){
        o_wins++;
        cout << endl;
        cout << turn << " Wins!" << endl;
        }
      else
        no_wins = 0;
        cout << turn << "Stalemate!" << endl;
      }
    
      cout << "X wins: " << x_wins << endl;
      cout << "O wins: " << o_wins << endl;
      cout << "Non-wins: " << no_wins << endl;
     }
    3) Instead of only displaying "Enter a position(1-9):" over and over again when an invalid integer is entered, I want to display "Invalid move. Please try again". Where might I add this in?

    Code:
    int getMove(char board[], char turn)
    {
      int move;
      do{
        cout << "Enter a position(1-9) = ";
        cin >> move;
        }
      while ((move < 1) || (move > 9) || (board[move] != ' '));
      return move;
    }
    Thank you so much!!

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Code:
    while(!(cin >> a))
    {
        cin.clear();
        cin.ignore(1000, '\n');
        cout << "Wrong input";
    }
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    The code..... should be write with classes. It's more readable and efficient.

  8. #8
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Quote Originally Posted by Mathsniper
    The code..... should be write with classes. It's more readable and efficient.
    Don't you see he's a novice???
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Then it's a good time for him to learn

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. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  4. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 11:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21