MCPcopy Create free account
hub / github.com/PrajaktaSathe/Java / CellHandler

Class CellHandler

Programs/Games/peg-solitaire/PegSolitaire.java:960–1068  ·  view source on GitHub ↗

Inner class CellHandler that implements ActionListener

Source from the content-addressed store, hash-verified

958
959 /** Inner class CellHandler that implements ActionListener */
960 private class CellHandler implements ActionListener{
961 int row;
962 int col;
963 boolean flag;
964
965 /**
966 * Constructor of inner class with row and column values.
967 * Assign row, col to given parameters and flag to false.
968 */
969 public CellHandler(int rowVal, int colVal){
970 row = rowVal; col = colVal; flag = false;
971 }
972
973 /**
974 * actionPerformed method, invoked when an action occurs.
975 * @param event Indicates an action.
976 */
977 public void actionPerformed(ActionEvent event){
978 //IF USER FIRST CLICK BLACK CELL
979 //toggle becomes true, selected cell becomse yellow
980 if (board[row][col].getBackground() == Color.BLACK && !toggle){
981 toggle = true;
982 board[row][col].setBackground(Color.YELLOW);
983 }
984
985 //IF USER CLICK ANY OTHER CELL AFTER CLICKING ONE BLACK CELL
986 else if (toggle){
987 //Finding user's first selected row
988 int tempRow=0, tempCol=0;
989 for (int i = 0; i < board.length; ++i)
990 for (int j = 0; j < board.length; ++j)
991 if (board[i][j].getBackground() == Color.YELLOW){tempRow = i; tempCol = j; break;}
992 int yRow = tempRow, yCol = tempCol;
993
994 //If user clicks another black cell, make it yellow, make former one black again
995 if (board[row][col].getBackground() == Color.BLACK){
996 board[row][col].setBackground(Color.YELLOW);
997 board[yRow][yCol].setBackground(Color.BLACK);
998 }
999
1000 //If user clicks yellow cell again, make it black and make toggle false.
1001 else if (board[row][col].getBackground() == Color.YELLOW){ board[row][col].setBackground(Color.BLACK); toggle = false; }
1002
1003 //If user clicks gray cell (empty slot), check if move is valid
1004 else if (board[row][col].getBackground() == Color.GRAY) {
1005 //CHECK UP
1006 if (is_valid_up(yRow, yCol, row, col)){ //UP-RIGHT FOR BOARD 6
1007 board[yRow][yCol].setBackground(Color.GRAY);
1008 board[row+1][col].setBackground(Color.GRAY);
1009 board[row][col].setBackground(Color.BLACK);
1010 toggle = false;
1011 flag = true;
1012 moves.add(yRow*100+yCol*10+0);
1013 }
1014 //CHECK DOWN
1015 else if(is_valid_down(yRow, yCol, row, col)){ //DOWN-LEFT FOR BOARD 6
1016 board[yRow][yCol].setBackground(Color.GRAY);
1017 board[row-1][col].setBackground(Color.GRAY);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected