# Tic Tac Toe

Page 1 of 1

## 4 Replies - 1757 Views - Last Post: 08 July 2008 - 05:28 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=49158&amp;s=0eb9baae104ba77b2cb574f09c9f6cf4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Bucket

Reputation: 0
• Posts: 25
• Joined: 30-March 08

# Tic Tac Toe

Posted 15 April 2008 - 07:42 AM

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.

```#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);

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++)
{
if(p1p)
{
board[r][c] = p1;
p1p = false;
}
else
{
board[r][c] = p2;
p1p = true;
}

system("CLS");
print(board, row, col);
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Tic Tac Toe

### #2 _net

Reputation: 1
• Posts: 147
• Joined: 23-September 07

## Re: Tic Tac Toe

Posted 15 April 2008 - 08:02 AM

Whats the problem?

### #3 Bucket

Reputation: 0
• Posts: 25
• Joined: 30-March 08

## Re: Tic Tac Toe

Posted 15 April 2008 - 08:32 AM

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.

### #4 chuck981996

Reputation: 5
• Posts: 119
• Joined: 03-July 08

## Re: Tic Tac Toe

Posted 08 July 2008 - 05:04 AM

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.

### #5 captainhampton

• Jawsome++;

Reputation: 14
• Posts: 548
• Joined: 17-October 07

## Re: Tic Tac Toe

Posted 08 July 2008 - 05:28 AM

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.