Tic Tac Toe

input validation

Page 1 of 1

4 Replies - 1523 Views - Last Post: 08 July 2008 - 05:28 AM Rate Topic: -----

#1 Bucket  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
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);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe

#2 _net  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 23-September 07

Re: Tic Tac Toe

Posted 15 April 2008 - 08:02 AM

Whats the problem?
Was This Post Helpful? 0
  • +
  • -

#3 Bucket  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 chuck981996  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • 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. :D
Was This Post Helpful? 0
  • +
  • -

#5 captainhampton  Icon User is offline

  • Jawsome++;
  • member icon

Reputation: 13
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1