Please comment on my c++ game

This is a discussion on Please comment on my c++ game within the Game Programming forums, part of the General Programming Boards category; hey guys. I just started with c++ in hopes of slowly becomming a game programmer and i finished a game ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    47

    Please comment on my c++ game

    hey guys. I just started with c++ in hopes of slowly becomming a game programmer and i finished a game of Tic Tac Toe as instructed from a forum member. I didn't look up any programming tips on the process of creating tic tac toe (or any game theories) so my game might seem to be not too 'kosher'.
    please check out my game. supposedly there shouldn't be any way of winning. and if theres a tie game, the program doesn't end (which i could easily fix, but im in a bit of a rush.) please let me know.

    a) your thoughts of the game (any comments)
    b) what the proper way to make a tic tac toe game would be (perhaps link)
    c) where i should go next from here? or what i should do next to further understand game programming.

    Code:
    #include <iostream>
    using namespace std;
    
    void resetTable(char pTable[3][3]);
    void printTable(char pTable[3][3]);
    char checkTable(char pTable[3][3]);
    void cmpp(char pTable[3][3]);
    
    int main() {
        int clm, row;
        char tTable[3][3];
        char winner = 'a';
    
        resetTable(tTable);
        printTable(tTable);
    
        do{
            cout<<"Enter the row number then hit enter: ";
            cin>> row;
            cout<<"\nEnter the column number then hit enter: ";
            cin>> clm;
            if (tTable[row-1][clm-1] == ' ' && row < 4 && row > 0 && clm < 4 && clm > 0){
                tTable[row-1][clm-1] = 'x';
                cmpp(tTable);
                printTable(tTable);
                winner = checkTable(tTable);
            }
            else {
                cout<< "\nthat spot is either taken or is not a valid input.\nTRY AGAIN\n\n";
            }
        }while (winner == 'a');
        cout << "\n\nthe winner is: " << winner;
        cin.get();
        return 0;
    }
    
    
    
    /////////////////Computer AI////////////////
    void cmpp(char p2Table[3][3]){
        char check = 'a';
        int checktww = 0;
    ///////////// if Computer is about to win. then take that spot ///////////////
        if (check == 'a'){
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    if (p2Table[i][j] == ' '){
                        p2Table[i][j] = 'o';
                        check = checkTable(p2Table);
                        if (check == 'o'){
                            p2Table[i][j] = 'o';
                            break;
                        }
                        else {
                            p2Table[i][j] = ' ';
                            check = 'a';
                        }
                    }
                    if (check != 'a'){
                        break;
                    }
                }
                if (check != 'a'){
                    break;
                }
            }
        }
    ///////////// If player is about to win. take the spot that stops him ///////////////
    
        if (check == 'a'){
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    if (p2Table[i][j] == ' '){
                        p2Table[i][j] = 'x';
                        check = checkTable(p2Table);
                        if (check == 'x'){
                            p2Table[i][j] = 'o';
                            break;
                        }
                        else {
                            p2Table[i][j] = ' ';
                            check = 'a';
                        }
                    }
                    if (check != 'a'){
                        break;
                    }
                }
                if (check != 'a'){
                    break;
                }
            }
        }
    ///////////////Checks if cener square is taken////////////////
        if (p2Table[1][1] == ' ' && check == 'a'){
            cin.get();
            p2Table[1][1] = 'o';
            check = 'o';
        }
    ////////////Checks if user picked 2 diagnal oposing squares////////////
        else if (p2Table[0][0] == 'x' && p2Table[2][2] == 'x' && check == 'a' || p2Table[2][0] == 'x' && p2Table[0][2] == 'x' && check == 'a'){
            cin.get();
            p2Table[0][1] = 'o';
            check = 'o';
        }
    
    //////////Checks if any posibility for a two way win for Player///////////
        else if (check == 'a'){
            cin.get();
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    checktww = 0;
                    if (p2Table[i][j] == ' '){
                        p2Table[i][j] = 'x';
                        for (int k = 0; k < 3; k++) {
                            for (int l = 0; l < 3; l++) {
                                if (p2Table[k][l] == ' '){
                                    p2Table[k][l] = 'x';
                                    check = checkTable(p2Table);
                                    if (check == 'x'){
                                        checktww = checktww + 1;
                                        if (checktww == 2){
                                            p2Table[i][j] = 'o';
                                            p2Table[k][l] = ' ';
                                            check = 'o';
                                            break;
                                        }
                                    }
                                    if (p2Table[i][j] == 'o'){
                                        p2Table[k][l] = ' ';
                                        break;
                                    }
                                    check = 'a';
                                    p2Table[k][l] = ' ';
                                }
                            }
                        }
                        if (check != 'a'){
                            break;
                        }
                        p2Table[i][j] = ' ';
                    }
                    if (check != 'a'){
                        break;
                    }
                }
                if (check != 'a'){
                    break;
                }
            }
        }
    ////////////all else fails pick first available spot///////////
        if (check ==  'a'){
            for (int i = 0; i < 3; i++){
                for (int j = 0; j < 3; j++){
                    if (p2Table[i][j] == ' '){
                        check = 'o';
                        p2Table[i][j] = 'o';
                        break;
                    }
                }
                if (check != 'a'){
                    break;
                }
            }
        }
    }
    
    /////////////Looks for a winner///////////////////
    char checkTable(char pTable[3][3]){
        char wnr = 'a';
        for (int i = 0; i < 3; i++){
            if (pTable[i][0] == pTable[i][1] && pTable[i][0] == pTable[i][2] && pTable[i][0] != ' '){
                wnr = pTable[i][0];
                break;
            }
            else if (pTable[0][i] == pTable[1][i] && pTable[0][i] == pTable[2][i] && pTable[0][i] != ' '){
                wnr = pTable[0][i];
                break;
            }
        }
            if (pTable[0][0] == pTable[1][1] && pTable[0][0] == pTable[2][2] && pTable[0][0] != ' '){
                wnr = pTable[0][0];
            }
            else if (pTable[2][0] == pTable[1][1] && pTable[2][0] == pTable[0][2] && pTable[2][0] != ' '){
                wnr = pTable[2][0];
            }
        return wnr;
    }
    
    //////////////////////incase you want to start the game over/////////////
    void resetTable(char pTable[3][3]) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                pTable[i][j] = ' ';
            }
        }
    }
    
    ///////////////////display Tic Tac Toe Table///////////////////
    void printTable(char pTable[3][3]) {
        system("CLS");
        cout<<"  1  2  3\n";
        cout<<" =========\n";
        for (int i = 0; i < 3; i++) {
            cout<< i+1 ;
            for (int j = 0; j < 3; j++) {
                cout << "|"<<pTable[i][j]<<"|";
            }
            cout<<"\n =========\n";
        }
    }
    
    
    
    ////////I Hope I'm Done//////////

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably should make more use of temporary copies of the board [not a big problem with a 3 x 3 board anyways, and use the "winner" function to indicate if the current move leads to a win for either side. You can then recursively create a play until the end, and see which move is most likely for either side to make a win.

    Even if you don't use that idea, the cmpp() function is definitely too long/complex.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3

  4. #4
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    the board should be represented by a dynamic char array for efficiency:
    Code:
    char** pBoard = new char*[3];
    for (int i = 0; i < 3; ++i)
    {
    	pBoard[i] = new char[3]
    }
    so now you pass the array into a parameter as:

    Code:
    void MakeMove(char** board, /*other parameters if needed*/)
    {
    	//handle making move
    }
    and since you used dynamic memory be sure to include a function that deletes it:
    Code:
    void FreeMemory(char** pBoard)
    {
    	//Does the board exist ??
    	if(pBoard)
    	{
    		for(int i = 0; i < 3; ++i)
    		{
    			//Does the row exist??
    			if(pBoard[i])
    			{
    				//Delete it 
    				delete [] pBoard[i]
    			}
    			//Delete the columns 
    			delete [] pBoard;
    		}
    	}
    }
    if you feel that you dont wanna manage the memory yourself you could STL vector

    Code:
    vector<char> board;
    where each element represent a position


    Last edited by Hussain Hani; 01-20-2008 at 06:24 PM.
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    Try indenting code you post - even if you use spaces for indentation. We wouldn't want to confuse everyone who reads your example code!
    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.

  6. #6
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    Quote Originally Posted by Elysia View Post
    Try indenting code you post - even if you use spaces for indentation. We wouldn't want to confuse everyone who reads your example code!
    Oh sorry I will right now. When I posted i was in a hurry and i didn't use any text-editin program to tab the code
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    char** pBoard = new char*[3];
    Why not:

    Code:
    char *pBoard = new char[9];
    Less confusing and more straightforward since the board is 9 elements.

    ...the board should be represented by a dynamic char array for efficiency:...
    I fail to see how a dynamic array is more efficient in the context of this program.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    Quote Originally Posted by Hussain Hani View Post
    Oh sorry I will right now. When I posted i was in a hurry and i didn't use any text-editin program to tab the code
    Well, I think you should think about it next time, anyway
    Indentation is not optional - it is required! So just indent with a few spaces if you can't use tabs. It's better than nothing.
    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.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    If you get into the habit of indenting now then it will be like second nature later. At this point in my C/C++ journey I cringe inside every time I see unindented code. It sticks out like a sore thumb and a very ugly one.

  10. #10
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    i always indent my code, but as i said, i didnt have time . Anyway, let's not get far away from the original subject.

    Less confusing and more straightforward since the board is 9 elements.
    It's the same for me

    I fail to see how a dynamic array is more efficient in the context of this program.
    So the board 'lives' in the whole program
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    Quote Originally Posted by Hussain Hani View Post
    i always indent my code, but as i said, i didnt have time .
    But that should not even be an issue. When you indent, you do so while you write the code and not afterwards. If you didn't have time, then the code would be half-done and not lacking indentation.
    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. So you want to be a game programmer?
    By dxfoo in forum Game Programming
    Replies: 23
    Last Post: 09-26-2006, 08:38 AM
  2. C++ Game of Life Program
    By rayrayj52 in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 03:58 PM
  3. Try my game
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-15-2004, 11:58 AM
  4. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  5. Game Design Topic #1 - AI Behavior
    By TechWins in forum Game Programming
    Replies: 13
    Last Post: 10-11-2002, 10:35 AM

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