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;
}

New Topic/Question
Reply




MultiQuote





|