vendredi 1 février 2019

Soccer game continued

I'm making a javascript game based on a soccer cup draw. I want to check the results of each game and send the winner through to the next round. I've managed to achieve this... What I'm struggling with is managing to get any drawn games played again, and the winner of the replayed game sent through to the next round.

Any ideas?!

Code so far:

const teams = ['Burnley', 'Arsenal', 'Liverpool', 'Everton', 'Man Utd', 'Brighton', 'West Ham United', 'Tottenham Hotspur', 'Chelsea', 'Man City', 'Fulham'
, 'AFC Bournemouth', 'Cardiff City', 'Crystal Palace', 'Huddersfield Town', 'Leicester City', 'Newcastle', 'Southampton', 'Watford FC', 'Wolverhampton Wanderers',
'Aston Villa', 'Birmingham City', 'Blackburn Rovers', 'Bolton Wanderers', 'Brentford', 'Bristol City', 'Derby County', 'Hull City', 'Ipswich Town', 'Leeds United',
'Middlesborough', 'Millwall', 'Norwich City', 'Nottingham Forest', 'Preston North End', 'QPR', 'Reading', 'Rotherham United', 'Sheffield Utd',
'Sheffield Wednesday', 'Stoke City', 'Swansea City', 'West Bromwich Albion', 'Wigan Athletic', 'AFC Wimbledon', 'Accrington Stanley', 'Barnsley', 'Blackpool',
 'Bradford City', 'Bristol Rovers', 'Burton Albion', 'Charlton Athletic', 'Coventry City', 'Doncaster Rovers', 'Fleetwood Town', 'Gillingham', 'Luton Town', 'Oxford United',
 'Peterborough United', 'Plymouth Argyle', 'Portsmouth', 'Rochdale', 'Scunthorpe United', 'Shrewsbury Town', 'Southend United', 'Sunderland', 'Walsall',
 'Wycombe Wanderers','Bury', 'Cambridge United', 'Carlisle United', 'Cheltenham Town', 'Colchester United', 'Crawley Town', 'Crewe Alexandra', 'Exeter City', 'Forest Green Rovers',
 'Grimsby Town', 'Lincoln City', 'Macclesfield Town', 'Mansfield Town', 'Milton Keynes Dons', 'Morecombe', 'Newport County', 'Northampton Town',
 'Notts County', 'Oldham Athletic', 'Port Vale', 'Stevenage', 'Swindon Town', 'Tranmere Rovers', 'Yeovil Town']



var thirdRound = (reduceDraw(teams, 64));
var home, away, score;
var dice = Math.floor(Math.random() * 6) + 1;
var fourthRound = [];



// reduce original array by 28 random teams
function reduceDraw(arr, n) {
  var result = new Array(n),
      len = arr.length,
      taken = new Array(len);
  if (n > len)
    throw new RangeError("getRandom: more elements taken than available");
  while (n--) {
    var x = Math.floor(Math.random() * len);
    result[n] = arr[x in taken ? taken[x] : x];
    taken[x] = --len in taken ? taken[len] : len;
  }

  return result;
}






//draw 1st round (64 teams) individual

function draw(arr) {

   // If there are elements still in the array
   if (arr.length >= 30) {

    // Get two random numbers, one for home team, one for away team
    let rnd = Math.floor(Math.random() * arr.length - 1);
    let rnd2 = Math.floor(Math.random() * arr.length - 1);

    // Grab those random elements from the array
    home = {name: arr.splice(rnd, 1)[0],
            score: generateScore()
            };
    away = {name: arr.splice(rnd2, 1)[0],
            score: generateScore()
          };

    console.log(home.name + " " + home.score + " vs " + away.score + " " + away.name);
    // Wait 1 second before calling the function
    // again with the reduced array
    setTimeout(() => draw(arr), 100);

     if(home.score > away.score){
      fourthRound.push(home.name)
      console.log(fourthRound)
    } else if (home.score < away.score) {
      fourthRound.push(away.name)
      console.log(fourthRound)
    } else {
      console.log(away.name + " " + generateScore() + " vs " + generateScore() + " " + home.name);


    }



   }



}

  //Play Replay

  // function replay() {
  //     console.log(away.name + " " + generateScore() + " vs " + generateScore() + " " + home.name);
  //      console.log(this);
  //    }


    // Generate A Score
    function generateScore(){
     dice = Math.floor(Math.random() * 6) + 1;

        if(dice === 1){
          goals = 0
        } else if (dice === 2 || dice === 3){
          goals = 1
        } else if (dice === 3 || dice === 4){
          goals = 2
        } else if (dice === 5){
          goals = 3
        } else {
          goals = 4;
        }

        return goals
};

Aucun commentaire:

Enregistrer un commentaire