Jump to content
  • Advertisement
Sign in to follow this  
ksintath

LINE4 C++

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


//------------------------------------------------------------------------------
//The game line 4
//The aim is having 4 "X" or "O" vertically,horizantionally,diagonal together
//26.11.2005
//UK
//------------------------------------------------------------------------------


#include<iostream>
using std::cout;
using std::cin;
using std::endl;
void game();
bool menu();

#define ROW 7
#define COL 8 

class line4
{
public:

	void print();//print the board
	int check1(int);//Checks the line is empty or not
	void placeX(int,int);//Place X
	void placeO(int,int);//Place O
	bool checkv();//check the game vertical
	bool checkh();//check the game horizantal
	bool checkd1();//check the game in / direction
	bool checkd2();//check the game in \ direction
	void fill();//fill the board with0s.Create a new board
	bool finish();//

private:
	int board[ROW][COL];		
};


void line4::print()
{
	for(int c=0; c<COL; c++)
		cout<<" "<<c<<" ";

	cout<<endl;

	for(int a=0; a<ROW; a++)
	{
		for(int b=0; b<COL; b++)
		{
			if(board[a]==0)
				cout<<" . ";
			if(board[a]==1)
				cout<<" X ";
			if(board[a]==2)
				cout<<" O ";
		}cout<<endl;
	}
}

void line4::fill()
{
	for(int a=0; a<ROW; a++)
	{
		for(int b=0; b<COL; b++)
			board[a]=0;
	}

}

bool line4::checkh()
{
	for(int d=1; d<=2; d++)
	{
		for(int a=ROW; a>-1; a--)
		{
			for(int b=COL; b>-1; b--)
				if(board[a]==d && board[a][b-1]==d && board[a][b-2]==d && board[a][b-3]==d)
				{
					return true;
				}
		}
	}
	
	return false;
}

bool line4::checkv()
{
	for(int d=1; d<=2; d++)
	{
		for(int a=ROW; a>-1; a--)
		{
			for(int b=COL; b>-1; b--)
				if(board[a]==d && board[a-1]==d && board[a-2]==d && board[a-3]==d)
				{
					return true;
				}
		}
	}
	
	return false;

}

int line4::check1(int y)
{
	int x=ROW-1;
	if(board[0][y]!=0)
		return -1;

	while(board[x][y]!=0)
	{
		x--;
	}

	return x;
}

void line4::placeX(int x,int y)
{
	board[x][y]=1;

}

void line4::placeO(int x,int y)
{
	board[x][y]=2;
}

bool line4::finish()
{
	if(checkh()==true)
		return true;
	
	if(checkv()==true)
		return true;

	if(checkd1()==true)
		return true;

	if(checkd2()==true)
		return true;

	return false;	

}

bool line4::checkd1()
{	
	for(int d=1; d<=2; d++)
	{
		for(int a=0; a<ROW ; a++)
			for(int b=0; b<COL ;b++)
			{
				if(board[a]==d && board[a-1][b+1]==d && board[a-2][b+2] && board[a-3][b+3]==d)
					return true;
			}
	}

	return false;
}

bool line4::checkd2()
{
	for(int d=1; d<=2; d++)
	{
		for(int a=0; a<ROW ; a++)
			for(int b=0; b<COL ;b++)
			{
				if(board[a]==d && board[a-1][b-1]==d && board[a-2][b-2] && board[a-3][b-3]==d)
					return true;
			}
	}
	return false;
}

int main()
{
	while(menu()!=false);


	return 0;
}

void game()
{
	line4 u;
	int turn=0;
	int x;
	u.fill();
	u.print();
	while(u.finish()==false)
	{
		if(turn%2==0 && u.finish()==false)
		{
			cout<<"Player 1 Enter the column number:";
			cin>>x;
			   if(u.check1(x)==-1)
			   {
					cout<<"Please enter another column";
					cin>>x;
					turn=turn+2;
				}
			   else
			   {
				   u.placeX(u.check1(x),x);
				   u.print();
				   turn=turn+1;
			   }
		}

		if(turn%2==1 && u.finish()==false)
		{
			cout<<"Player 2 Enter the column number:";
			cin>>x;
			   if(u.check1(x)==-1)
			   {
					cout<<"Please enter another column";
					cin>>x;
					turn=turn+2;
				}
			   else
			   {
				   u.placeO(u.check1(x),x);
				   u.print();
				   turn=turn+1;
			   }
		}

	}

	if(turn%2==1)
		cout<<"PLAYER 1 WINS!!!\n";
	else
		cout<<"PALYER 2 WINS!!!\n";

}

bool menu()
{
	int x;
	cout<<"--------------------------------------------\n";
	cout<<"--------------------------------------------\n";
	cout<<"--------------------------------------------\n";
	cout<<"-----------LL   I N   N EEE 4   ------------\n";
	cout<<"-----------LL   I NN  N E   4   ------------\n";
	cout<<"-----------LL   I N N N EEE 4   ------------\n";
	cout<<"-----------LL   I N  NN E   4444------------\n";               
	cout<<"-----------LLLL I N   N EEE   4 ------------\n";
	cout<<"--------------------------------------------\n";
	cout<<"--------------------------------------------\n";
	cout<<"--------------------------------------------\n";

	cout<<"To PLAY the game press 1:\n";
	cout<<"For HELP press 2\n";
	cout<<"To EXIT press -1\n";
	cin>>x;
	switch(x)
	{
	case 1:
		game();
		return true;
	case 2:
		cout<<"The player you makes 4 lines in any direction";
		cout<<"\nwins the game.When you select the column the ";
		cout<<"\nelement falls to the blank place in that column.\n";
		return true;
	case -1:
		return false;

	}

	return true;
} 
This is my first C++ game.I knew it is simple but now i can only handle with this kind of codes.I want to ask a few questions.My game works as a test so in each movement the screenn goes down i want to stop it.How can i do it? And i want to add graphics to my game.Simple things.Like making "x" and "o"s as colorful circles.Thanxxx

Share this post


Link to post
Share on other sites
Advertisement
Thank you for complying.

There is no portable way to use colors in your output. If you are using DOS or Windows, you might want to look into ANSI sequences for changing the foreground and background color. Or you could decide to use PDCurses, which is a cross-platform console output library (that allows writing to anywhere on the screen, using colors).

Share this post


Link to post
Share on other sites
I fixed the source code for you...


//------------------------------------------------------------------------------
//The game line 4
//The aim is having 4 "X" or "O" vertically,horizantionally,diagonal together
//26.11.2005
//UK
//------------------------------------------------------------------------------


#include <iostream>
using namespace std;
void game();
bool menu();

#define ROW 7
#define COL 8

class line4
{
public:

void print();//print the board
int check1(int);//Checks the line is empty or not
void placeX(int,int);//Place X
void placeO(int,int);//Place O
bool checkv();//check the game vertical
bool checkh();//check the game horizantal
bool checkd1();//check the game in / direction
bool checkd2();//check the game in \ direction
void fill();//fill the board with0s.Create a new board
bool finish();//

private:
int board[ROW][COL];
};


void line4::print()
{
for(int c=0; c<COL; c++)
cout<<" "<<c<<" ";

cout<<endl;

for(int a=0; a<ROW; a++)
{
for(int b=0; b<COL; b++)
{
if(board[a]==0)
cout<<" . ";
if(board[a]==1)
cout<<" X ";
if(board[a]==2)
cout<<" O ";
}cout<<endl;
}
}

void line4::fill()
{
for(int a=0; a<ROW; a++)
{
for(int b=0; b<COL; b++)
board[a]=0;
}

}

bool line4::checkh()
{
for(int d=1; d<=2; d++)
{
for(int a=ROW; a>-1; a--)
{
for(int b=COL; b>-1; b--)
if(board[a]==d && board[a][b-1]==d && board[a][b-2]==d && board[a][b-3]==d)
{
return true;
}
}
}

return false;
}

bool line4::checkv()
{
for(int d=1; d<=2; d++)
{
for(int a=ROW; a>-1; a--)
{
for(int b=COL; b>-1; b--)
if(board[a]==d && board[a-1]==d && board[a-2]==d && board[a-3]==d)
{
return true;
}
}
}

return false;

}

int line4::check1(int y)
{
int x=ROW-1;
if(board[0][y]!=0)
return -1;

while(board[x][y]!=0)
{
x--;
}

return x;
}

void line4::placeX(int x,int y)
{
board[x][y]=1;

}

void line4::placeO(int x,int y)
{
board[x][y]=2;
}

bool line4::finish()
{
if(checkh()==true)
return true;

if(checkv()==true)
return true;

if(checkd1()==true)
return true;

if(checkd2()==true)
return true;

return false;

}

bool line4::checkd1()
{
for(int d=1; d<=2; d++)
{
for(int a=0; a<ROW ; a++)
for(int b=0; b<COL ;b++)
{
if(board[a]==d && board[a-1][b+1]==d && board[a-2][b+2] && board[a-3][b+3]==d)
return true;
}
}

return false;
}

bool line4::checkd2()
{
for(int d=1; d<=2; d++)
{
for(int a=0; a<ROW ; a++)
for(int b=0; b<COL ;b++)
{
if(board[a]==d && board[a-1][b-1]==d && board[a-2][b-2] && board[a-3][b-3]==d)
return true;
}
}
return false;
}

int main()
{
while(menu()!=false);


return 0;
}

void game()
{
line4 u;
int turn=0;
int x;
u.fill();
u.print();
while(u.finish()==false)
{
if(turn%2==0 && u.finish()==false)
{
cout<<"Player 1 Enter the column number:";
cin>>x;
if(u.check1(x)==-1)
{
cout<<"Please enter another column";
cin>>x;
turn=turn+2;
}
else
{
u.placeX(u.check1(x),x);
u.print();
turn=turn+1;
}
}

if(turn%2==1 && u.finish()==false)
{
cout<<"Player 2 Enter the column number:";
cin>>x;
if(u.check1(x)==-1)
{
cout<<"Please enter another column";
cin>>x;
turn=turn+2;
}
else
{
u.placeO(u.check1(x),x);
u.print();
turn=turn+1;
}
}

}

if(turn%2==1)
cout<<"PLAYER 1 WINS!!!\n";
else
cout<<"PALYER 2 WINS!!!\n";

}

bool menu()
{
int x;
cout<<"--------------------------------------------\n";
cout<<"--------------------------------------------\n";
cout<<"--------------------------------------------\n";
cout<<"-----------LL I N N EEE 4 ------------\n";
cout<<"-----------LL I NN N E 4 ------------\n";
cout<<"-----------LL I N N N EEE 4 ------------\n";
cout<<"-----------LL I N NN E 4444------------\n";
cout<<"-----------LLLL I N N EEE 4 ------------\n";
cout<<"--------------------------------------------\n";
cout<<"--------------------------------------------\n";
cout<<"--------------------------------------------\n";

cout<<"To PLAY the game press 1:\n";
cout<<"For HELP press 2\n";
cout<<"To EXIT press -1\n";
cin>>x;
switch(x)
{
case 1:
game();
return true;
case 2:
cout<<"The player you makes 4 lines in any direction";
cout<<"\nwins the game.When you select the column the ";
cout<<"\nelement falls to the blank place in that column.\n";
return true;
case -1:
return false;

}

return true;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!