General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 10th, 2004, 07:34 AM
Delusion Delusion is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 2 Delusion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Random script

Hello,

Generally, I'll describe what was my idea at first.

I've been trying to make a script for my school (football, 1vs1) that will make me random teams selection.

The main idea about it, I got 11 teams, The random I was trying to do:

Each team should play 20 games in a season, with each team playing each other TWICE at a season.

I've tried to make this using a 'FOR' loops but I've got double games and alot of mess.

Here's my try:
PHP Code:
<?
echo "<table>";
 for(
$i=$c=$b=$a=$i <= 220 $i++ , $c+=$b++, $a++) {

   echo 
"<tr>";
     echo 
"<td width='100' align='left'>$i</td>";
     echo 
"<td>team $a</td>";
     echo 
"<td>vs.</td>";
     echo 
"<td>team $b</td>";
   echo 
"</tr>";

  
// Storing the DATA at MySQL irrelevant now

   
if ($a >= "11") { $a "0"; }
   if (
$b >= "11") { $b "0"; }
   if (
$c >= "6") { $c "0"; echo "<tr><td colspan='5'><hr></td></tr>"; }
 }

echo 
"</table>";

?>


I've spent alot of time trying to make it work and failed
I hope someone around will be able helping me!

Thanks in advnace.

Reply With Quote
  #2  
Old January 13th, 2004, 09:12 PM
mattp23 mattp23 is offline
Moderated
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: UK
Posts: 82 mattp23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 43 m 44 sec
Reputation Power: 6
you may want to look at the rand function it is used to generate a random value: http://uk.php.net/manual/en/function.rand.php

Reply With Quote
  #3  
Old January 13th, 2004, 09:29 PM
Delusion Delusion is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 2 Delusion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Try to reply to the subject..
Reading the thread itself and not the title.
It might help..

Thanks anyway.

Reply With Quote
  #4  
Old January 14th, 2004, 08:38 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Tossing smart aleck remarks at people trying to help you is no way to get further help, Delusion.

What does your current code output?

Have you considered using arrays? Maybe something along the lines of this pseudocode:

PHP Code:
 $teams=array(1,2,3...);
$teams2=$teams;
$games=array();

for(
$i=0$i<sizeof($teams); $i++){
    for(
$j=0$j<sizeof($teams2); $j++){
        if(
$teams[$i] != $teams2[$j]){
            
array_push($games,array($teams[$i], $teams2[$j]));
            
array_push($games,array($teams[$i], $teams2[$j]));
        }
    }
}

$used=array();
for(
$i=0$i<sizeof($games); $i++){
    
$r=rand(0,sizeof($games));
    if(
in_array($r,$used)){
        while(
in_array($r,$used)){
            
$r=rand(0,sizeof($used));
        }
    }
    
//print table data using $games[$r];
    
array_push($used,$r);



I haven't tested that, but it makes sense to me. You're looping through your teams to add two matchups per team to the $games array. Then you're looping through the $games array and printing random matchups where the current random matchup hasn't been chosen yet.

This has the added advantage of being portable and of facilitating naming rather than numbering of teams. If you add team names or numbers to the one array, everything else expands automatically to accommodate it without hard-coding of counts, etc.

Reply With Quote
  #5  
Old January 14th, 2004, 08:40 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Update: I went back and tested the code after the fact, and it works like a charm.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Random script


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT