# Tic Tac Toe with classes

Page 1 of 1

## 6 Replies - 1191 Views - Last Post: 07 January 2011 - 04:48 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=208607&amp;s=17a6f01c4bc3e06fba9ec46583375857&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 AvL

Reputation: 1
• 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

• XNArchitect

Reputation: 1361
• Posts: 3,534
• 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 This will make it a lot easier on us.

### #3 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• 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.

### #4 AvL

Reputation: 1
• 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 !

### #5 AvL

Reputation: 1
• 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 )

I.ll come back later ! Bye !

• Saucy!

Reputation: 6246
• Posts: 24,014
• 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.

### #7 AvL

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

## Re: Tic Tac Toe with classes

Posted 07 January 2011 - 04:48 AM

Ok, thanks for the info !

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }