6 Replies - 783 Views - Last Post: 07 January 2011 - 04:48 AM Rate Topic: -----

#1 AvL  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 21-October 10

Tic Tac Toe with classes

Posted 06 January 2011 - 10:59 AM

My program consists of a gameboard(a .bmp)that , when clicked , a value is set in a matrix (1 for x, 2 for o). The board is 5by5 to 9by9 and winning is when a player has clicked 5 squares, not 3;
The project is in c++Builder 6;

The function that is supposed to return 1 if x wins and 2 if o wins is :
//---------------------------------------------------------------
int Game:: PlayerWins(int ct[10]){ //ct defined globally
        int i,j;
       
        for(i=0;i<n;i++)
                for(j=0;j<n;j++){
                        if(T[i][j]==T[i][j+1]==1) ct[0]++;    //1 aka x
                        else ct[0]=0;

                        if(T[i][j]==T[i][j+1]==2) ct[1]++;     //2
                        else ct[1]=0;

                        if((i==j)&&(T[i][j]==T[i+1][j+1]==1)) ct[2]++;   //1
                        else ct[2]=0;

                        if((i==j)&&(T[i][j]==T[i+1][j+1]==2)) ct[3]++;      //2
                        else ct[3]=0;
                }
        for(j=0;j<n;j++)
                for(i=0;i<n;i++){

                        if(T[i][j]==T[i+1][j]==1) ct[4]++;              //1
                        else ct[4]=0;

                        if(T[i][j]==T[i+1][j]==2) ct[5]++;                   //2
                        else ct[5]=0;

                        if(T[i][4-i]==T[i+1][4-i-1]==1) ct[6]++;           //1
                        else ct[6]=0;

                        if(T[i][4-i]==T[i+1][4-i-1]==1) ct[7]++;            //2
                        else ct[7]=0;
                }

        if ((ct[0]==4)||(ct[2]==4)||(ct[4]==4)||(ct[6]==4))      return 1;
        if ((ct[1]==4)||(ct[3]==4)||(ct[5]==4)||(ct[7]==4))      return 2;
}


//---------------------------------------------------------------
The problem is that only the first row seems to work ; any other possible winning group(on a column, or diagonally ) is not "seen" by the function.
I would be very grateful if anyone can spot the mistakes i've made.Thank you !

This post has been edited by AvL: 06 January 2011 - 11:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe with classes

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Tic Tac Toe with classes

Posted 06 January 2011 - 11:03 AM

Please edit your post and add code tags :code: This will make it a lot easier on us.
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1302
  • View blog
  • Posts: 4,485
  • Joined: 19-February 09

Re: Tic Tac Toe with classes

Posted 06 January 2011 - 08:48 PM

Comparing the next element seems unnecessary,

if(T[i][j]==T[i][j+1]...

Also, if you have 5 in a row and the next is not the same, the counter will be reset.

The following creates a local array called ct, which may cause confusion with the global version.
int Game:: PlayerWins(int ct[10]){ //ct defined globally


What about creating a function for checking rows etc. to simplify the code and logic.
Was This Post Helpful? 1
  • +
  • -

#4 AvL  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 21-October 10

Re: Tic Tac Toe with classes

Posted 07 January 2011 - 02:39 AM

Thank you for the suggestions and corrections !!
I've tried creating functions foe each case but it seemed to be more code ,so the last version of the function (which obviously doesn't work) is :

int Game:: PlayerWins(){
        int i,j;
        for(i=0;i<9;i++)
                for(j=0;j<9;j++){
                        if((T[i][j]==T[i][j+1]==T[i][j+2]==T[i][j+3]==T[i][j+4])&&(T[i][j]!=0))
                                return T[i][j];                 //ROWS

                        if((T[i][j]==T[i+1][j]==T[i+2][j]==T[i+3][j]==T[i+4][j])&&(T[i][j]!=0))
                                return T[i][j];                 //COLUMNS

                        if((T[i][j]==T[i+1][j+1]==T[i+2][j+2]==T[i+3][j+3]==T[i+4][j+4])&&(T[i][j]!=0))
                                return T[i][j];                 // diagonally like this"\\\"

                        if(j>=4){
                                if((T[i][j]==T[i+1][j-1]==T[i+2][j-2]==T[i+3][j-3]==T[i+4][j-4])&&(T[i][j]!=0))
                                        return T[i][j];         //diagonally like this "///"
                                }
                        }
        return 0;
}



The problem I have now is that after the first click it says that a player has won even though the double array has all elements 0 except one (the first click)...

Any thoughts on what i'm missing ?
Thank you !
Was This Post Helpful? 0
  • +
  • -

#5 AvL  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 21-October 10

Re: Tic Tac Toe with classes

Posted 07 January 2011 - 03:39 AM

The if(S) are wrong ..i just figured it out ...

By the way , can posts be deleted ?(as i no longer need a response to this one , only if you have a bullet proof tic tac toe program to share :D )

I.ll come back later ! Bye !
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: Tic Tac Toe with classes

Posted 07 January 2011 - 04:07 AM

We don't delete posts unless it's some sort of rules violation, which this clearly is not.
Was This Post Helpful? 0
  • +
  • -

#7 AvL  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 21-October 10

Re: Tic Tac Toe with classes

Posted 07 January 2011 - 04:48 AM

Ok, thanks for the info !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1