SkpLeaD 0 Newbie Poster

Hello guys I had an Exercise for a Noughts and Crosses game and i thought maybe it could help whoever needs to get an idea of Classes and Arrays.

#include <iostream>
using namespace std;

class CNoughts
{

	private:
		int grid[3][3];

	public:
		void Initialise();
		void Grid();
		void PlayerInput();
		int Player,i,j;
		int Winning();
		int Drawing();
};
void CNoughts::Initialise()//Sets Initial value in the grid as 0
{
	
	for(i=0; i<3; i++)
	{
		for(j=0; j<3; j++)
		{
			grid[i][j] = 0;
		}
	}
}

void CNoughts::Grid()//Creates the Grid on the screen!
{
	for(i=0; i<3; i++)
	{
		for(j=0; j<3; j++)
		{
			cout << "[ " << grid[i][j] << " ]";
		}
		cout << endl;
	}
}

void CNoughts::PlayerInput()//Selects the current player putting a value in the grid.
{
	do 
	{
		cout << "\nEnter the current player [1 or 2] : ";
		cin >> Player;	
	}
	while ( 0>= Player > 2); //Doesn't allow the player to enter any other integer than 1 or 2.

	do
	{
			do
			{
				cout << "Please enter the Row: ";
				cin >> i;
			}
			while( i<0 || i>2);//Check if player has put for row a number from 0 to 2.
			do
			{
				cout << "Please enter the Collumn: ";
				cin >> j;
			}
			while( j<0 || j>2);//Check if player has put for Collumn a number from 0 to 2.
			cout << endl;
			grid[i][j] = Player;//Assisgns the number(1 or 2) of the current player in the possition he chose in the grid.
	}
	while (grid[i][j]==0);

}
int CNoughts::Winning()
{
	//Check if Player 1 has won
	if (grid[0][0]==1 && grid[0][1]==1 && grid[0][2]==1) //Orizontal win for player 1
	{
		return 1;
	}	
	if (grid[1][0]==1 && grid[1][1]==1 && grid[1][2]==1)
	{
		return 1;
	}
	if (grid[2][0]==1 && grid[2][1]==1 && grid[2][2]==1)
	{
		return 1;
	}	
	if (grid[0][0]==1 && grid[1][0]==1 && grid[2][0]==1)//Vertical win for player 1
	{
		return 1;
	}
	if (grid[0][1]==1 && grid[1][1]==1 && grid[2][1]==1)
	{
		return 1;
	}
	if (grid[0][2]==1 && grid[1][2]==1 && grid[2][2]==1)
	{
		return 1;
	}
	if (grid[0][0]==1 && grid[1][1]==1 && grid[2][2]==1)//Diagonal win for player 1
	{
		return 1;
	}
	if (grid[0][2]==1 && grid[1][1]==1 && grid[2][0]==1)
	{
		return 1;
	}
	
	//Check if Player 2 has won
	if (grid[0][0]==2 && grid[0][1]==2 && grid[0][2]==2)//Orizontal win for player 2
	{
		return 1;
	}
	if (grid[1][0]==2 && grid[1][1]==2 && grid[1][2]==2)
	{
		return 1;
	}
	if (grid[2][0]==2 && grid[2][1]==2 && grid[2][2]==2)
	{
		return 1;
	}
	if (grid[0][0]==2 && grid[1][0]==2 && grid[2][0]==2)//Vertical win for player 2
	{
		return 1;
	}
	if (grid[0][1]==2 && grid[1][1]==2 && grid[2][1]==2)
	{
		return 1;
	}
	if (grid[0][2]==2 && grid[1][2]==2 && grid[2][2]==2)
	{
		return 1;
	}
	if (grid[0][0]==2 && grid[1][1]==2 && grid[2][2]==2)//Diagonal win for player 2
	{
		return 1;
	}
	if (grid[0][2]==2 && grid[1][1]==2 && grid[2][0]==2)
	{
		return 1;
	}
	return 0;
}

int CNoughts::Drawing()//Checks if the grid has no more 0's in it and if it becomes 1 then the main will show a msg for Draw.
{
	for(i=0; i<3; i++)
	{
		for(j=0; j<3; j++)
		{
			if (grid[i][j]==0)
			{
				return 0;
			}
		}			
	}
	return 1;
}
int main()
{
	CNoughts* Game = new CNoughts;

	cout << "TIPS!!\n";
	cout << "The Possitions of the game are:\n";
	cout << "(0, 0) (0, 1) (0, 2)\n";
	cout <<	"(1, 0) (1, 1) (1, 2)\n";
	cout <<	"(2, 0) (2, 1) (2, 2)\n";
	cout << "When you input Row or Collumn use numbers from 0 to 2!\n";

	Game->Initialise();//Creates a 3x3 grid and fills it with all 0's.

	do
	{
		Game->PlayerInput();
		Game->Grid();
		Game->Winning();
		Game->Drawing();
	}
	while (Game->Winning()!=1 && Game->Drawing()!=1);

	if (Game->Drawing()==1)
	{
		cout << "\nIt is a Draw , Thank you for playing!\n\n";
	}
	if(Game->Winning()==1)
	{
		cout << "\nCongratulations player " << Game->Player << " you have won!!!\n\n";
	}
	
	system("pause");
	delete(Game);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.