Plinko
<< Compare People | OtherProjectsTrailIndex | Ant Hill >>
If you are not familiar with the game, here is a link to the game show (Skip to minute 4:00 if you're in a hurry)
Disk.java
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.Color;
public class Disk
{
private int size;
private int vSize;
private int diam;
private int numCol;
private int numRow;
private int col;
private int row;
/**
* Constructor for objects of class Disk
*/
public Disk(int aNumCol, int aNumRow, int s, int c)
{
this(aNumCol, aNumRow, s);
col=c;
}
public Disk(int aNumCol, int aNumRow, int s)
{
numCol = aNumCol;
numRow = aNumRow;
diam=(int)(.5*s);
size=s;
vSize=(int)(s*Math.sin(Math.PI/3.0));
col=(int)(Math.random()*numCol);
row=0;
}
public void draw(Graphics2D g2)
{
g2.setColor(Color.BLUE );
int hOffset=size/4;
if (row%2==1) hOffset*=3;
int vOffset=vSize/6;
Ellipse2D.Double o=new Ellipse2D.Double(hOffset+col*size ,vOffset+row*vSize,diam,diam);
g2.fill(o);
}
public void fall()
{
if (row<numRow-1) {
row++;
if (Math.random()>.5)
{
//move right
int max=numCol-1;
if (row%2==1){
if(col>=max)
col--;
} else{
col++;
if (col>=max)
col=max;
}
} else{
//move left
col--;
if (row%2==0)
col++;
if (col<0)
col=0;
}
}
}
public int getRow(){return row;}
public int getCol(){return col;}
}
Board.java
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Color;
/**
This class displays a checkerboard with squares,
alternating between white and black.
*/
public class Board
{
private int numCol;
private int numRow;
private int size;
/**
Creates a Plinko Board object with a given number of rows and columns.
@param aNumSquares the number of squares in each row
@param aSize the size of each square
*/
public Board(int aNumCol, int aNumRow, int aSize)
{
numCol = aNumCol;
numRow = aNumRow;
size = aSize;
}
/**
Method used to draw the Plinko board.
@param g2 the graphics content
*/
public void draw(Graphics2D g2)
{
g2.setColor(new Color( 220,220,220 ) );
int offset=0;
for (int i = 0; i < numCol; i++)
{
int j=0;
while (j<numRow)
{
if (j % 2 == 0){
offset=0;
} else {
offset=size/2;
}
if ( i<numCol-1 || j%2==0){ //triange pointing down
Polygon t = new Polygon();
int y=(int)(j*size*Math.sin(Math.PI/3.0));
t.addPoint(offset+i * size, y);
t.addPoint(offset+(i+1)*size,y);
t.addPoint(offset+i*size+size/2, y+(int)(size*Math.sin(Math.PI/3.0)) );
g2.fill(t);
}
j++;
}
}
//Draw sides
g2.setColor(Color.ORANGE);
for (int j=0; j < numRow/2; j++){
Polygon t=new Polygon();
int vSize=(int)(2*size*Math.sin(Math.PI/3.0));
t.addPoint(0,j*vSize);
t.addPoint(size/2,vSize/2+j*vSize );
t.addPoint(0,vSize+j*vSize);
Polygon t2=new Polygon();
t2.addPoint(size*numCol,j*vSize);
t2.addPoint(size*numCol-size/2,vSize/2+j*vSize );
t2.addPoint(size*numCol,vSize+j*vSize);
g2.fill(t);
g2.fill(t2);
}
}
}
PlinkClick.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Rectangle;
public class PlinkClick extends JApplet implements MouseListener
{
int c=5;
final int NCOLS = 9;
final int NROWS = 13;
final String[] prize ={"$100","$500","$1,000","0","$10,000","0","$1,000","$500","$100"};
public void init()
{
addMouseListener(this);
}
/**
* Paint method for applet.
*
* @param g the Graphics object for this applet
*/
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.WHITE);
Rectangle bg=new Rectangle(0,0,getWidth(),getHeight());
g2.fill(bg);
int size = Math.min(getWidth(), getHeight()) / NCOLS;
Board pb = new Board(NCOLS,NROWS, size);
Disk d = new Disk(NCOLS, NROWS, size, c);
pb.draw(g2);
d.draw(g2);
for (int i=0;i<13;i++){
d.fall();
d.draw(g2);
}
g2.drawString( prize[ d.getCol() ], d.getCol()*size, getHeight()-15 );
}
/**
* Implementation of the MouseListener
*/
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
int x=e.getX();
c=(int)(x/(getWidth()/NCOLS));
repaint();
}
}
BoardComponent.java
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class BoardComponent extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
final int NCOLS = 9;
final int NROWS = 13;
final String[] prize ={"$100","$500","$1,000","0","$10,000","0","$1,000","$500","$100"};
int size = Math.min(getWidth(), getHeight()) / NCOLS;
Board pb = new Board(NCOLS,NROWS, size);
Disk d = new Disk(NCOLS, NROWS, size);
pb.draw(g2);
d.draw(g2);
for (int i=0;i<13;i++){
d.fall();
d.draw(g2);
}
g2.drawString( prize[ d.getCol() ], d.getCol()*size, getHeight()-15 );
}
}
BoardViewer.java
import javax.swing.JFrame;
/**
This program displays a checkerboard.
*/
public class BoardViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
final int FRAME_WIDTH = 360;
final int FRAME_HEIGHT = 500;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Plinko Board Viewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoardComponent component = new BoardComponent();
frame.add(component);
frame.setVisible(true);
}
}
