FedoraForum.org - Fedora Support Forums and Community
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2011

    Random selection assistance please? (C/C++)

    I have some 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. 

    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!


    As a note, this 'used' to be a function that called itself...but my computer really doesn't like segfaults >.>

  2. #2
    stevea Guest

    Re: Random selection assistance please? (C/C++)

    Use " % 3" not " % 2" everywhere in that code.

  3. #3
    Join Date
    Oct 2011

    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 ^_^

Similar Threads

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts