rockinaway Posted April 27, 2009 Share Posted April 27, 2009 I am creating a cricket simulation, the code i am using is below and the help i need is below that. <?php // Error reporting error_reporting(E_ALL); // Connect DB mysql_connect("wickettowicket.adminfuel.com", "xxxxxxxxx", "xxxxxxxxxxx") or die(mysql_error()); // Select DB mysql_select_db("wickettowicket") or die(mysql_error()); // MySQL queries $array_batsmen = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 1 ORDER BY id ASC'); $array_bowlers = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 2'); // Start table $data = '<table width="600px">'; // Create blank scorecard while ($array_bat = mysql_fetch_array($array_batsmen)) { $data .= '<tr><td>'.$array_bat['name'].'</td><td></td><td></td><td>0</td></tr>'; } // Set up arrays for players $array_bow = $array_bats = array(); // Reset query mysql_data_seek($array_batsmen,0); $in_one = $in_two = $it = ''; // Set up arrays of batsmen while ($array = mysql_fetch_array($array_batsmen)) { if ($it < 3 && $in_one == '') { $in_one = $array['id']; $it++; } else if ($it < 3 && $in_two == '') { $in_two = $array['id']; $it++; } $batsmen[] = array ( 'id' => $array['id'], 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'], 'out' => 0, 'in' => 0, 'runs' => 0 ); } // Bowler Array while ($array = mysql_fetch_array($array_bowlers)) { $bowlers[] = array ( 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'] ); } // Reset both queries mysql_data_seek($array_bowlers,0); mysql_data_seek($array_batsmen,0); $runs = $outs = $balls = $overs = 0; $played = array(); // Loop foreach ($batsmen as $bat_key => $batsman) { $played[] = $batsman['id']; while ($batsman['out'] == 0 && $balls <= 295 && $outs < 10) { // Loop through bowlers foreach ($bowlers as $bow_key => $bowler) { $batsman['in'] = 1; // Set the initial number $number = rand(0, 190); // Ability of batsman if ($batsman['ability'] > 90) $number += 30; else if ($batsman['ability'] > 70) $number += 15; else if ($batsman['ability'] > 50) $number += 2; else $number = $number; // Strength of batsman if ($batsman['strength'] > 90) $number += 15; else if ($batsman['strength'] > 70) $number += 10; else if ($batsman['strength'] > 50) $number += 5; else $number = $number; // Depending on overs if ($balls > 270) $number += 30; else if ($balls > 120) $number -= 10; // Ability if ($bowler['ability'] > 90) $number -= 30; else if ($bowler['ability'] > 70) $number -= 15; else if ($bowler['ability'] > 50) $number -= 2; else $number = $number; // If batsman has made a huge total of runs, we need to knock some numbers off if ($batsman['runs'] > 200) $number -= 70; else if ($batsman['runs'] > 100) $number -= 30; // Finally sort out runs if ($number > 190) $run = 6; else if ($number > 170) $run = 4; else if ($number > 160) $run = 3; else if ($number > 100) $run = 2; else if ($number > 50) $run = 1; else if ($number > 10) $run = 0; else if ($balls > 120 && $number > 0) $run = 0; else $run = -1; // Increase number of balls $balls += 1; // Are they out? if ($run == -1) { $batsman['out'] = 1; $find = '<tr><td>'.$batsman['name'].'</td><td></td><td></td><td>0</td></tr>'; $replace = '<tr><td>'.$batsman['name'].'</td><td></td><td>'.$bowler['name'].'</td><td>'.$batsman['runs'].'</td></tr>'; $data = str_replace($find, $replace, $data); } else { $batsman['runs'] += $run; $runs += $run; } } // Count outs if ($batsman['out'] == 1) $outs += 1; } } $rem = $balls % 6; $balls_full = $balls - $rem; $overs = $balls_full/6; $data .= '<tr><td>'.$outs.'</td><td>'.$overs.'.'.$rem.'</td><td>'.$runs.'</td></tr>'; $data .= '</table>'; echo $data; ?> Here are my problems: 1) Is there any efficient way of doing this (better than mine?) Any cleaning up that is needed? I am okay at using PHP, not the best yet 2) I need the batsmen to change when a person is out. So when $run == -1, I need to get a new batsman in. The problem is that it isn't necessarily the NEXT person in the array, it is the next person who ISN'T out. I need to get that working somehow. 3) When $run is an odd number, I need the batsmen to rotate over, so the array data being used it the partner batsmen - this gets complicated. 4) I need the bowler to bowl a max of 10 overs. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/155892-cleaning-up-and-some-serious-help/ Share on other sites More sharing options...
Cosizzle Posted April 27, 2009 Share Posted April 27, 2009 Hey, 1.) Honestly, I've seen a lot of ugly code and this really doesnt look bad at all. I'm not sure if this would work in your app, and nor would I recommand it at this stage (unless you really wanted to), but perhaps the use of classes to clean a bit of this up. The idea here would be to have [Class] // connects to DB [sqlConnect] // simulates the game (pulls it all together) [Game] // Super class that defines the player // name, ability, strength etc... [Player] // sub class to [Player] defines positions maybe [batsman] [bowler] // Same idea as Player class only with game objects [Pieces] [ball] [bat] This isnt ideal, and again I guess this comes from a.) your application and b.) your programming background (This is more of an OOP approach) 2.) Going off 1.) you could have a method, or function that sets if the batsman is active or not. 3.) sounds complicated 4.) Why not use a while loop? <?php $bowl = 0; while ($bowler < $bowl) { // actions of bowler bowl++; } ?> I hope this is helpful, again this is just input - besides the small bugs you spoke about... it looks pretty good Quote Link to comment https://forums.phpfreaks.com/topic/155892-cleaning-up-and-some-serious-help/#findComment-820600 Share on other sites More sharing options...
ignace Posted April 27, 2009 Share Posted April 27, 2009 Possible solutions: Problem 1) first: Code looks good although you may want to remove the html from your php code you could use a template engine like Smarty. Second: Refactor your code.. Third: Refactor your code... Fourth: Refactor your code... Try to find small pieces in your code which you end up using multiple times or which you may end up using more then once, wrap it, and give it a proper name if done well you will find your program less complex to code. You could see this like a book the paragraph titles immediatly tell you what will follow which in turn helps you to understand your program more easily much like your comments. Consider the following example which i created based on your code: <?php // Error reporting error_reporting(E_ALL); // Connect DB $db = mysql_connect("wickettowicket.adminfuel.com", "xxxxxxxxx", "xxxxxxxxxxx") or die(mysql_error()); // Select DB mysql_select_db("wickettowicket") or die(mysql_error()); $batsmen = setupBatsMen($db); $bowlers = setupBowlers($db); $data = gameLoop($batsmen, $bowlers); echo createScoreCard($data); ?> You must realise that these functions proxy to other functions not currently shown here. But you directly see what is going on and the complexity has decreased in a tremendous way. Problem 2) A simple loop over your batsmen will do the trick Problem 3) I have no knowledge of cricket so i can not help you with that explain the problem more precisely to get more precise solutions Problem 4) Same like 3 or something like: if ($bowler['bowl'] > 10) { ..logic.. } You could also wrap your bowler's into objects if ($bowler->reachedMax()) { ..logic.. } or something alike. Quote Link to comment https://forums.phpfreaks.com/topic/155892-cleaning-up-and-some-serious-help/#findComment-820609 Share on other sites More sharing options...
rockinaway Posted April 27, 2009 Author Share Posted April 27, 2009 okay, thanks for help so far. I admit there are comments missing, I shall be adding them soon. My main thing is searching the array for a the next batsman that hasn't batted, and then making the foreach loop swap to their data. The next problem is the runs being odd .. and getting the data to switch over. I have tried loops and everything, with little success. Are there any examples you can give me to help? Quote Link to comment https://forums.phpfreaks.com/topic/155892-cleaning-up-and-some-serious-help/#findComment-820618 Share on other sites More sharing options...
ignace Posted April 27, 2009 Share Posted April 27, 2009 Some thing i forgot to mention is when you start re-factoring your code you should always take baby-steps re-factor something small (never re-factor something big because then you would be doing something wrong) out of your code and check if it still works and then continue with the next thing and continue to do so until you end up with something that you can not re-factor further or you can easily tell what your program is doing. Quote Link to comment https://forums.phpfreaks.com/topic/155892-cleaning-up-and-some-serious-help/#findComment-820625 Share on other sites More sharing options...
rockinaway Posted April 27, 2009 Author Share Posted April 27, 2009 Yup, I always do that.. make sure nothing goes wrong .. but anything on what I said above? ---- My main thing is searching the array for a the next batsman that hasn't batted, and then making the foreach loop swap to their data. The next problem is the runs being odd .. and getting the data to switch over. I have tried loops and everything, with little success. Are there any examples you can give me to help? Quote Link to comment https://forums.phpfreaks.com/topic/155892-cleaning-up-and-some-serious-help/#findComment-820627 Share on other sites More sharing options...
rockinaway Posted May 4, 2009 Author Share Posted May 4, 2009 Anymore help with this? ANyone? Quote Link to comment https://forums.phpfreaks.com/topic/155892-cleaning-up-and-some-serious-help/#findComment-825825 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 What do you mean getting the data to switch over? Example please. Quote Link to comment https://forums.phpfreaks.com/topic/155892-cleaning-up-and-some-serious-help/#findComment-825827 Share on other sites More sharing options...
rockinaway Posted May 4, 2009 Author Share Posted May 4, 2009 Well, as I described, i have a loop going on for the bastman that is currently active. This takes that bastman's data and keeps applying it to certain things. However, when $run is an odd number (1 or 3), I want the data that is in the loop to CHANGE to the OTHER active batsman, and then the loop continues with their data. So it will resemble batsmen swapping ends in cricket. Is there any way to do this? I am happy to change the entire code with a new way of doing things effectively.. Quote Link to comment https://forums.phpfreaks.com/topic/155892-cleaning-up-and-some-serious-help/#findComment-825832 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Well there is. To find if $run is odd, you can run this check if ($run % 2 != 0) To swap data, you would want to keep track of who is active and inactive. Maybe keep 2 arrays - active and inactive? Find the "other" active batsman and swap data. I don't know what data to swap. I'm not talented in the game of crickets. Quote Link to comment https://forums.phpfreaks.com/topic/155892-cleaning-up-and-some-serious-help/#findComment-825836 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.