Fedora Linux Support Community & Resources Center
  #1  
Old 13th August 2012, 04:54 AM
Mepoe Offline
Registered User
 
Join Date: Oct 2011
Posts: 110
linuxopera
Random selection assistance please? (C/C++)

I have some code...

Code:
void opponent( int arg[ ][ 3 ], int &row, int &col )
{
	srand( time( NULL ) );
	
	short int new_row;
	short int new_col;

	//get new numbers. They start off unitialised basically. 
	new_row = rand( ) % 2;
	new_col = rand( ) % 2;

	while( ( ( new_row == row ) && ( new_col == col ) ) || arg[new_row][new_col] != 9 ) 
	{
		//Then we haven't actually got a new column have we. >.> baka. 
				new_row = rand( ) % 2;
				new_col = rand( ) % 2;
	}
	
	//On the other hand they should now be different. 

	//At this point it is different so... now we need to check the board if the position has been filled or not. 
	
	row = new_row;
	col = new_col;
	//Carry on while the value at the selected coordinates is 0 or 1. 
	
}
Tada!

Now. This is for a Tic Tac Toe game... all I want it to do is pick some random numbers from 0 to 2, they're coordinates on a board. Now this DOES work fine for the first...mmm four to six turns. There's a win condition that's easy enough to achieve after three turns...but it starts getting flustered if you purposely avoid winning.

I don't really want to subtract positions away from the array because that'd be a bit pointless. Instead I've took to putting in the number 9 as a blank space or a 1 or 0 as a taken space. A space that is none of these would screw up the computer. So it has to be a 9, 1 or 0.

My array is a multidimensional array, 3x3 (tic tac toe who da thunk it). [0-2], [0-2]. So it loops through the array and picks numbers. Now it seems to be able to go for both extremes just fine and according to the documentation that is how rand is supposed to work... but maybe the documentation is...slightly ambiguous? That said... it should loop until it finds a block that's empty.

Well it does loop. FOREVER. Maybe my computer is just dumb ¬_¬ *evils* so I need to give it some assistance. Can anyone think up a way to jog it's memory that other squares are in fact available and to get it to shift?

Would asking for the srand() again be of any assistance do you think? Thank you for your time!

edit

As a note, this 'used' to be a function that called itself...but my computer really doesn't like segfaults >.>
Reply With Quote
  #2  
Old 13th August 2012, 05:48 AM
stevea Offline
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 9,041
linuxfirefox
Re: Random selection assistance please? (C/C++)

Use " % 3" not " % 2" everywhere in that code.
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe
Reply With Quote
  #3  
Old 13th August 2012, 07:11 AM
Mepoe Offline
Registered User
 
Join Date: Oct 2011
Posts: 110
linuxopera
Re: Random selection assistance please? (C/C++)

Hm... it was 3 before, but then I read the documentation >.<; Thanks! (Obviously I'd misinterpreted it >.<)

Thanks ^_^
Reply With Quote
Reply

Tags
assistance, c or c, random, selection

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need a little assistance jeffevo Using Fedora 5 29th July 2012 03:28 PM
Assistance with YUM ImmortalMatrixo Using Fedora 0 15th June 2009 12:57 PM
rpm assistance Toast2120 Using Fedora 1 24th March 2009 01:56 AM
HDD Selection, Random? louiss3000 Installation, Upgrades and Live Media 13 20th September 2008 12:13 PM
Need assistance! Iggzaplaya Installation, Upgrades and Live Media 7 27th August 2007 08:13 PM


Current GMT-time: 02:18 (Tuesday, 23-12-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
...Paseo la Sexta - Porto Di Portopalo Di Capopassero Travel Photos on Instagram - Cafe Edna Photos on Instagram - Beja - Tallink M/S Victoria I Travel Photos