MCPcopy Create free account
hub / github.com/MolinDeng/Princeton-algs4 / mouseClicked

Method mouseClicked

LabEnv/09Lab/BoggleGame.java:682–727  ·  view source on GitHub ↗
(MouseEvent arg0)

Source from the content-addressed store, hash-verified

680 cubes[i].setBackground(new Color(146, 183, 219));
681 cubes[i].addMouseListener(new MouseListener() {
682 @Override
683 public void mouseClicked(MouseEvent arg0) {
684 if (inGame) {
685 if (path == null) {
686 path = new int[NUM_OF_CUBES];
687 for (int n = 0; n < path.length; n++) {
688 path[n] = -1;
689 }
690 path[0] = cur;
691 highlightCubes();
692 return;
693 }
694 for (int j = 0; j < path.length; j++) {
695 // if it is the first cube clicked
696 if (j == 0 && path[j] == -1) {
697 path[j] = cur;
698 break;
699 }
700 // if the cube clicked is in the path
701 else if (path[j] == cur) {
702 // check if it is the last cube or the last one in the current path
703 //if so un-highlight it
704 if (j == path.length-1 || path[j+1] == -1) {
705 cubes[cur].setBackground(new Color(146, 183, 219));
706 path[j] = -1;
707 }
708 break;
709 }
710 // check for adjacency to the last cube in the path
711 else if (path[j] == -1) {
712 // row above
713 if (path[j-1] >= cur-BOARD_COLS-1 && path[j-1] <= cur-BOARD_COLS+1)
714 path[j] = cur;
715 // next to (same row)
716 else if (path[j-1] == cur-1 || path[j-1] == cur+1)
717 path[j] = cur;
718 // row below
719 else if (path[j-1] >= cur+BOARD_COLS-1 && path[j-1] <= cur+BOARD_COLS+1)
720 path[j] = cur;
721
722 break;
723 }
724 }
725 highlightCubes();
726 }
727 }
728 @Override
729 public void mouseEntered(MouseEvent arg0) { }
730 @Override

Callers

nothing calls this directly

Calls 1

highlightCubesMethod · 0.95

Tested by

no test coverage detected