Jump to content

Backgammon movement problems

- - - - -

  • Please log in to reply
No replies to this topic

#1
Sheld

Sheld

    Newbie

  • Members
  • Pip
  • 2 posts
Hi, this is my first time posting on this forum so I apologize ahead of time if I don't follow proper etiquette or something, but just let me know and I will fix it. Right now I'm making a Java version of Backgammon (kind of) and I'm pretty close to finishing it but I have one issue. Sorry if my code is too long, but to make it easier to find the error I will pinpoint it. When I try and use my .move() method in the Piece class for a blackPiece, it does not operate properly and never lets me use dice2 first for some reason and constantly gives the "Illegal Move" message even when it is not an evil move. The method is now working fine for the whitePiece but not for the blackPiece.
import java.awt.*;

import java.awt.image.*;

import java.awt.event.*;

import javax.swing.*;

import javax.imageio.*;

import java.io.*;

import java.lang.*;

import java.util.*;


public class Backgammon {

	public static boolean trans, pvp, start, ai, turn1, turn2, done1, done2, win1, win2;							//public variables that determine what state the game is in

	public static void main(String[] args) {

		trans=false;

		turn1=false;

		win1=false;

		win2=false;

		Mainframe game = new Mainframe();

		game.setSize(800, 800);

		game.setVisible(true);

		game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

}

class Mainframe extends JFrame {

	private Board gui;

	private Player gameplay;

	private Container c;

	public Mainframe(){

		super("Backgammon");

		Board gui = new Board();

		c=getContentPane();

		c.add(gui);

	}

}

class Board extends JPanel implements MouseListener, MouseMotionListener{

	private JPanel showTotal;

	private boolean start;

	private BufferedImage[] diceImage;

	private BufferedImage backBoard, title, whitePiece, blackPiece;

	private Player gameplay;

	private Coords pixelPos;

	private ArrayList<Piece> onePiece, twoPiece;

	private int dice1, dice2, finishNum, total, point, x, y, mouseX, mouseY, selected, rollNum;					//selected is used to store the i position in the arraylist of pieces for the piece that was selected in mousePressed and transfer it to mouseReleased where it can be checked for possible movement by the move method in the Piece class

	public Board(){

		addMouseListener(this);

		addMouseMotionListener(this);

		dice1=1;

		dice2=1;

		finishNum=0;

		pixelPos=new Coords();

		onePiece = new ArrayList<Piece>();

		twoPiece = new ArrayList<Piece>();

		for (int i=0; i<2; i++){

			onePiece.add(i, new Piece(1, 1));

			twoPiece.add(i, new Piece(24, 2)); 

		}

		for (int i=2; i<7; i++) {

			onePiece.add(i, new Piece(19, 1));

			twoPiece.add(i, new Piece(6, 2));

		}

		for (int i=7; i<10; i++) {

			onePiece.add(i, new Piece(17, 1));

			twoPiece.add(i, new Piece(8, 2));

		}

		for (int i=10; i<15; i++){

			onePiece.add(i, new Piece(12, 1));

			twoPiece.add(i, new Piece(13, 2));

		}

		title = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);

		backBoard = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);

		diceImage = new BufferedImage[6];

		whitePiece = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);	

		blackPiece = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);

		try {

			backBoard = ImageIO.read(new File("bb.png"));

			diceImage[0]= ImageIO.read(new File("dice1.png"));

			diceImage[1]= ImageIO.read(new File("dice2.png"));

			diceImage[2]= ImageIO.read(new File("dice3.png"));

			diceImage[3]= ImageIO.read(new File("dice4.png"));

			diceImage[4]= ImageIO.read(new File("dice5.png"));

			diceImage[5]= ImageIO.read(new File("dice6.png"));

			title = ImageIO.read(new File("title.png"));

			whitePiece = ImageIO.read(new File("whitepiece.jpg"));

			blackPiece = ImageIO.read(new File("blackpiece.jpg"));

		}

		catch (IOException e) {

			System.out.println("Error: "+e.getMessage());

		}

		showTotal = new JPanel();

		showTotal.setLayout(new BorderLayout());

	}

	public boolean check(int player){

		finishNum=0;

		if (player==1){

			for (int i=0; i<onePiece.size(); i++){

				if ((onePiece.get(i)).returnPos()>18){

					finishNum++;

				}

			}

			if (finishNum==15){

				return true;

			}

		}

		else if (player==2) {

			for (int i=0; i<twoPiece.size(); i++){

				if ((twoPiece.get(i)).returnPos()<7){

					finishNum++;

				}

			}

			if (finishNum==15){

				return true;

			}

		}

		return false;

	}

	public int pieceTotal(int position, int player){

		total=0;

		if (player==1){

			for (int i=0; i<onePiece.size(); i++){

				if ((onePiece.get(i)).returnPos()==position){

					total++;

				}

			}

		}

		else if (player==2){

			for (int i=0; i<twoPiece.size(); i++){

				if ((twoPiece.get(i)).returnPos()==position){

					total++;

				}

			}

		}

		return total;

	}

	public void paintComponent(Graphics g) {

		super.paintComponent(g);

		if (Backgammon.trans) {

			g.drawImage(backBoard, 0, 0, null);

			g.drawImage(diceImage[dice1-1], 587, 633, 75, 75, null);

			g.drawImage(diceImage[dice2-1], 692, 633, 75, 75, null);

			for (int i=0; i<onePiece.size(); i++){

				for (int x=0; x<pieceTotal((onePiece.get(i)).returnPos(), 1); x++){

					if (onePiece.get(i).returnPos()>12){

						g.drawImage(whitePiece, pixelPos.convertX((onePiece.get(i)).returnPos()), pixelPos.convertY((onePiece.get(i)).returnPos())+(x*50), 50, 50, null);

					}

					else {

						g.drawImage(whitePiece, pixelPos.convertX((onePiece.get(i)).returnPos()), pixelPos.convertY((onePiece.get(i)).returnPos())-(x*50), 50, 50, null);

					}

				}

			}

			for (int i=0; i<twoPiece.size(); i++){

				for (int x=0; x<pieceTotal((twoPiece.get(i)).returnPos(), 2); x++){

					if (twoPiece.get(i).returnPos()>12){

						g.drawImage(blackPiece, pixelPos.convertX((twoPiece.get(i)).returnPos()), pixelPos.convertY((twoPiece.get(i)).returnPos())+(x*50), 50, 50, null);

					}

					else {

						g.drawImage(blackPiece, pixelPos.convertX((twoPiece.get(i)).returnPos()), pixelPos.convertY((twoPiece.get(i)).returnPos())-(x*50), 50, 50, null);

					}

				}

			}

		}

		else if (!Backgammon.trans){

			g.drawImage(title, 0, 0, null);

		}

	}

	public void mouseClicked(MouseEvent arg0) {

		if (arg0.getX()<767&&arg0.getX()>587&&arg0.getY()<708&&arg0.getY()>633){

			dice1=(int)(Math.random()*6+1);

			dice2=(int)(Math.random()*6+1);

			if (dice1!=dice2){

				rollNum=2;

			}

			else {

				rollNum=4;

			}

			if (Backgammon.turn1==false){

				Backgammon.turn1=true;

				Backgammon.turn2=false;

			}

			else if (Backgammon.turn2==false){

				Backgammon.turn2=true;

				Backgammon.turn1=false;

			}

			repaint();

			System.out.println(dice1+" "+dice2);

		}

		if (check(1)&&Backgammon.turn1) {

			Backgammon.done1=true;

		}

		else if (check(2)&&Backgammon.turn2) {

			Backgammon.done2=true;

		}

	}

	public void mouseEntered(MouseEvent arg0) {}

	public void mouseExited(MouseEvent arg0) {}

	public void mousePressed(MouseEvent arg0) {

		if (Backgammon.done1&&onePiece.size()!=0){	

			for (int i=0; i<onePiece.size(); i++){

			if (24-(onePiece.get(i)).returnPos()<dice1&&rollNum>0){		    //make this code so the user can choose the pieces to remove on their own

					onePiece.remove(i);

					rollNum--;

				}

				else if (24-(onePiece.get(i)).returnPos()<dice2&&rollNum>0){

					onePiece.remove(i);

					rollNum--;

				}

			}

		}	

		if (onePiece.size()==0){											/////////////////////////////////////////////////////////////////////////

			JOptionPane.showMessageDialog(null, "Player 1 wins!", "Congratulations",JOptionPane.PLAIN_MESSAGE);	

			Backgammon.trans=false;

		}

		else if (Backgammon.done2&&twoPiece.size()!=0){

			for (int i=0; i<twoPiece.size(); i++){

				if ((twoPiece.get(i)).returnPos()<dice1&&rollNum>0){

					twoPiece.remove(i);

					rollNum--;

				}					

				else if ((twoPiece.get(i)).returnPos()<dice2&&rollNum>0){

					twoPiece.remove(i);

					rollNum--;

				}

			}

		}

		if (twoPiece.size()==0){

			JOptionPane.showMessageDialog(null, "Player 2 wins!", "Congratulations", JOptionPane.PLAIN_MESSAGE);

			Backgammon.trans=false;

		}

		else if (Backgammon.trans==false&&arg0.getX()<400&&arg0.getY()>500){

			Backgammon.trans=true;

			Backgammon.start=true;

			repaint();

		}

		else if (Backgammon.trans==false&&arg0.getX()>400&&arg0.getY()>500){

			Backgammon.trans=true;

			Backgammon.start=true;

			repaint();

		}

		else if (Backgammon.turn1&&rollNum>0){

			for (int i=0; i<15; i++){

				System.out.println(onePiece.get(i).returnPos()+" "+pixelPos.convertPos(arg0.getX(), arg0.getY()));

				if ((onePiece.get(i)).returnPos()==pixelPos.convertPos(arg0.getX(), arg0.getY())){

					selected=i;

				}

			}

		}

		else if (Backgammon.turn2&&rollNum>0){

			for (int i=0; i<15; i++){

				System.out.println(onePiece.get(i).returnPos()+" "+pixelPos.convertPos(arg0.getX(), arg0.getY()));

				if ((onePiece.get(i)).returnPos()==pixelPos.convertPos(arg0.getX(), arg0.getY())){

					selected=i;

				}

			}

		}

		//mouseX=arg0.getX();

		//mouseY=arg0.getY();

		//System.out.println(mouseX+" "+mouseY);

	}

	public void mouseReleased(MouseEvent arg0) {

		point=pixelPos.convertPos(arg0.getX(), arg0.getY());

		if (Backgammon.turn1&&rollNum>0){

			if ((onePiece.get(selected)).move(dice1, dice2, point)==1&&pieceTotal(point, 2)<2){

				System.out.println("Reached1W");

				(onePiece.get(selected)).setPos(point);

				//use the other dice with move

				rollNum--;

				//System.out.println(rollNum);

			}

			else if ((onePiece.get(selected)).move(dice1, dice2, point)==2&&pieceTotal(point, 2)<2){

				System.out.println("Reached2W");

				(onePiece.get(selected)).setPos(point);

				//use the other dice with move

				rollNum--;

				//System.out.println(rollNum);

			}		

			else {

				System.out.println("Illegal MoveW");

			}

		}

		else if (Backgammon.turn2&&rollNum>0){

			if ((twoPiece.get(selected)).move(dice1, dice2, point)==1&&pieceTotal(point, 1)<2){

				System.out.println("Reached1B");

				(twoPiece.get(selected)).setPos(point);

				//use other dice with move

				rollNum--;

				//System.out.println(rollNum);

			}

			else if ((twoPiece.get(selected)).move(dice1, dice2, point)==2&&pieceTotal(point, 1)<2){

				System.out.println("Reached2B");

				(twoPiece.get(selected)).setPos(point);

				//use other dice with move

				rollNum--;

				//System.out.println(rollNum);

			}

			else {

				System.out.println("Illegal MoveB");

			}

		}

		//System.out.println((onePiece.get(selected)).move(dice1, dice2, pixelPos.convertPos(arg0.getX(), arg0.getY())));

		//System.out.println(rollNum);

		repaint();

	}

	public void mouseDragged(MouseEvent arg0) {}

	public void mouseMoved(MouseEvent arg0) {}

}

class Coords {

	private File pixelFile;

	private int[][] allPos;

	Coords() {

		allPos=new int[3][26];

		File pixelFile = new File("pixel.txt");						//add code to check if file exists or not

		try {

			FileReader in = new FileReader(pixelFile);

			BufferedReader read = new BufferedReader(in);

			for (int x=0; x<3; x++){					//first is the position numbers, then x pixel, then y pixel

				for (int i=0; i<26; i++){

					if (x==0){

						allPos[x][i]= i;

					}

					else if (x==1){

						allPos[x][i]=Integer.parseInt(read.readLine());

						allPos[x+1][i]=Integer.parseInt(read.readLine());

					}

				}

			}

			read.close();

			in.close();

		}

		catch (FileNotFoundException e){

			System.out.println("You have lost the pixel file. Please contact the programmer for a new file.");

		}

		catch (IOException e){

			System.out.println("Error: "+e.getMessage());

		}

	}

	public int convertX(int pos){

		for (int i=0; i<26; i++){

			if (pos==allPos[0][i]){

				return allPos[1][i];

			}

		}

		return 0;

	}

	public int convertY(int pos){

		for (int i=0; i<26; i++){

			if (pos==allPos[0][i]){

				return allPos[2][i];

			}

		}

		return 0;

	}

	public int convertPos(int x, int y){

		for (int i=0; i<26; i++){

			if (x>allPos[1][i]&&x<(allPos[1][i]+50)&&(y>allPos[2][i]&&y<(allPos[2][i]+50))){

				return allPos[0][i];

			}

		}

		return 0;

	}

}

class Piece {

	//store the number of pieces at each position in here as well

	int x, y, player, currentPos;

	Piece(int n, int p){

		currentPos=n;

		player=p;

	}

	public int returnPlayer(){

		return player;

	}

	public int move(int roll1, int roll2, int pos){

		if (player==1){

			if (pos==25){

				return 3;

			}

			else  if (currentPos+roll1==pos){

				return 1;

			}

			else if (currentPos+roll2==pos){

				return 2;

			}

			else {

				return 3;

			}

		}

		else {

			if (pos==0){

				return 3;

			}

			else if (currentPos-roll1==pos){

				return 1;

			}

			else if (currentPos-roll2==pos){

				return 2;

			}

			else {

				return 3;

			}

		}

	}

	public int returnPos(){

		return currentPos;

	}

	public void setPos(int pos){

		currentPos=pos;

	}

}
Any help is appreciated.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users