• Guest, HEROCRAFT PUBLIC RELEASE IS HAPPENING AN HOUR EARLIER! TONIGHT @ 7PM CST GET READY FOR IT! play.hc.to
    Read up on the guides and new systems! Here.
    View the LIVE Map here @ hc.to/map
    Stuck or have a problem? use "/pe create" to to open a ticket with staff (There are some known issues and other hotfixes we will be pushing asap)
  • Guest, Make sure to use our LAUNCHER! Read more here!

Java Checkers

jwplayer0

Legacy Supporter 6
Joined
Jan 14, 2011
Location
Columbus, OH
Hello old friends, i know you haven't seen me around recently (though i did stop by for a few minutes 2 or 3 days ago)

Back on topic, im in a programming class and where making checkers, its in java and i have pieces moving and stuff kinda. But im currently stuck.

Dont have enough java experience and was wondering if anyone knows how to limit how far a checker piece can move. Im coding this on eclipse and when i get home i could just post of the src folder (im currently in class working on it now. Well if anybody would like to help i'd be grateful

If their any info you may need post and i'll help you as much as i can with helping me =3

update in the code for anyone who cares, trying to figure out turn switching right now


Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Checkers3 extends JFrame implements KeyListener
	{
	static final long serialVersionUID = 0;
	Image im;
	int xCoordinate = 0;
	int yCoordinate = 25;
	int pickup=0;
	int jumpx=0;
	int jumpy=0;
	boolean drop=false;
	boolean BlueTurn = true;
	
	int[] BlueXcoord = { 0, 100, 200, 300,
						50, 150, 250, 350,
						0, 100, 200, 300};

	int[] BlueYcoord = { 25, 25, 25, 25, 
						75, 75, 75, 75, 
						125, 125, 125, 125};

	int[] BlackXcoord = { 50, 150, 250, 350, 
						0, 100, 200, 300, 
						50, 150, 250, 350};

	int[] BlackYcoord = { 275, 275, 275, 275, 
						325, 325, 325, 325, 
						375, 375, 375, 375};
	

	
	
	
	Checkers3()
		{
		super("Game Skeleton");
		JPanel p = new JPanel();
		this.setSize(400,425);
		this.setVisible(true);
		p.requestFocus();
		addKeyListener(this);
		newGame();
		}
	
	public void newGame()
		{
		xCoordinate = 0;
		yCoordinate = 25;
		}
	
	public void makeProgramWait(int milliseconds)
		{
		try
			{
			Thread.sleep(milliseconds);
			}
		catch (Exception e)
			{
			System.out.println("An error in sleep process.");
			}
		}
	
	public void playGame()
		{
		while (true)
			{
			
			makeProgramWait(100);
			repaint();
			}
		}
	
	public void paint(Graphics g)
		{
		if (im == null)
			{
			im = createImage( 400, 425);
			}
		Graphics tempG = im.getGraphics ();
		paintScreen(tempG);
		g.drawImage(im, 0, 0, this);
		}
	
	public void paintScreen(Graphics g)
		{
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(0,0, 400, 425);
		g.setColor(Color.red);
		g.fillRect(0, 25, 50, 50);
		g.fillRect(0, 125, 50, 50);
		g.fillRect(0, 225, 50, 50);
		g.fillRect(0, 325, 50, 50);
		
		g.fillRect(50, 75, 50, 50);
		g.fillRect(50, 175, 50, 50);
		g.fillRect(50, 275, 50, 50);
		g.fillRect(50, 375, 50, 50);
		
		g.fillRect(100, 25, 50, 50);
		g.fillRect(100, 125, 50, 50);
		g.fillRect(100, 225, 50, 50);
		g.fillRect(100, 325, 50, 50);
		
		g.fillRect(150, 75, 50, 50);
		g.fillRect(150, 175, 50, 50);
		g.fillRect(150, 275, 50, 50);
		g.fillRect(150, 375, 50, 50);
		
		g.fillRect(200, 25, 50, 50);
		g.fillRect(200, 125, 50, 50);
		g.fillRect(200, 225, 50, 50);
		g.fillRect(200, 325, 50, 50);
		
		g.fillRect(250, 75, 50, 50);
		g.fillRect(250, 175, 50, 50);
		g.fillRect(250, 275, 50, 50);
		g.fillRect(250, 375, 50, 50);
		
		g.fillRect(300, 25, 50, 50);
		g.fillRect(300, 125, 50, 50);
		g.fillRect(300, 225, 50, 50);
		g.fillRect(300, 325, 50, 50);
		
		g.fillRect(350, 75, 50, 50);
		g.fillRect(350, 175, 50, 50);
		g.fillRect(350, 275, 50, 50);
		g.fillRect(350, 375, 50, 50);
				
		
		g.setColor(Color.blue);
		g.fillOval(BlueXcoord[0], BlueYcoord[0], 50, 50);
		g.fillOval(BlueXcoord[1], BlueYcoord[1], 50, 50);
		g.fillOval(BlueXcoord[2], BlueYcoord[2], 50, 50);
		g.fillOval(BlueXcoord[3], BlueYcoord[3], 50, 50);
		
		g.fillOval(BlueXcoord[4], BlueYcoord[4], 50, 50);
		g.fillOval(BlueXcoord[5], BlueYcoord[5], 50, 50);
		g.fillOval(BlueXcoord[6], BlueYcoord[6], 50, 50);
		g.fillOval(BlueXcoord[7], BlueYcoord[7], 50, 50);
		
		g.fillOval(BlueXcoord[8], BlueYcoord[8], 50, 50);
		g.fillOval(BlueXcoord[9], BlueYcoord[9], 50, 50);
		g.fillOval(BlueXcoord[10], BlueYcoord[10], 50, 50);
		g.fillOval(BlueXcoord[11], BlueYcoord[11], 50, 50);
		
		g.setColor(Color.black);
		g.fillOval(BlackXcoord[0], BlackYcoord[0], 50, 50);
		g.fillOval(BlackXcoord[1], BlackYcoord[1], 50, 50);
		g.fillOval(BlackXcoord[2], BlackYcoord[2], 50, 50);
		g.fillOval(BlackXcoord[3], BlackYcoord[3], 50, 50);
		
		g.fillOval(BlackXcoord[4], BlackYcoord[4], 50, 50);
		g.fillOval(BlackXcoord[5], BlackYcoord[5], 50, 50);
		g.fillOval(BlackXcoord[6], BlackYcoord[6], 50, 50);
		g.fillOval(BlackXcoord[7], BlackYcoord[7], 50, 50);
		
		g.fillOval(BlackXcoord[8], BlackYcoord[8], 50, 50);
		g.fillOval(BlackXcoord[9], BlackYcoord[9], 50, 50);
		g.fillOval(BlackXcoord[10], BlackYcoord[10], 50, 50);
		g.fillOval(BlackXcoord[11], BlackYcoord[11], 50, 50);
		
		if (yCoordinate<425)
			{
			g.setColor(Color.white);
			g.drawOval(xCoordinate, yCoordinate, 50, 50);
			g.setColor(Color.pink);
			g.drawOval(xCoordinate+5, yCoordinate+5, 40, 40);
			}
		}
	
	public void keyPressed(KeyEvent e)
		{
		int inKey;
		inKey = e.getKeyCode();
		if (KeyEvent.VK_ESCAPE == inKey)
			{
			System.exit(0);
			}

		else if (KeyEvent.VK_LEFT == inKey)
			{
			xCoordinate = xCoordinate - 100;			
			}
		
		else if (KeyEvent.VK_RIGHT == inKey)
			{
			xCoordinate = xCoordinate + 100;	
			}
		
		else if (KeyEvent.VK_UP == inKey)
			{
			yCoordinate = yCoordinate - 50;
			xCoordinate = xCoordinate - 50;
			}
		
		else if (KeyEvent.VK_DOWN == inKey)
			{
		yCoordinate = yCoordinate + 50;
		xCoordinate = xCoordinate + 50;
			}
		
		
		
		
		else if (KeyEvent.VK_ENTER == inKey)
			
		{
			for (int Q=0;Q<12;Q = Q+1)
			{	
				if(BlueXcoord[Q] == xCoordinate && BlueYcoord[Q] == yCoordinate && BlueTurn == true)					
					{	
						{
					
						pickup = Q;
						System.out.println("checkers is Blue #" + Q );
						
						drop=true;
						}
					}
				
				else if (BlackXcoord[Q] == xCoordinate && BlackYcoord[Q] == yCoordinate && BlueTurn == false)
				{		
					pickup = Q;
					System.out.println("checkers is Black #" + Q );
					BlueTurn=true;
					drop=true;
				}
			}
		}
		
		else if(KeyEvent.VK_PERIOD == inKey/* && BlueTurn == true*/)
			{
			BlueTurn = false;
			if(drop==true)
			{
				drop=false;
				
				if  (BlueXcoord[pickup] +50 == xCoordinate || BlueXcoord[pickup] -50 == xCoordinate)
				{
				if (BlueYcoord[pickup] +50 == yCoordinate)
					{
					BlueXcoord[pickup]=xCoordinate; 
					BlueYcoord[pickup]=yCoordinate; 
		    	 	}
				}
					jumpx = (BlueXcoord[pickup]+ xCoordinate)/2;
					jumpy = (BlueYcoord[pickup]+ yCoordinate)/2;
					for (int j=0;j<12;j = j+1)
					{
					if (BlackXcoord[j] == jumpx && BlackYcoord[j] == jumpy )
						{
							System.out.println("Peice#"+j);
							BlackXcoord[j] = -50;
							BlackYcoord[j] = -50;
							BlueXcoord[pickup]=xCoordinate; 
							BlueYcoord[pickup]=yCoordinate;
							
						}
					}
			}
			
		}
		
		else if(KeyEvent.VK_SPACE == inKey /*&& BlueTurn == false*/)
	
		{
			BlueTurn = true;
			if(drop==true)
			{
				drop=false;
				
				if  (BlackXcoord[pickup] -50 == xCoordinate || BlackXcoord[pickup] +50 == xCoordinate )
				{
					if (BlackYcoord[pickup] -50 == yCoordinate)
				
						{
						BlackXcoord[pickup]=xCoordinate; 
						BlackYcoord[pickup]=yCoordinate; 
						}
				}
					{
					
							 jumpx = (BlackXcoord[pickup]+ xCoordinate)/2;
							 jumpy = (BlackYcoord[pickup]+ yCoordinate)/2;
							for (int J=0;J<12;J = J+1)
							{
							if (BlueXcoord[J] == jumpx && BlueYcoord[J] == jumpy)
								{
									System.out.println("Peice#"+J);
									BlueXcoord[J] = -50;
									BlueYcoord[J] = -50;
									BlackXcoord[pickup]=xCoordinate; 
									BlackYcoord[pickup]=yCoordinate;
									
								}
						
						
							}
						}
			}
		}
		
			
		
		if (xCoordinate==400)
		{	
			xCoordinate=0;
		}
		if (xCoordinate==-100)
		{	
			xCoordinate=300;
		}
		if (xCoordinate==450)
		{	
			xCoordinate=50;
		}
		if (xCoordinate==-50)
		{	
			xCoordinate=350;
		}
		if (yCoordinate==-25)
		{	
			yCoordinate=375;
		}
		if (yCoordinate==425)
		{	
			yCoordinate=25;
		}		
			
		
		repaint();
		
		
		
		
		
		
		
		
		}
	
	public void keyReleased(KeyEvent e)
		{
		}
	
	public void keyTyped(KeyEvent e)
		{
		}
	
	public static void main(String args[])
		{
		Checkers3 app = new Checkers3();
		app.addWindowListener(new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
					{
					System.exit(0);
					}
			});
		app.playGame();
		}
	}
 
Top