3 Replies - 419 Views - Last Post: 16 May 2014 - 08:57 PM Rate Topic: -----

#1 steen324  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-May 14

Anyone familiar with Khan Academy? Homework question!

Posted 16 May 2014 - 11:59 AM

Hello!

I just need ideas for my project. I'm working on a memory game project on Khan and I'm stuck. Right now my game works by matching 2 of the same image. However, my professor wants me to add a button that allows 4 images to be paired at the same time. Right now, there are 20 images total but half of them are duplicates so that they have a match. My issue with this button is that I'm not sure how to make 5 groups of 4 of the same image. So rather than 10 different images, there will be only 5. (Still a total of 20 because every image has duplicates) My professor told me that a hint is to use a boolean variable called quadButton and work from there.

Any thoughts? The link to the game is below:
http://www.khanacade...833933030686720

Is This A Good Question/Topic? 0
  • +

Replies To: Anyone familiar with Khan Academy? Homework question!

#2 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 673
  • View blog
  • Posts: 2,958
  • Joined: 03-December 12

Re: Anyone familiar with Khan Academy? Homework question!

Posted 16 May 2014 - 01:34 PM

How are you creating 2 of the same image? It would be the same process to create 4.
Was This Post Helpful? 0
  • +
  • -

#3 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Anyone familiar with Khan Academy? Homework question!

Posted 16 May 2014 - 05:31 PM

This may be a bit much, but it is robust!

All you have to do is create an ImageGroup class and associate it with each ImageCard object. Then you make both objects comparable, and create an every() method that short-circuits if at any time the current item does not match the next item in the list.

ImageDriver.java
package q347048;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class ImageDriver {
	public static void main(String[] args) {
		int rows = 5, cols = 4;
		ImageGroup ig1 = new ImageGroup(1, "Group 1");
		ImageGroup ig2 = new ImageGroup(2, "Group 2");
		ImageCard[][] grid = generateGrid(rows, cols, ig1, ig2);
		
		printGrid(grid);
		
		List<ImageCard> selections = getSelections(grid, 0,0, 1,0, 2,0, 3,0, 4,0);
		
		System.out.println(every(selections));
	}
	
	public static <T> boolean every(Collection<T> c) {
		if (c == null || c.isEmpty()) {
			return true;
		}
		
		Iterator<T> it = c.iterator();
		T curr = null;
		
		if (it.hasNext()) {
			curr = it.next();
		}
		
		while (it.hasNext()) {
			T next = it.next();
			
			if (!curr.equals(next)) {
				return false;
			}
			
			curr = next;
		}
		
		return true;
	}
	
	public static ImageCard[][] generateGrid(int rows, int cols, ImageGroup... groups) {
		ImageCard[][] grid = new ImageCard[rows][cols];

		for (int r = 0; r < rows; r++) {
			for (int c = 0; c < cols; c++) {
				int id = cols * r + c;
				int gindex = id % groups.length;
				ImageGroup g = groups[gindex];
				grid[r][c] = new ImageCard(id, g);
			}
		}
		
		return grid;
	}
	
	public static <T> void printGrid(T[][] grid) {
		for (int r = 0; r < grid.length; r++) {
			for (int c = 0; c < grid[r].length; c++) {
				System.out.print(grid[r][c]);
			}
			System.out.println();
		}
	}
	
	/**
	 * @param indicies A list of x,y pairs.
	 */
	public static <T> List<T> getSelections(T[][] grid, int... indicies) {
		if (indicies.length % 2 == 1) {
			throw new IllegalArgumentException("Must enter even list of numbers");
		}
		
		List<T> selections = new ArrayList<T>();
		
		for (int i = 0; i < indicies.length; i+=2) {
			selections.add(grid[indicies[i]][indicies[i+1]]);
		}
		
		return selections;
	}
}

ImageCard.java
package q347048;

import java.util.Comparator;

public class ImageCard implements Comparable<ImageCard>, Comparator<ImageCard> {
	private int id;
	public ImageGroup imagegroup;

	public ImageCard(int id) {
		this(id, null);
	}

	public ImageCard(int id, ImageGroup imagegroup) {
		this.id = id;
		this.imagegroup = imagegroup;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((imagegroup == null) ? 0 : imagegroup.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ImageCard other = (ImageCard) obj;

		return this.compareTo(other) == 0;
	}

	@Override
	public int compare(ImageCard ic1, ImageCard ic2) {
		return ic1.compareTo(ic2);
	}

	@Override
	public int compareTo(ImageCard ic) {
		if (this == ic)
			return 0;
		if (ic == null)
			return -1;
		if (imagegroup == null && ic.imagegroup != null)
			return 1;
		return imagegroup.compareTo(ic.imagegroup);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public ImageGroup getImagegroup() {
		return imagegroup;
	}

	public void setImagegroup(ImageGroup imagegroup) {
		this.imagegroup = imagegroup;
	}

	public String formatCell() {
		int gid = imagegroup != null ? imagegroup.getId() : 0;
		return String.format("[%02d:%d]", id, gid);
	}

	@Override
	public String toString() {
		//return "ImageCard [id=" + id + " imagegroup=" + imagegroup + "]";
		return formatCell();
	}
}

ImageGroup.java
package q347048;

import java.util.Comparator;

public class ImageGroup implements Comparable<ImageGroup>, Comparator<ImageGroup> {
	private int id;
	private String name;
	
	public ImageGroup(int id, String name) {
		this.id = id;
		this.name = name;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ImageGroup other = (ImageGroup) obj;
		
		return this.compareTo(other) == 0;
	}

	@Override
	public int compare(ImageGroup ig1, ImageGroup ig2) {
		return ig1.compareTo(ig2);
	}

	@Override
	public int compareTo(ImageGroup ig) {
		if (this == ig)
			return 0;
		if (ig == null)
			return -1;
		if (name == null && ig.name != null)
			return 1;
		return name.compareTo(ig.name);
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public String toString() {
		return "ImageGroup [name=" + name + "]";
	}
}

This post has been edited by x68zeppelin80x: 16 May 2014 - 05:31 PM

Was This Post Helpful? 0
  • +
  • -

#4 steen324  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-May 14

Re: Anyone familiar with Khan Academy? Homework question!

Posted 16 May 2014 - 08:57 PM

View Postx68zeppelin80x, on 16 May 2014 - 05:31 PM, said:

This may be a bit much, but it is robust!

All you have to do is create an ImageGroup class and associate it with each ImageCard object. Then you make both objects comparable, and create an every() method that short-circuits if at any time the current item does not match the next item in the list.

ImageDriver.java
package q347048;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class ImageDriver {
	public static void main(String[] args) {
		int rows = 5, cols = 4;
		ImageGroup ig1 = new ImageGroup(1, "Group 1");
		ImageGroup ig2 = new ImageGroup(2, "Group 2");
		ImageCard[][] grid = generateGrid(rows, cols, ig1, ig2);
		
		printGrid(grid);
		
		List<ImageCard> selections = getSelections(grid, 0,0, 1,0, 2,0, 3,0, 4,0);
		
		System.out.println(every(selections));
	}
	
	public static <T> boolean every(Collection<T> c) {
		if (c == null || c.isEmpty()) {
			return true;
		}
		
		Iterator<T> it = c.iterator();
		T curr = null;
		
		if (it.hasNext()) {
			curr = it.next();
		}
		
		while (it.hasNext()) {
			T next = it.next();
			
			if (!curr.equals(next)) {
				return false;
			}
			
			curr = next;
		}
		
		return true;
	}
	
	public static ImageCard[][] generateGrid(int rows, int cols, ImageGroup... groups) {
		ImageCard[][] grid = new ImageCard[rows][cols];

		for (int r = 0; r < rows; r++) {
			for (int c = 0; c < cols; c++) {
				int id = cols * r + c;
				int gindex = id % groups.length;
				ImageGroup g = groups[gindex];
				grid[r][c] = new ImageCard(id, g);
			}
		}
		
		return grid;
	}
	
	public static <T> void printGrid(T[][] grid) {
		for (int r = 0; r < grid.length; r++) {
			for (int c = 0; c < grid[r].length; c++) {
				System.out.print(grid[r][c]);
			}
			System.out.println();
		}
	}
	
	/**
	 * @param indicies A list of x,y pairs.
	 */
	public static <T> List<T> getSelections(T[][] grid, int... indicies) {
		if (indicies.length % 2 == 1) {
			throw new IllegalArgumentException("Must enter even list of numbers");
		}
		
		List<T> selections = new ArrayList<T>();
		
		for (int i = 0; i < indicies.length; i+=2) {
			selections.add(grid[indicies[i]][indicies[i+1]]);
		}
		
		return selections;
	}
}

ImageCard.java
package q347048;

import java.util.Comparator;

public class ImageCard implements Comparable<ImageCard>, Comparator<ImageCard> {
	private int id;
	public ImageGroup imagegroup;

	public ImageCard(int id) {
		this(id, null);
	}

	public ImageCard(int id, ImageGroup imagegroup) {
		this.id = id;
		this.imagegroup = imagegroup;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((imagegroup == null) ? 0 : imagegroup.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ImageCard other = (ImageCard) obj;

		return this.compareTo(other) == 0;
	}

	@Override
	public int compare(ImageCard ic1, ImageCard ic2) {
		return ic1.compareTo(ic2);
	}

	@Override
	public int compareTo(ImageCard ic) {
		if (this == ic)
			return 0;
		if (ic == null)
			return -1;
		if (imagegroup == null && ic.imagegroup != null)
			return 1;
		return imagegroup.compareTo(ic.imagegroup);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public ImageGroup getImagegroup() {
		return imagegroup;
	}

	public void setImagegroup(ImageGroup imagegroup) {
		this.imagegroup = imagegroup;
	}

	public String formatCell() {
		int gid = imagegroup != null ? imagegroup.getId() : 0;
		return String.format("[%02d:%d]", id, gid);
	}

	@Override
	public String toString() {
		//return "ImageCard [id=" + id + " imagegroup=" + imagegroup + "]";
		return formatCell();
	}
}

ImageGroup.java
package q347048;

import java.util.Comparator;

public class ImageGroup implements Comparable<ImageGroup>, Comparator<ImageGroup> {
	private int id;
	private String name;
	
	public ImageGroup(int id, String name) {
		this.id = id;
		this.name = name;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ImageGroup other = (ImageGroup) obj;
		
		return this.compareTo(other) == 0;
	}

	@Override
	public int compare(ImageGroup ig1, ImageGroup ig2) {
		return ig1.compareTo(ig2);
	}

	@Override
	public int compareTo(ImageGroup ig) {
		if (this == ig)
			return 0;
		if (ig == null)
			return -1;
		if (name == null && ig.name != null)
			return 1;
		return name.compareTo(ig.name);
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public String toString() {
		return "ImageGroup [name=" + name + "]";
	}
}


Thank you for your efforts. However, this does not run in Khan Academy. Did you see the link I provided?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1