Game help

This is a discussion on Game help within the C++ Programming forums, part of the General Programming Boards category; hello i am having trouble creating a function that determins a winner in tictactoe : so far what ive tried ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    1

    Game help

    hello i am having trouble creating a function that determins a winner in tictactoe : so far what ive tried doesnt do any thing , please help?

    source code:
    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    
    // i still cannot get the program to determin a winnerr
    
    class TICTACTOE
    {
        int xplayer ;
        int oplayer;
        colour Cs;
        string sp;
        string ps;
        
    public:
        void Board();// will display the main part of the game
        void score();// this will keep the score
        bool checkwin();
        void win();
        void HumanTCT();//this will determine human input
        //void ComputerTCT();//this will alllow the computer to be player 2
       
        
         ;
    };
    
    
    
    void TICTACTOE::HumanTCT()
    {
        char Map[10] = {'o','1','2','3','4','5','6','7','8','9'};
    
        char x = 'x';
        char o = 'o';
    
        char mone = '1';
        char mtwo = '2';
        char mthree = '3';
        char mfour = '4';
        char mfive = '5';
        char msix = '6';
        char mseven = '7';
        char meight = '8';
        char mnine = '9';
    
        int counter = 0;
        int xplayer = 1;
        int oplayer = 1;
    
        cout << " player 1 please enter your name :";
        cin >> sp;
    
        cout << " player 2 please enter your name :";
        cin >> ps;
           
           
        while ( xplayer = 1) {
            counter = counter + 1;
    
        cout << "player one:"<<sp<<" Enter the number on the board  please ?";
        cin >> mone;
    
        if ( mone == '1') {
    
            Map[1] = 'x';
            system("cls");
             cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
    
        
    
        }else if ( mone == '2') {
            Map[2] = 'x';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
    
        
        }else if ( mone == '3') {
            Map[3] = 'x';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
        
        
    }
    
        else if ( mone == '4') {
            Map[4] = 'x';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
        
        
        }else if ( mone == '5') {
            Map[5] = 'x';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
        
        
        }else if ( mone == '6') {
            Map[6] = 'x';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
        
        
        }else if ( mone == '7') {
            Map[7] = 'x';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
    
        
        }else if ( mone == '8') {
            Map[8] = 'x';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
        
        
        }else if ( mone == '9') {
            Map[9] = 'x';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
        }
        else{
            cout<< " you have entered something wrong , try again " << endl; 
        
    }
        
        cout << "Player 2"<<ps<<" Enter a number on the  board  please ?";
        cin >> mtwo;
    
        if ( mtwo == '1') {
    
            Map[1] = 'o';
            system("cls");
             cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
    
        
    
        }else if ( mtwo == '2') {
            Map[2] = 'o';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
        
        
        }else if ( mtwo == '3') {
            Map[3] = 'o';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
        
        
    }
    
        else if ( mtwo == '4') {
            Map[4] = 'o';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
    
        
        }else if ( mtwo == '5') {
            Map[5] = 'o';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
        
        
        }else if ( mtwo == '6') {
            Map[6] = 'o';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
        
        
        }else if ( mtwo == '7') {
            Map[7] = 'o';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
        
        
        }else if ( mtwo == '8') {
            Map[8] = 'o';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
        
        
        }else if ( mtwo == '9') {
            Map[9] = 'o';
            system("cls");
                     cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
    
        
        }
        else{
            cout<< " you have entered something wrong , try again " << endl;
    
        }
        checkwin();
        }
    }
    
        
    bool TICTACTOE::checkwin()
        {
            bool win = false;
            char Map[10] = {'o','1','2','3','4','5','6','7','8','9'};
            for(int i = 0 ; i < 10 ; i++)
            if (Map[1] == 'X' && Map[2] == 'X' && Map[3] ==  'X' ||Map[1] == 'X' && Map[4] == 'X' && Map[7] == 'X' ||  Map[2] == 'X' && Map[5] == 'X' && Map[8] == 'X' ||  Map[4] == 'X' && Map[5] == 'X' && Map[6] == 'X' ||  Map[3] == 'X' && Map[6] == 'X' && Map[9] == 'X' ||  Map[3] == 'X' && Map[5] == 'X' && Map[7] == 'X' ||  Map[7] == 'X' && Map[8] == 'X' && Map [9] == 'X') 
            {
            win = true ;
            break;
        }
            for(int i = 0 ; i < 10 ; i++)
            if (Map[1] == 'o' && Map[2] == 'o' && Map[3] ==  'o' ||Map[1] == 'o' && Map[4] == 'o' && Map[7] == 'o' ||  Map[2] == 'o' && Map[5] == 'o' && Map[8] == 'o' ||  Map[4] == 'o' && Map[5] == 'o' && Map[6] == 'o' ||  Map[3] == 'o' && Map[6] == 'o' && Map[9] == 'o' ||  Map[3] == 'o' && Map[5] == 'o' && Map[7] == 'o' ||  Map[7] == 'o' && Map[8] == 'o' && Map [9] == 'o') 
        
            win = true ;
            return win;
        
                
        }
        
        void TICTACTOE::win()
        {
            checkwin();
            cout<< " YOU HAVE ONE THE GAME YAYYYY " <<endl;
        }
    
    
    void TICTACTOE::Board ()
    {
        char Map[10] = {'o','1','2','3','4','5','6','7','8','9'};
        
         
     
          cout << "      user must choose a number on the board to play    \n";
       
        cout << "        |     |     \n"; 
        cout << "     " << Map[1] << "  |  " << Map[2] << "  |  " << Map[3] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[4] << "  |  " << Map[5] << "  |  " << Map[6] << endl;
        cout << "   _____|_____|_____\n"; 
        cout << "        |     |     \n"; 
        cout << "     " << Map[7] << "  |  " << Map[8] << "  |  " <<Map[9] << endl;
        cout << "        |     |     \n\n";
        
    }
    
    int main()
    {
        char Map[10] = {'o','1','2','3','4','5','6','7','8','9'};
        
        cout << "\nWelcome To The Awesome Game Of\n\t MEGA Tic Tac Toe\n\n";
        cout << "Player 1 uses X  and  Player 2(computer) uses O" << endl << endl;
        cout << " to play the game the user must select the number on  the board where they want to place their pieces"<<endl;
    
    
        TICTACTOE T;
        T.Board();
        T.HumanTCT();
        bool checkwin();
        T.win();
       
    
    
        
        return 0;
    }
    Last edited by Salem; 04-29-2012 at 04:31 AM. Reason: removed excess tags

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Please please please, take lines 69 to 77 and put them into a function that you call from the various places where you've naughtily copy and pasted code.
    There's no sense making your program four times longer than it should be.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    To determine your winner perhaps something like stepping through every position from top left and checking if any of the neighbour squares contain the same figure as the current square. If so you need to record the 'direction' the neighbour is in then skip to that neighbour and check if it also has another touching it in the same direction, if so it is three in a row, and a win.
    You will need to check that your search is within array bounds each time.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-18-2010, 09:42 PM
  2. Should I use a game engine in making a RPG game?
    By m3rk in forum Game Programming
    Replies: 6
    Last Post: 01-26-2009, 03:58 AM
  3. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 09:53 AM
  4. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 06:58 PM
  5. Game Designer vs Game Programmer
    By the dead tree in forum Game Programming
    Replies: 8
    Last Post: 04-28-2005, 09:17 PM

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