reorders the cells in the k-map, see #382

This commit is contained in:
hneemann 2020-03-11 11:31:07 +01:00
parent 94d7134a1e
commit 391b9e5b8f
3 changed files with 60 additions and 74 deletions

View File

@ -42,12 +42,9 @@ public class KarnaughMap implements Iterable<KarnaughMap.Cover> {
for (int row = 0; row < 2; row++) for (int row = 0; row < 2; row++)
for (int col = 0; col < 2; col++) for (int col = 0; col < 2; col++)
cells.add(new Cell(row, col)); cells.add(new Cell(row, col));
addToRow(0, 2, 0, true); // add the variables to the cells
addToRow(1, 2, 0, false); headerLeft = new Header(0, true, false).toRows(2, this);
addToCol(0, 2, 1, true); headerTop = new Header(1, true, false).toCols(2, this);
addToCol(1, 2, 1, false);
headerLeft = new Header(0, true, false); // define the headers needed
headerTop = new Header(1, true, false);
headerRight = null; headerRight = null;
headerBottom = null; headerBottom = null;
break; break;
@ -55,19 +52,10 @@ public class KarnaughMap implements Iterable<KarnaughMap.Cover> {
for (int row = 0; row < 2; row++) for (int row = 0; row < 2; row++)
for (int col = 0; col < 4; col++) for (int col = 0; col < 4; col++)
cells.add(new Cell(row, col)); cells.add(new Cell(row, col));
addToRow(0, 4, 0, true);
addToRow(1, 4, 0, false); headerLeft = new Header(0, true, false).toRows(4, this);
addToCol(0, 2, 1, true); headerTop = new Header(1, true, true, false, false).toCols(2, this);
addToCol(1, 2, 1, true); headerBottom = new Header(2, true, false, false, true).toCols(2, this);
addToCol(2, 2, 1, false);
addToCol(3, 2, 1, false);
addToCol(0, 2, 2, false);
addToCol(1, 2, 2, true);
addToCol(2, 2, 2, true);
addToCol(3, 2, 2, false);
headerLeft = new Header(0, true, false);
headerTop = new Header(1, true, true, false, false);
headerBottom = new Header(2, false, true, true, false);
headerRight = null; headerRight = null;
break; break;
case 4: case 4:
@ -75,26 +63,10 @@ public class KarnaughMap implements Iterable<KarnaughMap.Cover> {
for (int col = 0; col < 4; col++) for (int col = 0; col < 4; col++)
cells.add(new Cell(row, col)); cells.add(new Cell(row, col));
addToRow(0, 4, 0, true); headerLeft = new Header(0, true, true, false, false).toRows(4, this);
addToRow(1, 4, 0, true); headerRight = new Header(1, true, false, false, true).toRows(4, this);
addToRow(2, 4, 0, false); headerTop = new Header(2, true, true, false, false).toCols(4, this);
addToRow(3, 4, 0, false); headerBottom = new Header(3, true, false, false, true).toCols(4, this);
addToRow(0, 4, 1, false);
addToRow(1, 4, 1, true);
addToRow(2, 4, 1, true);
addToRow(3, 4, 1, false);
addToCol(0, 4, 2, true);
addToCol(1, 4, 2, true);
addToCol(2, 4, 2, false);
addToCol(3, 4, 2, false);
addToCol(0, 4, 3, false);
addToCol(1, 4, 3, true);
addToCol(2, 4, 3, true);
addToCol(3, 4, 3, false);
headerLeft = new Header(0, true, true, false, false);
headerRight = new Header(1, false, true, true, false);
headerTop = new Header(2, true, true, false, false);
headerBottom = new Header(3, false, true, true, false);
break; break;
default: default:
throw new KarnaughException(Lang.get("err_toManyVars")); throw new KarnaughException(Lang.get("err_toManyVars"));
@ -105,16 +77,6 @@ public class KarnaughMap implements Iterable<KarnaughMap.Cover> {
addExpression(expr); // create the covers addExpression(expr); // create the covers
} }
private void addToRow(int row, int cols, int var, boolean invert) {
for (int col = 0; col < cols; col++)
getCell(row, col).add(new VarState(var, invert));
}
private void addToCol(int col, int rows, int var, boolean invert) {
for (int row = 0; row < rows; row++)
getCell(row, col).add(new VarState(var, invert));
}
/** /**
* Returns the cell at the given position * Returns the cell at the given position
* *
@ -533,5 +495,33 @@ public class KarnaughMap implements Iterable<KarnaughMap.Cover> {
return invert[i]; return invert[i];
} }
/**
* Initializes the table according to the selected header.
*
* @param cols the number columns in the table
* @param kmap the k-map to use
* @return this for chained calls
*/
public Header toRows(int cols, KarnaughMap kmap) {
for (int row = 0; row < invert.length; row++)
for (int col = 0; col < cols; col++)
kmap.getCell(row, col).add(new VarState(var, invert[row]));
return this;
}
/**
* Initializes the table according to the selected header.
*
* @param rows the number rows in the table
* @param kmap the k-map to use
* @return this for chained calls
*/
public Header toCols(int rows, KarnaughMap kmap) {
for (int col = 0; col < invert.length; col++)
for (int row = 0; row < rows; row++)
kmap.getCell(row, col).add(new VarState(var, invert[col]));
return this;
}
} }
} }

View File

@ -5,10 +5,7 @@
*/ */
package de.neemann.digital.gui.components.karnaugh; package de.neemann.digital.gui.components.karnaugh;
import de.neemann.digital.analyse.expression.Constant; import de.neemann.digital.analyse.expression.*;
import de.neemann.digital.analyse.expression.Expression;
import de.neemann.digital.analyse.expression.ExpressionException;
import de.neemann.digital.analyse.expression.Variable;
import de.neemann.digital.analyse.parser.ParseException; import de.neemann.digital.analyse.parser.ParseException;
import de.neemann.digital.analyse.parser.Parser; import de.neemann.digital.analyse.parser.Parser;
import de.neemann.digital.analyse.quinemc.BoolTableBoolArray; import de.neemann.digital.analyse.quinemc.BoolTableBoolArray;
@ -86,7 +83,7 @@ public class KarnaughMapTest extends TestCase {
// in 4x4 map a 8 cell block is drawn in wrong orientation // in 4x4 map a 8 cell block is drawn in wrong orientation
public void testBUG_1() throws IOException, ParseException, KarnaughException { public void testBUG_1() throws IOException, ParseException, KarnaughException {
Expression exp = new Variable("D"); Expression exp = Not.not(new Variable("D"));
KarnaughMap c = new KarnaughMap(Variable.vars(4), exp); KarnaughMap c = new KarnaughMap(Variable.vars(4), exp);
assertEquals(1, c.size()); assertEquals(1, c.size());
@ -96,7 +93,7 @@ public class KarnaughMapTest extends TestCase {
assertFalse(co.onlyEdges()); assertFalse(co.onlyEdges());
assertTrue(co.isVerticalDivided()); assertTrue(co.isVerticalDivided());
exp = new Variable("B"); exp = Not.not(new Variable("B"));
c = new KarnaughMap(Variable.vars(4), exp); c = new KarnaughMap(Variable.vars(4), exp);
assertEquals(1, c.size()); assertEquals(1, c.size());

View File

@ -245,14 +245,14 @@ public class TestInGUI extends TestCase {
.add(new EnterTruthTable(0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1)) .add(new EnterTruthTable(0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1))
.press("F1") .press("F1")
.delay(500) .delay(500)
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 146, 110, new Color(215, 175, 175))) .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 197, 110, new Color(255, 127, 127)))
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 266, 109, new Color(187, 187, 221))) .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 317, 108, new Color(127, 255, 127)))
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 86, 169, new Color(255, 187, 187))) .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 137, 169, new Color(191, 127, 127)))
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 205, 169, new Color(127, 255, 127))) .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 257, 170, new Color(127, 127, 191)))
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 145, 228, new Color(127, 127, 255))) .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 197, 228, new Color(227, 227, 127)))
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 266, 230, new Color(255, 175, 255))) .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 316, 228, new Color(127, 255, 255)))
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 86, 288, new Color(242, 242, 191))) .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 137, 290, new Color(127, 127, 255)))
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 205, 289, new Color(127, 255, 255))) .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 257, 290, new Color(255, 127, 255)))
// .add(new GuiTester.ColorPickerCreator(KarnaughMapComponent.class)) // .add(new GuiTester.ColorPickerCreator(KarnaughMapComponent.class))
// .ask("Shows the k-map a checkerboard pattern?") // .ask("Shows the k-map a checkerboard pattern?")
.add(new GuiTester.CloseTopMost()) .add(new GuiTester.CloseTopMost())
@ -262,14 +262,14 @@ public class TestInGUI extends TestCase {
public void testEdges() { public void testEdges() {
new GuiTester() new GuiTester()
.use(createNew4VarTruthTable) .use(createNew4VarTruthTable)
.add(new EnterTruthTable(0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1)) .add(new EnterTruthTable(1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0))
.press("F1") .press("F1")
.delay(500) .delay(500)
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 136, 100, new Color(255, 127, 127))) .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 137, 98, new Color(255, 127, 127)))
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 266, 100, new Color(255, 127, 127))) .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 265, 95, new Color(255, 127, 127)))
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 266, 305, new Color(255, 127, 127))) .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 265, 291, new Color(255, 127, 127)))
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 136, 305, new Color(255, 127, 127))) .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 137, 296, new Color(255, 127, 127)))
// .add(new GuiTester.ColorPickerCreator()) // .add(new GuiTester.ColorPickerCreator(KarnaughMapComponent.class))
// .ask("Are the edges covered in the k-map?") // .ask("Are the edges covered in the k-map?")
.add(new GuiTester.CloseTopMost()) .add(new GuiTester.CloseTopMost())
.execute(); .execute();
@ -286,11 +286,10 @@ public class TestInGUI extends TestCase {
.press("RIGHT", 3) .press("RIGHT", 3)
.add(new EnterTruthTable(0, 0, 0, 1, 0, 1, 1, 1)) .add(new EnterTruthTable(0, 0, 0, 1, 0, 1, 1, 1))
.press("F1") .press("F1")
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 136, 230, new Color(255, 127, 127))) .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 213, 179, new Color(191, 127, 127)))
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 209, 230, new Color(127, 255, 127))) .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 144, 231, new Color(255, 127, 127)))
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 311, 184, new Color(191, 127, 127))) .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 315, 230, new Color(127, 255, 127)))
.add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 266, 231, new Color(255, 127, 127))) // .add(new GuiTester.ColorPickerCreator(KarnaughMapComponent.class))
// .add(new GuiTester.ColorPickerCreator())
// .ask("Shows the k-map a 'two out of three' pattern?") // .ask("Shows the k-map a 'two out of three' pattern?")
.add(new GuiTester.CloseTopMost()) .add(new GuiTester.CloseTopMost())
.execute(); .execute();