#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

using namespace std;

bool isP1;
enum results{xWins, oWins, draw, goOn};

void compMove(const char board[3][3]);
void drawBoard(const char board[3][3]);
void getMove(char board[3][3]);
void getMove2(char board[3][3]);
results getResults(const char board[3][3]);
void displayResults(results result, const char board[3][3]);

int main()
{
  
  srand ( time(NULL) );
  results result;
  char ans;
  int move;
  int cpMv;
  int numPlyrs;
    do
    {
  system("cls");
  char board[3][3] = {{' ', ' ', ' '},
		      {' ', ' ', ' '},
		      {' ', ' ', ' '}};
  do
  {
	cout <<"Would you like to play 1 or 2 player?";
	cin >> numPlyrs;
  }while(numPlyrs != 1 && numPlyrs != 2);
  
  do
  {
    if(numPlyrs == 1)
    {
        isP1 = true;
        break;
    }
	cout <<"Would you like to be x or o?\n";
	cin >> ans;
  if(ans == 'x')
	isP1 = true;
  else if(ans == 'o')
	isP1 = false;
  else
  {
	cout <<"That is not a valid choice\n";
	ans = 'r';
  }
  }while(ans == 'r');
  
  if(numPlyrs == 1)
  {
  
  do
    {
		getMove2(board);
		drawBoard(board);
		result = getResults(board);
		displayResults(result,board);
	}while(result == goOn);
   }
  else if(numPlyrs == 2)
  {
  do
    {
      getMove(board);
      drawBoard(board);
      result = getResults(board);
      displayResults(result,board);
    }while(result == goOn);
   }
    cout <<"Play again? (y/n)\n";
    cin >>ans;
    }while(ans == 'y' || ans == 'Y');
  
  return 0;  
}

void drawBoard(const char board[3][3])
{
  cout << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;
  cout << "--+---+--\n";
  cout << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;
  cout << "--+---+--\n";
  cout << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;
}   

void getMove(char board[3][3])
{
  int move;
  cout << "Where would you like to move?\n";
  cout << "7 | 8 | 9\n";
  cout << "--+---+--\n";
  cout << "4 | 5 | 6\n";
  cout << "--+---+--\n";
  cout << "1 | 2 | 3\n";
  cin >> move;
  system("cls");

  if (move == 7)
    {
      if (board[0][0] == 'o' || board[0][0] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[0][0] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[0][0] = 'o';
	      isP1 = !isP1;
		}
	}
    }
  else if (move == 8)
    {
      if (board[0][1] == 'o' || board[0][1] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[0][1] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[0][1] = 'o';
	      isP1 = !isP1;
		}
	}
    }
  else if (move == 9)
    {
      if (board[0][2] == 'o' || board[0][2] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[0][2] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[0][2] = 'o';
	      isP1 = !isP1;
		}
	}
    }
  else if (move == 4)
    {
      if (board[1][0] == 'o' || board[1][0] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[1][0] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[1][0] = 'o';
	      isP1 = !isP1;
		}
	}
    }
  else if (move == 5)
    {
      if (board[1][1] == 'o' || board[1][1] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[1][1] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[1][1] = 'o';
	      isP1 = !isP1;
		}
	}
    }
  else if (move == 6)
    {
      if (board[1][2] == 'o' || board[1][2] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[1][2] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[1][2] = 'o';
	      isP1 = !isP1;
		}
	}
    }
  else if (move == 1)
    {
      if (board[2][0] == 'o' || board[2][0] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[2][0] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[2][0] = 'o';
	      isP1 = !isP1;
		}
	}
    }
  else if (move == 1)
    {
      if (board[2][0] == 'o' || board[2][0] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[2][0] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[2][0] = 'o';
	      isP1 = !isP1;
		}
	}
    }
 else if (move == 2)
    {
      if (board[2][1] == 'o' || board[2][1] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[2][1] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[2][1] = 'o';
	      isP1 = !isP1;
		}
	}
    }
 else if (move == 3)
    {
      if (board[2][2] == 'o' || board[2][2] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[2][2] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[2][2] = 'o';
	      isP1 = !isP1;
		}
	}
    }
}
		  
	      
void getMove2(char board[3][3])
{
  int move;
  
  if(isP1)
  {
  cout << "Where would you like to move?\n";
  cout << "7 | 8 | 9\n";
  cout << "--+---+--\n";
  cout << "4 | 5 | 6\n";
  cout << "--+---+--\n";
  cout << "1 | 2 | 3\n";
  cin >> move;
  }
  
  else if(!isP1)
  { 
    if((board[2][0] == 'x' && board[1][0] == 'x')
        ||(board[2][2] == 'x' && board[1][1] == 'x')
        ||(board[0][1] == 'x' && board[0][2] == 'x'))
        if(board[0][0] != 'x' && board[0][0] != 'o')
            move = 7;
        else
            move = rand() % 9 + 1;
            
    else if((board[0][0] == 'x' && board[0][2] == 'x')
            ||(board[1][1] == 'x' && board[2][1] == 'x'))
        if(board[0][1] != 'x' && board[0][1] != 'o')
            move = 8; 
        else
            move = rand() % 9 + 1;  
    
    else  if((board[0][0] == 'x' && board[0][1] == 'x')
            ||(board[2][0] == 'x' && board[1][1] == 'x')
            ||(board[2][2] == 'x' && board[1][2] == 'x'))
        if(board[0][2] != 'x' && board[0][2] != 'o')
            move = 9;
        else
            move = rand() % 9 + 1;  
        
    else if((board[1][1] == 'x' && board[1][2] == 'x')
            ||(board[0][0] == 'x' && board[2][0] == 'x'))
        if(board[1][0] != 'x' && board[1][0] != 'o')
            move = 4;
        else
            move = rand() % 9 + 1;  
        
    else if((board[0][0] == 'x' && board[2][2] == 'x')
            ||(board[1][0] == 'x' && board[1][2] == 'x')
            ||(board[2][0] == 'x' && board[0][2] == 'x')
            ||(board[2][1] == 'x' && board[0][1] == 'x'))
        if(board[1][1] != 'x' && board[1][1] != 'o')
            move = 5;
        else
            move = rand() % 9 + 1;  
        
    else if((board[1][0] == 'x' && board[1][1] == 'x')
            ||(board[0][2] == 'x' && board[2][2] == 'x'))
        if(board[1][2] != 'x' && board[1][2] != 'o')
            move = 6;
        else
            move = rand() % 9 + 1;  
        
    else if((board[0][0] == 'x' && board[1][0] == 'x')
           ||(board[0][2] == 'x' && board[1][1] == 'x')
           ||(board[2][1] == 'x' && board[2][2] == 'x'))
        if(board[2][0] != 'x' && board[2][0] != 'o')
            move = 1;
        else
            move = rand() % 9 + 1;  
        
    else if((board[0][1] == 'x' && board[1][1] == 'x')
            ||(board[2][0] == 'x' && board[2][2] == 'x'))
        if(board[2][1] != 'x' && board[2][1] != 'o')
            move = 2;
        else
            move = rand() % 9 + 1;  
        
    else if((board[0][2] == 'x' && board[1][2] == 'x')
            ||(board[0][0] == 'x' && board[1][1] == 'x')
            ||(board[2][0] == 'x' && board[2][1] == 'x'))
        if(board[2][2] != 'x' && board[2][2] != 'o')
            move = 3;
        else
            move = rand() % 9 + 1;    
            
    if((board[2][0] == 'o' && board[1][0])
        ||(board[2][2] == 'o' && board[1][1] == 'o')
        ||(board[0][1] == 'o' && board[0][2] == 'o'))
        if(board[0][0] != 'x' && board[0][0] != 'o')
            move = 7;
        else
            move = rand() % 9 + 1;
            
    else if((board[0][0] == 'o' && board[0][2] == 'o')
            ||(board[1][1] == 'o' && board[2][1] == 'o'))
        if(board[0][1] != 'x' && board[0][1] != 'o')
            move = 8; 
        else
            move = rand() % 9 + 1;  
    
    else  if((board[0][0] == 'o' && board[0][1] == 'o')
            ||(board[2][0] == 'o' && board[1][1] == 'o')
            ||(board[2][2] == 'o' && board[1][2] == 'o'))
        if(board[0][2] != 'x' && board[0][2] != 'o')
            move = 9;
        else
            move = rand() % 9 + 1;  
        
    else if((board[1][1] == 'o' && board[1][2] == 'o')
            ||(board[0][0] == 'o' && board[2][0] == 'o'))
        if(board[1][0] != 'x' && board[1][0] != 'o')
            move = 4;
        else
            move = rand() % 9 + 1;  
        
    else if((board[0][0] == 'o' && board[2][2] == 'o')
            ||(board[1][0] == 'o' && board[1][2] == 'o')
            ||(board[2][0] == 'o' && board[0][2] == 'o')
            ||(board[2][1] == 'o' && board[0][1] == 'o'))
        if(board[1][1] != 'x' && board[1][1] != 'o')
            move = 5;
        else
            move = rand() % 9 + 1;  
        
    else if((board[1][0] == 'o' && board[1][1] == 'o')
            ||(board[0][2] == 'o' && board[2][2] == 'o'))
        if(board[1][2] != 'x' && board[1][2] != 'o')
            move = 6;
        else
            move = rand() % 9 + 1;  
        
    else if((board[0][0] == 'o' && board[1][0] == 'o')
           ||(board[0][2] == 'o' && board[1][1] == 'o')
           ||(board[2][1] == 'o' && board[2][2] == 'o'))
        if(board[2][0] != 'x' && board[2][0] != 'o')
            move = 1;
        else
            move = rand() % 9 + 1;  
        
    else if((board[0][1] == 'o' && board[1][1] == 'o')
            ||(board[2][0] == 'o' && board[2][2] == 'o'))
        if(board[2][1] != 'x' && board[2][1] != 'o')
            move = 2;
        else
            move = rand() % 9 + 1;  
        
    else if((board[0][2] == 'o' && board[1][2] == 'o')
            ||(board[0][0] == 'o' && board[1][1] == 'o')
            ||(board[2][0] == 'o' && board[2][1] == 'o'))
        if(board[2][2] != 'x' && board[2][2] != 'o')
            move = 3;
        else
            move = rand() % 9 + 1;           
     
    else
    {
        move = rand() % 9 + 1;
    }
}
  system("cls");
  
  if (move == 7)
    {
      if (board[0][0] == 'o' || board[0][0] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[0][0] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[0][0] = 'o';
	      isP1 = !isP1;
		}
	}
    }
  else if (move == 8)
    {
      if (board[0][1] == 'o' || board[0][1] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[0][1] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[0][1] = 'o';
	      isP1 = !isP1;
		}
	}
    }
  else if (move == 9)
    {
      if (board[0][2] == 'o' || board[0][2] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[0][2] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[0][2] = 'o';
	      isP1 = !isP1;
		}
	}
    }
  else if (move == 4)
    {
      if (board[1][0] == 'o' || board[1][0] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[1][0] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[1][0] = 'o';
	      isP1 = !isP1;
		}
	}
    }
  else if (move == 5)
    {
      if (board[1][1] == 'o' || board[1][1] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[1][1] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[1][1] = 'o';
	      isP1 = !isP1;
		}
	}
    }
  else if (move == 6)
    {
      if (board[1][2] == 'o' || board[1][2] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[1][2] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[1][2] = 'o';
	      isP1 = !isP1;
		}
	}
    }
  else if (move == 1)
    {
      if (board[2][0] == 'o' || board[2][0] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[2][0] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[2][0] = 'o';
	      isP1 = !isP1;
		}
	}
    }
  else if (move == 1)
    {
      if (board[2][0] == 'o' || board[2][0] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[2][0] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[2][0] = 'o';
	      isP1 = !isP1;
		}
	}
    }
 else if (move == 2)
    {
      if (board[2][1] == 'o' || board[2][1] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[2][1] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[2][1] = 'o';
	      isP1 = !isP1;
		}
	}
    }
 else if (move == 3)
    {
      if (board[2][2] == 'o' || board[2][2] == 'x')
	{
	  cout <<"Someone is on this spot already\n";
	}
      else
	{
	  if (isP1)
	    {
	      board[2][2] = 'x';
	      isP1 = !isP1;
	    }
	  else
	    {
	      board[2][2] = 'o';
	      isP1 = !isP1;
		}
	}
    }
}

		  
results getResults(const char board[3][3])
{
  if ((board[0][0] == 'x' && board[0][1] == 'x' && board[0][2] == 'x')
      || (board[0][0] == 'x' && board[1][1] == 'x' && board[2][2] == 'x')
      || (board[0][2] == 'x' && board[1][1] == 'x' && board[2][0] == 'x')
      || (board[0][0] == 'x' && board[1][0] == 'x' && board[2][0] == 'x')
      || (board[0][1] == 'x' && board[1][1] == 'x' && board[2][1] == 'x')
      || (board[0][2] == 'x' && board[1][2] == 'x' && board[2][2] == 'x')
      || (board[1][0] == 'x' && board[1][1] == 'x' && board[1][2] == 'x')
      || (board[2][0] == 'x' && board[2][1] == 'x' && board[2][2] == 'x'))
    return xWins;

  else if ((board[0][0] == 'o' && board[0][1] == 'o' && board[0][2] == 'o')
	   || (board[0][0] == 'o' && board[1][1] == 'o' && board[2][2] == 'o')
	   || (board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o')
	   || (board[0][0] == 'o' && board[1][0] == 'o' && board[2][0] == 'o')
	   || (board[0][1] == 'o' && board[1][1] == 'o' && board[2][1] == 'o')
	   || (board[0][2] == 'o' && board[1][2] == 'o' && board[2][2] == 'o')
	   || (board[1][0] == 'o' && board[1][1] == 'o' && board[1][2] == 'o')
	   || (board[2][0] == 'o' && board[2][1] == 'o' && board[2][2] == 'o'))
    return oWins;

  else if ((board[0][0] != ' ' && board[1][0] != ' ' && board[2][0] != ' ')
	   && (board[0][1] != ' ' && board[1][1] != ' ' && board[2][1] != ' ')
	   && (board[0][2] != ' ' && board[1][2] != ' ' && board[2][2] != ' '))
    return draw;
  
  else
    return goOn;
}

void displayResults(results result, const char board[3][3])
{
  if(result == oWins)
    cout <<"O wins!\n";
  
  else if(result == xWins)
    cout <<"X wins!\n";
  
  else if(result == draw)
    cout <<"You tied!\n";
}
3
Contributors
2
Replies
20
Views
5 Years
Discussion Span
Last Post by PalashBansal96
1

That is a lot of if-else statements. Think you can rewrite this with a more logical approach?

Note: This is not to bash you! If you can think of creating this with another approach, it may help with other types of OOP. Consider using a class for each player as either X or O, and using enum. ::hint hint:: :D
2nd Note: Oh, sorry. This was posted ~2 weeks ago. Likely should not have replied to this thread by now.

Edited by Saith

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.