You will need to fork your JSFiddle for your List into a new

You will need to fork your JSFiddle for your List into a new Fiddle. In this Fiddle we are going to add sorting functions. This is a great time to clean up your list with things that you have learned. You should automatically populate the List with 20 element (random strings). Once you have completed this - you will add 2 sort functions, you can use any sort method that you desire for each of the sort functions. You can also delineate the functions by the name of the Sort function - so your functions might be QuickSort() and MergeSort() - if you chose to implement those 2 algorithms (you can select from any sort functions). The interface should have buttons to (1) Repopulate the list with Random strings. (2) Sort list with selected algorithm 1 (3) Sort list with selected algorithm 2 and (4) Insert a new random string entered by the user into the list. After each operation it should display the new list. The option 4 here will insert the new string entered by the user (you will need a check box) in the correct sorted position and you should print the new list with the new element on the screen.

Solution

Paste this in Js section

--------------------

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.sai.game.BoardGame;
import com.sai.game.BoardGame.GameState;
import com.sai.model.BoardTwoPlayers;

public class Gametictactoe extends JPanel implements Observer{

   private static final long serialVersionUID = 6474899766649949747L;

   private int dvsonwidth = 0;
  
   private int dvsnsHgt = 0;

   BoardGame crntBrdGm;

  
   boolean dltCliks = true;
  
  
   public Gametictactoe(BoardGame brdGame){
       resetWithNewGame(brdGame);
   }
  
   public void resetWithNewGame(BoardGame brdGame){
       if (crntBrdGm != null) {
           crntBrdGm.deleteObservers();
       }
       crntBrdGm = brdGame;
       crntBrdGm.addObserver(this);
       init();
   }


  
   private void init(){
       dvsnsHgt = crntBrdGm.getBoard().getHeight();
       dvsonwidth = crntBrdGm.getBoard().getWidth();
       dltCliks = true;
       addMouseListener(new MouseAdapter() {
           @Override
           public void mouseClicked(MouseEvent e) {
               if (dltCliks) {
                   mouseClickHandler(e);
               }
           }
       });
   }


   @Override
   protected void paintComponent(Graphics g) {
       super.paintComponent(g);
       g.setColor(Color.BLACK);
       g.fillRect(0, 0, getBounds().width, getBounds().height);

       drawLines(g);
       fillWithMoves(g);
   }
   private void drawLines(Graphics g){
       Graphics2D g2 = (Graphics2D) g;
       g.setColor(Color.YELLOW);

       int fullWidth = getBounds().width;
       int fullheight = getBounds().height;

       g2.setStroke(new BasicStroke(3));
       for (int i = 1; i < dvsonwidth; i++) {
           float currentX = i * (fullWidth / dvsonwidth);
           Line2D newLine = new Line2D.Float(new Point2D.Float(currentX, 0.0f), new Point2D.Float(currentX, fullheight));
           g2.draw(newLine);  
       }

       for (int i = 1; i < dvsnsHgt; i++) {
           float currentY = i * (fullheight / dvsnsHgt);
           Line2D newLine = new Line2D.Float(new Point2D.Float(0, currentY), new Point2D.Float(fullWidth, currentY));
           g2.draw(newLine);  
       }
   }

   private void fillWithMoves(Graphics g){
       int divisionWidth = getWidth() / dvsonwidth;
       int divisionHeight = getHeight() / dvsnsHgt;
      
       int fontSize = Math.min(divisionWidth, divisionHeight) /2;
       g.setFont(new Font(\"Arial\", Font.PLAIN, fontSize));
      
      
       for (int i = 0; i < dvsonwidth; i++) {
           for (int j = 0; j < dvsnsHgt; j++) {
              
               String str = \"-\";
               int currentPosition = this.crntBrdGm.getBoard().getPiece(new Point(i,j));
               if (currentPosition == BoardTwoPlayers.PLAYER_EMPTY) {
                   str = \"-\";
               }else if (currentPosition == BoardTwoPlayers.PLAYER_O) {
                   str = \"O\";
               }else {
                   str = \"X\";
               }
              
               int currentOffsetX = (i * divisionWidth) + (divisionWidth / 2) - fontSize/2;
               int currentOffsetY = (j * divisionHeight) + (divisionHeight /2);
               g.drawString(str, currentOffsetX, currentOffsetY);
           }
       }

   }

   private void mouseClickHandler(MouseEvent event){
       if (!dltCliks) {
           return;
       }
       int width = getWidth();
       int height = getHeight();

       int cellWidth = width / dvsonwidth;
       int cellHeight = height / dvsnsHgt;

       int column = event.getX() / cellWidth;
       int row = event.getY() / cellHeight;

       Point selectedCell = new Point(column, row);
       System.out.println(\"point touched: \" + selectedCell);
      
  
       if (crntBrdGm.getBoard().isValidMove(selectedCell)){
           try {
               crntBrdGm.makeMoveForNextCurrentPlayer(selectedCell);
           } catch (Exception e) {
               e.printStackTrace();
           }
       }else{
           System.out.println(\"not a valid move. Valid moves are:\");
           System.out.println(crntBrdGm.getBoard().getEmptyValidSpaces().toString());
           System.out.println(\"\ \");
       }
      
      
   }

   public enum GameType{
       connect4, tictactoe
   }

   @Override
   public void update(Observable o, Object arg) {
       this.repaint();
      
       GameState currentState = crntBrdGm.getCurrentGameState();
       if (currentState == GameState.oWins || currentState == GameState.xWins) {
           JOptionPane.showMessageDialog(null, (currentState == GameState.oWins? \"O\" : \"X\") + \" wins the game!\");
           dltCliks = false;
       }else {
           if (crntBrdGm.getCurrentGameState() == GameState.draw) {
               JOptionPane.showMessageDialog(null, \"Game is a draw!\");
           }
       }
      
       System.out.println(crntBrdGm.getBoard());
   }
  
   public boolean gameHasStopped(){
       return !dltCliks;
   }
}

=============

Paste this in HTML section

<button onclick=\"fillrandomStrings()\">Repopulate the list with randome String algorithm</button>
<button onclick=\"quicksort()\">Sort list with QuickSort algorithm</button>
<span id=\"quicklist\"></span>
<button onclick=\"mergesort()\">Sort list with QuickSort algorithm</button>
<span id=\"mergelist\"></span>
<button onclick=\"insertnewlistbyuser()\">Insert new list byuser</button>
<span id=\"userlist\"></span>

 You will need to fork your JSFiddle for your List into a new Fiddle. In this Fiddle we are going to add sorting functions. This is a great time to clean up you
 You will need to fork your JSFiddle for your List into a new Fiddle. In this Fiddle we are going to add sorting functions. This is a great time to clean up you
 You will need to fork your JSFiddle for your List into a new Fiddle. In this Fiddle we are going to add sorting functions. This is a great time to clean up you
 You will need to fork your JSFiddle for your List into a new Fiddle. In this Fiddle we are going to add sorting functions. This is a great time to clean up you

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site