In: Computer Science
IN JAVA create a program that has 8 players and ranks them randomly and contains 3 rounds. For each round the highest ranked player plays the lowest ranked one i.e., in quarter-final round, player ranked 1 plays player ranked 8, player ranked 2 plays player ranked 7 and so on. Report the winner of each match, simulating using random values of your choice USING JAVA COLLECTIONS IS NOT ALLOWED
The detailed JAVA program is as given. I have randomly given the names of the players. You can change the name and ranking as per your choice.
*********************************************************************************************************************************************
import java.util.Random;
public class Tournament {
public static void main(String[] args) {
// TODO Auto-generated method
stub
String[] players = new
String[]{"Abc","Bcd","Cde","Def","Efg","Fgh","Ghi","Hij"}; //String
array to hold the name of the players
int ranking[] = new int[]
{2,3,1,5,6,7,8,4}; // Integer Array to hold the ranking of the
player. You can change the ranking according to your choice
//Displaying the Players as is
given in the array
System.out.println("Ranking of the
players are as follows :");
for(int i =0;i<8;i++)
System.out.println(players[i]+" is ranked "+ ranking[i]);
//Sorting the ranking array and
players array as per ranking using selection sort
for (int i = 0; i < 7;
i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < 8; j++)
if (ranking[j] < ranking[min_idx])
min_idx = j;
// Swap the found minimum element with the first
// element
int temp = ranking[min_idx];
ranking[min_idx] = ranking[i];
ranking[i] = temp;
String temp1=players[min_idx];
players[min_idx] = players[i];
players[i] = temp1;
}
System.out.println("Ranking in
sorted order :");
for(int i =0;i<8;i++)
System.out.println(players[i]+" is ranked "+ ranking[i]);
//Code for Quarter-Final
String qtrfinal[][]=new
String[4][2]; // Array to hold the competitors of the Quarter -
Final
for(int i=0,j=7;i<4
;i++,j--)
{
qtrfinal[i][0]=players[i];
qtrfinal[i][1]=players[j];
}
//Displaying the order of
matches
System.out.println("Order of
Matches(Quarter Final) :");
for(int i=0,j=7;i<4
;i++,j--)
{
System.out.println("["+(i+1)+"]
"+qtrfinal[i][0]+" Vs ["+(j+1)+"]"+qtrfinal[i][1]);
}
Random ran = new Random(); //Random
fuction to generate random number for winner
System.out.println("Result of
Matches(Quarter Final) :");
int semi[] = new int[4];// array to
hold the index of winners in quarter final
for(int i=0,j=7;i<4
;i++,j--)
{
System.out.println("["+(i+1)+"] "+qtrfinal[i][0]+" Vs
["+(j+1)+"]"+qtrfinal[i][1]);
int nxt =
ran.nextInt(2); //Finding the winner
int winindex =
findfunction(players,qtrfinal[i][nxt]); //Calling the find function
to get the index of the winner in ranking array.
System.out.println("["+(ranking[winindex])+"] "+qtrfinal[i][nxt]+"
wins");
semi[i]=winindex;// Assigning the index of the winner qualified for
semifinal
//qtrfinal[i][0]=players[i];
//qtrfinal[i][1]=players[j];
}
/*Code for semi final Starts here*/
String semifinal[][]=new
String[2][2];// Array to hold the names of semifinalists
//Sorting the array using selection sort for players
qualified in semi final
for (int i = 0; i < 4;
i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < 4; j++)
if (semi[j] < semi[min_idx])
min_idx = j;
// Swap the found minimum element with the first
// element
int temp = semi[min_idx];
semi[min_idx] = semi[i];
semi[i] = temp;
}
for(int i=0,j=3;i<2
;i++,j--)
{
semifinal[i][0]=players[semi[i]];
semifinal[i][1]=players[semi[j]];
}
System.out.println("Order of
Matches(Semi Final) : ");
for(int i=0;i<2;i++)
{
int findex =
findfunction(players,semifinal[i][0]);
int secindex =
findfunction(players,semifinal[i][1]);
System.out.println("["+(ranking[findex])+"] "+semifinal[i][0]+" Vs
"+(ranking[secindex])+"] "+semifinal[i][1]);
}
System.out.println("Result of
Matches(Semi Final) :");
int final1[] = new int[2];// Array
to hold the players qualified for final
for(int i=0;i<2;i++)
{
int findex =
findfunction(players,semifinal[i][0]);
int secindex =
findfunction(players,semifinal[i][1]);
System.out.println("["+(ranking[findex])+"] "+semifinal[i][0]+" Vs
"+(ranking[secindex])+"] "+semifinal[i][1]);
int nxt = ran.nextInt(2); //
Finding the winners in Semi final
int winindex =
findfunction(players,semifinal[i][nxt]);
System.out.println("["+(ranking[winindex])+"] "+semifinal[i][nxt]+"
wins");
final1 [i]=winindex;
}
/*Coding for findign final starts
Here*/
String fnl[][]=new String[1][2];
//Array to hold the playeres
fnl[0][0]=players[final1[0]];
fnl[0][1]=players[final1[1]];
System.out.println("Order of Matches(Final) : ");
int idx1 =
findfunction(players,fnl[0][0]);
int idex2 =
findfunction(players,fnl[0][1]);
System.out.println("["+(ranking[idx1])+"] "+fnl[0][0]+" Vs
"+(ranking[idex2])+"] "+fnl[0][1]);
System.out.println("Result of Final : ");
idx1 =
findfunction(players,fnl[0][0]);
idex2 =
findfunction(players,fnl[0][1]);
System.out.println("["+(ranking[idx1])+"]
"+fnl[0][0]+" Vs "+(ranking[idex2])+"] "+fnl[0][1]);
int nxt =
ran.nextInt(2);
int temp1 =0
;
if(nxt ==
0)
temp1 = 1;
int winindex =
findfunction(players,fnl[0][nxt]);
System.out.println("["+(ranking[winindex])+"] "+fnl[0][nxt]+" wins
the tournament");
int idex =
findfunction(players,fnl[0][temp1]);
System.out.println("["+(ranking[idex])+"] "+fnl[0][temp1]+" is
Runner-UP");
}
//Function to find the ranking of a given player. It
takes the players array and player name as input and provides
the
// index of the ranking array for that perticular
player.
public static int findfunction (String []a, String
player)
{
int i=0;
for(;i<a.length;i++)
{
if(a[i].equals(player))
break;//str1.equals(str2);)
}
return(i);
}
}
********************************************************************************************************************************************
The output of the program is as given below for a particular run. For every run the Output will change i.e. it may not be the same.
Ranking of the players are as follows :
Abc is ranked 2
Bcd is ranked 3
Cde is ranked 1
Def is ranked 5
Efg is ranked 6
Fgh is ranked 7
Ghi is ranked 8
Hij is ranked 4
Ranking in sorted order :
Cde is ranked 1
Abc is ranked 2
Bcd is ranked 3
Hij is ranked 4
Def is ranked 5
Efg is ranked 6
Fgh is ranked 7
Ghi is ranked 8
Order of Matches(Quarter Final) :
[1] Cde Vs [8]Ghi
[2] Abc Vs [7]Fgh
[3] Bcd Vs [6]Efg
[4] Hij Vs [5]Def
Result of Matches(Quarter Final) :
[1] Cde Vs [8]Ghi
[8] Ghi wins
[2] Abc Vs [7]Fgh
[2] Abc wins
[3] Bcd Vs [6]Efg
[3] Bcd wins
[4] Hij Vs [5]Def
[4] Hij wins
Order of Matches(Semi Final) :
[2] Abc Vs 8] Ghi
[3] Bcd Vs 4] Hij
Result of Matches(Semi Final) :
[2] Abc Vs 8] Ghi
[8] Ghi wins
[3] Bcd Vs 4] Hij
[3] Bcd wins
Order of Matches(Final) :
[8] Ghi Vs 3] Bcd
Result of Final :
[8] Ghi Vs 3] Bcd
[3] Bcd wins the tournament
[8] Ghi is Runner-UP