New Posts  All Forums:Forum Nav:

Beginner C++ "Tic Tac Toe"

post #1 of 3
Thread Starter 
Hello again! biggrin.gif

I've set myself on a project to see if I can recreate the Tic Tac Toe game inside a Windows Console. So far it's been going well, and I feel like i'm far from finished but there's a small problem that I've run into and sorta need help on. blushsmiley.gif

I have a series of If and nested If statements to deal with user input in terms of what the player chooses to put his mark (X or O) in whichever space he chooses by entering a number from 1 - 9.

Player 1 = 'X';
Player 2 = 'O';

The function works well enough, the problem is that whenever I enter a number higher than the range of squares, for example 88, or 907, It always includes all the numbers separately. So for essence if I enter a number like 123, it includes all the numbers so that it automatically sees each separate digit as an input for a turn. (if 123, it displays X in space 1, O in space 2, and X in space 3, all without "taking turns") I want it to prompt whichever player's turn it is each time instead of shooting through it, by giving them an error and saying to try again if they input a 0, or number greater than 9.

Just for referencing what the board looks like:
1|2|3
4|5|6
7|8|9

Updated Code: (just thought of a new way to do it, but the same problem is there) Warning: Spoiler! (Click to show)
Code:
void PlayerTurn()
{
   char playerMark = 'X';
   char playerInput;
   if (iPlayer == 1)
   {
      cout << "Player 1's Turn!" << endl;
      playerMark = 'X';
      Player1Turn: // Bookmark Player 1
      cin >> playerInput;
     
      if (playerInput == '1' && square1 == '1')
      {
            square1 = 'X';
            iPlayer = 2;
      }
      else if (playerInput == '2' && square2 == '2')
      {
            square2 = 'X';
            iPlayer = 2;
      }
      else if (playerInput == '3' && square3 == '3')
      {
            square3 = 'X';
            iPlayer = 2;
      }
      else if (playerInput == '4' && square4 == '4')
      {
            square4 = 'X';
            iPlayer = 2;
      }
      else if (playerInput == '5' && square5 == '5')
      {
            square5 = 'X';
            iPlayer = 2;
      }
      else if (playerInput == '6' && square6 == '6')
      {
            square6 = 'X';
            iPlayer = 2;
      }
      else if (playerInput == '7' && square7 == '7')
      {
            square7 = 'X';
            iPlayer = 2;
      }
      else if (playerInput == '8' && square8 == '8')
      {
            square8 = 'X';
            iPlayer = 2;
      }
      else if (playerInput == '9' && square9 == '9')
      {
            square9 = 'X';
            iPlayer = 2;
      }
      else
      {
         cout << "Invalid entry! Try again!" << endl;
         goto Player1Turn;
      }
   }
   else
   {
      cout << "Player 2's Turn!" << endl;
      playerMark = 'O';

      Player2Turn: // Bookmark Player 2
      cin >> playerInput;
      if (playerInput == '1' && square1 == '1')
      {
            square1 = 'O';
            iPlayer = 1;
      }
      else if (playerInput == '2' && square2 == '2')
      {
            square2 = 'O';
            iPlayer = 1;
      }
      else if (playerInput == '3' && square3 == '3')
      {
            square3 = 'O';
            iPlayer = 1;
      }
      else if (playerInput == '4' && square4 == '4')
      {
            square4 = 'O';
            iPlayer = 1;
      }
      else if (playerInput == '5' && square5 == '5')
      {
            square5 = 'O';
            iPlayer = 1;
      }
      else if (playerInput == '6' && square6 == '6')
      {
            square6 = 'O';
            iPlayer = 1;
      }
      else if (playerInput == '7' && square7 == '7')
      {
            square7 = 'O';
            iPlayer = 1;
      }
      else if (playerInput == '8' && square8 == '8')
      {
            square8 = 'O';
            iPlayer = 1;
      }
      else if (playerInput == '9' && square9 == '9')
      {
            square9 = 'O';
            iPlayer = 1;
      }
      else
      {
         cout << "Invalid entry! Try again!" << endl;
         goto Player2Turn;
      }
   }
}


My old code: (This is the only file that I used for defining functions and variables, the main cpp I'm thinking of using it for running loops of functions in this page) Warning: Spoiler! (Click to show)
Code:
#ifndef TICTACFUNC_H_
#define TICTACFUNC_H_

using namespace std;

//Space variables
char square1 = '1';
char square2 = '2';
char square3 = '3';
char square4 = '4';
char square5 = '5';
char square6 = '6';
char square7 = '7';
char square8 = '8';
char square9 = '9';
bool iGameOver = false;
short iPlayer = 1;


void PrintBoard()
{
   
   cout << endl;
   cout << square1 << "|" << square2 << "|" << square3 << endl;
   cout << "-+-+-" << endl;
   cout << square4 << "|" << square5 << "|" << square6 << endl;
   cout << "-+-+-" << endl;
   cout << square7 << "|" << square8 << "|" << square9 << endl;
   cout << endl;
   
}

void PlayerTurn()
{
   char playerMark = 'X';
   char playerInput;
   if (iPlayer == 1)
   {
      cout << "Player 1's Turn!" << endl;
      playerMark = 'X';
      Player1Turn: // Bookmark Player 1
      cin >> playerInput;
     
      if (playerInput == '1')
      {
         if (square1 == 'X' || square1 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player1Turn;
         }
         else
         {
            square1 = 'X';
            iPlayer = 2;
         }
      }
      else if (playerInput == '2')
      {
         if (square2 == 'X' || square2 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player1Turn;
         }
         else
         {
            square2 = 'X';
            iPlayer = 2;
         }
      }
      else if (playerInput == '3')
      {
         if (square3 == 'X' || square3 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player1Turn;
         }
         else
         {
            square3 = 'X';
            iPlayer = 2;
         }
      }
      else if (playerInput == '4')
      {
         if (square4 == 'X' || square4 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player1Turn;
         }
         else
         {
            square4 = 'X';
            iPlayer = 2;
         }
      }
      else if (playerInput == '5')
      {
         if (square5 == 'X' || square5 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player1Turn;
         }
         else
         {
            square5 = 'X';
            iPlayer = 2;
         }
      }
      else if (playerInput == '6')
      {
         if (square6 == 'X' || square6 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player1Turn;
         }
         else
         {
            square6 = 'X';
            iPlayer = 2;
         }
      }
      else if (playerInput == '7')
      {
         if (square7 == 'X' || square7 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player1Turn;
         }
         else
         {
            square7 = 'X';
            iPlayer = 2;
         }
      }
      else if (playerInput == '8')
      {
         if (square8 == 'X' || square8 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player1Turn;
         }
         else
         {
            square8 = 'X';
            iPlayer = 2;
         }
      }
      else if (playerInput == '9')
      {
         if (square9 == 'X' || square9 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player1Turn;
         }
         else
         {
            square9 = 'X';
            iPlayer = 2;
         }
      }
      else
      {
         if (playerInput > 9 || playerInput == 0)
         {
         cout << "Invalid entry! Try again!" << endl;
         goto Player1Turn;
         }
      }
   }
   else
   {
      cout << "Player 2's Turn!" << endl;
      playerMark = 'O';

      Player2Turn: // Bookmark Player 2
      cin >> playerInput;
      if (playerInput == '1')
      {
         if (square1 == 'X' || square1 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player2Turn;
         }
         else
         {
            square1 = 'O';
            iPlayer = 1;
         }
      }
      else if (playerInput == '2')
      {
         if (square2 == 'X' || square2 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player2Turn;
         }
         else
         {
            square2 = 'O';
            iPlayer = 1;
         }
      }
      else if (playerInput == '3')
      {
         if (square3 == 'X' || square3 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player2Turn;
         }
         else
         {
            square3 = 'O';
            iPlayer = 1;
         }
      }
      else if (playerInput == '4')
      {
         if (square4 == 'X' || square4 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player2Turn;
         }
         else
         {
            square4 = 'O';
            iPlayer = 1;
         }
      }
      else if (playerInput == '5')
      {
         if (square5 == 'X' || square5 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player2Turn;
         }
         else
         {
            square5 = 'O';
            iPlayer = 1;
         }
      }
      else if (playerInput == '6')
      {
         if (square6 == 'X' || square6 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player2Turn;
         }
         else
         {
            square6 = 'O';
            iPlayer = 1;
         }
      }
      else if (playerInput == '7')
      {
         if (square7 == 'X' || square7 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player2Turn;
         }
         else
         {
            square7 = 'O';
            iPlayer = 1;
         }
      }
      else if (playerInput == '8')
      {
         if (square8 == 'X' || square8 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player2Turn;
         }
         else
         {
            square8 = 'O';
            iPlayer = 1;
         }
      }
      else if (playerInput == '9')
      {
         if (square9 == 'X' || square9 == 'O')
         {
            cout << "Space already occupied! Try again!" << endl;
            goto Player2Turn;
         }
         else
         {
            square9 = 'O';
            iPlayer = 1;
         }
      }
      else
      {
         if (playerInput > 9 || playerInput == 0)
         {
         cout << "Invalid entry! Try again!" << endl;
         goto Player1Turn;
         }
      }
   }
}



#endif


My main.cpp (Right now it doesn't really add to anything in this question, i'm just using it for testing the program along the way for now) Warning: Spoiler! (Click to show)
Code:
#include <iostream>
#include "tictacfunc.h"
using namespace std;

int main()
{
   cout << "Welcome to Tic Tac Toe!" << endl;
   PrintBoard();
   PlayerTurn();
   PrintBoard();
   PlayerTurn();
   PrintBoard();
   PlayerTurn();
   PrintBoard();
   return 0;
}

Thanks in advance for all the help! happysmiley.gif
Edited by Salads - 10/21/12 at 6:16pm
post #2 of 3
I haven't had to work with c++ or java for quite some time now...

But I'm pretty sure its related to asking for a character instead of say an integer.
post #3 of 3
CODE INSIDE! (Click to show)
Code:
[code]void PlayerTurn()
{
        char playerMark;
        char playerInput;
        bool correct;

        if (iPlayer == 1)
        {
                cout << "Player 1's Turn!" << endl;
                playerMark = 'X';
                correct = false;

                while (!correct)
                {
                        playerInput = cin.get();
                        cin.ignore(256, ' ')

                        if (playerInput < 10)
                        {
                                if (playerInput == '1' && square1 == '1')
                                {
                                        square1 = playerMark;
                                        iPlayer = 2;
                                        correct = true;
                                }
                                if (playerInput == '2' && square2 == '2')       
                                {
                                        square2 = playerMark;
                                        iPlayer = 2;
                                        correct = true;
                                }
                                if (playerInput == '3' && square3 == '3')
                                {
                                        square3 = playerMark;
                                        iPlayer = 2;
                                        correct = true;
                                }       
                                if (playerInput == '4' && square4 == '4')
                                {
                                        square4 = playerMark;
                                        iPlayer = 2;
                                        correct = true;
                                }
                                if (playerInput == '5' && square5 == '5')
                                {
                                        square5 = playerMark;
                                        iPlayer = 2;
                                        correct = true;
                                }
                                if (playerInput == '6' && square6 == '6')
                                {
                                        square6 = playerMark;
                                        iPlayer = 2;
                                        correct = true;
                                }
                                if (playerInput == '7' && square7 == '7')
                                {
                                        square7 = playerMark;
                                        iPlayer = 2;
                                        correct = true;
                                }
                                if (playerInput == '8' && square8 == '8')
                                {
                                        square8 = playerMark;
                                        iPlayer = 2;
                                        correct = true;
                                }
                                if (playerInput == '9' && square9 == '9')
                                {
                                        square9 = playerMark;
                                        iPlayer = 2;
                                        correct = true;
                                }
                        }
                        else
                        {
                                cout << "Invalid entry! Try again!" << endl;
                        }

                }
        }
        
        else
        {
                cout << "Player 2's Turn!" << endl;
                playerMark = 'O';
                correct = false;

                while (!correct)
                {
                        playerInput = cin.get();
                        cin.ignore(256, ' ')

                        if (playerInput < 10)
                        {
                                if (playerInput == '1' && square1 == '1')
                                {
                                        square1 = playerMark;
                                        iPlayer = 1;
                                        correct = true;
                                }
                                if (playerInput == '2' && square2 == '2')
                                {
                                        square2 = playerMark;
                                        iPlayer = 1;
                                        correct = true;
                                }
                                if (playerInput == '3' && square3 == '3')
                                {
                                        square3 = playerMark;
                                        iPlayer = 1;
                                        correct = true;
                                }       
                                if (playerInput == '4' && square4 == '4')
                                {
                                        square4 = playerMark;
                                        iPlayer = 1;
                                        correct = true;
                                }
                                if (playerInput == '5' && square5 == '5')
                                {
                                        square5 = playerMark;
                                        iPlayer = 1;
                                        correct = true;
                                }
                                if (playerInput == '6' && square6 == '6')
                                {
                                        square6 = playerMark;
                                        iPlayer = 1;
                                        correct = true;
                                }
                                if (playerInput == '7' && square7 == '7')
                                {
                                        square7 = playerMark;
                                        iPlayer = 1;
                                        correct = true;
                                }
                                if (playerInput == '8' && square8 == '8')
                                {
                                        square8 = playerMark;
                                        iPlayer = 1;
                                        correct = true;
                                }
                                if (playerInput == '9' && square9 == '9')
                                {
                                        square9 = playerMark;
                                        iPlayer = 1;
                                        correct = true;
                                }
                        }
                        else
                        {
                                cout << "Invalid entry! Try again!" << endl;
                        }

                }
        }
}
[/code]

This is how I would do it. There might be some errors because I made it with notepad
Edited by nikolauska - 10/30/12 at 10:06am
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming