Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,630 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,434 people online right now. Registration is fast and FREE... Join Now!




Tic Tac Toe

 
Reply to this topicStart new topic

Tic Tac Toe, input validation

Bucket
post 15 Apr, 2008 - 06:42 AM
Post #1


New D.I.C Head

*
Joined: 30 Mar, 2008
Posts: 7

This was a class example to make simple tic-tac-toe to explain 2d-arrays. Our professor did the board and some of the logic, as the students followed.

The thing I want to do is to make sure the player one's 'X' doesn't overwrite the player two's 'O' and visa-versa, when playing the game.

CODE

#include <cstdlib>
#include <iostream>

using namespace std;

const int ROW = 5;
const int COL = 5;

void print(char [][COL], int, int);
void init(char [][COL], int, int);
void play(char [][COL], int, int);
void menu(int &, int &);

int main(int argc, char *argv[])
{
    char board[ROW][COL];
    
    init(board, ROW, COL);
    print(board, ROW, COL);
    play(board, ROW, COL);
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
//print the game board

void print(char board[][COL], int row, int col)
{
    for(int r = 0; r < row; r++)
    {
        for(int c = 0; c < col; c++)
        {
           cout << board[r][c];
        }
        cout << endl;
    }
}
//initialize the playing field

void init(char board[][COL], int row, int col)
{
    board[0][0] = 201;
    board[row-1][0] = 200;
    board[0][col-1] = 187;
    board[row-1][col-1] = 188;
    //horizontal
    for(int c = 1; c < col-1; c++)
    {
        board[0][c] = 205;
        board[row-1][c] = 205;
    }
    //vertical
    for(int r = 1; r < row-1; r++)
    {
        board[r][0] = 186;
        board[r][col-1] = 186;
    }
    
    //center of table
    for(int r = 1; r < row-1; r++)
    {
        for(int c = 1; c < col-1; c++)
        {
           board[r][c] = 250;
        }

    }
}

void menu(int & r, int & c)
{
    cout << "Please, enter the row coord. (1-3): ";
    cin >> r;
    cout << "\nNow, enter the column coord. (1-3): ";
    cin >> c;
    
    while (r <= 0 || r >= 4 || c <= 0 || c >= 4)  // input validation for the array
    {
        cout << "\nPlease, enter the CORRECT coordinates again" << endl;
        cout << "Enter Row: ";
        cin >> r;
        cout << "\nEnter Column: ";
        cin >> c;
    }
}


//play
void play(char board[][COL], int row, int col)
{
    char p1 = 'X';
    char p2 = 'O';
    int r, c;
    bool p1p = true;

    cout << "Welcome" << endl;
    
    for(int i = 0; i < 9; i++)
    {
        menu(r, c);
            if(p1p)
            {
                board[r][c] = p1;
                p1p = false;
            }
            else
            {
                board[r][c] = p2;
                p1p = true;
            }
            
            system("CLS");
            print(board, row, col);
    }
}


User is offlineProfile CardPM

Go to the top of the page


_net
post 15 Apr, 2008 - 07:02 AM
Post #2


D.I.C Head

**
Joined: 22 Sep, 2007
Posts: 138


My Contributions


Whats the problem?
User is offlineProfile CardPM

Go to the top of the page

Bucket
post 15 Apr, 2008 - 07:32 AM
Post #3


New D.I.C Head

*
Joined: 30 Mar, 2008
Posts: 7

Playing the game, any player can overwrite the other player's movement; for example, if I put X on coordinate (1,1) you can overwrite the movement by placing an 'O' on (1,1). A problem I'm still trying to solve.
User is offlineProfile CardPM

Go to the top of the page

chuck981996
post 8 Jul, 2008 - 04:04 AM
Post #4


New D.I.C Head

*
Joined: 3 Jul, 2008
Posts: 9


My Contributions


Make a Boolean function that checks whether there is already input at those co-ordinates like if (variable1 = "X" || "O") then return true if the space is occupied and false if it is vacant.

Hope this helps. biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

captainhampton
post 8 Jul, 2008 - 04:28 AM
Post #5


Jawsome++;

Group Icon
Joined: 17 Oct, 2007
Posts: 518



Thanked 2 times

Dream Kudos: 825
My Contributions


You could have a condition to check if the space if is already filled up by an 'x' or 'o'. If so you could print an error message stating that it is already full.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 05:31AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month