Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar Mark Forums Read
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 04-05-2005, 08:25 PM   #1
buddyjuf
Guest
 
Posts: n/a
basic C++ question:

So I have this 2 dimensional Array (3X3)

and I want to make it so

"if there is an "X" in slot 2-2, return a "YES"

tried doing

if ( board[2][2] hahahaha 'X' )
{
cout << "Yes";
}
else
{
cout << "No";
}

and I keep on getting "No"s at every turn


can anybody help me out on this one?


what I am actually trying to do, is that it's a TIC TAC TOE game, and I need to stop the program once someone wins...

help me out yooo!!!

178340550
  Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-05-2005, 08:57 PM   #2
BuggyG
Confirmed User
 
Join Date: Oct 2002
Location: Ottawa
Posts: 4,179
ah shit.. haven't done any C++ in over 10years

damn.. now I wanna know too
and I know it's a fuckin simple thing
BuggyG is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-05-2005, 09:20 PM   #3
skinnay
Confirmed User
 
Join Date: Apr 2004
Location: NEW YORK CITY
Posts: 2,274
post the code somewhere where it wont go hahahaha
__________________
Make Real Green with ORGANIC SEO | Blog post exchange | Non-index page trades | A-B C-D Trades [icq: 194-215-962] [mail: [email protected]]
skinnay is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-05-2005, 09:45 PM   #4
buddyjuf
Guest
 
Posts: n/a
Quote:
Originally Posted by skinnay
post the code somewhere where it wont go hahahaha
its just 2 "=" next to eachother
  Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-05-2005, 09:53 PM   #5
rickholio
Confirmed User
 
Industry Role:
Join Date: Jan 2004
Location: Nor'easterland
Posts: 1,914
Quote:
Originally Posted by bdjuf
So I have this 2 dimensional Array (3X3)

and I want to make it so

"if there is an "X" in slot 2-2, return a "YES"

tried doing

if ( board[2][2] hahahaha 'X' )
{
cout << "Yes";
}
else
{
cout << "No";
}

and I keep on getting "No"s at every turn


can anybody help me out on this one?


what I am actually trying to do, is that it's a TIC TAC TOE game, and I need to stop the program once someone wins...

help me out yooo!!!

178340550
Because [2][2] is actually referencing the 'bottom right' element of a 3x3 array. Arrays in C/C++ are referenced from 0. If you're trying to check the MIDDLE square, you're checking the wrong spot... and if you reference [3][3], you're overflowing and begging for a h4x0r 2 r0x0r j00r b0x0rz.

Try this:
Code:
  if(board[1][1]hahahaha'X') {       // Is there an X in the middle square?
    cout << "Yes";
  } else {
    cout << "No";
  }
BTW - The best way to make a TTT game is to have the program 'learn'. I did the same thing back in 80s on an atari 400... it's possible to make a damn-near unbeatable computerized opponent.

Last edited by rickholio; 04-05-2005 at 09:54 PM..
rickholio is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-05-2005, 10:12 PM   #6
rickholio
Confirmed User
 
Industry Role:
Join Date: Jan 2004
Location: Nor'easterland
Posts: 1,914
A 'win' state checker:

Code:
char *checkwin(char board[2][2])  // Prototype might be a bit off, adjust accordingly ;)
{
  // Check horizontal
  for(int y=0;y<3;y++) {
    int xs, os;
    for(int x=0;x<3;x++) {
      xs += (board[x][y] = = 'X');
      os += (board[x][y] = = 'O');
    }
  }
  if(xs= =3) return("X Wins");
  if(os= =3) return("O Wins");

  // Check vertical
  for(int x=0;x<3;x++) {
    int xs, os;
    for(int y=0;y<3;y++) {
      xs += (board[x][y] = = 'X');
      os += (board[x][y] = = 'O');
    }
  }
  if(xs= =3) return("X Wins");
  if(os= =3) return("O Wins");

  // Check left-right diag
  for(int a=0,xs=0,os=0;a<3;a++) {
    xs += (board[a][a] = = 'X');
    os += (board[a][a] = = 'O');
  }
  if(xs= =3) return("X Wins");
  if(os= =3) return("O Wins");

  // Check right-left diag
  for(int a=0,xs=0,os=0;a<3;a++) {
    xs += (board[a][2-a] = = 'X');
    os += (board[a][2-a] = = 'O');
  }
  if(xs= =3) return("X Wins");
  if(os= =3) return("O Wins");

// No winnar.
  return;
}
There is almost certainly better ways of doing this (making a string of 3 characters in the array and matching that against a string of 'XXX' or 'OOO', for instance). This was just a 60 second slap together that'll do the job and demonstrate the logic cleanly.
rickholio is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-05-2005, 10:16 PM   #7
wdsguy
Ryde or Die
 
Industry Role:
Join Date: Dec 2002
Location: California-Shanghai
Posts: 19,568
Quote:
Originally Posted by bdjuf
So I have this 2 dimensional Array (3X3)

and I want to make it so

"if there is an "X" in slot 2-2, return a "YES"

tried doing

if ( board[2][2] hahahaha 'X' )
{
cout << "Yes";
}
else
{
cout << "No";
}

and I keep on getting "No"s at every turn


can anybody help me out on this one?


what I am actually trying to do, is that it's a TIC TAC TOE game, and I need to stop the program once someone wins...

help me out yooo!!!

178340550

Doing a assignment for cs class?
wdsguy is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-05-2005, 10:18 PM   #8
vas
Confirmed User
 
Join Date: Dec 2003
Location: Toronto
Posts: 562
use the min max algo, i coded a tic tac toe game for school a couple years ago using this algo, and it was undefeatable..

http://personal.vsnl.com/erwin/tictactoe.htm
__________________
icq: 375956791
vas is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-05-2005, 11:31 PM   #9
nastyking
 
Join Date: Nov 2002
Posts: 2,174
array element numbering starts with 0 not 1
__________________
nastyking is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-06-2005, 12:01 AM   #10
Slacker
Confirmed User
 
Join Date: Feb 2003
Location: Mostly the couch
Posts: 331
Nerds








just kidding
Slacker is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-06-2005, 12:41 AM   #11
darnit
Confirmed User
 
Join Date: Jul 2001
Location: Teh Interweb
Posts: 2,439
Nerds


darnit is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-06-2005, 02:04 PM   #12
buddyjuf
Guest
 
Posts: n/a
got it! thanx guys

now another question, how do I reset an array? make it all empty again?
  Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-06-2005, 02:10 PM   #13
tranza
ICQ: 197-556-237
 
Join Date: Jun 2003
Location: BRASIL !!!
Posts: 57,559
Damn! I used to know C++, now I don't remember shit!
__________________
I'm just a newbie.
tranza is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-06-2005, 02:27 PM   #14
rickholio
Confirmed User
 
Industry Role:
Join Date: Jan 2004
Location: Nor'easterland
Posts: 1,914
Quote:
Originally Posted by bdjuf
got it! thanx guys

now another question, how do I reset an array? make it all empty again?
Assuming it's a contiguous area of memory and a simple array:

Code:
memset((void *)array_ptr, number_of_elements * sizeof(array_type), 0);
Alternately, you can request it be zeroed while allocating:

Code:
array_ptr = (array_type *)calloc(number_of_elements, sizeof(array_type));
You're doing c++, so you can always just destroy the old object and create a new one, which should set all values to their prototypes as well... if not, you can do the memset() thing in the object constructor.

HTH. HAND.

PS - If you malloc()'d the array originally, make sure you free() it before doing a new malloc(), otherwise you'll leak ram.

Last edited by rickholio; 04-06-2005 at 02:29 PM..
rickholio is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-06-2005, 02:43 PM   #15
buddyjuf
Guest
 
Posts: n/a
everything works!!! thanx alot for your help guys
  Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks
Thread Tools



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.