3

I am making a Tic Tac Toe game and i created a function that inserts X or O into my array. I have run into one problem with my design. I call the function to make a move for X, but when it is the next players turn how do i make it call for O?

Is there a way after i put makeMove() i can just call somehow it to take in O turn instead of X. Because as you can see if i do X it will just always ask for X and not O. How can i make it choose to pull in X or O turn.

The problem is i need to only have one function that makes moves.

int main()
{
    while(SOME CONDITION HERE)
    {
        printBoard();
        cout << "Player X please choose a position: ";
        makeMove('X');
        cout << "Player O please choose a position: ";
        makeMove('O');
    }
}


int makeMove(char marker)
{
    int choosePosition = 0;

    cin >> choosePosition;

    ticTacBoard[choosePosition - 1] = marker;
}
soniccool
  • 5,790
  • 22
  • 60
  • 98
  • 4
    Your program stucture is a little off. You shouldn't really be calling main() from anywhere, let alone a function that is called by main() itself. This program will never end. – jedwards Nov 07 '11 at 23:00
  • Oh this was only meant as an example – soniccool Nov 07 '11 at 23:01
  • the way your doing it, you consider moves in pairs, and I guess it works fine since a tic tac toe game can only go so long... i'd never have thought to write it that way because the "infinite loop" alarm would go off for me, but I think your way works... – Aaron Anodide Nov 07 '11 at 23:01
  • Note that StackOverflow has several discussions of tic-tac-toe you can see by visiting the tag. You can even do a sub-search to see which ones are specifically expressed in C++ http://stackoverflow.com/questions/tagged/c%2b%2b%20tic-tac-toe – HostileFork says dont trust SE Nov 07 '11 at 23:14
  • Recursive, nice. Illegal in C++: the `main` function cannot be called by other functions. – Thomas Matthews Nov 08 '11 at 01:19
  • @Thomas There is nothing "nice" about this solution. Recursion is ugly and slow, and so completely unnecessary in this case as to be considered a serious logic bug. – user229044 Nov 08 '11 at 04:39

5 Answers5

1

Start with this:

int main()
{
    while(SOME CONDITION HERE)
    {
        printBoard();
        cout << "Player X please choose a position: ";
        makeMove('X');
        cout << "Player O please choose a position: ";
        makeMove('O');
    }
}


int makeMove(char marker)
{
    int choosePosition = 0;

    cin >> choosePosition;

    ticTacBoard[choosePosition - 1] = marker;
}

Note that you're going to want to change the SOME CONDITION HERE part, but you could quickly replace it by 1 and get the same behavior of your current script (actually, a bit better).

But you'll eventually want to put something there that makes sense -- something that will tell the program to stop prompting the players for positions and, say, declare a winner.


The following is just a more streamlined way of doing the same thing:

int main()
{
    while(SOME CONDITION HERE)
    {
        printBoard();

        makeMove('X');
        makeMove('O');
    }
}


int makeMove(char marker)
{
    cout << "Player " << marker << " please choose a position: ";

    int choosePosition = 0;

    cin >> choosePosition;

    ticTacBoard[choosePosition - 1] = marker;

    return 0;
}

Note the added return 0 -- if you don't want to return something, you should just make makeMove return void so as not to be confusing.

jedwards
  • 29,432
  • 3
  • 65
  • 92
0

You might try using an argument:

int makeMove(char player);

makeMove('O');
makeMove('X');
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

First of all, don't call main() recursively. Use a loop instead.

Secondly, use a variable (such as player below) to indicate whose turn it is.

int main()
{
    char player = 'X';
    while (/* game not finished */) {
      printBoard();
      makeMove(player);
      player = (player == 'X') ? 'O' : 'X';
    }
}

void makeMove(char player)
{
    cout << "Player " << player << " please choose a position: ";
    int choosePosition = 0;
    cin >> choosePosition;
    ticTacBoard[choosePosition - 1] = player;
}
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Something like this may work... just be sure to use a loop for the moves.

char player = 'X';

while(...) {
  cout << "choose position...";
  makeMove(player);

  if(player == 'X')
    player = 'O';
  else
    player = 'X';
  ...
}

//in make move:
int makeMove(char player) {
  int choosePosition = 0;
  cin >> choosePosition;
  ticTacBoard[choosePosition - 1] = player;
}
aleph_null
  • 5,766
  • 2
  • 24
  • 39
0

http://scripts.franciscocharrua.com/javascript/tic-tac-toe/

I added this to my website a few months ago. I admit it may be a bit complex, and in JavaScript, but it may be of some help.

JS:

function tic_tac_toe(blank_token, player_tokens, artificial_intelligence)
{         
         this.board = [[blank_token, blank_token, blank_token],
                   [blank_token, blank_token, blank_token],
                   [blank_token, blank_token, blank_token]];

     this.blank_token = blank_token;
     this.player_tokens = player_tokens;         
     this.display_choice = function() {};
     this.declare_human_win = function() {};
     this.declare_computer_win = function() {};
     this.declare_tie = function() {};         
     this.artificial_intelligence = artificial_intelligence; 

     this.start = 
     function() 
     {                  
              //Randomly choose a token for the human player.
              var human_token_index = Math.floor(Math.random() * this.player_tokens.length);
              this.human_player = this.player_tokens[human_token_index];

              //Place the chosen token at the end of the array.
              this.player_tokens[human_token_index] = this.player_tokens[this.player_tokens.length - 1];
              this.player_tokens[this.player_tokens.length - 1] = this.human_player;

              //Randomly choose a different token for the computer player.
              var computer_token_index = Math.floor(Math.random() * (this.player_tokens.length - 1));
              this.computer_player = this.player_tokens[computer_token_index];                                    

              //Clear the board.
              for(var row = 0; row < 3; row++)
              for(var collumn = 0; collumn < 3; collumn++)
              {
                  this.place(this.blank_token, row, collumn);
              }   

              if(Math.random() < 0.5)
              {                    
                 this.turn = this.computer_player;
                 this.computer_turn();
              }
              else
              {
                 this.turn = this.human_player;
              }                           
     };

     //Returns the token of the winning player. 
     //If no one has won yet or the game is tied, returns the blank token.
     //Used in combination with blank_token_count() to determine if the game is tied.
     this.winner =
     function()
     {
              var winner = this.blank_token;

              //Check for 3 consecutive horisontal tokens.
              for(var row = 0; row < 3; row++)
              {
                  winner = this.board[row][0];
                  for(var collumn = 1; collumn < 3; collumn++)
                  {
                      if(this.board[row][collumn] != winner)
                      {
                         winner = this.blank_token;
                      }                        
                  }

                  if(winner != this.blank_token)
                  {
                     return(winner);
                  }                      
              }

              //Check for 3 consecutive vertical tokens.
              for(var collumn = 0; collumn < 3; collumn++)
              {
                  winner = this.board[0][collumn];
                  for(var row = 1; row < 3; row++)
                  {
                      if(this.board[row][collumn] != winner)
                      {
                         winner = this.blank_token;
                      }                               
                  }

                  if(winner != this.blank_token)
                  {
                     return(winner);
                  }                                            
              }

             //Check for 3 consecutive diagonal tokens.
             winner = this.board[0][0];
             for(var row = 1; row < 3; row++)
             {
                 if(this.board[row][row] != winner)
                 {
                    winner = this.blank_token;
                 }
             }

             if(winner != this.blank_token)
     {
        return(winner);
             }

             winner = this.board[0][2];
             for(var row = 1; row < 3; row++)
             {
                 if(this.board[row][2 - row] != winner)
                 {
                    winner = this.blank_token;
                 }
             }

             if(winner != this.blank_token)
     {
        return(winner);
             }

             return(winner);
     };                

     this.blank_token_count =
     function()
     {
              var blank_token_count = 0;

              for(var row = 0; row < 3; row++)
              for(var collumn = 0; collumn < 3; collumn++)
              {
                  if(this.board[row][collumn] == this.blank_token)
                  {
                     blank_token_count++;
                  }
              }

              return(blank_token_count);
     };

     this.computer_turn =
     function()
     {  
             //Lets the computer take its turn if the game is not over.
             if(this.turn != this.blank_token)
             {
                this.turn = this.computer_player;
                var computer_move = this.artificial_intelligence();
                this.place(this.computer_player, computer_move.row, computer_move.collumn);                                  
             }
     };

     this.human_turn = 
     function(row, collumn)
     {
             this.place(this.human_player, row, collumn);
             this.computer_turn();
     }

     this.place =
     function(token, row, collumn)
     {
              if(row < 3 && collumn < 3 && 
                ((this.turn == token && this.board[row][collumn] == this.blank_token) || token == this.blank_token))
              {                     
                 this.board[row][collumn] = token;
                 this.display_choice(token, row, collumn)

                 //Finishes the game in case a of a win or a tie.
                 //When the board is not being reset.
                 if(token != this.blank_token)
                 {
                    var winner_token = this.winner(); 

                    if(winner_token == this.human_player)
                    {
                       this.declare_human_win();
                       this.turn = this.blank_token;
                    }

                    if(winner_token == this.computer_player)
                    {
                       this.declare_computer_win();
                       this.turn = this.blank_token;
                    }

                    if(winner_token == this.blank_token && this.blank_token_count() == 0)
                    {
                       this.declare_tie();
                       this.turn = this.blank_token;
                    }

                    //Gives the human player a turn, if the game is not over.
                    if(this.turn == this.computer_player)
                    {
                       this.turn = this.human_player
                    }
                 }
              }
     };

}

sdo
  • 652
  • 3
  • 13
Frank
  • 459
  • 3
  • 9
  • You should copy your solution here. If the link breaks the solution continues on the network. – Rego Nov 08 '11 at 00:37