Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 difficulty with a piece of code, tic-tac-toe related
Index -> Programming, C++ -> C++ Help
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Fonzie




PostPosted: Wed Dec 07, 2005 3:40 am   Post subject: 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.
Sponsor
Sponsor
Sponsor
sponsor
Fonzie




PostPosted: Wed Dec 07, 2005 3:46 am   Post subject: (No subject)

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




PostPosted: Wed Dec 07, 2005 2:25 pm   Post subject: (No subject)

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




PostPosted: Wed Dec 07, 2005 2:51 pm   Post subject: (No subject)

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




PostPosted: Wed Dec 07, 2005 2:57 pm   Post subject: (No subject)

Fonzie wrote:
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




PostPosted: Wed Dec 07, 2005 3:08 pm   Post subject: (No subject)

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




PostPosted: Wed Dec 07, 2005 10:03 pm   Post subject: (No subject)

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
c++:
before your code and
after your code.


Ex.

c++:
  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




PostPosted: Wed Dec 07, 2005 10:06 pm   Post subject: (No subject)

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
Sponsor
Sponsor
Sponsor
sponsor
Geminias




PostPosted: Wed Dec 07, 2005 10:22 pm   Post subject: (No subject)

and i'm just curious.. do you realize that in this piece of code:


[syntax="cpp"]

count3=-1; //by the way it would be more legible written: count3 = -1;

[syntax="cpp"]

you are saying count3 is equal to negative one. 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




PostPosted: Wed Dec 07, 2005 11:00 pm   Post subject: (No subject)

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]




PostPosted: Wed Dec 07, 2005 11:40 pm   Post subject: (No subject)

Just in case you want to know, to use C++ syntax highlightning you type:
c++:
code goes here
[Gandalf]




PostPosted: Wed Dec 07, 2005 11:42 pm   Post subject: (No subject)

Argh! I made just the mistake I was trying to avoid... Sorry about that.

You type this:
[syntax="cpp"]code goes here[/syntax]
Geminias




PostPosted: Thu Dec 08, 2005 5:35 pm   Post subject: (No subject)

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 <iostream>
#include <stdlib.h> //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 <= max ; i++) //this for loop adds 3 values in the array together to see if its 15
{
checkC = computer [i % max] + computer [(i + 1) % max ] + computer [(i + 2) % max ] ;
checkP = player [i % max] + player [(i + 1) % max ] + player [(i + 2) % max ] ;

if (checkC == 15)
return (2);
else if (checkP == 15)
return (1);
checkC = 0;
checkP = 0;
}

if (max >= 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 << "Choose your next move: " << std::endl;
std::cin >> choice;

player [i] = choice - 1;
std::cout << "Your choice was: " << choice << std::endl;
for (int s = 0 ; s <= i ; s++)
{
while ((computer [i] == 10) || (computer [i] == player [s]) )
{
computer [i] = rand() % 9;
}

}

std::cout << "Computer chooses: " << computer [i] << std::endl;

player [i] = board [choice - 1];
computer [i] = board [computer [i]];
choice = 0;


if ( i >= 2 )
choice = check(player, computer, i); //choice is equal to the value check returns. Note: Choice is used in two contexts.
if (choice != 0)
if (choice == 1)
std::cout << "YOU WIN!";
else if (choice == 2)
std::cout << "YOU LOSE!";

}
std::cout << "no one won!";
std::cin.get(); //chill when program finishes.

return 0;
}



[/syntax]
Geminias




PostPosted: Thu Dec 08, 2005 5:36 pm   Post subject: (No subject)

p.s. not having the ability to edit is just ridiculous.

c++:

#include <iostream>
#include <stdlib.h>  //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 <= max  ; i++)  //this for loop adds 3 values in the array together to see if its 15
 {
         checkC = computer [i % max] + computer [(i + 1) % max ] + computer [(i + 2) % max ]  ; 
         checkP = player   [i % max] + player   [(i + 1) % max ] + player   [(i + 2) % max ]  ;   
       
    if (checkC == 15)
      return (2);
    else if (checkP == 15)
      return (1);
checkC = 0;
checkP = 0;
  }
 
  if (max >= 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 << "Choose your next move: " << std::endl;
     std::cin  >> choice;
     
player [i] = choice - 1
     std::cout << "Your choice was: " << choice << std::endl;
   for (int s = 0 ; s <= i ; s++)
   {
      while ((computer [i] == 10) || (computer [i] == player [s]) ) 
      {
             computer [i] = rand() % 9;
      }
     
   }
   
   std::cout << "Computer chooses: " << computer [i] << std::endl;
   
player   [i] = board [choice - 1];     
computer [i] = board [computer [i]];
choice       = 0;
 
     
    if ( i >= 2 )
      choice = check(player, computer, i)//choice is equal to the value check returns.  Note: Choice is used in two contexts.
      if (choice != 0)
         if (choice == 1)
           std::cout << "YOU WIN!";
         else if (choice == 2)
           std::cout << "YOU LOSE!";
     
 }
    std::cout << "no one won!";
    std::cin.get()//chill when program finishes.
   
    return 0;
}



Fonzie




PostPosted: Thu Dec 08, 2005 6:02 pm   Post subject: (No subject)

I believe (if you were having the same mistake I was) that the reason it doesn't add correctly is because 2 numbers can add to 15. so, lets say human[4] is 0, and human [1] is 9 and human[2] is 6. 9+6+0=15. so you have to set it so that it only checks if no value of the three numbers being checked equals 0. That's the problem that I was stuck on for hours. I think it may also be the error in your code.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 35 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: