
-----------------------------------
Fonzie
Wed Dec 07, 2005 3:40 am

difficulty with a piece of code, tic-tac-toe related
-----------------------------------
borland compiler

so I'm nearly done a tic tac toe game and I realize that towards the middle of the game I either inexplicably win or lose. meaning the game thinks that I or the computer has gotten 3 in a row when clearly we have not. I've looked thoroughly through the program and can't seem to find any errors (it's possible I missed them I'm friggen tired). at any rate, since I can't find an error I can only assume that the part of my program that checks to see if I or the computer has 3 in a row is faulty.

int count1;
int count2;
int count3;
int ans;
int win1;
win1=0;
count1=-1;
ans = 0;
while (count1!=4&& ans!=15){
count1+=1;
count2=-1;
while (count2!=4&& ans!=15){
count2+=1;
count3=-1;
while (count3!=4&& ans!=15){
count3+=1;
if (count1!=count2 && count2!=count3 && count1!= count3){
ans = human[count1] + human[count2] + human[count3];}

}
}
}
if (ans == 15){
win1=1;}
if (ans == 15){
win1=1;}
ans=0;
count1=-1;
while (count1!=4&& ans!=15){
count1+=1;
count2=-1;
while (count2!=4&& ans!=15){
count2+=1;
count3=-1;
while (count3!=4&& ans!=15){
count3+=1;
if (count1!=count2 && count2!=count3 && count1!= count3){
ans = computer[count1] + computer[count2] + computer[count3];}
}
}
}
if (ans == 15){
win1=2;}
return win1;
}

human and computer are both 5 place global arrays which hold the value each square that was taken has. by value I mean each square has a value that is stored in the array of the person that took the square. it looks like this.               2|9|4
                                    7|5|3
                                    6|1|8
so that any three in a row will add up to 15. I believed that this piece of program checked every combination of three numbers from human and computer's 5 place array. if any combination of three numbers added to 15, the game would end and it would say win or lose. Does this piece of code do what I think it does? and if not, how can I fix it so that it does?

a speedy reply would be greatly appreciated.

-----------------------------------
Fonzie
Wed Dec 07, 2005 3:46 am


-----------------------------------
sorry to double post I can't find edit. the whole code is a function. and 

2|9|4
7|5|3
6|1|8

is a model of the tic tac toe board showing which square is given what value.

-----------------------------------
Andy
Wed Dec 07, 2005 2:25 pm


-----------------------------------
why are u using numbers? cant a simple character do the job? '1' for first player '2' for second player and '0' for empty

-----------------------------------
Fonzie
Wed Dec 07, 2005 2:51 pm


-----------------------------------
I do to represent which spaces are taken. I must of not been clear. The numbers are also stored along with an array called taken which holds something like what you've descibed. the point of the numbers is that you'll notice that any three in a row add up to 15. for example the first row 2|9|4
2+9+4=15
or the diagonal, 4,5,6 which the sum of is also 15.
this is how the program identifies when a game has been won.

-----------------------------------
wtd
Wed Dec 07, 2005 2:57 pm


-----------------------------------
I do to represent which spaces are taken. I must of not been clear. The numbers are also stored along with an array called taken which holds something like what you've descibed. the point of the numbers is that you'll notice that any three in a row add up to 15. for example the first row 2|9|4
2+9+4=15
or the diagonal, 4,5,6 which the sum of is also 15.
this is how the program identifies when a game has been won.

All of the rows and diagonals add up to 15.  When was the game won?  Who won it?

-----------------------------------
Fonzie
Wed Dec 07, 2005 3:08 pm


-----------------------------------
there are two arrays; human and computer. lets say on turn one the human picks the top right corner. human[0] is assigned the value 4. then on the computers turn it picks the middle. computer[0] is assigned value 5. then this repeats on the second turn except that instead of 0 in the square brackets, it'll be one. the code I've shown here is the part of the program that determines if any three numbers in either the array human or computer adds to 15 (this is run after every turn). if this happens on the computer array, the computer wins, if it happens on the human array the human wins.

-----------------------------------
Geminias
Wed Dec 07, 2005 10:03 pm


-----------------------------------
hey there.  if you want help than being polite is a good way to start.  Good manners include using a good use of indentation in your submitted code, that way people can actually make sense of it.  And please take advantage of the syntax highlighting that this forum offers.. add the tag  before your code and  after your code.  


Ex.  

    code 

Also, use comments instead of writing a big blurb after your code.  So comment places where you use tricky logic, or anything that might not be obvious to an experienced c++ programmer.  

if you re-submit your code politely i'll be glad to take a look at it.

-----------------------------------
Geminias
Wed Dec 07, 2005 10:06 pm


-----------------------------------
oops.. it went and did the code.  i meant to show you the tags for displaying c++ code.  

so here's a way i know will work.  


Let x be: syntax="cpp"

[x]  and then [/x] to end ur code... (actually x - the ="cpp" part) lol

-----------------------------------
Geminias
Wed Dec 07, 2005 10:22 pm


-----------------------------------
and i'm just curious.. do you realize that in this piece of code:


NOT decrement count3 by one.  you declare all kinds of variables equal to -1 in your while loops.. i'm guessing this is a mistake because it looks very reduntant to me...

can i see a full source of your tick tac toe game also?

-----------------------------------
Fonzie
Wed Dec 07, 2005 11:00 pm


-----------------------------------
thanks for the help all the same but I've been able to crack my errors and fix the problems. and yes I was declaring them to -1 rather than a decrement, and if it looked redundant, well, my programming practices aren't perfect yet as I only recently started c++.

As for the issue of how to post things, I will use the proper etiquette next time.

-----------------------------------
[Gandalf]
Wed Dec 07, 2005 11:40 pm


-----------------------------------
Just in case you want to know, to use C++ syntax highlightning you type:
code goes here

-----------------------------------
[Gandalf]
Wed Dec 07, 2005 11:42 pm


-----------------------------------
Argh!  I made just the mistake I was trying to avoid...  Sorry about that.

You type this:
[syntax="cpp"]code goes here[/syntax]

-----------------------------------
Geminias
Thu Dec 08, 2005 5:35 pm


-----------------------------------
i began to write a tic-tac-toe game based on your idea.. and i just can't get the check function to operate properly.  

This is an example of a more succinct way of testing if the values add to 15 meaning someone has won...

small problem with it though, as for some reason the values in the array's are not adding together properly and thus not performing the check right.

maybe someone can identify what the problem is?

[syntax="cpp]
#include 
#include   //for rand()

int check (int computer [], int player [],const int max);

int check (int computer[], int player[],const int max) 
{
     int checkC = 0, checkP = 0;  
     
       
 for (int i = 0 ; i = 4)   //since the for loop could get all but one possible combo, this loop is to check for the last possibility.
  {   
      checkC = computer [0] + computer [2] + computer [3];
      checkP = player   [0] + player   [2] + player   [3];
  }    
        if (checkC == 15)
          return (2);
        else if (checkP == 15)
          return (1);
 
    
      return 0;
} 


 
int main ()

{  
    
  int board    [9] = {2,9,4, 
                     7,5,3, 
                     6,1,8}; 
  int player   [5];
  int computer [5];
  
  int choice; 
 
 
 for (int i = 0 ; i < 5 ; i++)  //initializes everything to make sure all varables have value.
 {   
     player   [i] = 10;
     computer [i] = 10;
    
    
 }     
 
 for (int i = 0 ; i < 5 ; i++)
 {
     std::cout  choice;
     
player [i] = choice - 1;  
     std::cout 