Click here to Skip to main content
15,891,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include "GUI.cpp"
using namespace std;

void display( char board[3][3][3][3] )
{
    for( int R=0; R<3; R++ )
    {
        for( int r=0; r<3; r++ )
        {
            for( int C=0; C<3; C++ )
            {
                for( int c=0; c<3; c++ )
                {
                    setColor(RGBI(1,0,0,1),RGBI(1,1,0,1));
                    cout << board[R][C][r][c];
                    setColor(RGBI(1,1,1,0),RGBI(0,0,0,0));
                }
                cout <<" ";
            }
            cout << endl;
        }
        cout << endl;
    }
    setColor(RGBI(1,1,1,0),RGBI(0,0,0,0));
    cout <<"Click on ";
    setColor(RGBI(1,0,0,1),RGBI(1,1,0,1));
    cout <<"highlighted cell";
    setColor(RGBI(1,1,1,0),RGBI(0,0,0,0));
    cout <<" above using the [Mouse]"<< endl;
}

bool mouseToBoardCoord( MouseClick const& m, int& R, int& C, int& r, int& c )
{
    // Player clicked on first big row?
    if( (0 <= m.x) && (m.x < 3) && (0 <= m.y) && (m.y < 3) )
    {
        R = 0;
        C = 0;

        if     ( m.x == 0 )c = 0;
        else if( m.x == 1 )c = 1;
        else if( m.x == 2 )c = 2;

        if     ( m.y == 0 )r = 0;
        else if( m.y == 1 )r = 1;
        else if( m.y == 2 )r = 2;
    }
    else if( (4 <= m.x) && (m.x < 7) && (0 <= m.y) && (m.y < 3) )
    {
        R = 0;
        C = 1;

        if     ( m.x == 4 )c = 0;
        else if( m.x == 5 )c = 1;
        else if( m.x == 6 )c = 2;

        if     ( m.y == 0 )r = 0;
        else if( m.y == 1 )r = 1;
        else if( m.y == 2 )r = 2;
    }
    else if( (8 <= m.x) && (m.x < 11) && (0 <= m.y) && (m.y < 3) )
    {
        R = 0;
        C = 2;

        if     ( m.x == 8  )c = 0;
        else if( m.x == 9  )c = 1;
        else if( m.x == 10 )c = 2;

        if     ( m.y == 0  )r = 0;
        else if( m.y == 1  )r = 1;
        else if( m.y == 2  )r = 2;
    }

    // Player clicked on second big row?
    else if( (0 <= m.x) && (m.x < 3) && (4 <= m.y) && (m.y < 7) )
    {
        R = 1;
        C = 0;

        if     ( m.x == 0 )c = 0;
        else if( m.x == 1 )c = 1;
        else if( m.x == 2 )c = 2;

        if     ( m.y == 4 )r = 0;
        else if( m.y == 5 )r = 1;
        else if( m.y == 6 )r = 2;
    }
    else if( (4 <= m.x) && (m.x < 7) && (4 <= m.y) && (m.y < 7) )
    {
        R = 1;
        C = 1;

        if     ( m.x == 4 )c = 0;
        else if( m.x == 5 )c = 1;
        else if( m.x == 6 )c = 2;

        if     ( m.y == 4 )r = 0;
        else if( m.y == 5 )r = 1;
        else if( m.y == 6 )r = 2;
    }
    else if( (8 <= m.x) && (m.x < 11) && (4 <= m.y) && (m.y < 7) )
    {
        R = 1;
        C = 2;

        if     ( m.x == 8  )c = 0;
        else if( m.x == 9  )c = 1;
        else if( m.x == 10 )c = 2;

        if     ( m.y == 4  )r = 0;
        else if( m.y == 5  )r = 1;
        else if( m.y == 6  )r = 2;
    }

    // Player clicked on third big row?
    else if( (0 <= m.x) && (m.x < 3) && (8 <= m.y) && (m.y < 11) )
    {
        R = 2;
        C = 0;

        if     ( m.x == 0  )c = 0;
        else if( m.x == 1  )c = 1;
        else if( m.x == 2  )c = 2;

        if     ( m.y == 8  )r = 0;
        else if( m.y == 9  )r = 1;
        else if( m.y == 10 )r = 2;
    }
    else if( (4 <= m.x) && (m.x < 7) && (8 <= m.y) && (m.y < 11) )
    {
        R = 2;
        C = 1;

        if     ( m.x == 4  )c = 0;
        else if( m.x == 5  )c = 1;
        else if( m.x == 6  )c = 2;

        if     ( m.y == 8  )r = 0;
        else if( m.y == 9  )r = 1;
        else if( m.y == 10 )r = 2;
    }
    else if( (8 <= m.x) && (m.x < 11) && (8 <= m.y) && (m.y < 11) )
    {
        R = 2;
        C = 2;

        if     ( m.x == 8  )c = 0;
        else if( m.x == 9  )c = 1;
        else if( m.x == 10 )c = 2;

        if     ( m.y == 8  )r = 0;
        else if( m.y == 9  )r = 1;
        else if( m.y == 10 )r = 2;
    }

    // Player clicked outside our 9x9 board?
    else
    {
        return false;
    }

    // Player clicked inside our 9x9 board
    return true;
}

int main()
{
    char board[3][3][3][3] = {};
    bool FirstTime = true;
    int prev_r;
    int prev_c;

    while(true)
    {
        system("CLS");
        display(board);

        MouseClick m = getMouseClick();
        int                     R,C,r,c;
        if( mouseToBoardCoord(m,R,C,r,c) )
        {
//
// neeed help in this section
//
           if (FirstTime)
            {
                 board[R][C][r][c] = 'X';
                 FirstTime = false;
                 prev_r = r;
                 prev_c = c;
            }
            else
            {
             if ( R == prev_r && C == prev_c)
             {
                 board[R][C][r][c] = 'o';
                 prev_r = r;
                 prev_c = c;// need help at here!! 
             }


              }
             }
            }


        }


i dont knw how to code for the alternative X and O and for the win / lose/ tie
hope to get ur answer as soon as possible and some explanation on it. really very appreciate
Posted
Updated 4-Sep-14 6:28am
v3
Comments
Richard MacCutchan 4-Sep-14 12:30pm    
You need to provide more details about what is not working for us to help you. And why are you using an 81 square board (3x3x3x3)?
Member 11059211 4-Sep-14 12:39pm    
i dont know how to code for the player to click "X" and "O" alternatively.
For now when i run my program~ the 2nd n third move are the same is "O" symbol~
and the game is designed like this is because my lecturer required~
the game is when u click on the middle of the box the next player muz click on any small box in the big middle, after that if the 2nd player click on the left side of the small box in middle box the first player muz continue the game at the big left box.
sorry if it is hard to understand.. or maybe can juz to help me to solve out the alternative part?? tq very much
Richard MacCutchan 4-Sep-14 12:45pm    
Sorry, I can't really understand that. I can only suggest you try drawing things out on paper in order to figure out the logic of the problem.
Member 11059211 4-Sep-14 12:52pm    
ok~ anyway thx for trying to understand my program and try to help me out~ appreciate it~

1 solution

In such a game, if count the moves of both users then (suppose we start with 1)
  • if count is odd then the current move is an 'X' one.

on the other hand
  • if count is even then the current move is an 'O' one.


Changing your code for counting moves and act according is almost trivial.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900