tic-tac-toe


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: tic-tac-toe

  1. #1
    Join Date
    Jul 2007
    Posts
    2

    tic-tac-toe

    Any1 can help out on this? i cant determine winner when there 3 x or 3 0 meet up..






    /*
    Tic-Tac-Toe
    This program will allow 2 players to play a game of tic-tac-toe.
    Input: Interactive keyboard input from the players.
    Output: The tic-tac-toe board
    */
    #include <iostream>
    #include <iomanip>
    using namespace std;
    const int SIZE = 3; //size of tic-tac-toe board array
    const int SIZE2 = 2; //size of tic-tac-toe piece array
    void display_board(const char[][SIZE]); //displays board
    void get_location(int&, int&,const int, const char[]);
    //gets location from player
    bool check_valid(const char[][SIZE], int, int);
    //checks for valid location
    bool check_win(const char[][SIZE],const int, const char[]);
    //checks if players wins
    bool check_row(const char[][SIZE],const int, const char[]);
    //checks for win on each row
    bool check_column(const char[][SIZE],const int, const char[]);
    //checks for win on each column
    void check_diagonal();
    //checks for on both diaganols
    int main()
    {
    char board[SIZE][SIZE] = {'*','*','*','*','*','*','*','*','*'},//tic-tac-toe board
    mark[SIZE2] = {'X', 'O'}; //both tic-tac-toe pieces
    int count = 0, //move counter
    r, //row
    c, //column
    p; //player
    bool valid, //valid move variable
    winner = false; //winner variable

    while(!winner && count < 9)
    {
    for(p = 1; p < SIZE; p++)
    {
    if(count < 9)
    {
    count++;
    display_board(board);
    get_location(r, c, p, mark);
    valid = check_valid(board, r, c);
    while(!valid)
    {
    cout << "Sorry, please choose another row and column."
    << endl << "Row(1-3): ";
    cin >> r;
    cout << endl << "Column(1-3): ";
    cin >> c;
    valid = check_valid(board, r, c);
    }

    board[r - 1][c - 1] = mark[p - 1];
    winner = check_win(board, p, mark);
    if(winner)
    {
    display_board(board);
    break;
    }
    }

    }
    }

    if(winner)
    {
    cout << "Winner is Player " << p << "!" << endl << endl;
    }
    else if(count == 9)
    {
    display_board(board);
    cout << "Players Tied!" << endl << endl;
    }

    system("PAUSE");
    return 0;
    }
    //*****************************************************************************
    //function display_board
    //parameters: board(char array)
    //returns: void
    //*****************************************************************************
    void display_board(const char board[SIZE][SIZE])
    {
    cout << endl << " 1 2 3" << endl <<endl
    << " 1 " << board[0][0] << " | " << board[0][1] << " | "
    << board[0][2] << endl
    << " ---|---|---" << endl
    << " 2 " << board[1][0] << " | " << board[1][1] << " | "
    << board[1][2] << endl
    << " ---|---|---" << endl
    << " 3 " << board[2][0] << " | " << board[2][1] << " | "
    << board[2][2] << endl << endl;
    }
    //*****************************************************************************
    //function get_location
    //parameters: row(int), column(int), player(int), and mark(char array)
    //returns: row(int) and column(int)
    //*****************************************************************************
    void get_location(int& r, int& c,const int p, const char mark[SIZE2])
    {
    cout << "Player " << p << " (" << mark[p - 1] << ") choose the row "
    << "and column of your next move."
    << endl << "Row(1-3): ";
    cin >> r;
    cout << endl << "Column(1-3): ";
    cin >> c;
    }
    //*****************************************************************************
    //function check_valid
    //parameters: board(char array), row(int), and column(int)
    //returns: bool(true or false)
    //*****************************************************************************
    bool check_valid(const char board[SIZE][SIZE], int r, int c)
    {
    if(r >= 1 && r <= SIZE && c >= 1 && c <= SIZE &&
    board[r - 1][c - 1] != 'X' && board[r - 1][c - 1] != 'O')
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    //*****************************************************************************
    //function check_win
    //parameters: board(char array), player(int), and mark(char array)
    //returns: bool(true or false)
    //*****************************************************************************
    bool check_win(const char board[SIZE][SIZE],const int p, const char mark[SIZE2])
    {
    bool win; //local variable
    win = check_row(board, p, mark);
    if(win)
    {
    return true;
    }
    win = check_column(board, p, mark);
    if(win)
    {
    return true;
    }
    win = check_diagonal();
    if(win)
    {
    return true;
    }
    return false;

    }
    //*****************************************************************************
    //function check_row
    //parameters: board(char array), player(int), and mark(char array)
    //returns: bool(true or false)
    //*****************************************************************************
    bool check_row(const char board[SIZE][SIZE],const int p, const char mark[SIZE2])
    {
    for(int r = 0; r < SIZE; r++)
    {
    if(board[r][0] == mark[p - 1] &&
    board[r][1] == mark[p - 1] &&
    board[r][2] == mark[p - 1])
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    }
    //*****************************************************************************
    //function check_column
    //parameters: board(char array), player(int), and mark(char array)
    //returns: bool(true or false)
    //*****************************************************************************
    bool check_column(const char board[SIZE][SIZE],const int p,
    const char mark[SIZE2])
    {
    for(int c = 0; c < SIZE; c++)
    {
    if(board[0][c] == mark[p - 1] &&
    board[1][c] == mark[p - 1] &&
    board[2][c] == mark[p - 1])
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    }
    //*****************************************************************************
    //function check_diagonal
    //parameters: board(char array), player(int), and mark(char array)
    //returns: bool(true or false)
    //*****************************************************************************
    void check_diagonal()
    {
    if(board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != 0)
    {
    p = board[0][0];
    return;
    }

  2. #2
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    You are referencing p and board[][] in check_diagonal() which will give you an "undeclared identifier" error.. You also say in your comments that it should return a bool however you use void?

    Did you actually get this to compile?

Similar Threads

  1. Java Tic Tac Toe Project Help Needed
    By SuperApple in forum Java
    Replies: 10
    Last Post: 05-22-2007, 11:30 PM
  2. Tic Tac Toe
    By EHump20 in forum C++
    Replies: 3
    Last Post: 03-30-2007, 03:26 AM
  3. Java Tic Tac Toe
    By doctor26 in forum Java
    Replies: 0
    Last Post: 03-20-2007, 09:07 PM
  4. help with NxN Board in Tic Tac Toe
    By Cunninlynguist in forum Java
    Replies: 0
    Last Post: 11-12-2006, 01:09 AM
  5. Simple tic tac toe applet
    By Ant_Magma in forum Java
    Replies: 5
    Last Post: 09-26-2005, 04:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center