From 1189ad2319d49f7eb2a1a4a6ac7b171efd0cb755 Mon Sep 17 00:00:00 2001 From: Gardy Date: Wed, 15 Mar 2017 17:32:02 -0700 Subject: [PATCH 01/13] Added when click left gridX changes the row color randomly --- Lab5/src/MyMouseAdapter.java | 84 ++++++++++++++++++++++++++---------- Lab5/src/MyPanel.java | 4 ++ 2 files changed, 65 insertions(+), 23 deletions(-) diff --git a/Lab5/src/MyMouseAdapter.java b/Lab5/src/MyMouseAdapter.java index 69f1659..899f068 100644 --- a/Lab5/src/MyMouseAdapter.java +++ b/Lab5/src/MyMouseAdapter.java @@ -7,8 +7,34 @@ import javax.swing.JFrame; -public class MyMouseAdapter extends MouseAdapter { +public class MyMouseAdapter extends MouseAdapter +{ private Random generator = new Random(); + + public Color randomColor() // set newColor t random color + { + Color newColor=null; + switch (generator.nextInt(5)) + { + case 0: + newColor = Color.YELLOW; + break; + case 1: + newColor = Color.MAGENTA; + break; + case 2: + newColor = Color.BLACK; + break; + case 3: + newColor = new Color(0x964B00); //Brown (from http://simple.wikipedia.org/wiki/List_of_colors) + break; + case 4: + newColor = new Color(0xB57EDC); //Lavender (from http://simple.wikipedia.org/wiki/List_of_colors) + break; + } + return newColor; + } + public void mousePressed(MouseEvent e) { switch (e.getButton()) { case 1: //Left mouse button @@ -19,6 +45,7 @@ public void mousePressed(MouseEvent e) { return; } } + JFrame myFrame = (JFrame) c; MyPanel myPanel = (MyPanel) myFrame.getContentPane().getComponent(0); Insets myInsets = myFrame.getInsets(); @@ -76,36 +103,47 @@ public void mouseReleased(MouseEvent e) { //Do nothing } else { //Released the mouse button on the same cell where it was pressed - if ((gridX == 0) || (gridY == 0)) { - //On the left column and on the top row... do nothing - } else { - //On the grid other than on the left column and on the top row: + if ((gridX == 0) || (gridY == 0))//Paint random color every cell on row + { + + for (int i = 1; i < myPanel.getTotalCol(); i++) + { + Color newColor = null; + do + { + newColor=randomColor(); + } + while(myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY].equals(newColor)); + + + //Top row + if(!(gridX==0 && gridY==0)) + { + myPanel.colorArray[(myPanel.mouseDownGridX)+i][myPanel.mouseDownGridY] = newColor; + myPanel.repaint(); + } + } + //myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = newColor; + + + } else{ + //On the grid other than on the left column and on the top row: set random color Color newColor = null; - switch (generator.nextInt(5)) { - case 0: - newColor = Color.YELLOW; - break; - case 1: - newColor = Color.MAGENTA; - break; - case 2: - newColor = Color.BLACK; - break; - case 3: - newColor = new Color(0x964B00); //Brown (from http://simple.wikipedia.org/wiki/List_of_colors) - break; - case 4: - newColor = new Color(0xB57EDC); //Lavender (from http://simple.wikipedia.org/wiki/List_of_colors) - break; + do + { + newColor=randomColor(); } + while(myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY].equals(newColor)); myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = newColor; myPanel.repaint(); + + } } } + myPanel.repaint(); + break; } - myPanel.repaint(); - break; case 3: //Right mouse button //Do nothing break; diff --git a/Lab5/src/MyPanel.java b/Lab5/src/MyPanel.java index 3283509..ad8b846 100644 --- a/Lab5/src/MyPanel.java +++ b/Lab5/src/MyPanel.java @@ -17,6 +17,10 @@ public class MyPanel extends JPanel { public int mouseDownGridX = 0; public int mouseDownGridY = 0; public Color[][] colorArray = new Color[TOTAL_COLUMNS][TOTAL_ROWS]; + + public int getTotalCol() { + return TOTAL_COLUMNS; + } public MyPanel() { //This is the constructor... this code runs first to initialize if (INNER_CELL_SIZE + (new Random()).nextInt(1) < 1) { //Use of "random" to prevent unwanted Eclipse warning throw new RuntimeException("INNER_CELL_SIZE must be positive!"); From cb6c862f0eda2181d5dcf8748316b521f23c0676 Mon Sep 17 00:00:00 2001 From: Gardy Date: Wed, 15 Mar 2017 17:52:59 -0700 Subject: [PATCH 02/13] Added when clicked top cell on top row pain entire colum --- Lab5/src/MyMouseAdapter.java | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/Lab5/src/MyMouseAdapter.java b/Lab5/src/MyMouseAdapter.java index 899f068..f3d56bc 100644 --- a/Lab5/src/MyMouseAdapter.java +++ b/Lab5/src/MyMouseAdapter.java @@ -103,9 +103,11 @@ public void mouseReleased(MouseEvent e) { //Do nothing } else { //Released the mouse button on the same cell where it was pressed - if ((gridX == 0) || (gridY == 0))//Paint random color every cell on row + if ((gridX == 0) || (gridY == 0))//Paint random color entire row or colum except top and left { + if(!(gridY==0))//paint row + { for (int i = 1; i < myPanel.getTotalCol(); i++) { Color newColor = null; @@ -116,14 +118,35 @@ public void mouseReleased(MouseEvent e) { while(myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY].equals(newColor)); - //Top row + if(!(gridX==0 && gridY==0)) { myPanel.colorArray[(myPanel.mouseDownGridX)+i][myPanel.mouseDownGridY] = newColor; myPanel.repaint(); } + } + }else//paint colum + { + for (int i = 1; i < myPanel.getTotalCol(); i++) + { + Color newColor = null; + do + { + newColor=randomColor(); + } + while(myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY].equals(newColor)); + + + + if(!(gridX==0 && gridY==0)) + { + myPanel.colorArray[(myPanel.mouseDownGridX)][myPanel.mouseDownGridY+i] = newColor; + myPanel.repaint(); + } //myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = newColor; + } + } } else{ From da5ac771d061aa5c4e92f32f741c3520b84530f4 Mon Sep 17 00:00:00 2001 From: Gardy Date: Wed, 15 Mar 2017 18:06:17 -0700 Subject: [PATCH 03/13] Added when clicked 0,0 cell change color diagonal --- Lab5/src/MyMouseAdapter.java | 100 ++++++++++++++--------------------- 1 file changed, 39 insertions(+), 61 deletions(-) diff --git a/Lab5/src/MyMouseAdapter.java b/Lab5/src/MyMouseAdapter.java index f3d56bc..6e290ba 100644 --- a/Lab5/src/MyMouseAdapter.java +++ b/Lab5/src/MyMouseAdapter.java @@ -10,7 +10,7 @@ public class MyMouseAdapter extends MouseAdapter { private Random generator = new Random(); - + public Color randomColor() // set newColor t random color { Color newColor=null; @@ -105,74 +105,52 @@ public void mouseReleased(MouseEvent e) { //Released the mouse button on the same cell where it was pressed if ((gridX == 0) || (gridY == 0))//Paint random color entire row or colum except top and left { - - if(!(gridY==0))//paint row - { - for (int i = 1; i < myPanel.getTotalCol(); i++) - { - Color newColor = null; - do - { - newColor=randomColor(); - } - while(myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY].equals(newColor)); - - - - if(!(gridX==0 && gridY==0)) - { - myPanel.colorArray[(myPanel.mouseDownGridX)+i][myPanel.mouseDownGridY] = newColor; - myPanel.repaint(); - } - - } - }else//paint colum - { - for (int i = 1; i < myPanel.getTotalCol(); i++) - { - Color newColor = null; - do - { - newColor=randomColor(); - } - while(myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY].equals(newColor)); - - - if(!(gridX==0 && gridY==0)) - { - myPanel.colorArray[(myPanel.mouseDownGridX)][myPanel.mouseDownGridY+i] = newColor; - myPanel.repaint(); - } - //myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = newColor; - } - } - - - } else{ - //On the grid other than on the left column and on the top row: set random color - Color newColor = null; - do + if(gridY!=0)//paint row { - newColor=randomColor(); + for (int i = 1; i < myPanel.getTotalCol(); i++) + { + myPanel.colorArray[(myPanel.mouseDownGridX)+i][myPanel.mouseDownGridY] = randomColor(); + } + } + else if(gridX!=0)//paint row + { + for (int i = 1; i < myPanel.getTotalCol(); i++) + { + myPanel.colorArray[(myPanel.mouseDownGridX)][myPanel.mouseDownGridY+i] = randomColor(); + } + } + else { + for (int i = 1; i < myPanel.getTotalCol(); i++) + { + myPanel.colorArray[(myPanel.mouseDownGridX)+i][myPanel.mouseDownGridY+i] = randomColor(); + } } - while(myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY].equals(newColor)); - myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = newColor; - myPanel.repaint(); + }else{ + //On the grid other than on the left column and on the top row: set random color + Color newColor = null; + do + { + newColor=randomColor(); + } + while(myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY].equals(newColor)); + myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = newColor; + myPanel.repaint(); + + } } } + myPanel.repaint(); + break; } - myPanel.repaint(); + case 3: //Right mouse button + //Do nothing + break; + default: //Some other button (2 = Middle mouse button, etc.) + //Do nothing break; } - case 3: //Right mouse button - //Do nothing - break; - default: //Some other button (2 = Middle mouse button, etc.) - //Do nothing - break; } - } -} \ No newline at end of file + } \ No newline at end of file From 7fe12a0f6ce5d7fcf80962ccaeca0a418aae4d00 Mon Sep 17 00:00:00 2001 From: Gardy Date: Thu, 16 Mar 2017 17:07:56 -0700 Subject: [PATCH 04/13] added a 3x3 color --- Lab5/src/MyMouseAdapter.java | 17 +++++++++++++++-- Lab5/src/MyPanel.java | 14 +++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Lab5/src/MyMouseAdapter.java b/Lab5/src/MyMouseAdapter.java index 6e290ba..e948f0a 100644 --- a/Lab5/src/MyMouseAdapter.java +++ b/Lab5/src/MyMouseAdapter.java @@ -60,7 +60,8 @@ public void mousePressed(MouseEvent e) { myPanel.mouseDownGridY = myPanel.getGridY(x, y); myPanel.repaint(); break; - case 3: //Right mouse button + case 3: + //Right mouse button //Do nothing break; default: //Some other button (2 = Middle mouse button, etc.) @@ -90,6 +91,8 @@ public void mouseReleased(MouseEvent e) { myPanel.y = y; int gridX = myPanel.getGridX(x, y); int gridY = myPanel.getGridY(x, y); + + if ((myPanel.mouseDownGridX == -1) || (myPanel.mouseDownGridY == -1)) { //Had pressed outside //Do nothing @@ -105,6 +108,16 @@ public void mouseReleased(MouseEvent e) { //Released the mouse button on the same cell where it was pressed if ((gridX == 0) || (gridY == 0))//Paint random color entire row or colum except top and left { + if(gridX==0 && gridY==10)//colors 3x3 center randomly + { + for(int i=4;i<7;i++) + { + for (int j=4;j<7;j++) + { + myPanel.colorArray[i][j] = randomColor(); + } + } + } if(gridY!=0)//paint row { @@ -113,7 +126,7 @@ public void mouseReleased(MouseEvent e) { myPanel.colorArray[(myPanel.mouseDownGridX)+i][myPanel.mouseDownGridY] = randomColor(); } } - else if(gridX!=0)//paint row + else if(gridX!=0)//paint colum { for (int i = 1; i < myPanel.getTotalCol(); i++) { diff --git a/Lab5/src/MyPanel.java b/Lab5/src/MyPanel.java index ad8b846..c07da39 100644 --- a/Lab5/src/MyPanel.java +++ b/Lab5/src/MyPanel.java @@ -18,9 +18,20 @@ public class MyPanel extends JPanel { public int mouseDownGridY = 0; public Color[][] colorArray = new Color[TOTAL_COLUMNS][TOTAL_ROWS]; - public int getTotalCol() { + public Color[][] getColorArray() + { + return colorArray; + } + public int getTotalCol() + { return TOTAL_COLUMNS; } + + public int getTotalRows() + { + return TOTAL_ROWS; + } + public MyPanel() { //This is the constructor... this code runs first to initialize if (INNER_CELL_SIZE + (new Random()).nextInt(1) < 1) { //Use of "random" to prevent unwanted Eclipse warning throw new RuntimeException("INNER_CELL_SIZE must be positive!"); @@ -43,6 +54,7 @@ public MyPanel() { //This is the constructor... this code runs first to initia } } } + public void paintComponent(Graphics g) { super.paintComponent(g); From 50285602d317ff80fb45de5e2e140a4a6e771d9e Mon Sep 17 00:00:00 2001 From: Gardy Date: Fri, 17 Mar 2017 19:16:47 -0700 Subject: [PATCH 05/13] added when right click outside the grid change all white cells --- Lab5/src/MyMouseAdapter.java | 90 +++++++++++++++++++++++++++++++++--- Lab5/src/MyPanel.java | 4 -- 2 files changed, 83 insertions(+), 11 deletions(-) diff --git a/Lab5/src/MyMouseAdapter.java b/Lab5/src/MyMouseAdapter.java index e948f0a..cc004c9 100644 --- a/Lab5/src/MyMouseAdapter.java +++ b/Lab5/src/MyMouseAdapter.java @@ -61,6 +61,25 @@ public void mousePressed(MouseEvent e) { myPanel.repaint(); break; case 3: + Component cr = e.getComponent(); + while (!(cr instanceof JFrame)) { + cr = cr.getParent(); + if (cr == null) { + return; + } + } + + JFrame myRightClickFrame = (JFrame) cr; + MyPanel myRightClickPanel = (MyPanel) myRightClickFrame.getContentPane().getComponent(0); + Insets myRightClickInsets = myRightClickFrame.getInsets(); + int xr1 = myRightClickInsets.left; + int yr1 = myRightClickInsets.top; + e.translatePoint(-xr1, -yr1); + int xr2 = e.getX(); + int yr2 = e.getY(); + myRightClickPanel.x = xr2; + myRightClickPanel.y = yr2; + myRightClickPanel.repaint(); //Right mouse button //Do nothing break; @@ -70,7 +89,8 @@ public void mousePressed(MouseEvent e) { } } public void mouseReleased(MouseEvent e) { - switch (e.getButton()) { + switch (e.getButton()) + { case 1: //Left mouse button Component c = e.getComponent(); while (!(c instanceof JFrame)) { @@ -121,7 +141,7 @@ public void mouseReleased(MouseEvent e) { if(gridY!=0)//paint row { - for (int i = 1; i < myPanel.getTotalCol(); i++) + for (int i = 1; i < myPanel.getTotalCol(); i++)//created getter for private variable in myPanel { myPanel.colorArray[(myPanel.mouseDownGridX)+i][myPanel.mouseDownGridY] = randomColor(); } @@ -134,11 +154,11 @@ else if(gridX!=0)//paint colum } } else { - for (int i = 1; i < myPanel.getTotalCol(); i++) - { - myPanel.colorArray[(myPanel.mouseDownGridX)+i][myPanel.mouseDownGridY+i] = randomColor(); + for (int i = 1; i < myPanel.getTotalCol(); i++)//paint diagonal + { + myPanel.colorArray[(myPanel.mouseDownGridX)+i][myPanel.mouseDownGridY+i] = randomColor(); + } } - } }else{ //On the grid other than on the left column and on the top row: set random color @@ -158,7 +178,63 @@ else if(gridX!=0)//paint colum myPanel.repaint(); break; } - case 3: //Right mouse button + case 3: //when right click outside grid change white cells color + Component cr = e.getComponent(); + while (!(cr instanceof JFrame)) { + cr = cr.getParent(); + if (cr == null) { + return; + } + } + + JFrame myRightClickFrame = (JFrame) cr; + MyPanel myRightClickPanel = (MyPanel) myRightClickFrame.getContentPane().getComponent(0); + Insets myRightClickInsets = myRightClickFrame.getInsets(); + int xr1 = myRightClickInsets.left; + int yr1 = myRightClickInsets.top; + e.translatePoint(-xr1, -yr1); + int xr2 = e.getX(); + int yr2 = e.getY(); + myRightClickPanel.x = xr2; + myRightClickPanel.y = yr2; + + + int rightClickGridX = myRightClickPanel.getGridX(xr2, yr2); + int rightClickGridY = myRightClickPanel.getGridY(xr2, yr2); + Color newColor=null; + + if ((myRightClickPanel.mouseDownGridX == -1) || (myRightClickPanel.mouseDownGridY == -1)) + { + }else + { + if ((rightClickGridX == -1) || (rightClickGridY == -1)) + { + for(int i=1;i Date: Sun, 19 Mar 2017 08:33:29 -0700 Subject: [PATCH 06/13] added arrays to set mines, neighbores and reveal cell when clicked. if a mine is clicked reveal all mines --- Lab5/src/MyMouseAdapter.java | 173 +++++++++++++---------------------- Lab5/src/MyPanel.java | 54 ++++++++++- 2 files changed, 115 insertions(+), 112 deletions(-) diff --git a/Lab5/src/MyMouseAdapter.java b/Lab5/src/MyMouseAdapter.java index cc004c9..5a313ef 100644 --- a/Lab5/src/MyMouseAdapter.java +++ b/Lab5/src/MyMouseAdapter.java @@ -9,31 +9,7 @@ public class MyMouseAdapter extends MouseAdapter { - private Random generator = new Random(); - - public Color randomColor() // set newColor t random color - { - Color newColor=null; - switch (generator.nextInt(5)) - { - case 0: - newColor = Color.YELLOW; - break; - case 1: - newColor = Color.MAGENTA; - break; - case 2: - newColor = Color.BLACK; - break; - case 3: - newColor = new Color(0x964B00); //Brown (from http://simple.wikipedia.org/wiki/List_of_colors) - break; - case 4: - newColor = new Color(0xB57EDC); //Lavender (from http://simple.wikipedia.org/wiki/List_of_colors) - break; - } - return newColor; - } + public void mousePressed(MouseEvent e) { switch (e.getButton()) { @@ -61,25 +37,7 @@ public void mousePressed(MouseEvent e) { myPanel.repaint(); break; case 3: - Component cr = e.getComponent(); - while (!(cr instanceof JFrame)) { - cr = cr.getParent(); - if (cr == null) { - return; - } - } - - JFrame myRightClickFrame = (JFrame) cr; - MyPanel myRightClickPanel = (MyPanel) myRightClickFrame.getContentPane().getComponent(0); - Insets myRightClickInsets = myRightClickFrame.getInsets(); - int xr1 = myRightClickInsets.left; - int yr1 = myRightClickInsets.top; - e.translatePoint(-xr1, -yr1); - int xr2 = e.getX(); - int yr2 = e.getY(); - myRightClickPanel.x = xr2; - myRightClickPanel.y = yr2; - myRightClickPanel.repaint(); + //Right mouse button //Do nothing break; @@ -128,50 +86,34 @@ public void mouseReleased(MouseEvent e) { //Released the mouse button on the same cell where it was pressed if ((gridX == 0) || (gridY == 0))//Paint random color entire row or colum except top and left { - if(gridX==0 && gridY==10)//colors 3x3 center randomly - { - for(int i=4;i<7;i++) - { - for (int j=4;j<7;j++) - { - myPanel.colorArray[i][j] = randomColor(); - } - } - } - - if(gridY!=0)//paint row - { - for (int i = 1; i < myPanel.getTotalCol(); i++)//created getter for private variable in myPanel - { - myPanel.colorArray[(myPanel.mouseDownGridX)+i][myPanel.mouseDownGridY] = randomColor(); - } - } - else if(gridX!=0)//paint colum - { - for (int i = 1; i < myPanel.getTotalCol(); i++) - { - myPanel.colorArray[(myPanel.mouseDownGridX)][myPanel.mouseDownGridY+i] = randomColor(); - } - } - else { - for (int i = 1; i < myPanel.getTotalCol(); i++)//paint diagonal - { - myPanel.colorArray[(myPanel.mouseDownGridX)+i][myPanel.mouseDownGridY+i] = randomColor(); - } - } + }else{ //On the grid other than on the left column and on the top row: set random color - Color newColor = null; - do + //Color newColor = null; + myPanel.revealed[myPanel.mouseDownGridX][myPanel.mouseDownGridY]= true; + if(myPanel.mines[myPanel.mouseDownGridX][myPanel.mouseDownGridY]== 1)//if it hits a bomb reveal everything { - newColor=randomColor(); - } - while(myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY].equals(newColor)); - myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = newColor; - myPanel.repaint(); - - + for (int i = 1; i < myPanel.getTotalCol(); i++) + { + for (int j = 1; j < myPanel.getTotalRows(); j++) + { + myPanel.colorArray[i][j] = Color.GRAY; + if(myPanel.mines[i][j]== 1)//paints bomb cells black + myPanel.colorArray[i][j] = Color.BLACK; + + } + } + myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = Color.BLACK; + + + }else + { + + myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = Color.GRAY; + System.out.println(myPanel.neighbores[myPanel.mouseDownGridX][myPanel.mouseDownGridY]); + } + } } } @@ -203,37 +145,48 @@ else if(gridX!=0)//paint colum int rightClickGridY = myRightClickPanel.getGridY(xr2, yr2); Color newColor=null; - if ((myRightClickPanel.mouseDownGridX == -1) || (myRightClickPanel.mouseDownGridY == -1)) - { - }else - { - if ((rightClickGridX == -1) || (rightClickGridY == -1)) - { - for(int i=1;i1 && y>1 && mines[x-1][y-1]==1)//top left + neighborCount++; + if(x>1 && y1 && mines[x-1][y]==1)//left + neighborCount++; + if(y>1 && mines[x][y-1]==1)//top + neighborCount++; + if(y Date: Sun, 19 Mar 2017 14:04:58 -0700 Subject: [PATCH 07/13] when clicked on bomb only reveal remaining bombs on grid --- Lab5/src/MyMouseAdapter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lab5/src/MyMouseAdapter.java b/Lab5/src/MyMouseAdapter.java index 5a313ef..c19d8db 100644 --- a/Lab5/src/MyMouseAdapter.java +++ b/Lab5/src/MyMouseAdapter.java @@ -98,7 +98,7 @@ public void mouseReleased(MouseEvent e) { { for (int j = 1; j < myPanel.getTotalRows(); j++) { - myPanel.colorArray[i][j] = Color.GRAY; + if(myPanel.mines[i][j]== 1)//paints bomb cells black myPanel.colorArray[i][j] = Color.BLACK; From 9c3c76caabd6610837b0a783e73bcac73d705f82 Mon Sep 17 00:00:00 2001 From: Gardy Date: Thu, 23 Mar 2017 12:37:03 -0700 Subject: [PATCH 08/13] fixed flagged bug --- Lab5/src/MyMouseAdapter.java | 61 +++++++++++++++++++----------------- Lab5/src/MyPanel.java | 13 +++++--- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/Lab5/src/MyMouseAdapter.java b/Lab5/src/MyMouseAdapter.java index c19d8db..965f389 100644 --- a/Lab5/src/MyMouseAdapter.java +++ b/Lab5/src/MyMouseAdapter.java @@ -91,29 +91,33 @@ public void mouseReleased(MouseEvent e) { }else{ //On the grid other than on the left column and on the top row: set random color //Color newColor = null; - myPanel.revealed[myPanel.mouseDownGridX][myPanel.mouseDownGridY]= true; - if(myPanel.mines[myPanel.mouseDownGridX][myPanel.mouseDownGridY]== 1)//if it hits a bomb reveal everything + //myPanel.revealed[myPanel.mouseDownGridX][myPanel.mouseDownGridY]= true; + if(myPanel.flagged[myPanel.mouseDownGridX][myPanel.mouseDownGridY]==false) { - for (int i = 1; i < myPanel.getTotalCol(); i++) - { - for (int j = 1; j < myPanel.getTotalRows(); j++) + if(myPanel.mines[myPanel.mouseDownGridX][myPanel.mouseDownGridY]== 1)//if it hits a bomb reveal everything + { + for (int i = 1; i < myPanel.getTotalCol(); i++) + { + for (int j = 1; j < myPanel.getTotalRows(); j++) + { + + if(myPanel.mines[i][j]== 1)//paints bomb cells black + myPanel.colorArray[i][j] = Color.BLACK; + + } + } + + myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = Color.BLACK; + + + }else { - - if(myPanel.mines[i][j]== 1)//paints bomb cells black - myPanel.colorArray[i][j] = Color.BLACK; - + myPanel.revealed[myPanel.mouseDownGridX][myPanel.mouseDownGridY]= true; + myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = Color.GRAY; + + System.out.println(myPanel.neighbores[myPanel.mouseDownGridX][myPanel.mouseDownGridY]); } - } - myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = Color.BLACK; - - - }else - { - - myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = Color.GRAY; - System.out.println(myPanel.neighbores[myPanel.mouseDownGridX][myPanel.mouseDownGridY]); - } - + } } } } @@ -137,6 +141,8 @@ public void mouseReleased(MouseEvent e) { e.translatePoint(-xr1, -yr1); int xr2 = e.getX(); int yr2 = e.getY(); + myRightClickPanel.mouseDownGridX = myRightClickPanel.getGridX(xr2, yr2); + myRightClickPanel.mouseDownGridY = myRightClickPanel.getGridY(xr2, yr2); myRightClickPanel.x = xr2; myRightClickPanel.y = yr2; @@ -158,17 +164,14 @@ public void mouseReleased(MouseEvent e) { //Do nothing } else { //Released the mouse button on the same cell where it was pressed - if ((rightClickGridX == 0) || (rightClickGridY == 0))//Paint random color entire row or colum except top and left - { - - - }else{ + //On the grid other than on the left column and on the top row: set random color - //Color newColor = null; + //Color newColor = null;*/ + - //SI QUITAS EL IF VES COMO PRIMERO TIENES QUE DAR LEFT CLICK PARA QUE COJA EL FLAGGED CON EL RIGHT CLICK if(myRightClickPanel.revealed[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY]!= true)//does not let to marked revealed mines { + if(myRightClickPanel.flagged[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY]== false)//paints the cell if it doesn't have a bomb { myRightClickPanel.colorArray[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY] = Color.RED; @@ -178,10 +181,10 @@ public void mouseReleased(MouseEvent e) { }else { myRightClickPanel.flagged[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY] = false; - + myRightClickPanel.colorArray[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY] = Color.WHITE; } } - } + } } myRightClickPanel.repaint(); diff --git a/Lab5/src/MyPanel.java b/Lab5/src/MyPanel.java index dd6c265..3f9ec33 100644 --- a/Lab5/src/MyPanel.java +++ b/Lab5/src/MyPanel.java @@ -19,10 +19,11 @@ public class MyPanel extends JPanel { public Color[][] colorArray = new Color[TOTAL_COLUMNS][TOTAL_ROWS]; Random random = new Random(); - int mines[][] = new int [10][11]; - int[][] neighbores = new int [10][11]; - boolean [][] revealed = new boolean [10][11]; - boolean [][] flagged = new boolean [10][11]; + int mines[][] = new int [TOTAL_COLUMNS][TOTAL_ROWS]; + int[][] neighbores = new int [TOTAL_COLUMNS][TOTAL_ROWS]; + boolean [][] revealed = new boolean [TOTAL_COLUMNS][TOTAL_ROWS]; + boolean [][] flagged = new boolean [TOTAL_COLUMNS][TOTAL_ROWS]; + String[][] neighboresText = new String [TOTAL_COLUMNS][TOTAL_ROWS]; @@ -94,6 +95,10 @@ public MyPanel() { //This is the constructor... this code runs first to initia neighborCount++; neighbores[x][y]=neighborCount; + if(neighborCount==0) + neighboresText[x][y] = ""; + else + neighboresText[x][y] = String.valueOf(neighbores[x][y]); } } From 7c28e3c5979aa7cee605f6ce79233dbb84106671 Mon Sep 17 00:00:00 2001 From: Gardy Date: Fri, 24 Mar 2017 09:25:20 -0700 Subject: [PATCH 09/13] added the mine sweeper branch to master --- Lab5/src/MyMouseAdapter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lab5/src/MyMouseAdapter.java b/Lab5/src/MyMouseAdapter.java index 965f389..eb3ae38 100644 --- a/Lab5/src/MyMouseAdapter.java +++ b/Lab5/src/MyMouseAdapter.java @@ -149,7 +149,7 @@ public void mouseReleased(MouseEvent e) { int rightClickGridX = myRightClickPanel.getGridX(xr2, yr2); int rightClickGridY = myRightClickPanel.getGridY(xr2, yr2); - Color newColor=null; + if ((myRightClickPanel.mouseDownGridX == -1) || (myRightClickPanel.mouseDownGridY == -1)) { //Had pressed outside From 7bdabbf642f23b0d312a48d7ec466e9706749cd4 Mon Sep 17 00:00:00 2001 From: Gardy Date: Sat, 25 Mar 2017 14:26:27 -0700 Subject: [PATCH 10/13] fixed grid to 9x9 white tiles and bug that let you flag bombs after being revealed. --- Lab5/src/MyMouseAdapter.java | 51 ++++++++++++++++++------------------ Lab5/src/MyPanel.java | 43 ++++++++++++++---------------- 2 files changed, 45 insertions(+), 49 deletions(-) diff --git a/Lab5/src/MyMouseAdapter.java b/Lab5/src/MyMouseAdapter.java index eb3ae38..eef13a6 100644 --- a/Lab5/src/MyMouseAdapter.java +++ b/Lab5/src/MyMouseAdapter.java @@ -84,41 +84,40 @@ public void mouseReleased(MouseEvent e) { //Do nothing } else { //Released the mouse button on the same cell where it was pressed - if ((gridX == 0) || (gridY == 0))//Paint random color entire row or colum except top and left - { - - - }else{ - //On the grid other than on the left column and on the top row: set random color - //Color newColor = null; - //myPanel.revealed[myPanel.mouseDownGridX][myPanel.mouseDownGridY]= true; + + if(myPanel.flagged[myPanel.mouseDownGridX][myPanel.mouseDownGridY]==false) { if(myPanel.mines[myPanel.mouseDownGridX][myPanel.mouseDownGridY]== 1)//if it hits a bomb reveal everything { - for (int i = 1; i < myPanel.getTotalCol(); i++) + for (int i = 0; i < myPanel.getTotalCol(); i++) { - for (int j = 1; j < myPanel.getTotalRows(); j++) + for (int j = 0; j < myPanel.getTotalRows(); j++) { if(myPanel.mines[i][j]== 1)//paints bomb cells black myPanel.colorArray[i][j] = Color.BLACK; + myPanel.revealed[i][j]=true; } } - myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = Color.BLACK; + }else { - myPanel.revealed[myPanel.mouseDownGridX][myPanel.mouseDownGridY]= true; - myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = Color.GRAY; - - System.out.println(myPanel.neighbores[myPanel.mouseDownGridX][myPanel.mouseDownGridY]); + if(myPanel.revealed[myPanel.mouseDownGridX][myPanel.mouseDownGridY]==true){ + }else + { + myPanel.revealed[myPanel.mouseDownGridX][myPanel.mouseDownGridY]= true; + myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = Color.GRAY; + + System.out.println(myPanel.neighbores[myPanel.mouseDownGridX][myPanel.mouseDownGridY]); + } } } - } + } } myPanel.repaint(); @@ -169,19 +168,19 @@ public void mouseReleased(MouseEvent e) { //Color newColor = null;*/ - if(myRightClickPanel.revealed[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY]!= true)//does not let to marked revealed mines + if(myRightClickPanel.revealed[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY]!= true)//does not let to marked revealed spaces { - if(myRightClickPanel.flagged[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY]== false)//paints the cell if it doesn't have a bomb - { - myRightClickPanel.colorArray[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY] = Color.RED; - myRightClickPanel.flagged[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY] = true; - - - }else + if(myRightClickPanel.flagged[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY]== false)//paints the cell if it doesn't have a bomb { - myRightClickPanel.flagged[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY] = false; - myRightClickPanel.colorArray[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY] = Color.WHITE; + myRightClickPanel.colorArray[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY] = Color.RED; + myRightClickPanel.flagged[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY] = true; + + + }else + { + myRightClickPanel.flagged[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY] = false; + myRightClickPanel.colorArray[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY] = Color.WHITE; } } diff --git a/Lab5/src/MyPanel.java b/Lab5/src/MyPanel.java index 3f9ec33..08fe322 100644 --- a/Lab5/src/MyPanel.java +++ b/Lab5/src/MyPanel.java @@ -10,8 +10,8 @@ public class MyPanel extends JPanel { private static final int GRID_X = 25; private static final int GRID_Y = 25; private static final int INNER_CELL_SIZE = 29; - private static final int TOTAL_COLUMNS = 10; - private static final int TOTAL_ROWS = 11; //Last row has only one cell + private static final int TOTAL_COLUMNS = 9; + private static final int TOTAL_ROWS = 9; public int x = -1; public int y = -1; public int mouseDownGridX = 0; @@ -47,15 +47,10 @@ public MyPanel() { //This is the constructor... this code runs first to initia if (TOTAL_ROWS + (new Random()).nextInt(1) < 3) { //Use of "random" to prevent unwanted Eclipse warning throw new RuntimeException("TOTAL_ROWS must be at least 3!"); } - for (int x = 0; x < TOTAL_COLUMNS; x++) { //Top row - colorArray[x][0] = Color.LIGHT_GRAY; - } - for (int y = 0; y < TOTAL_ROWS; y++) { //Left column - colorArray[0][y] = Color.LIGHT_GRAY; - } - for (int x = 1; x < TOTAL_COLUMNS; x++) + + for (int x = 0; x < TOTAL_COLUMNS; x++) { //The rest of the grid - for (int y = 1; y < TOTAL_ROWS; y++) + for (int y = 0; y < TOTAL_ROWS; y++) { if(random.nextInt(100)<20)//asigns mines to cell 20% chance { @@ -70,26 +65,26 @@ public MyPanel() { //This is the constructor... this code runs first to initia colorArray[x][y] = Color.WHITE; } } - for (int x=1;x1 && y>1 && mines[x-1][y-1]==1)//top left + if(x>0 && y>0 && mines[x-1][y-1]==1)//top left neighborCount++; - if(x>1 && y0 && y1 && mines[x-1][y]==1)//left + if(x>0 && mines[x-1][y]==1)//left neighborCount++; - if(y>1 && mines[x][y-1]==1)//top + if(y>0 && mines[x][y-1]==1)//top neighborCount++; if(y0&&x= 0 && y == TOTAL_ROWS - 1) { //The lower left extra cell return x; } if (x < 0 || x > TOTAL_COLUMNS - 1 || y < 0 || y > TOTAL_ROWS - 2) { //Outside the rest of the grid @@ -187,7 +184,7 @@ public int getGridY(int x, int y) { } x = x / (INNER_CELL_SIZE + 1); y = y / (INNER_CELL_SIZE + 1); - if (x == 0 && y == TOTAL_ROWS - 1) { //The lower left extra cell + if (x >= 0 && y == TOTAL_ROWS - 1) { //The lower left extra cell return y; } if (x < 0 || x > TOTAL_COLUMNS - 1 || y < 0 || y > TOTAL_ROWS - 2) { //Outside the rest of the grid From bff808dd54dfea837013097253d4ef149686edfa Mon Sep 17 00:00:00 2001 From: Gardy Date: Sat, 25 Mar 2017 19:02:07 -0700 Subject: [PATCH 11/13] added number display when cells are clicked --- Lab5/src/MyPanel.java | 86 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 16 deletions(-) diff --git a/Lab5/src/MyPanel.java b/Lab5/src/MyPanel.java index 08fe322..d425a9f 100644 --- a/Lab5/src/MyPanel.java +++ b/Lab5/src/MyPanel.java @@ -24,6 +24,7 @@ public class MyPanel extends JPanel { boolean [][] revealed = new boolean [TOTAL_COLUMNS][TOTAL_ROWS]; boolean [][] flagged = new boolean [TOTAL_COLUMNS][TOTAL_ROWS]; String[][] neighboresText = new String [TOTAL_COLUMNS][TOTAL_ROWS]; + int MINE = 1, notClicked = 0; @@ -54,11 +55,11 @@ public MyPanel() { //This is the constructor... this code runs first to initia { if(random.nextInt(100)<20)//asigns mines to cell 20% chance { - mines [x][y]=1; + mines [x][y]=MINE; }else { - mines[x][y]=0; + mines[x][y]=notClicked; } flagged[x][y]=false;//sets all cell unflagged revealed[x][y]=false;//sets all the cells to not revealed @@ -69,24 +70,24 @@ public MyPanel() { //This is the constructor... this code runs first to initia { for (int y=0;y0 && y>0 && mines[x-1][y-1]==1)//top left + if(x>0 && y>0 && mines[x-1][y-1]==MINE)//top left neighborCount++; - if(x>0 && y0 && y0 && mines[x-1][y]==1)//left + if(x>0 && mines[x-1][y]==MINE)//left neighborCount++; - if(y>0 && mines[x][y-1]==1)//top + if(y>0 && mines[x][y-1]==MINE)//top neighborCount++; - if(y0&&x0&&x0 && y>0 && mines[x-1][y-1]==1)//top left + + if(x>0 && y0 && mines[x-1][y]==1)//left + + if(y>0 && mines[x][y-1]==1)//top + + if(y0&&x Date: Mon, 27 Mar 2017 14:36:24 -0400 Subject: [PATCH 12/13] Patch Notes: -Fixed JFrame Name -Added square clearing recursion method on myPanel -Called method when left click on a square with no neighbores --- Lab5/src/Main.java | 2 +- Lab5/src/MyMouseAdapter.java | 5 +-- Lab5/src/MyPanel.java | 70 ++++++++++++++++++++++++++++-------- 3 files changed, 58 insertions(+), 19 deletions(-) diff --git a/Lab5/src/Main.java b/Lab5/src/Main.java index ab19155..2cbda48 100644 --- a/Lab5/src/Main.java +++ b/Lab5/src/Main.java @@ -2,7 +2,7 @@ public class Main { public static void main(String[] args) { - JFrame myFrame = new JFrame("Color Grid"); + JFrame myFrame = new JFrame("MineSweeper"); myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); myFrame.setLocation(400, 150); myFrame.setSize(400, 400); diff --git a/Lab5/src/MyMouseAdapter.java b/Lab5/src/MyMouseAdapter.java index eef13a6..de9c6db 100644 --- a/Lab5/src/MyMouseAdapter.java +++ b/Lab5/src/MyMouseAdapter.java @@ -110,10 +110,7 @@ public void mouseReleased(MouseEvent e) { if(myPanel.revealed[myPanel.mouseDownGridX][myPanel.mouseDownGridY]==true){ }else { - myPanel.revealed[myPanel.mouseDownGridX][myPanel.mouseDownGridY]= true; - myPanel.colorArray[myPanel.mouseDownGridX][myPanel.mouseDownGridY] = Color.GRAY; - - System.out.println(myPanel.neighbores[myPanel.mouseDownGridX][myPanel.mouseDownGridY]); + myPanel.clearZeros(gridX, gridY); } } } diff --git a/Lab5/src/MyPanel.java b/Lab5/src/MyPanel.java index d425a9f..6bfa67d 100644 --- a/Lab5/src/MyPanel.java +++ b/Lab5/src/MyPanel.java @@ -24,7 +24,7 @@ public class MyPanel extends JPanel { boolean [][] revealed = new boolean [TOTAL_COLUMNS][TOTAL_ROWS]; boolean [][] flagged = new boolean [TOTAL_COLUMNS][TOTAL_ROWS]; String[][] neighboresText = new String [TOTAL_COLUMNS][TOTAL_ROWS]; - int MINE = 1, notClicked = 0; + int MINE = 1, notClicked = 0, noNeighbores = 0; @@ -217,34 +217,76 @@ public int getGridY(int x, int y) { return y; } - public void clearZeros(int [][] toClear,int xpos, int ypos) + public void clearZeros(int xpos, int ypos) { - if(toClear[xpos][ypos]==0) + if(neighbores[xpos][ypos]!=noNeighbores) { + revealed[xpos][ypos]= true; + colorArray[xpos][ypos] = Color.GRAY; return ; }else { - if(neighbores[xpos][ypos]==0) + revealed[xpos][ypos]= true; + colorArray[xpos][ypos] = Color.GRAY; + if(xpos>0 && ypos>0 && revealed[xpos-1][ypos-1]!=true )//top left { - if(x>0 && y>0 && mines[x-1][y-1]==1)//top left - - if(x>0 && y0 && mines[x-1][y]==1)//left + if(xpos>0 && ypos0 && mines[x][y-1]==1)//top + if(xpos>0 && revealed[xpos-1][ypos]!=true)//left + { + revealed[xpos-1][ypos]= true; + colorArray[xpos-1][ypos] = Color.GRAY; + clearZeros(xpos-1,ypos); + } - if(y0 && revealed[xpos][ypos-1]!=true)//top + { + revealed[xpos][ypos-1]= true; + colorArray[xpos][ypos-1] = Color.GRAY; + clearZeros(xpos,ypos-1); + } - if(x0&&x0 && xpos Date: Tue, 28 Mar 2017 15:15:04 -0400 Subject: [PATCH 13/13] Patch notes: -Erased unnecessary commentary --- Lab5/src/MyMouseAdapter.java | 9 +-------- Lab5/src/MyPanel.java | 14 -------------- 2 files changed, 1 insertion(+), 22 deletions(-) diff --git a/Lab5/src/MyMouseAdapter.java b/Lab5/src/MyMouseAdapter.java index de9c6db..5acc738 100644 --- a/Lab5/src/MyMouseAdapter.java +++ b/Lab5/src/MyMouseAdapter.java @@ -37,9 +37,6 @@ public void mousePressed(MouseEvent e) { myPanel.repaint(); break; case 3: - - //Right mouse button - //Do nothing break; default: //Some other button (2 = Middle mouse button, etc.) //Do nothing @@ -120,7 +117,7 @@ public void mouseReleased(MouseEvent e) { myPanel.repaint(); break; } - case 3: //when right click outside grid change white cells color + case 3: //when right click flag (paint red) Component cr = e.getComponent(); while (!(cr instanceof JFrame)) { cr = cr.getParent(); @@ -161,8 +158,6 @@ public void mouseReleased(MouseEvent e) { } else { //Released the mouse button on the same cell where it was pressed - //On the grid other than on the left column and on the top row: set random color - //Color newColor = null;*/ if(myRightClickPanel.revealed[myRightClickPanel.mouseDownGridX][myRightClickPanel.mouseDownGridY]!= true)//does not let to marked revealed spaces @@ -186,8 +181,6 @@ public void mouseReleased(MouseEvent e) { myRightClickPanel.repaint(); break; } - //Right mouse button - //Do nothing break; default: //Some other button (2 = Middle mouse button, etc.) //Do nothing diff --git a/Lab5/src/MyPanel.java b/Lab5/src/MyPanel.java index 6bfa67d..c344dc5 100644 --- a/Lab5/src/MyPanel.java +++ b/Lab5/src/MyPanel.java @@ -125,7 +125,6 @@ public void paintComponent(Graphics g) g.fillRect(x1, y1, width + 1, height + 1); //Draw the grid minus the bottom row (which has only one cell) - //By default, the grid will be 10x10 (see above: TOTAL_COLUMNS and TOTAL_ROWS) g.setColor(Color.BLACK); for (int y = 0; y <= TOTAL_ROWS ; y++) { g.drawLine(x1 + GRID_X, y1 + GRID_Y + (y * (INNER_CELL_SIZE + 1)), x1 + GRID_X + ((INNER_CELL_SIZE + 1) * TOTAL_COLUMNS), y1 + GRID_Y + (y * (INNER_CELL_SIZE + 1))); @@ -134,9 +133,6 @@ public void paintComponent(Graphics g) g.drawLine(x1 + GRID_X + (x * (INNER_CELL_SIZE + 1)), y1 + GRID_Y, x1 + GRID_X + (x * (INNER_CELL_SIZE + 1)), y1 + GRID_Y + ((INNER_CELL_SIZE + 1) * (TOTAL_ROWS ))); } - //Draw an additional cell at the bottom left - //g.drawRect(x1 + GRID_X, y1 + GRID_Y + ((INNER_CELL_SIZE + 1) * (TOTAL_ROWS - 1)), INNER_CELL_SIZE + 1, INNER_CELL_SIZE + 1); - //Paint cell colors for (int x = 0; x < TOTAL_COLUMNS; x++) { for (int y = 0; y < TOTAL_ROWS; y++) { @@ -153,16 +149,6 @@ public void paintComponent(Graphics g) } } -// for (int i = 0; i < TOTAL_COLUMNS; i++) -// { -// for (int j = 0; j < TOTAL_ROWS; j++) -// { -//// g.setColor(Color.WHITE); -// g.drawString(neighboresText[i][j], x *(INNER_CELL_SIZE+1)+38, (y*30)+44); -// g.setColor(Color.BLACK); -// -// } -// } } }