Skip to content

C A R D #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 110 additions & 2 deletions src/Card.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,12 +1,120 @@
public class Card {
// Create the rest of this class yourself

private int quantity;
private int color;
private int shading;
private int shape;


public Card(int quant, int clr, int shd, int shp) {
quantity = fixValue(quant);
color = fixValue(clr);
shading = fixValue(shd);
shape = fixValue(shp);
}

public int getQuantity() {
return quantity;
}

public int getColor() {
return color;
}

public int getShading() {
return shading;
}

public int getShape() {
return shape;
}

public boolean isSet(Card B, Card C) {

if((B.getQuantity() + C.getQuantity() + quantity) % 3 == 0 &&
(B.getColor() + C.getColor() +color) % 3 == 0 &&
(B.getShading() + C.getShading() + shading) % 3 == 0 &&
(B.getShape() + C.getShape() + shape) % 3 == 0) {

return true;
}

else {

return false;
}

}

//Tried to do my own toString method but I couldn't figure it out.
//So I did this code even though it's longer than I wanted it too.

public String toString() {

String str = "";

str += quantity;

if (color == 1) {
str += "R";
}

if (color == 2) {
str += "G";
}

if (color == 3) {
str += "P";
}

if (shading == 1) {
str += "O";
}

if (shading == 2) {
str += "T";
}

if (shading == 3) {
str += "S";
}

if (shape == 1) {
str += "O";
}

if (shape == 2) {
str += "D";
}

if (shape == 3) {
str += "S";
}

return str;

}
//From class. Thanks to me!
private int fixValue(int valueToFix) {

if(valueToFix < 1 || valueToFix > 3) {

return(((valueToFix % 3) + 3) % 3) + 1;
}

else {

return valueToFix;
}
}

public boolean equals(Object obj) {

Card that = (Card)obj;

return quantity == that.getQuantity() &&
color == that.getColor() &&
shading == that.getShading() &&
shape == that.getShape();
}
}
}
38 changes: 38 additions & 0 deletions src/CardClassTesting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import junit.framework.TestCase;

public class CardClassTesting extends TestCase {

public void testIsSet() {
Card A = new Card(2,1,1,1);
Card B = new Card(2,1,1,1);
Card C = new Card(4,1,1,1);
Card D = new Card(1,1,4,1);
Card E = new Card(5,4,4,4);

assertEquals(true, A.isSet(A,B));
assertEquals(false, A.isSet(C,D));
assertEquals(false, B.isSet(A,C));
assertEquals(false, E.isSet(D,B));
}

public void testValues() {
Card A = new Card(2,1,1,1);
Card B = new Card(-2,-3,7,1);

assertEquals(3, A.getQuantity());
assertEquals(2, A.getColor());
assertEquals(2, A.getShading());
assertEquals(2, A.getShape());

assertEquals(2, B.getQuantity());
assertEquals(1, B.getColor());
assertEquals(2, B.getShading());
assertEquals(2, B.getShape());
}

public void testToString() {
Card A = new Card(9,-3,8,2);

assertEquals("1RSS", A.toString());
}
}
123 changes: 85 additions & 38 deletions src/Deck.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,45 +1,92 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
import java.util.ArrayList;

public class Deck {
// Implement the rest of this class yourself

public Deck(String filename) {
cards = new ArrayList<Card>(81);
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Collections;

public class Deck {

private ArrayList<Card> cards = new ArrayList<Card>(81);
private int nextCardIndex = 0;

public Deck() {

try {
String line;
BufferedReader infile = new BufferedReader(new FileReader(filename));
int position = 0;

while((line = infile.readLine()) != null) {
// Blank lines might contain white space, so trim it off
line = line.trim();

// ignore blank lines
if(line.length() == 0)
continue;
for ( int quant = 1; quant <= 3; quant ++) {

for (int clr = 1; clr <= 3; clr ++) {

// ignore comments
if(line.startsWith("#"))
continue;
for (int shd = 1; shd <= 3; shd ++) {
for (int shp = 1; shp <=3; shp ++) {

// a valid line contains 4 integers
StringTokenizer tokenizer = new StringTokenizer(line);

int quantity = Integer.parseInt(tokenizer.nextToken());
int color = Integer.parseInt(tokenizer.nextToken());
int shading = Integer.parseInt(tokenizer.nextToken());
int shape = Integer.parseInt(tokenizer.nextToken());
Card card = new Card(quant, clr, shd, shp);
cards.add(card);
}
}
}
}

Collections.shuffle(cards);

}

public boolean hasNext(){

if (nextCardIndex < cards.size()){
return true;
}

else {

cards.add(new Card(quantity, color, shading, shape));
nextCardIndex = 0;
}
return false;
}

}
catch(Exception e) {
throw new RuntimeException("Error while reading file: " + e.toString());

public Card getNext(){

if (hasNext() == false){

return null;
}

else {

nextCardIndex += 1;
return cards.get(nextCardIndex-1);
}
}

}
public Deck(String filename) {
cards = new ArrayList<Card>(81);

try {
String line;
BufferedReader infile = new BufferedReader(new FileReader(filename));
int position = 0;

while((line = infile.readLine()) != null) {
line = line.trim();

if(line.length() == 0)
continue;

if(line.startsWith("#"))
continue;

StringTokenizer tokenizer = new StringTokenizer(line);

int quantity = Integer.parseInt(tokenizer.nextToken());
int color = Integer.parseInt(tokenizer.nextToken());
int shading = Integer.parseInt(tokenizer.nextToken());
int shape = Integer.parseInt(tokenizer.nextToken());

cards.add(new Card(quantity, color, shading, shape));
nextCardIndex = 0;
}
}
catch(Exception e) {
throw new RuntimeException("Error while reading file: " + e.toString());
}
}
}
95 changes: 95 additions & 0 deletions src/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
public class Game {
private Deck d;
private Table t;

public Game() {
d = new Deck();
t = new Table();

for (int i = 0; i < 12; i++)
t.add(d.getNext());

}

public Game(String str) {
d = new Deck(str);
t = new Table();
for (int i = 0; i < 12 && d.hasNext(); i++) {
t.add(d.getNext());
}
}

public int numSets() {

return t.numSets();
}

public int numCards() {

return t.numCards();
}

public void playRound() {

if (t.numSets() == 0 && d.hasNext() == true) {
for (int i = 0; i < 3; i++) {

if (d.hasNext() == false)
return;

t.add(d.getNext());
}
return;
}

else if (d.hasNext() == true && t.numSets() != 0) {
for (int j = 0; j < t.numCards() - 2; j++) {
for (int k = j + 1; k < t.numCards() - 1; k++) {
for (int l = k + 1; l < t.numCards(); l++) {

if (t.getCard(j).isSet(t.getCard(k), t.getCard(l)) == true) {
t.removeSet(t.getCard(j), t.getCard(k), t.getCard(l));

while (t.numCards() < 12) {

if (d.hasNext() == false)
return;

t.add(d.getNext());
}

return;
}
}
}
}
}

else if (t.numSets() != 0 && d.hasNext() == false) {
for (int j = 0; j < t.numCards() - 2; j++) {
for (int k = j + 1; k < t.numCards() - 1; k++) {
for (int l = k + 1; l < t.numCards(); l++) {

if (t.getCard(j).isSet(t.getCard(k), t.getCard(l))) {
t.removeSet(t.getCard(j), t.getCard(k), t.getCard(l));

return;
}
}
}
}
}

return;
}

public boolean isGameOver() {
if (!d.hasNext() && t.numSets() == 0) {
return true; }

else {

return false;
}
}
}
Loading