APCSP Program Code
APCSP Program Code
var top_ten = ["Bayern Munich", "Real Madrid", "Liverpool" , "Manchester City", "Chelsea",
"Villareal", "Benfica", "Atletico Madrid"];
// the counter for all the teams based on wins
var team_1 = 0;
var team_2 = 0;
var team_3 = 0;
var team_4 = 0;
//to go to the main page
onEvent("button1", "click", function( ) {
setScreen("screen2");
});
//to call the function for the first time
onEvent("button2", "click", function( ) {
setText("text_area14", func(getText("team1"), getText("team2")));
setText("text_area5", (getText("team1") + " VS ") + getText("team2") );
});
//to call the function another time using different teams
onEvent("button3", "click", function( ) {
setText("text_area13", func(getText("team3"), getText("team4")));
setText("text_area6", (getText("team3") + " VS ") + getText("team4") );
});
//function to give the scores and update the counter
function func(team1, team2) {
var score1 = 0;
var score2 = 0;
var win;
for (var i = 0; i < top_ten.length; i++) {
if (top_ten.indexOf(team1) < 5) {
score1 = randomNumber(1,4);
} else if ((top_ten.indexOf(team1) >= 5)) {
score1 = randomNumber(0,3);
}
if (top_ten.indexOf(team2) < 5) {
score2 = randomNumber(1,4);
} else if ((top_ten.indexOf(team2) >= 5)) {
score2 = randomNumber(0,3);
}
}
if (score1 == score2 && (team1 == "Real Madrid" || team1 == "Benfica")) {
team_1 = team_1 + 1;
team_2 = team_2 + 1;
win = (((((((("Hi "+getText("text_input1"))+"!") + (" The result for this match is: "+ team1)+ " "
+ score1) + " - ") + team2)+ " ")+ score2) + "...") + "It's a draw!" ;
setText("text_area3", team_1);
setText("text_area7", team_2);
} else if ((score1 > score2 && (team1 == "Real Madrid" || team1 == "Benfica"))) {
team_1 = team_1 + 1;
win = ((((((((("Hi "+getText("text_input1"))+"!") + (" The result for this match is: "+ team1)+ " "
+ score1) + " - ") + team2)+ " ")+ score2) + "...") + team1) + " wins!";
setText("text_area3", team_1);
setText("text_area7", team_2);
} else if ((score2 > score1 && (team2 == "Ateltico Madrid" || "Liverpool" == "Liverpool"))) {
team_2 = team_2 + 1;
win = ((((((((("Hi "+getText("text_input1"))+"!") + (" The result for this match is: "+ team1)+ " "
+ score1) + " - ") + team2)+ " ")+ score2) + "...") + team2) + " wins!" ;
setText("text_area3", team_1);
setText("text_area7", team_2);
}