Skip to content

Commit

Permalink
Table and TableNode
Browse files Browse the repository at this point in the history
  • Loading branch information
mpacitti committed Apr 15, 2015
1 parent 5051e4a commit 7d5d730
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
113 changes: 113 additions & 0 deletions src/Table.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
public class Table {
private TableNode head;
private int length;

public Table() {
head = null;
length = 0;
}

public void add(Card card) {
TableNode newNode = new TableNode(card);
if (head == null) {
head = newNode;
}
else {
newNode.setNext(head);
head = newNode;
}
}

public void removeSet(Card card1, Card card2, Card card3) {
if (card1.isSet(card2, card3) == false) {
return;
}
TableNode temp = head;
Card cardnum1 = null;
Card cardnum2 = null;
Card cardnum3 = null;

while(temp !=null) {
if(temp.getCard() == card1) {
temp = temp.getNext();
card1 = null;
}
}
}

//while (curr != null) {
//if (curr.getCard() == card1) {
//cardnum1 = card1;
// }
//if (curr.getCard() == card2) {
//cardnum2 = card2;
//}
//if (curr.getCard() == card3) {
//cardnum3 = card3;
//}
//curr = curr.getNext();
//}

// while (curr == null) {
//if (curr.getCard() != card1) {
//cardnum1 = card1;
//}
//if (curr.getCard() != card2) {
//cardnum2 = card2;
//}
//if (curr.getCard() != card3) {
//cardnum3 = card3;
//}
//}
//}

public int numCards() {
TableNode temp = head;
int card = 0;
if (temp == null) {
return 0;
}
else {
while (temp != null) {
card += 1;
temp = temp.getNext();
}
return card;
}
}

public Card getCard(int index){
TableNode temp = head;
if(temp == null) {
return null;
}
for(int t = 0; t < index; t++) {
temp = temp.getNext();
if(temp==null) {
return null;
}
}
return temp.getCard();
}

public int numSets() {
TableNode card1 = head;
TableNode card2 = head;
TableNode card3 = head;
int setnum = 0;
for (int a = 0; a < length - 2; a++) {
for (int b = 1; b < length - 1; b++) {
for (int c = 2; c < length; c++){

Card carda = getCard(a);
Card cardb = getCard(b);
Card cardc = getCard(c);
if(carda.isSet(cardb, cardc)) {
setnum += 1;
}
}
}
}
return setnum;
}
}
21 changes: 21 additions & 0 deletions src/TableNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class TableNode {
private Card card;
private TableNode next;

public TableNode(Card card1) {
card = card1;
next = null;
}

public void setNext(TableNode nextcard) {
next = nextcard;
}

public TableNode getNext() {
return next;
}

public Card getCard() {
return card;
}
}
21 changes: 21 additions & 0 deletions src/TableNodeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import junit.framework.TestCase;

public class TableNodeTest extends TestCase {

public void testTableNodeCards() {
Card card1 = new Card(1, 3, 3, 2);
TableNode node1 = new TableNode(card1);
assertEquals(null, node1.getNext());
assertEquals(card1, node1.getCard());

Card card2 = new Card(1, 3, 1, 2);
TableNode node2 = new TableNode(card2);
assertEquals(null, node2.getNext());
assertEquals(card2, node2.getCard());

Card card3 = new Card(1, 3, 2, 2);
TableNode node3 = new TableNode(card3);
assertEquals(null, node3.getNext());
assertEquals(card3, node3.getCard());
}
}
21 changes: 21 additions & 0 deletions src/TableTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import junit.framework.TestCase;

public class TableTest extends TestCase{

public void testTable() {
Table tt = new Table();
Card card1 = new Card(1, 2, 3, 2);
tt.add(card1);

Card card2 = new Card(3, 2, 3, 1);
tt.add(card2);

Card card3 = new Card(1, 2, 2, 3);
tt.add(card3);

assertEquals(3, tt.numCards());
assertTrue(tt.getCard(0).equals(card3));
assertTrue(tt.getCard(1).equals(card2));
assertTrue(tt.getCard(2).equals(card1));
}
}

0 comments on commit 7d5d730

Please sign in to comment.