GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   basic C++ question: (https://gfy.com/showthread.php?t=452575)

buddyjuf 04-05-2005 08:25 PM

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

BuggyG 04-05-2005 08:57 PM

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 :disgust

skinnay 04-05-2005 09:20 PM

post the code somewhere where it wont go hahahaha

buddyjuf 04-05-2005 09:45 PM

Quote:

Originally Posted by skinnay
post the code somewhere where it wont go hahahaha

its just 2 "=" next to eachother

rickholio 04-05-2005 09:53 PM

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. :winkwink:

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.

rickholio 04-05-2005 10:12 PM

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. :2 cents:

wdsguy 04-05-2005 10:16 PM

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?

vas 04-05-2005 10:18 PM

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

nastyking 04-05-2005 11:31 PM

array element numbering starts with 0 not 1

Slacker 04-06-2005 12:01 AM

Nerds :error








just kidding :pimp

darnit 04-06-2005 12:41 AM

Nerds :error


:1orglaugh

buddyjuf 04-06-2005 02:04 PM

got it! thanx guys

now another question, how do I reset an array? make it all empty again?

tranza 04-06-2005 02:10 PM

Damn! I used to know C++, now I don't remember shit!

rickholio 04-06-2005 02:27 PM

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. :thumbsup

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

buddyjuf 04-06-2005 02:43 PM

everything works!!! thanx alot for your help guys


All times are GMT -7. The time now is 06:50 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123