Decision Making

tic tac toe

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 905 Views - Last Post: 18 August 2008 - 10:02 PM Rate Topic: -----

#1 sms123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-August 08

Decision Making

Post icon  Posted 17 August 2008 - 02:03 AM

hey guys u kl?
um i am making a tic tac toe game i just need a few things, the first one is the one i am having most trouble one, getting the computer to make a choice.
i.e. the function "computersTurn()"
the second thing i need is that when it establishes that the player has one, it breaks out and doesnt let the computer make a turn;
here's my code so far
#include <cstdlib>
#include <iostream>

using namespace std;

char tryAgain, sqr1=' ',sqr2=' ',sqr3=' ',sqr4=' ',sqr5=' ',sqr6=' ',sqr7=' ',sqr8=' ',sqr9=' ',theBoard[3][3];
int p, i, j, playersChoices[9], noOfChoices=0, isPlayerWinner;
bool noWinner, playAgain;

void playersTurn(){
	cout << "Please enter your choice;" << endl;
	cin >> p;
	switch(p){
		case 1:
			if((theBoard[0][0] != 'X')||(theBoard[0][0] != 'O')){
				sqr1 = 'X';
				theBoard[0][0] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 2:
			if((theBoard[0][1] != 'X')||(theBoard[0][0] != 'O')){
				sqr2 = 'X';
				theBoard[0][1] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 3:
			if((theBoard[0][2] != 'X')||(theBoard[0][0] != 'O')){
				sqr3 = 'X';
				theBoard[0][2] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 4:
			if((theBoard[1][0] != 'X')||(theBoard[0][0] != 'O')){
				sqr4 = 'X';
				theBoard[1][0] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 5:
			if((theBoard[1][1] != 'X')||(theBoard[0][0] != 'O')){
				sqr5 = 'X';
				theBoard[1][1] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 6:
			if((theBoard[1][2] != 'X')||(theBoard[0][0] != 'O')){
				sqr6 = 'X';
				theBoard[1][2] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 7:
			if((theBoard[2][0] != 'X')||(theBoard[0][0] != 'O')){
				sqr7 = 'X';
				theBoard[2][0] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 8:
			if((theBoard[2][1] != 'X')||(theBoard[0][0] != 'O')){
				sqr8 = 'X';
				theBoard[2][1] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 9:
			if((theBoard[2][2] != 'X')||(theBoard[0][0] != 'O')){
				sqr9 = 'X';
				theBoard[2][2] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
	}
}
void computersTurn(){
	cout << "Computer's choice;" << endl;	
}
void drawBoard(){
	cout << sqr1 << "|" << sqr2 << "|" << sqr3 << endl;
	cout << "-+-+-" << endl;
	cout << sqr4 << "|" << sqr5 << "|" << sqr6 << endl;
	cout << "-+-+-" << endl;
	cout << sqr7 << "|" << sqr8 << "|" << sqr9 << "\n" << endl;
}
char checkWinner(){
	isPlayerWinner=2;
	//check the rows
	for (int i = 0; i < 3; i++){
		if ((theBoard[i][0] == theBoard[i][1]) && (theBoard[i][1] == theBoard[i][2]) && (theBoard[i][0] != ' ')){
			 isPlayerWinner = theBoard[i][0];
		}
	}
	
	//check the clmns
	for (int i = 0; i < 3; i++){
		 if ((theBoard[0][i] == theBoard[1][i]) && (theBoard[1][i] == theBoard[2][i]) && (theBoard[0][i] != ' ')){
			   isPlayerWinner = theBoard[0][i];
		 }
	}
	
	//check the diagnals
	if ((theBoard[0][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[2][2]) && (theBoard[0][0] != ' ')){
				isPlayerWinner = theBoard[0][0];
	}
	
	if ((theBoard[2][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[0][2]) && (theBoard[2][0] != ' ')) {
				isPlayerWinner = theBoard[2][0];
	}
	if(isPlayerWinner==1){
		return 'X';
	}else if(isPlayerWinner==0){
		return 'O';
	}else{
		return ' ';
	}
}
void decideWinner(){
	char winner = checkWinner();
	if(noOfChoices<9){
		if(winner == 'X'){
			cout << "You won." << endl;
			noWinner=false;
		}else if(winner == 'O'){
			cout << "You lost." << endl;
			noWinner=false;
		}else{
			noWinner=true;
		}
	}else{
		cout << "Its a tie." << endl;
		noWinner=true;
	}
	cout << "noOfChoices:" << noOfChoices << endl;
	cout << "winner:" << winner << endl;
}
void playAgainFunction(){
	cout << "Play Again?(Y/N)" << endl;
	cin >> tryAgain;
	if((tryAgain=='Y')||(tryAgain=='y')){
		playAgain=true;
	}else{
		playAgain=false;
	}
}
int main(){
	for(j=0;j<3;j++){
		for(i=0;i<3;i++){
			theBoard[j][i]=' ';
		}
	}
	playAgain=true;
	noWinner=true;
	while(playAgain){
		while(noWinner){
			playersTurn();
			drawBoard();
			decideWinner();
			computersTurn();
			drawBoard();
			decideWinner();
		}
		playAgainFunction();
	}
	system("PAUSE");
	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Decision Making

#2 rquinn  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-August 08

Re: Decision Making

Posted 17 August 2008 - 04:17 AM

I'm playing around with it and I noticed a few things:

1: How you have it now, it doesn't really check if a square is taken. You're using the || (or) condition with two negative checks. So it's checking if it's not X or if it's not O. Regardless of what's in that spot, it will be seen as true.

2: No matter what a row is filled with, the checkwinner() function thinks that the player won since it is only checking the theBoard[][] array which is only ever filled with 'true' (or 1) if it is filled. I've changed it so that theBoard[][] array takes in whatever the said square is filled with. And also changed checkwinner() so that it will check the difference between a computer or player win.

This kinda makes either the sqr1 - sqr9 or theBoard[][] array redundant, but I didn't change that out.

3: If you press 'y' at the end when it asks if you wanna play again, it doesn't. This is because the noWinner bool is set to false, so it never goes back to the game. I changed that and also had to clear the board so that it wouldn't make you auto-win / auto-lose.

As for the computer's turns, I've added a random play piece of code. It's weak, but it at least works. I had to change a thing here and there to work it. Check it out:

#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

char tryAgain, sqr1=' ',sqr2=' ',sqr3=' ',sqr4=' ',sqr5=' ',sqr6=' ',sqr7=' ',sqr8=' ',sqr9=' ',theBoard[3][3];
int p, c, i, j, playersChoices[9], noOfChoices=0, isPlayerWinner;
bool noWinner, playAgain;

void playersTurn(){
	cout << "\nPlease enter your choice:" << endl;
	cin >> p;
	switch(p){
		case 1:
			if((theBoard[0][0] == ' ')){
				sqr1 = 'X';
				theBoard[0][0] = sqr1;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 2:
			if((theBoard[0][1] == ' ')){
				sqr2 = 'X';
				theBoard[0][1] = sqr2;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 3:
			if((theBoard[0][2] == ' ')){
				sqr3 = 'X';
				theBoard[0][2] = sqr3;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 4:
			if((theBoard[1][0] == ' ')){
				sqr4 = 'X';
				theBoard[1][0] = sqr4;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 5:
			if((theBoard[1][1] == ' ')){
				sqr5 = 'X';
				theBoard[1][1] = sqr5;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 6:
			if((theBoard[1][2] == ' ')){
				sqr6 = 'X';
				theBoard[1][2] = sqr6;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 7:
			if((theBoard[2][0] == ' ')){
				sqr7 = 'X';
				theBoard[2][0] = sqr7;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 8:
			if((theBoard[2][1] == ' ')){
				sqr8 = 'X';
				theBoard[2][1] = sqr8;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 9:
			if((theBoard[2][2] == ' ')){
				sqr9 = 'X';
				theBoard[2][2] = sqr9;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
	}
}
void computersTurn(){
	//random square for the comp to choose
	c = rand() % 9 + 1;
	switch(c){
		case 1:
			if((theBoard[0][0] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr1 = 'O';
				theBoard[0][0] = sqr1;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 2:
			if((theBoard[0][1] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr2 = 'O';
				theBoard[0][1] = sqr2;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 3:
			if((theBoard[0][2] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr3 = 'O';
				theBoard[0][2] = sqr3;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 4:
			if((theBoard[1][0] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr4 = 'O';
				theBoard[1][0] = sqr4;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 5:
			if((theBoard[1][1] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr5 = 'O';
				theBoard[1][1] = sqr5;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 6:
			if((theBoard[1][2] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr6 = 'O';
				theBoard[1][2] = sqr6;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 7:
			if((theBoard[2][0] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr7 = 'O';
				theBoard[2][0] = sqr7;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 8:
			if((theBoard[2][1] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr8 = 'O';
				theBoard[2][1] = sqr8;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 9:
			if((theBoard[2][2] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr9 = 'O';
				theBoard[2][2] = sqr9;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
	}


}
void drawBoard(){
	cout << sqr1 << "|" << sqr2 << "|" << sqr3 << endl;
	cout << "-+-+-" << endl;
	cout << sqr4 << "|" << sqr5 << "|" << sqr6 << endl;
	cout << "-+-+-" << endl;
	cout << sqr7 << "|" << sqr8 << "|" << sqr9 << "\n" << endl;
}
char checkWinner(){
	isPlayerWinner=2;
	//check the rows
	for (int i = 0; i < 3; i++){
		if ((theBoard[i][0] == theBoard[i][1]) && (theBoard[i][1] == theBoard[i][2]) && (theBoard[i][0] != ' ')){
			if(theBoard[i][0] == 'X')
				isPlayerWinner = 1;
			else
				isPlayerWinner = 0;
		}
	}
 
	//check the clmns
	for (int i = 0; i < 3; i++){
		 if ((theBoard[0][i] == theBoard[1][i]) && (theBoard[1][i] == theBoard[2][i]) && (theBoard[0][i] != ' ')){
			if(theBoard[0][i] == 'X')
				isPlayerWinner = 1;
			else
				isPlayerWinner = 0;
		 }
	}
	
	//check the diagnals
	if ((theBoard[0][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[2][2]) && (theBoard[0][0] != ' ')){
			if(theBoard[0][0] == 'X')
				isPlayerWinner = 1;
			else
				isPlayerWinner = 0;
	}
	
	if ((theBoard[2][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[0][2]) && (theBoard[2][0] != ' ')) {
			
		if(theBoard[2][0] == 'X')
				isPlayerWinner = 1;
			else
				isPlayerWinner = 0;
	}
	if(isPlayerWinner==1){
		return 'X';
	}else if(isPlayerWinner==0){
		return 'O';
	}else{
		return ' ';
	}
}
void decideWinner(){
	char winner = checkWinner();
		if(winner == 'X'){
			cout << "You won." << endl;
			noWinner=false;
		}else if(winner == 'O'){
			cout << "You lost." << endl;
			noWinner=false;
		}else if (noOfChoices == 9){
		cout << "Its a tie." << endl;
		noWinner=false;
	}else{
			noWinner=true;
		}

		
	cout << "noOfChoices:" << noOfChoices << endl;
	cout << "winner:" << winner << endl;
}
void playAgainFunction(){
	cout << "Play Again?(Y/N)" << endl;
	cin >> tryAgain;
	if((tryAgain=='Y')||(tryAgain=='y')){
		playAgain=true;
		
	}else{
		playAgain=false;
	}
}
void clearBoard(){
	//clear squares, board and number of choices
		sqr1=' ';
		sqr2=' ';
		sqr3=' ';
		sqr4=' ';
		sqr5=' ';
		sqr6=' ';
		sqr7=' ';
		sqr8=' ';
		sqr9=' ';
		for(int i = 0; i<3; i++){
			for(int j = 0; j<3; j++){
				theBoard[i][j] = ' ';
			}
		}
		noOfChoices = 0;
}
int main(){
	//initialize random seed for rand
	srand ( time(NULL));


	for(j=0;j<3;j++){
		for(i=0;i<3;i++){
			theBoard[j][i]=' ';
		}
	}
	playAgain=true;
	noWinner=true;
	cout << "TIC TAC TOE" << endl;
	while(playAgain){
		while(noWinner){
			playersTurn();
			drawBoard();
			decideWinner();
			if(noOfChoices == 9)
				break;
			computersTurn();
			drawBoard();
			decideWinner();
		}
		playAgainFunction();
		clearBoard();
		noWinner = true;
	}
	system("PAUSE");
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#3 sms123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-August 08

Re: Decision Making

Posted 17 August 2008 - 04:29 AM

thanks for you reply, i have changed it since i first posted, but i cant see why the tryAgain doesnt work. thanks for you computersTurn() help.
here's me code
#include <cstdlib>
#include <iostream>

using namespace std;

char tryAgain, sqr1=' ',sqr2=' ',sqr3=' ',sqr4=' ',sqr5=' ',sqr6=' ',sqr7=' ',sqr8=' ',sqr9=' ',theBoard[3][3];
int p, c, i, j, playersChoices[9], noOfChoices=0, isPlayerWinner;
bool noWinner, playAgain;

void playersTurn(){
	cout << "Please enter your choice;" << endl;
	cin >> p;
	switch(p){
		case 1:
			if((theBoard[0][0] != 'X')||(theBoard[0][0] != 'O')){
				sqr1 = 'X';
				theBoard[0][0] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 2:
			if((theBoard[0][1] != 'X')||(theBoard[0][0] != 'O')){
				sqr2 = 'X';
				theBoard[0][1] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 3:
			if((theBoard[0][2] != 'X')||(theBoard[0][0] != 'O')){
				sqr3 = 'X';
				theBoard[0][2] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 4:
			if((theBoard[1][0] != 'X')||(theBoard[0][0] != 'O')){
				sqr4 = 'X';
				theBoard[1][0] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 5:
			if((theBoard[1][1] != 'X')||(theBoard[0][0] != 'O')){
				sqr5 = 'X';
				theBoard[1][1] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 6:
			if((theBoard[1][2] != 'X')||(theBoard[0][0] != 'O')){
				sqr6 = 'X';
				theBoard[1][2] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 7:
			if((theBoard[2][0] != 'X')||(theBoard[0][0] != 'O')){
				sqr7 = 'X';
				theBoard[2][0] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 8:
			if((theBoard[2][1] != 'X')||(theBoard[0][0] != 'O')){
				sqr8 = 'X';
				theBoard[2][1] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 9:
			if((theBoard[2][2] != 'X')||(theBoard[0][0] != 'O')){
				sqr9 = 'X';
				theBoard[2][2] = true;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
	}
}
void computersTurn(){
	//random square for the comp to choose
	c = rand() % 9 + 1;
	switch(c){
		case 1:
			if((theBoard[0][0] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr1 = 'O';
				theBoard[0][0] = sqr1;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 2:
			if((theBoard[0][1] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr2 = 'O';
				theBoard[0][1] = sqr2;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 3:
			if((theBoard[0][2] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr3 = 'O';
				theBoard[0][2] = sqr3;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 4:
			if((theBoard[1][0] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr4 = 'O';
				theBoard[1][0] = sqr4;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 5:
			if((theBoard[1][1] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr5 = 'O';
				theBoard[1][1] = sqr5;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 6:
			if((theBoard[1][2] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr6 = 'O';
				theBoard[1][2] = sqr6;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 7:
			if((theBoard[2][0] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr7 = 'O';
				theBoard[2][0] = sqr7;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 8:
			if((theBoard[2][1] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr8 = 'O';
				theBoard[2][1] = sqr8;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 9:
			if((theBoard[2][2] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr9 = 'O';
				theBoard[2][2] = sqr9;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
	}
}
void drawBoard(){
	cout << sqr1 << "|" << sqr2 << "|" << sqr3 << endl;
	cout << "-+-+-" << endl;
	cout << sqr4 << "|" << sqr5 << "|" << sqr6 << endl;
	cout << "-+-+-" << endl;
	cout << sqr7 << "|" << sqr8 << "|" << sqr9 << "\n" << endl;
}
char checkWinner(){
	isPlayerWinner=2;
	//check the rows
	for (int i = 0; i < 3; i++){
		if ((theBoard[i][0] == theBoard[i][1]) && (theBoard[i][1] == theBoard[i][2]) && (theBoard[i][0] != ' ')){
			 isPlayerWinner = theBoard[i][0];
		}
	}
	
	//check the clmns
	for (int i = 0; i < 3; i++){
		 if ((theBoard[0][i] == theBoard[1][i]) && (theBoard[1][i] == theBoard[2][i]) && (theBoard[0][i] != ' ')){
			   isPlayerWinner = theBoard[0][i];
		 }
	}
	
	//check the diagnals
	if ((theBoard[0][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[2][2]) && (theBoard[0][0] != ' ')){
				isPlayerWinner = theBoard[0][0];
	}
	
	if ((theBoard[2][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[0][2]) && (theBoard[2][0] != ' ')) {
				isPlayerWinner = theBoard[2][0];
	}
	if(isPlayerWinner==1){
		return 'X';
	}else if(isPlayerWinner==0){
		return 'O';
	}else{
		return ' ';
	}
}
void decideWinner(){
	char winner = checkWinner();
	if(noOfChoices<9){
		if(winner == 'X'){
			cout << "You won." << endl;
			noWinner=false;
		}else if(winner == 'O'){
			cout << "You lost." << endl;
			noWinner=false;
		}else{
			noWinner=true;
		}
	}else{
		cout << "Its a tie." << endl;
		noWinner=true;
	}
	cout << "noOfChoices:" << noOfChoices << endl;
	cout << "winner:" << winner << endl;
}
int main(){
	for(j=0;j<3;j++){
		for(i=0;i<3;i++){
			theBoard[j][i]=' ';
		}
	}
	playAgain=true;
	noWinner=true;
	while(playAgain){
		while(noWinner){
			playersTurn();
			drawBoard();
			decideWinner();
			computersTurn();
			drawBoard();
			decideWinner();
		}
		cout << "Play Again?(Y/N)" << endl;
		cin >> tryAgain;
		if((tryAgain=='Y')||(tryAgain=='y')){
			playAgain=true;
		}else{
			playAgain=false;
		}
	}
	system("PAUSE");
	return 0;
}


thanks again for your reply
Was This Post Helpful? 0
  • +
  • -

#4 webboy42  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-August 08

Re: Decision Making

Posted 17 August 2008 - 04:48 AM

Hi
Unfortunately I found your design difficult to understand, but I can point you in the right direction for allowing the computer to make choices.

One thing before I go ahead, you might want to put the code for modifying the board for a turn in its own function, so that the code for getting the human's choice and the computer's choice can call the same function to cut down on the repeated code.

The essence of getting the computer to make a choice is simplicity itself, theoretically speaking. There are essentially 2 major ways to go about it, either get the computer to work out each possible move so that it can choose the best, or program the basic logic manually. The second option sounds complicated, but it's actually quite simple, so that is the option I'll talk about.

To allow the computer to make an intelligent move, it's necessary to program the strategy. Obviously, the first thing it needs to do is check to see if there is a winning move. To perform this part, you need to check to see if 2 out of 3 squares in a row, column, or diagonal, have the computer's mark, with the 3rd square being empty. After this, check for a blocking move in a similar fassion, but instead of checking for the computer's mark, check for the human's mark. This might be able to be put in its own function. For this description I will skip some strategy which has a large degree of complexity, but if you want to add it anyway, check on wikipedia for tic tac toe, under the Strategy section. Next, if no other moves are found, select a square with the following priority, center, corner, edge. It doesn't matter in most cases which of those the computer chooses, as long as those priorities are kept in mind.

This method of selecting a move isn't perfect, but it is quite good, and at least 1 step better than another implementation I saw that didn't check for a winning move for the computer.

I hope I've been able to help you head in the right direction. Sorry for the absense of actual code, because it's getting a bit on the late side where I am, and I don't code so well when I'm tired.

Just for the record, your code at present seems overly complicated for such a simple game. For a single instance of a game, it's only necessary to have an array for the board, and a variable for the current player. For easy printing, just use a character array with the actual marks being used for printing, 'X' and 'O' for the players, and ' ' for blank squares, the array is still easy to test for a win or tie. If you want a record of moves, to look at when the game is over, just use the array like you were doing, but otherwise you don't actually need to keep track of moves, except for the board. The computer only needs to know about the current board to make a decision.
Was This Post Helpful? 1
  • +
  • -

#5 rquinn  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-August 08

Re: Decision Making

Posted 17 August 2008 - 04:53 AM

Please try the following with your new code:
  • On any play, choose a square that is already taken. If you take your own, the computer will go again. If you choose an 'O' spot, it will change it to 'X'.
  • Let the game go until the board is full. If this happens the game will crash. I added the following to prevent this:
    while(playAgain){
    		while(noWinner){
    [...]
    			if(noOfChoices == 9)
    				break;
    [...]
    		}
    		playAgainFunction();
    		clearBoard();
    		noWinner = true;
    	}
    
    

  • Try and let the computer win. The thing that checks this doesn't seem to work. I changed the checkwinner() in the previous post to fix this.
  • Play multiple games, you will notice the computer always chooses the same spots. You need to do an srand to have truely random activity:
    int main(){
    	//initialize random seed for rand
    	srand ( time(NULL));
    
    . Keep in mind that this needs the time.h header to work:
    #include <time.h>
    

  • Try choosing 1, then 2, then 3. You will win, then choose 'y' to play again. It will repeat the question in a loop until you say no. You need to set the noWinner bool back to true since it becomes false as soon as you win a game. Otherwise the second while loop in int main() will see a 'false' and be skipped.

Was This Post Helpful? 0
  • +
  • -

#6 sms123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-August 08

Re: Decision Making

Posted 17 August 2008 - 04:54 AM

View Postwebboy42, on 17 Aug, 2008 - 04:48 AM, said:

Hi
Unfortunately I found your design difficult to understand, but I can point you in the right direction for allowing the computer to make choices.

One thing before I go ahead, you might want to put the code for modifying the board for a turn in its own function, so that the code for getting the human's choice and the computer's choice can call the same function to cut down on the repeated code.

The essence of getting the computer to make a choice is simplicity itself, theoretically speaking. There are essentially 2 major ways to go about it, either get the computer to work out each possible move so that it can choose the best, or program the basic logic manually. The second option sounds complicated, but it's actually quite simple, so that is the option I'll talk about.

To allow the computer to make an intelligent move, it's necessary to program the strategy. Obviously, the first thing it needs to do is check to see if there is a winning move. To perform this part, you need to check to see if 2 out of 3 squares in a row, column, or diagonal, have the computer's mark, with the 3rd square being empty. After this, check for a blocking move in a similar fassion, but instead of checking for the computer's mark, check for the human's mark. This might be able to be put in its own function. For this description I will skip some strategy which has a large degree of complexity, but if you want to add it anyway, check on wikipedia for tic tac toe, under the Strategy section. Next, if no other moves are found, select a square with the following priority, center, corner, edge. It doesn't matter in most cases which of those the computer chooses, as long as those priorities are kept in mind.

This method of selecting a move isn't perfect, but it is quite good, and at least 1 step better than another implementation I saw that didn't check for a winning move for the computer.

I hope I've been able to help you head in the right direction. Sorry for the absense of actual code, because it's getting a bit on the late side where I am, and I don't code so well when I'm tired.

Just for the record, your code at present seems overly complicated for such a simple game. For a single instance of a game, it's only necessary to have an array for the board, and a variable for the current player. For easy printing, just use a character array with the actual marks being used for printing, 'X' and 'O' for the players, and ' ' for blank squares, the array is still easy to test for a win or tie. If you want a record of moves, to look at when the game is over, just use the array like you were doing, but otherwise you don't actually need to keep track of moves, except for the board. The computer only needs to know about the current board to make a decision.

thanks man, i can see your point, i will make a v2.0 lol sleep well mate
Was This Post Helpful? 0
  • +
  • -

#7 sms123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-August 08

Re: Decision Making

Posted 17 August 2008 - 05:15 AM

View Postrquinn, on 17 Aug, 2008 - 04:53 AM, said:

Please try the following with your new code:
  • On any play, choose a square that is already taken. If you take your own, the computer will go again. If you choose an 'O' spot, it will change it to 'X'.
  • Let the game go until the board is full. If this happens the game will crash. I added the following to prevent this:
    while(playAgain){
    		while(noWinner){
    [...]
    			if(noOfChoices == 9)
    				break;
    [...]
    		}
    		playAgainFunction();
    		clearBoard();
    		noWinner = true;
    	}
    
    

  • Try and let the computer win. The thing that checks this doesn't seem to work. I changed the checkwinner() in the previous post to fix this.
  • Play multiple games, you will notice the computer always chooses the same spots. You need to do an srand to have truely random activity:
    int main(){
    	//initialize random seed for rand
    	srand ( time(NULL));
    
    . Keep in mind that this needs the time.h header to work:
    #include <time.h>
    

  • Try choosing 1, then 2, then 3. You will win, then choose 'y' to play again. It will repeat the question in a loop until you say no. You need to set the noWinner bool back to true since it becomes false as soon as you win a game. Otherwise the second while loop in int main() will see a 'false' and be skipped.

thanks man that was really helpful i have made some modifications, but for some reason if you get three in a diagonal, i doesnt read it.
char checkWinner(){
	//check the rows
	for (int i = 0; i < 3; i++){
		if ((theBoard[i][0] == theBoard[i][1]) && (theBoard[i][1] == theBoard[i][2]) && (theBoard[i][0] != ' ')){
			return theBoard[i][0];
		}
	}
	
	//check the clmns
	for (int i = 0; i < 3; i++){
		 if ((theBoard[0][i] == theBoard[1][i]) && (theBoard[1][i] == theBoard[2][i]) && (theBoard[0][i] != ' ')){
			return theBoard[0][i];
		 }
	}
	
	//check the diagnals
	if ((theBoard[0][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[2][2]) && (theBoard[0][0] != ' ')){
		cout << theBoard[0][0] << endl;
		return theBoard[0][0];
	}
	
	if ((theBoard[2][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[0][2]) && (theBoard[2][0] != ' ')) {
		cout << theBoard[2][0] << endl;
		return theBoard[2][0];
	}
}


Was This Post Helpful? 0
  • +
  • -

#8 rquinn  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-August 08

Re: Decision Making

Posted 17 August 2008 - 05:32 AM

If you compare your code to my original post, you should have some reference for a working version:
char checkWinner(){
	isPlayerWinner=2;
	//check the rows
	for (int i = 0; i < 3; i++){
		if ((theBoard[i][0] == theBoard[i][1]) && (theBoard[i][1] == theBoard[i][2]) && (theBoard[i][0] != ' ')){
			if(theBoard[i][0] == 'X')
				isPlayerWinner = 1;
			else
				isPlayerWinner = 0;
		}
	}

	//check the clmns
	for (int i = 0; i < 3; i++){
		 if ((theBoard[0][i] == theBoard[1][i]) && (theBoard[1][i] == theBoard[2][i]) && (theBoard[0][i] != ' ')){
			if(theBoard[0][i] == 'X')
				isPlayerWinner = 1;
			else
				isPlayerWinner = 0;
		 }
	}
	
	//check the diagnals
	if ((theBoard[0][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[2][2]) && (theBoard[0][0] != ' ')){
			if(theBoard[0][0] == 'X')
				isPlayerWinner = 1;
			else
				isPlayerWinner = 0;
	}
	
	if ((theBoard[2][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[0][2]) && (theBoard[2][0] != ' ')) {
			
		if(theBoard[2][0] == 'X')
				isPlayerWinner = 1;
			else
				isPlayerWinner = 0;
	}
	if(isPlayerWinner==1){
		return 'X';
	}else if(isPlayerWinner==0){
		return 'O';
	}else{
		return ' ';
	}
}



The code you posted looks like it should work just as well as it did in the original code. I didn't change how it checks if a row is complete since it worked in the original. I only changed what it returned to decideWinner() so it could tell the diff between an comp and a player's win.
Was This Post Helpful? 0
  • +
  • -

#9 sms123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-August 08

Re: Decision Making

Posted 17 August 2008 - 05:48 AM

thanks for your help rquinn its working, now i had put a typo lol thanks again man.
and here is the edited code; (any constructive criticism?)
#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

char tryAgain, sqr1, sqr2, sqr3, sqr4, sqr5, sqr6, sqr7, sqr8, sqr9, theBoard[3][3];
int p, c, i, j, playersChoices[9], noOfChoices;
bool noWinner, playAgain;

void playersTurn(){
	cout << "Please enter your choice;" << endl;
	cin >> p;
	switch(p){
		case 1:
			if(theBoard[0][0] == ' '){
				sqr1 = 'X';
				theBoard[0][0] = sqr1;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 2:
			if(theBoard[0][1] == ' '){
				sqr2 = 'X';
				theBoard[0][1] = sqr2;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 3:
			if(theBoard[0][2] == ' '){
				sqr3 = 'X';
				theBoard[0][2] = sqr3;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 4:
			if(theBoard[1][0] == ' '){
				sqr4 = 'X';
				theBoard[1][0] = sqr4;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 5:
			if(theBoard[1][1] == ' '){
				sqr5 = 'X';
				theBoard[1][1] = sqr5;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 6:
			if(theBoard[1][2] == ' '){
				sqr6 = 'X';
				theBoard[1][2] = sqr6;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 7:
			if(theBoard[2][0] == ' '){
				sqr7 = 'X';
				theBoard[2][0] = sqr7;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 8:
			if(theBoard[2][1] == ' '){
				sqr8 = 'X';
				theBoard[2][1] = sqr8;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 9:
			if(theBoard[2][2] == ' '){
				sqr9 = 'X';
				theBoard[2][2] = sqr9;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
	}
}
void computersTurn(){
	//random square for the comp to choose
	c = rand() % 9 + 1;
	switch(c){
		case 1:
			if((theBoard[0][0] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr1 = 'O';
				theBoard[0][0] = sqr1;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 2:
			if((theBoard[0][1] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr2 = 'O';
				theBoard[0][1] = sqr2;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 3:
			if((theBoard[0][2] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr3 = 'O';
				theBoard[0][2] = sqr3;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 4:
			if((theBoard[1][0] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr4 = 'O';
				theBoard[1][0] = sqr4;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 5:
			if((theBoard[1][1] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr5 = 'O';
				theBoard[1][1] = sqr5;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 6:
			if((theBoard[1][2] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr6 = 'O';
				theBoard[1][2] = sqr6;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 7:
			if((theBoard[2][0] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr7 = 'O';
				theBoard[2][0] = sqr7;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 8:
			if((theBoard[2][1] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr8 = 'O';
				theBoard[2][1] = sqr8;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
		case 9:
			if((theBoard[2][2] == ' ')){
				cout << "\nComputer's choice;" << endl;
				sqr9 = 'O';
				theBoard[2][2] = sqr9;
				noOfChoices++;
			}else{
				computersTurn();
			}
			break;
	}
}
void drawBoard(){
	cout << sqr1 << "|" << sqr2 << "|" << sqr3 << endl;
	cout << "-+-+-" << endl;
	cout << sqr4 << "|" << sqr5 << "|" << sqr6 << endl;
	cout << "-+-+-" << endl;
	cout << sqr7 << "|" << sqr8 << "|" << sqr9 << "\n" << endl;
}
char checkWinner(){
	//check the rows
	for (int i = 0; i < 3; i++){
		if ((theBoard[i][0] == theBoard[i][1]) && (theBoard[i][1] == theBoard[i][2]) && (theBoard[i][0] != ' ')){
			return theBoard[i][0];
		}
	}
	//check the clmns
	for (int i = 0; i < 3; i++){
		 if ((theBoard[0][i] == theBoard[1][i]) && (theBoard[1][i] == theBoard[2][i]) && (theBoard[0][i] != ' ')){
			return theBoard[0][i];
		 }
	}
	//check the diagnals
	if ((theBoard[0][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[2][2]) && (theBoard[0][0] != ' ')){
		return theBoard[0][0];
	}
	if ((theBoard[2][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[0][2]) && (theBoard[2][0] != ' ')) {
		return theBoard[2][0];
	}
}
void decideWinner(){
	char winner = checkWinner();
	if(noOfChoices<9){
		if(winner == 'X'){
			cout << "You won." << endl;
			noWinner=false;
		}else if(winner == 'O'){
			cout << "You lost." << endl;
			noWinner=false;
		}else{
			noWinner=true;
		}
	}else{
		cout << "Its a tie." << endl;
		noWinner=false;
	}
}
int main(){
	srand ( time(NULL));
	playAgain=true;
	while(playAgain){
		for(j=0;j<3;j++){
			for(i=0;i<3;i++){
				theBoard[j][i]=' ';
			}
		}
		sqr1=' ';
		sqr2=' ';
		sqr3=' ';
		sqr4=' ';
		sqr5=' ';
		sqr6=' ';
		sqr7=' ';
		sqr8=' ';
		sqr9=' ';
		noWinner=true;
		noOfChoices=0;
		while(noWinner){
			playersTurn();
			drawBoard();
			decideWinner();
			if(noWinner==false){
				break;
			}
			computersTurn();
			drawBoard();
			decideWinner();
		}
		cout << "Play Again?(Y/N)" << endl;
		cin >> tryAgain;
		if((tryAgain=='Y')||(tryAgain=='y')){
			playAgain=true;
		}else{
			playAgain=false;
		}
	}
	system("PAUSE");
	return 0;
}


This post has been edited by sms123: 17 August 2008 - 06:07 AM

Was This Post Helpful? 0
  • +
  • -

#10 sms123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-August 08

Re: Decision Making

Posted 17 August 2008 - 08:20 AM

right i am having some trouble joining this code together, basically i have a function which accepts a parameter called player which can either be an 'X' or an 'O' and if the parameter is O it will see if the computer can make a winning move, if the parameter is X it can see if it can block the player's winning move, the function will then set two variables a and b to the co-ordinates of the move. (the computer's move is 0 and the player's move is X).

and what i want to do is say, if the function has not set the variables a and b, then move on to the second piece of code i have which checks which squares are avaliable according to the order by "webboy42"
here is the winningMoveFunction;
void checkWinningMove(char player){
	for (int i = 0; i < 3; i++){
		if((theBoard[i][0] == theBoard[i][1])&&(theBoard[i][0] == player)){
		a = i;
		b = 2;
		}else if((theBoard[i][1] == theBoard[i][2])&&(theBoard[i][1] == player)){
		a = i;
		b = 0
	}else if((theBoard[i][0] == theBoard[i][2])&&(theBoard[i][0] == player)){
		a = i;
		b = 1;
	}
	}
	for (int i = 0; i < 3; i++){
		if((theBoard[0][i] == theBoard[1][i])&&(theBoard[0][i] == player)){
		a = 2;
		b = i;
	}else if((theBoard[1][i] == theBoard[2][i])&&(theBoard[1][i] == player)){
		a = 0;
		b = i;
	}else if((theBoard[0][i] == theBoard[2][i])&&(theBoard[0][i] == player)){
		a = 1;
		b = i;
	}
	}
	if((theBoard[0][0] == theBoard[1][1])&&(theBoard[0][0] == player)){
	a = 2;
	b = 2;
	}else if((theBoard[1][1] == theBoard[2][2])&&(theBoard[1][1] == player)){
	a = 0;
	b = 0;
	}else if((theBoard[0][0] == theBoard[2][2])&&(theBoard[0][0] == player)){
	a = 1;
	b = 1;
	}
	if((theBoard[2][0] == theBoard[1][1])&&(theBoard[2][0] == player)){
	a = 0;
	b = 2;
	}else if((theBoard[1][1] == theBoard[0][2])&&(theBoard[1][1] == player)){
	a = 2;
	b = 0;
	}else if((theBoard[2][0] == theBoard[0][2])&&(theBoard[2][0] == player)){
	a = 1;
	b = 1;
	}
}


here is the other piece of code;
	if(theBoard[1][1] == ' '){
		a=1;
		b=1;
	}else if(theBoard[1][0] == ' '){
		a = 1;
		b = 0;
	}else if(theBoard[0][1] == ' '){
		a = 0;
		b = 1;
	}else if(theBoard[2][1] == ' '){
		a = 2;
		b = 1;
	}else if(theBoard[1][2] == ' '){
		a = 1;
		b = 2;
	}


Was This Post Helpful? 0
  • +
  • -

#11 sms123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-August 08

Re: Decision Making

Posted 17 August 2008 - 09:39 AM

come on guys how can i do this, it basically needs to help the computer make a decision so it needs to first see if the computer can make a winning move by calling the function and passing 'O' to it. then it needs to see if the player can make a winning move and block it by calling the function and passing 'X' then if there are no winning moves it needs to do the second piece of code
Was This Post Helpful? 0
  • +
  • -

#12 ibaraku  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 188
  • Joined: 12-May 07

Re: Decision Making

Posted 17 August 2008 - 10:40 AM

View Postsms123, on 17 Aug, 2008 - 09:39 AM, said:

come on guys how can i do this, it basically needs to help the computer make a decision so it needs to first see if the computer can make a winning move by calling the function and passing 'O' to it. then it needs to see if the player can make a winning move and block it by calling the function and passing 'X' then if there are no winning moves it needs to do the second piece of code


Hey man, check out my blog here at DIC, I have a sample TicTacToe program, I think it might help you...
Was This Post Helpful? 0
  • +
  • -

#13 sms123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-August 08

Re: Decision Making

Posted 17 August 2008 - 11:17 AM

View Postibaraku, on 17 Aug, 2008 - 10:40 AM, said:

Hey man, check out my blog here at DIC, I have a sample TicTacToe program, I think it might help you...

hey man thanks for your reply, but yours works with random numbers, mine is working (nearly) with strategy
Was This Post Helpful? 0
  • +
  • -

#14 sms123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-August 08

Re: Decision Making

Posted 17 August 2008 - 12:05 PM

hey i think it is finished, a tic tac toe game which uses strategy to beat the player. here is the code;
#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

char tryAgain, sqr1, sqr2, sqr3, sqr4, sqr5, sqr6, sqr7, sqr8, sqr9, theBoard[3][3];
int a, b, p, c, i, j, playersChoices[9], noOfChoices;
bool noWinner, playAgain, bestMoveFound, winningMoveFound, blockingMoveFound;

void playersTurn(){
	cout << "Please enter your choice;" << endl;
	cin >> p;
	switch(p){
		case 1:
			if(theBoard[0][0] == ' '){
				sqr1 = 'X';
				theBoard[0][0] = sqr1;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 2:
			if(theBoard[0][1] == ' '){
				sqr2 = 'X';
				theBoard[0][1] = sqr2;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 3:
			if(theBoard[0][2] == ' '){
				sqr3 = 'X';
				theBoard[0][2] = sqr3;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 4:
			if(theBoard[1][0] == ' '){
				sqr4 = 'X';
				theBoard[1][0] = sqr4;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 5:
			if(theBoard[1][1] == ' '){
				sqr5 = 'X';
				theBoard[1][1] = sqr5;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 6:
			if(theBoard[1][2] == ' '){
				sqr6 = 'X';
				theBoard[1][2] = sqr6;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 7:
			if(theBoard[2][0] == ' '){
				sqr7 = 'X';
				theBoard[2][0] = sqr7;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 8:
			if(theBoard[2][1] == ' '){
				sqr8 = 'X';
				theBoard[2][1] = sqr8;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
		case 9:
			if(theBoard[2][2] == ' '){
				sqr9 = 'X';
				theBoard[2][2] = sqr9;
				playersChoices[noOfChoices]=p;
				noOfChoices++;
			}else{
				cout << "Position taken." << endl;
				playersTurn();
			}
			break;
	}
}
void checkWinningMove(char player, char otherPlayer){
	bestMoveFound=false;
	winningMoveFound=false;
	blockingMoveFound=false;
	for (int i = 0; i < 3; i++){
		if((theBoard[i][0] == theBoard[i][1])&&(theBoard[i][0] == player)&&(theBoard[i][2] != otherPlayer)){
			a = i;
			b = 2;
			bestMoveFound = true;
			cout << "error" << endl;
		}else if((theBoard[i][1] == theBoard[i][2])&&(theBoard[i][1] == player)&&(theBoard[i][0] != otherPlayer)){
			a = i;
			b = 0;
			bestMoveFound = true;
		}else if((theBoard[i][0] == theBoard[i][2])&&(theBoard[i][0] == player)&&(theBoard[i][1] != otherPlayer)){
			a = i;
			b = 1;
			bestMoveFound = true;
		}
	}
	for (int i = 0; i < 3; i++){
		if((theBoard[0][i] == theBoard[1][i])&&(theBoard[0][i] == player)&&(theBoard[2][i] != otherPlayer)){
		   a = 2;
		   b = i;
		   bestMoveFound = true;
		}else if((theBoard[1][i] == theBoard[2][i])&&(theBoard[1][i] == player)&&(theBoard[0][i] != otherPlayer)){
		   a = 0;
		   b = i;
		   bestMoveFound = true;
		}else if((theBoard[0][i] == theBoard[2][i])&&(theBoard[0][i] == player)&&(theBoard[1][i] != otherPlayer)){
		   a = 1;
		   b = i;
		   bestMoveFound = true;
	   }
	}
	if((theBoard[0][0] == theBoard[1][1])&&(theBoard[0][0] == player)&&(theBoard[2][2] != otherPlayer)){
		a = 2;
		b = 2;
		bestMoveFound = true;
	}else if((theBoard[1][1] == theBoard[2][2])&&(theBoard[1][1] == player)&&(theBoard[0][0] != otherPlayer)){
		a = 0;
		b = 0;
		bestMoveFound = true;
	}else if((theBoard[0][0] == theBoard[2][2])&&(theBoard[0][0] == player)&&(theBoard[1][1] != otherPlayer)){
		a = 1;
		b = 1;
		bestMoveFound = true;
	}
	if((theBoard[2][0] == theBoard[1][1])&&(theBoard[2][0] == player)&&(theBoard[0][2] != otherPlayer)){
		a = 0;
		b = 2;
		bestMoveFound = true;
	}else if((theBoard[1][1] == theBoard[0][2])&&(theBoard[1][1] == player)&&(theBoard[2][0] != otherPlayer)){
		a = 2;
		b = 0;
		bestMoveFound = true;
	}else if((theBoard[2][0] == theBoard[0][2])&&(theBoard[2][0] == player)&&(theBoard[1][1] != otherPlayer)){
		a = 1;
		b = 1;
		bestMoveFound = true;
	}
	if(bestMoveFound){
		if(player=='X'){
			blockingMoveFound = true;
		}else{
			winningMoveFound = true;
		}
	}
}
void computersTurn(){
	checkWinningMove('O','X');
	if(!winningMoveFound){
		checkWinningMove('X','O');
		if(!blockingMoveFound){
			if(theBoard[1][1] == ' '){
				a=1;
				b=1;
			}else if(theBoard[0][0] == ' '){
				a = 0;
				b = 0;
			}else if(theBoard[0][2] == ' '){
				a = 0;
				b = 2;
			}else if(theBoard[2][0] == ' '){
				a = 2;
				b = 0;
			}else if(theBoard[2][2] == ' '){
				a = 2;
				b = 2;
			}else if(theBoard[0][1] == ' '){
				a = 0;
				b = 1;
			}else if(theBoard[1][0] == ' '){
				a = 1;
				b = 0;
			}else if(theBoard[1][2] == ' '){
				a = 1;
				b = 2;
			}else if(theBoard[2][1] == ' '){
				a = 2;
				b = 1;
			}
		}			
	}
	//now using a and b, interpret the computer's move
	//column is the first [] in a 2d array
	cout << "Computer's choice;" << endl;
	cout << "a:" << a << "\nb:" << b << endl;
	if(a==0){
		if(b==0){
			sqr1 = 'O';
			theBoard[0][0] = sqr1;
			noOfChoices++;
		}else if(b==1){
			sqr2 = 'O';
			theBoard[0][1] = sqr2;
			noOfChoices++;
		}else if(b==2){
			sqr3 = 'O';
			theBoard[0][2] = sqr3;
			noOfChoices++;
		}
	}else if(a==1){
		if(b==0){
			sqr4 = 'O';
			theBoard[1][0] = sqr4;
			noOfChoices++;
		}else if(b==1){
			sqr5 = 'O';
			theBoard[1][1] = sqr5;
			noOfChoices++;
		}else if(b==2){
			sqr6 = 'O';
			theBoard[1][2] = sqr6;
			noOfChoices++;
		}
	}else if(a==2){
		if(b==0){
			sqr7 = 'O';
			theBoard[2][0] = sqr7;
			noOfChoices++;
		}else if(b==1){
			sqr8 = 'O';
			theBoard[2][1] = sqr8;
			noOfChoices++;
		}else if(b==2){
			sqr9 = 'O';
			theBoard[2][2] = sqr9;
			noOfChoices++;
		}
	}
}
void drawBoard(){
	cout << sqr1 << "|" << sqr2 << "|" << sqr3 << endl;
	cout << "-+-+-" << endl;
	cout << sqr4 << "|" << sqr5 << "|" << sqr6 << endl;
	cout << "-+-+-" << endl;
	cout << sqr7 << "|" << sqr8 << "|" << sqr9 << "\n" << endl;
}
char checkWinner(){
	//check the rows
	for (int i = 0; i < 3; i++){
		if ((theBoard[i][0] == theBoard[i][1]) && (theBoard[i][1] == theBoard[i][2]) && (theBoard[i][0] != ' ')){
			return theBoard[i][0];
		}
	}
	//check the clmns
	for (int i = 0; i < 3; i++){
		 if ((theBoard[0][i] == theBoard[1][i]) && (theBoard[1][i] == theBoard[2][i]) && (theBoard[0][i] != ' ')){
			return theBoard[0][i];
		 }
	}
	//check the diagnals
	if ((theBoard[0][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[2][2]) && (theBoard[0][0] != ' ')){
		return theBoard[0][0];
	}
	if ((theBoard[2][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[0][2]) && (theBoard[2][0] != ' ')) {
		return theBoard[2][0];
	}
}
void decideWinner(){
	char winner = checkWinner();
	if(noOfChoices<9){
		if(winner == 'X'){
			cout << "You won." << endl;
			noWinner=false;
		}else if(winner == 'O'){
			cout << "You lost." << endl;
			noWinner=false;
		}else{
			noWinner=true;
		}
	}else{
		cout << "Its a tie." << endl;
		noWinner=false;
	}
}
int main(){
	srand ( time(NULL));
	playAgain=true;
	while(playAgain){
		for(j=0;j<3;j++){
			for(i=0;i<3;i++){
				theBoard[j][i]=' ';
			}
		}
		sqr1=sqr2=sqr3=sqr4=sqr5=sqr6=sqr7=sqr8=sqr9=' ';
		noWinner=true;
		noOfChoices=0;
		cout << "Let's play Noughts and Crosses." << endl;
		drawBoard();
		while(noWinner){
			playersTurn();
			drawBoard();
			decideWinner();
			if(noWinner==false){
				break;
			}
			computersTurn();
			drawBoard();
			decideWinner();
		}
		cout << "Play Again?(Y/N)" << endl;
		cin >> tryAgain;
		if((tryAgain=='Y')||(tryAgain=='y')){
			playAgain=true;
		}else{
			playAgain=false;
		}
	}
	return 0;
}


now that v1.0 is finished, i think version 2.0 will have a menu to allow the player to choose if they go first or second and maybe the ability for the player to play against another player rather than the computer. watch this space lol, feel free to copy the code and compile it and try it. if you have any constructive criticism feel free to add to this thread. thanks to all those who helped me, you guys are the best.
Was This Post Helpful? 0
  • +
  • -

#15 webboy42  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-August 08

Re: Decision Making

Posted 17 August 2008 - 05:25 PM

Hi there
I just had a read through your most recent code listing, and just noticed a potential problem I didn't notice before. The PlayersTurn function (excuse me if I spelt it incorrectly) calls itself everytime a move is attempted in a square where a mark already exists, however this has the potential to cause an eventual stack overflow if someone were to keep trying filled squares. Given that you don't use parameters or local variables, it would probably take a very long time to happen.

Just a little something to keep in mind as you continue down the programming road.

Finally, here's a handy piece of code to get a set of co-ordinates for a square.

// This assumes a 3 by 3 grid, with a
// range of 0-2 on the x and y axis.
// This also assumes an input of 1-9.
int y = (input - 1) / 3;
int x = (input - 1) % 3;



Ok, for an explaination, though the code essentially says it all. We take an input value of type int, between 1 and 9. To get values suitable for indexing an array we take 1 from the input before doing the division or remainder calculation. For a setup where each row is represented by 3 adjacent indices an integer division of 3 gives the row number, and the remainder of a division by 3 provides the column. Reverse the calculations where columns are represented by 3 adjacent indices.

It's easy enough to do the reverse, take co-ordinates and get a single integer.

// This assumes 2 integers, x and y, have a value in the range 0-2.
int square = y * 3 + x + 1;



Even easier. If rows are represented by 3 adjacent indices, then multiply the y value by 3 to come up with the first square in a row, and add x+1 to get the end value. Multiply x by 3 and add y+1 if columns are represented by adjacent indices. You might ask why we need to add 1, recall that the co-ordinates taken were array indices, and therefore 0 based, to change the value to be based on 1 the addition is required, obviously the addition must be done after the multiplication.

I hope you enjoy these little tid bits.

Almost forgot, when you remove the redundant sq1..sq9 variables, the mighty switch statements become unnecessary with the formulas I have shown you.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2