Sei sulla pagina 1di 11

1.

THE JOSEPHUS GAME (USING LINKED LISTS)

Algorithm JosephusGame /*This algorithm implements the josephus game which finds the best position for the person to stay alive*/ //HowMany Total number of soldiers //EveryWhich Total number of passes //BestPosition Best Position to stay alive Step 1: Read HowMany, EveryWhich Step 2: BestPosition BestSpot(HowMany, EveryWhich) Step 3: Form_Circle(HowMany) Step 4: PlayGame(HowMany, EveryWhich) Step 5: End

Algorithm BestSpot(int HowMany, int EveryWhich) /* To find the bestposition for Josephus to stand*/ Output: BestPosition of You Step 1: if (EveryWhich%20) then 1.1. BestPosition[2] 2 else 1.2. BestPosition[2] 1 Step 2: Repeat through step 3 for i3 to HowMany Step 3: BestPosition[i] (BestPosition[i-1]+EveryWhich)%i Step 4: if BestPosition[i]=0 then BestPosition[i] i Step 5: return BestPosition[i]

Algorithm Form_circle (int HowMany) /* To form the circle of soldiers*/ Output: Creation of circularly linked list

Node creation in a circularly linked list head number next

Data structure: Circularly linked list Step 1: Create a node consisting of number and next Struct node { int number; node *next; }; Step 2: start end head Step 3: headnext NULL Step 4: Repeat through step 7 for i 1 to HowMany Step 5: Create a new node p Step 6: p.number 1 Step 7: if (headnext=NULL) then 7.1. start end p; 7.2. headnext p; 7.3. pnext p; else 7.4. end.next p;

7.5. p.next start; 7.6. end end.next; Step 8: Return

Algorithm DeleteSoldier() : integer /*The position of dead soldiers*/ Data structure: Circularly linked list Step 1: Create a new node t Step 2: t start; Step 3: k start.data; Step 4: start start.next; Step 5: end.next start; Step 6: return(k);

Algorithm PlayGame(int HowMany, int EveryWhich) /* To determine the order of dead soldiers and to print the alive soldiers*/ Output: Order of elimination of soldiers, Alive soldier Step 1: Repeat through step 5while (HowMany > 1) Step 2: Repeat through step 5 for i 1 to EveryWhich Step 3: start startnext Step 4: end endnext Step 5: OrderEliminate [i++] DeleteSoldier ( ) Step 6: Repeat Step 7 for i 1 to HowMany Step 7: Print OrderEliminateWinner[i] Step 8: Return

1. THE JOSEPHUS GAME (USING LINKED LISTS)

Name

: M.Latha Karthigaa

Roll Number

: 09

Date of Assignment

: 09.09.2009

Due date for Demo

: 18.09.2009

Actual date of Demo

: 05.10.2009

Review: 1.

Suggestions from Faculty Date of Review Action taken Demo date

: Write a brief algorithm : 14.09.2009 : Rewritten the algorithm : 25.09.2009 : No need of separate module for small number of instructions

2.

Suggestions from Faculty

Date of Review Action taken Demo date

: 25.09.2009 : Rewritten the algorithm : 26.09.2009

PROGRAM:

package josephusproj1; //import appropriate header files import java.io.*; import java.lang.*; import java.applet.*; import java.awt.*; import java.awt.event.*; //Creating Applet Class public class JosephusProj1 extends Applet implements ActionListener { int HowMany, EveryWhich,ii; double S, C; private Label n = new Label("HowMany");//Number of Soldiers private Label m = new Label("EveryWhich"); //Number of passes private TextField m1 = new TextField(3); //Text field for "m" private TextField n1 = new TextField(3); //Text field for "n" Button bestspot = new Button("BESTSPOT"); //Button to calculate best spot Button reset = new Button("RESET"); //To reset the values private Label bs1 = new Label("THE BEST SPOT IS:"); //To display the label "best spot" private TextField bs = new TextField(3);//Text Field for "best spot" display Button play = new Button("PLAY");//To play game public void init() { //Add the Labels,buttons,TextFields in the Applet window add(n); add(m1); add(m); add(n1); add(bestspot); add(bs1); add(bs); add(reset); add(play); resize(750, 650);//Resize the Applet //add the action listeners for the buttons bestspot.addActionListener(this); reset.addActionListener(this); play.addActionListener(this); }

public void actionPerformed(ActionEvent e) { int[] BestPosition; int i; String str1 = m1.getText(); HowMany = Integer.parseInt(str1);//Convert "m" into integer ii=HowMany; String str2 = n1.getText(); EveryWhich = Integer.parseInt(str2); //convert "n" into integer BestPosition = new int[50]; String ss = e.getActionCommand(); //return the label of the button if (ss == "BESTSPOT") { /******Finding the best spot*******/ if ((EveryWhich % 2) != 0) { BestPosition[2] = 2; } else { BestPosition[2] = 1; } for (i = 3; i <= HowMany; i++) { BestPosition[i] = (BestPosition[i - 1] + EveryWhich) % i; if (BestPosition[i] == 0) { BestPosition[i] = i; } }/******Finding the best spot*******/ String io = Integer.toString(BestPosition[i - 1]); bs.setText(io); //Set the text field "best spot" } else if (ss == "RESET") //To reset the values { m1.setText(""); n1.setText(""); bs.setText(""); } else if (ss == "PLAY") { repaint();//call paint() function } }

public class node //create the linked list nodes { int number; //start field of the node node next;//second field which is a pointer to the next node node() { //Empty Constructor } } public class game { node start,end,head;//Objects for node game()//Default constructor { head=new node(); head.next=null; start=end=head; } /*****************Inserting elements in the Linked List********************/ void Form_Circle(int x) { node p=new node(); p.number=x; if(head.next==null) { start=end=p; head.next=p; p.next=p; } else { end.next=p; p.next=start; end=end.next; } } /*****************Inserting elements in the Linked List********************/ /***************Traverse the next element in the Linked List****************/ void nextnode() { start=start.next; end=end.next; }

int DeleteSoldier() { node t; int k; t=start; k=start.number; start=start.next; end.next=start; return(k); } } /*****************Traverse the next element in the Linked List****************/ /*************************Defining Paint() function*************************/ public void paint(Graphics g) { int flag=0,r=0; int []OrderEliminate = new int[50]; /********************Displaying Soldiers in a circular fashion******************/ for (int j=1;j<=HowMany;j++) { S = Math.sin(2 * Math.PI / HowMany * j) * 100; C = Math.cos(2 * Math.PI / HowMany * j) * 100; g.setColor(Color.black); String M = Integer.toString(j); g.setFont(new Font("Serif", Font.BOLD, 15)); g.drawString(M, 300 + (int) S, 308 + (int) C); g.setColor(Color.blue); g.fillOval(300+(int)S, 275+(int)C, 20, 20); g.setColor(Color.black); g.fillOval(305+(int)S,280+(int)C,4,4); g.fillOval(311+(int)S,280+(int)C,4,4); g.drawArc(305+(int)S,280+(int)C,10,10,-0,-180); String ll=bs.getText(); int g3=Integer.parseInt(ll); S = Math.sin(2 * Math.PI / ii * g3) * 100; C = Math.cos(2 * Math.PI / ii * g3) * 100; g.setColor(Color.green); g.fillOval(300+(int)S,275+(int)C,20,20); g.setColor(Color.black); g.fillOval(305+(int)S,280+(int)C,4,4); g.fillOval(311+(int)S,280+(int)C,4,4); g.drawArc(305+(int)S,280+(int)C,10,10,-0,-180); flag++; } /*********************Displaying Soldiers in a circular fashion*****************/

game q=new game(); for(int d=1;d<=HowMany;d++) { q.Form_Circle(d);//Insert the soldiers } while(HowMany>1) { for(int i=1;i<EveryWhich;i++) { q.nextnode(); //Move to the next element } OrderEliminate[r++]=q.DeleteSoldier();//Remove the dead soldiers Thread t=Thread.currentThread();//Thread creation try { for(int i=0;i<r;i++) { /*********************Fill Oval for the dead Soldiers*********************/ S = Math.sin(2 * Math.PI / ii * OrderEliminate[i]) * 100; C = Math.cos(2 * Math.PI / ii * OrderEliminate[i]) * 100; g.setColor(Color.red); g.fillOval(300+(int)S, 275+(int)C, 20, 20); g.setColor(Color.black); g.fillOval(305+(int)S,280+(int)C,4,4); g.fillOval(311+(int)S,280+(int)C,4,4); g.drawArc(305+(int)S,284+(int)C,10,10,0,180); g.setColor(Color.green); g.fillOval(700,500,20,20); g.setColor(Color.black); g.fillOval(705,504,4,4); g.fillOval(711,504,4,4); g.drawArc(705,504,10,10,-0,-180); g.drawString(" -Winner!!!",700,515); g.setColor(Color.red); g.fillOval(700,550,20,20); g.setColor(Color.black); g.fillOval(705,554,4,4); g.fillOval(711,554,4,4); g.drawArc(705,558,10,10,0,180); g.drawString(" -Dead Soldiers",700,565); g.setColor(Color.blue); g.fillOval(700,600,20,20); g.setColor(Color.black); g.fillOval(705,604,4,4); g.fillOval(711,604,4,4);

g.drawArc(705,604,10,10,-0,-180); g.drawString(" -Alive Soldiers",700,615); Thread.sleep(100);//Delay time setting /*********************Fill Oval for the dead Soldier*********************/ } --HowMany; } catch(InterruptedException ob) { System.out.println(ob); } } g.setColor(Color.black);

/**********************Print the order of death of soldiers*******************/ g.drawString("Order Of Elimination:",10,500); for(int i=0;i<ii-1;i++) { g.setColor(Color.black); String kkk=Integer.toString(OrderEliminate[i]); g.drawString(kkk,35*(i+5),500); /**********************Print the order of death of soldiers*******************/ } String ll=bs.getText(); int g3=Integer.parseInt(ll); /************************Fill Oval for alive soldiers*****************/ S = Math.sin(2 * Math.PI / ii * g3) * 100; C = Math.cos(2 * Math.PI / ii * g3) * 100; g.setColor(Color.green); g.fillOval(300+(int)S,275+(int)C,20,20); g.setColor(Color.black); g.fillOval(305+(int)S,280+(int)C,4,4); g.fillOval(311+(int)S,280+(int)C,4,4); g.drawArc(305+(int)S,280+(int)C,10,10,-0,-180); /************************Fill Oval for alive soldiers*****************/ } /**************Defining Paint() function*************/ }

RESULT: Test Case 1: HowMany = 2, EveryWhich > HowMany

Test Case 2: HowMany = 12, EveryWhich = 8

Potrebbero piacerti anche