Like Tree1Likes
  • 1 Post By Elysia

Write a two-player tic-tac-toe game; use enums when possible to represent the values

This is a discussion on Write a two-player tic-tac-toe game; use enums when possible to represent the values within the C++ Programming forums, part of the General Programming Boards category; Here is the code I have written so far. My problem is that when the second user enters its position ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    36

    Write a two-player tic-tac-toe game; use enums when possible to represent the values

    Here is the code I have written so far. My problem is that when the second user enters its position the last user's position is wiped of the board. I want to know how I can hold that position and keep doing so until the game is finished. I thought that calling the previous function would do that (and you can see where I have put that into a comment) but it doesn't. thanks

    Code:
    #include <iostream> //includes header file
    using namespace std;
    
    
    //function prototypes
    void printLeftUpper(int i, int j);
    void printMiddleUpper(int i, int j);
    //delcares enumaration
    enum NaughtsCrossesSquare {
    leftUpper,
    middleUpper,
    rightUpper,
    leftMiddle,
    middle,
    rightMiddle,
    leftLower,
    middleLower,
    rightLower,
    };
    
    
    //start of main body of fcn
    int main()
    {
        int i=0; //delcares and initialises integers
        int NC;
        int playerTurn;
    
    
        //prints out tic tac toe square
        cout << leftUpper << "|" << middleUpper << "|" << rightUpper << endl;
        cout << "-----" << endl;
        cout << leftMiddle << "|" << middle << "|" << rightMiddle << endl;
        cout << "-----" << endl;
        cout << leftLower << "|" << middleLower << "|" << rightLower << endl;
        cout << endl;
        //Expalnes games dynamics
        cout << "Two players to play naughts and crosses \n" << endl;
        cout << "Player one is X's, Player two is O's" << endl;
    
    
        while(i<9) //start of while loop incremneting to 9 by 1 each time
        {
            cout << "Player one enter 1 or player two enter 2 to play" << endl; //gets players to confirm which user is playing
            cin >> playerTurn;
            cout << "Please select from table which position you would like" << endl; //prompts user for input and stores it
            cin >> NC;
            switch (NC) //start of swtich fcn
            {
            case leftUpper:                                        //determines which case selected and calls fcn
                cout << "You have selected leftUpper" << endl;
                printLeftUpper(playerTurn,NC);
                //break;
            case middleUpper:
                cout << "You have selected middleUpper" << endl;
                printMiddleUpper(playerTurn,NC);
                break;
            case rightUpper:
                cout << "You have selected rightUpper" << endl;
                break;
            case leftMiddle:
                cout << "You have selected leftMiddle" << endl;
                break;
            case middle:
                cout << "You have selected middle" << endl;
                break;
            case rightMiddle:
                cout << "You have selected rightMiddle" << endl;
                break;
            case leftLower:
                cout << "You have selected leftLower" << endl;
                break;
            case middleLower:
                cout << "You have selected middleLower" << endl;
                break;
            case rightLower:
                cout << "You have selected rightLower" << endl;
                break;
            }
         i++;
        }
    
    
    }
    
    
    void printMiddleUpper (int i,int j) //start of fcn
    {
        char playerMark1 = 'X';
        char playerMark2 = 'O';
        if (i==1&&j==1) //if first player move puts a X in place
        {
            cout << leftUpper << "|" << playerMark1 << "|" << rightUpper << endl;
            cout << "-----" << endl;
            cout << leftMiddle << "|" << middle << "|" << rightMiddle << endl;
            cout << "-----" << endl;
            cout << leftLower << "|" << middleLower << "|" << rightLower << endl;
            cout << endl;
        }
        else if (i==2&&j==1) //if second players move puts a O in place
        {
            cout << leftUpper << "|" << playerMark2 << "|" << rightUpper << endl;
            cout << "-----" << endl;
            cout << leftMiddle << "|" << middle << "|" << rightMiddle << endl;
            cout << "-----" << endl;
            cout << leftLower << "|" << middleLower << "|" << rightLower << endl;
            cout << endl; ;
        }
        //printLeftUpper(i,j); //thought that this would hold previous result
    }
    
    
    void printLeftUpper (int i,int j) //start of fcn
    {
        char playerMark1 = 'X';
        char playerMark2 = 'O';
        if (i==1&&j==0) //if first player move puts a X in place
        {
            cout << playerMark1 << "|" << middleUpper << "|" << rightUpper << endl;
            cout << "-----" << endl;
            cout << leftMiddle << "|" << middle << "|" << rightMiddle << endl;
            cout << "-----" << endl;
            cout << leftLower << "|" << middleLower << "|" << rightLower << endl;
            cout << endl;
        }
        else if (i==2&&j==0) //if second players move puts a O in place
        {
            cout << playerMark2 << "|" << middleUpper << "|" << rightUpper << endl;
            cout << "-----" << endl;
            cout << leftMiddle << "|" << middle << "|" << rightMiddle << endl;
            cout << "-----" << endl;
            cout << leftLower << "|" << middleLower << "|" << rightLower << endl;
            cout << endl;
        }
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    You seem to be confusing what enum is. It is essentially a type which can only take on values that you have specified. It isn't a variable and is not meant to hold state.
    What is probably expected is that you create some array to hold the state of the board. Now, since the board can only hold very specific amount of states, this is where you can use an enum.
    stahta01 likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. write() and read() int values from socket returning wrong values.
    By Premjith P S in forum Linux Programming
    Replies: 8
    Last Post: 11-29-2012, 01:59 PM
  2. C++ Video Game Player Program
    By zonen in forum C++ Programming
    Replies: 33
    Last Post: 10-01-2011, 06:12 PM
  3. Replies: 5
    Last Post: 11-27-2010, 11:42 AM
  4. Basic structure for 2 player game
    By pants in forum C Programming
    Replies: 2
    Last Post: 05-10-2009, 05:16 PM
  5. Replies: 9
    Last Post: 11-11-2007, 01:31 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21