Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can i have difficulty levels when play 1vspc??


#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

class tictactoe
{
	private:
		
		char table[9],eis,tf;
		int ne;
		
	public:
		void menu();
		void cr(int);
		void setempty(char);
		void control(int,char);
		void seteis(char);
		void nexteis();
		char gettable(int);
		int gettf();
		int whowin();
		char geteis();
		int rules();
		int pcmove();
		int getpcor1();
		void setpcor1(int);
		void turnpcor1();
};

/*menu*/
void tictactoe::menu()
{
	system("cls");
	cout<<"1 -> 1vs1 \n";
	cout<<"2 -> 1vsPc \n";
	cout<<"3 -> Rules and regulatios \n";
}
/*emfanizei ton pinaka*/
void tictactoe::cr(int ch){
	system("cls");
	cout<<"|\t" << gettable(0) << "\t|\t" << gettable(1) << "\t|\t" << gettable(2) << "\t|\t"<<endl;
    cout<< "-------------------------------------------------"<<endl;
    cout<<"|\t" << gettable(3) << "\t|\t" << gettable(4) << "\t|\t" << gettable(5) << "\t|\t"<<endl; 
    cout<< "-------------------------------------------------"<<endl;
    cout<<"|\t" << gettable(6) << "\t|\t" << gettable(7) << "\t|\t" << gettable(8) << "\t|\t"<<endl;
	
	
	if(eis=='x' && whowin()!=0 && ch==1)
		cout<<"Play X\n\n";
	if(eis=='o' && whowin()!=0 && ch==1)
		cout<<"Play O\n\n";
}
void tictactoe::setempty(char vacancy)
{
	int i;
	for(i=0;i<=9;i++){
			table[i]=vacancy;
	}
}
/*orizei to arxiko sumvolo*/
void tictactoe::seteis(char t)
{
	eis=t;
}
/*allazei to sumvolo*/
void tictactoe::nexteis()
{
	
	if(eis=='x')
		seteis('o');
	else
		seteis('x');
	
}
/*eisagei ta sumvola*/
void tictactoe::control(int mv,char vacancy)
{
	tf=0;
	if(mv==1)
	{
		if(table[0]==vacancy)
		{
			if(eis=='x')
			{
				table[0]='x';
				tf=1;
			}
			if(eis=='o')
			{
				table[0]='o';
				tf=1;
			}
		}
	}
	if(mv==2)
	{
		if(table[1]==vacancy)
		{
			if(eis=='x')
			{
				table[1]='x';
				tf=1;
			}
			if(eis=='o')
			{
				table[1]='o';
				tf=1;
			}
		}
	}
	if(mv==3)
	{
		if(table[2]==vacancy)
		{
			if(eis=='x')
			{
				table[2]='x';
				tf=1;
			}
			if(eis=='o')
			{
				table[2]='o';
				tf=1;
			}
		}
	}
	if(mv==4)
	{
		if(table[3]==vacancy)
		{
			if(eis=='x')
			{
				table[3]='x';
				tf=1;
			}
			if(eis=='o')
			{
				table[3]='o';
				tf=1;
			}
		}
	}
	if(mv==5)
	{
		if(table[4]==vacancy)
		{
			if(eis=='x')
			{
				table[4]='x';
				tf=1;
			}
			if(eis=='o')
			{
				table[4]='o';
				tf=1;
			}
		}
	}
	if(mv==6)
	{
		if(table[5]==vacancy)
		{
			if(eis=='x')
			{
				table[5]='x';
				tf=1;
			}
			if(eis=='o')
			{
				table[5]='o';
				tf=1;
			}
		}
	}
	if(mv==7)
	{
		if(table[6]==vacancy)
		{
			if(eis=='x')
			{
				table[6]='x';
				tf=1;
			}
			if(eis=='o')
			{
				table[6]='o';
				tf=1;
			}
		}
	}
	if(mv==8)
	{
		if(table[7]==vacancy)
		{
			if(eis=='x'){
				table[7]='x';
				tf=1;
			}
			if(eis=='o')
			{
				table[7]='o';
				tf=1;
			}
		}
	}
	if(mv==9)
	{
		if(table[8]==vacancy)
		{
			if(eis=='x')
			{
				table[8]='x';
				tf=1;
			}
			if(eis=='o')
			{
				table[8]='o';
				tf=1;
			}
		}
	}	
}
/*mas emfanizei ti exei o pinakas*/
char tictactoe::gettable(int tr)
{
	return table[tr];
}
/*ean mporei na eisagei sumvolo*/
int tictactoe::gettf()
{
	return tf;
}
/*mas deixnei ean kapoios kerdise*/
int tictactoe::whowin()
{
	
	/*orizontia*/
	if((table[0]=='x' && table[1]=='x' && table[2]=='x') || (table[0]=='o' && table[1]=='o' && table[2]=='o') )
	{
		return 0;
	}
	if((table[3]=='x' && table[4]=='x' && table[5]=='x') || (table[3]=='o' && table[4]=='o' && table[5]=='o') )
	{
		return 0;
	}
	if((table[6]=='x' && table[7]=='x' && table[8]=='x') || (table[6]=='o' && table[7]=='o' && table[8]=='o') )
	{
		return 0;
	}
	
	/*katheta*/
	if((table[0]=='x' && table[3]=='x' && table[6]=='x') || (table[0]=='o' && table[3]=='o' && table[6]=='o') )
	{
		return 0;
	}
	if((table[1]=='x' && table[4]=='x' && table[7]=='x') || (table[1]=='o' && table[4]=='o' && table[7]=='o') )
	{
		return 0;
	}
	if((table[2]=='x' && table[5]=='x' && table[8]=='x') || (table[2]=='o' && table[5]=='o' && table[8]=='o') )
	{
		return 0;
	}
	
	/*diagwnia*/
	if((table[0]=='x' && table[4]=='x' && table[8]=='x') || (table[0]=='o' && table[4]=='o' && table[8]=='o') )
	{
		return 0;
	}
	if((table[2]=='x' && table[4]=='x' && table[6]=='x') || (table[2]=='o' && table[4]=='o' && table[6]=='o') )
	{
		return 0;
	}
}
/*gurizei to sumvolo*/
char tictactoe::geteis()
{
	return eis;
}
/*kanones*/
int tictactoe::rules()
{
	int p;
system("cls");
cout
<<"You can play 1vs1 or 1vsPc\n"
<<"Every box symbolizes a number\n"
<<"The first position is  1 and the last one is 9 \n\n";
cout<<"Give 1 to return in menu \n";
cout<<"Give 0 to exit \n";
cin>>p;
while(p>1 || p<0)
{
		cout
		<< "Not a Valid Choice\n"
		<< "Choose again:\n";
		cout<<"Give 1 to return in menu\n";
		cout<<"Give 0 exit\n";
		cin>>p;
		}
		return p;			  
}
int tictactoe::pcmove()
{
	int random;
	srand (time(NULL));
	random=(rand() % 9)+1;

	return random;
}
/*deixnei pios pezei*/
int tictactoe::getpcor1()
{
	return ne;
}
/*poios 3ekinaei*/
void tictactoe::setpcor1(int lol)
{
	ne=lol;
}
/*poios pezh*/
void tictactoe::turnpcor1()
{
	if(ne==1)
		ne=2;
	else
		ne=1;
}

int main()
	{
		
	int ch1,mv,cm=0,rewin=1,ot=0;;
	char vancancy=' ';
	tictactoe t; 
	do{
	ot=0;
	t.menu(); 
	
	
	cout<<"Give a choice: ";
	cin>>ch1;
	if(ch1!=0){
	while(ch1<1 || ch1>3){
		cout<<"Give right choice: ";
		cin>>ch1;
		if(ch1==0)
			break;
	}
	}
	/*gia thn prwth epilogh*/
	if(ch1==1){
		t.setempty(vancancy);	
		t.seteis('x');
		t.cr(ch1);
		do{
		do{
		cout<<"Give again choice: ";
		cin>>mv;
		if(mv==0)
			break;
		t.control(mv,vancancy);
		if(t.gettf()==0 && (mv>0 && mv<10)){
			cout<<"Give again the right choice\n";
		}
		}while(t.gettf()==0 && (mv>0 || mv<10 ));
		if(mv==0){
			cout<<"Arrivederci";
			break;
		}
		t.nexteis();
		t.cr(ch1);
		rewin=t.whowin();
		cm=cm+1;
	}while(cm<9 && rewin!=0);
		if(rewin==0){
			t.nexteis();
			cout<<"And the winner is: "<<t.geteis();
		}
		if(cm==9 && rewin!=0){
			cout<<"Draw";
		}

	}
	

	
	/*Gia thn 2h epilogh*/
	if(ch1==2){
		t.setpcor1(1);
		t.setempty(vancancy);
		t.seteis('x');
		t.cr(ch1);
		do{
		if(t.getpcor1()==1 && rewin!=0){
		do{
		cout<<"Give again choice: ";
		cin>>mv;
		if(mv==0)
			break;
		t.control(mv,vancancy);
		if(t.gettf()==0 && (mv>0 && mv<10)){
			cout<<"Give again the right choice\n";
		}
		}while(t.gettf()==0 && (mv>0 || mv<10 ));
		if(mv==0){
			cout<<"Arrivederci dal pc";
			break;
		}
		t.nexteis();
		t.cr(ch1);
		rewin=t.whowin();
		cm=cm+1;
		if(cm<9)
		t.turnpcor1();
		}
		
		if(t.getpcor1()==2 && rewin!=0){
		do{
		mv=t.pcmove();
		t.control(mv,vancancy);
		}while(t.gettf()==0);
		t.nexteis();
		t.cr(ch1);
		rewin=t.whowin();
		cm=cm+1;
		if(cm<9)
		t.turnpcor1();
		}
		}while(cm<9 && rewin!=0);
		
		if(rewin==0){
			t.nexteis();
			cout<<"And the winner is: "<<t.geteis();
		}
		if(cm==9 && rewin!=0){
			cout<<"Draw";
		}

	}
	/*gia thn 3h epilogh*/
	if(ch1==3)
	{
		ot=t.rules();
	}
	}while(ot!=0);
	return 0;
}


What I have tried:

i dont try anything............
Posted
Updated 1-Jun-21 8:14am

1 solution

You can use some random or false move in low levels mixed with probabilities.

Like in the "dummy" level ALWAYS not the best move or only 33% of good move.

So you first have to rate all next moves and than choose in that rated list via your level control.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900