Brettsolitaer

Nachtschatten

Aktives Mitglied
Hallo zusammen,
Ich habe da ein paar große Probleme. Ich bin noch ein ziemlicher Anfänger im Programmieren und soll bis Freitag ein Solitaer (Brett nicht Karten) Spiel programmieren. Leider hackt es an allen Ecken und Enden. Ich hoffe mal ihr habt ein paar Tips für mich wo und was für Fehler ich überall drin habe. Bin für jeden Tipp äußerst Dankbar.

Java:
package Solitaer;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;

import javax.swing.*;

public class Copy2ofsetzenST implements ActionListener{
	

	public int [][] stones = new int [7][7];
	public boolean [][] stonesbt = new boolean [7][7];
	public JButton[][] btnStones = new JButton[7][7];

	
	// Image Icons
	ImageIcon iiMarmor = new ImageIcon("sprites/Marmorxx.jpg");
	ImageIcon iiStone = new ImageIcon("sprites/Stone.gif");
	ImageIcon iiStone2 = new ImageIcon("sprites/Stone2.gif");
	ImageIcon iiMulde = new ImageIcon("sprites/mulde.gif");
	ImageIcon iiWinner = new ImageIcon("sprites/gewonnen.jpg");
	ImageIcon iiLoser = new ImageIcon("sprites/GameOver.jpg");
	
	JFrame frame = new JFrame();
	JLabel label = new JLabel(iiMarmor);
	
	// Spielsteuerungs Buttons
	JButton btnRegistrieren = new JButton("Registrieren");
	JButton btnAnAbmelden = new JButton("Anmelden");
	JButton btnNeuesSpiel = new JButton("Restart");
	JButton btnRegeln = new JButton("Regeln");
	JButton btnHighscore = new JButton("Highscore");
	
	// Spielanzeige Labels
	JLabel lbTimer = new JLabel("Timer");
	JLabel lbSteine = new JLabel("32");
	JLabel lbCopyright = new JLabel("<html><body>&copy; Patrik<br>Münzberg</body></html>");
	JLabel lbWinner = new JLabel(iiWinner);
	JLabel lbLoser = new JLabel(iiLoser);
	
	//Filewriter
	FileWriter fwUser;
	FileReader frUser;

	// Variablen fuer die Zuagabfrage
	int Start = 0;
	int Ziel = 0;
	int xstart = 0;
	int ystart = 0;
	int xziel = 0;
	int yziel = 0;
	int steine = 0;
	int klick = 0;
	int koordinate = 0;

		public setzenST()
		  {
			// frame settings
			// bei ebenden beenden
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			// Titel definieren
			frame.setTitle("Soliaer");
			// Größe festlegen
			frame.setSize(650, 525);		
			// Position bestimmen
			frame.setLocation(200, 200);
			// Frame Größe gespert
			frame.setResizable(false);
			// Layout ausschalten
			frame.setLayout(null);

			
		    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		    // ~~~~~~~~~ Schleife zum setzen von true und false der Spielsteine ~~~~~~~~~~~~~~
		    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			
		    for (int y = 0; y < 7; y++)
		    {
		      if (y < 2 || y > 4)
		      {
		        for (int x = 2; x < 5; x++)
		        {
		        	stonesbt[x][y]=true;
//		        	System.out.println(""+stonesbt[x][y]);
//		        	System.out.println("x="+x+" y="+y);
		        }
		      }
		      else
		      {
		        for (int x = 0; x < 7; x++)
		        {
		        	if (!((x==3)&&(y==3)))
		        	{
			        stonesbt[x][y]=true;
//			        System.out.println(""+stonesbt[x][y]);
//			        System.out.println("x="+x+" y="+y);
		        }
		      }
		    }   
		  }

		    for (int y=0;  y<7;y++)
		    {
		    	for (int x = 0; x<7;x
		    	++)
		    	{
		    		stones[x][y]=x+(y*10);
//		    		System.out.println("x "+x+" y "+y+" "+stones[x][y]);
		    	}
		    }	
		    
		    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		    // ~~~~~~~~~~~~~~~~~~~~~~~ Schleife zum Steine Setzen ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		    			   		    
		    for (int i=0; i<7;i++)
		    {
			    for (int i2=0;i2<7;i2++){
				    btnStones[i][i2] = new JButton(iiStone);
				    frame.add(btnStones[i][i2]);
				    btnStones[i][i2].setActionCommand(""+i+""+i2);
				    btnStones[i][i2].addActionListener(this);
				    if (stonesbt[i][i2]==true)
				    {
				    btnStones[i][i2].setBounds(i*70, i2*70, 70, 70);
				    }
				    else
				    {
			        	if (!((i==3)&&(i2==3)))
			        	{
			        		btnStones[i][i2].setBounds(i*70, i2*70, 70, 70);
			        		btnStones[i][i2].setEnabled(false);
			        		btnStones[i][i2].setVisible(false);
			        		
			        	}
			        	if ((i==3)&&(i2==3))
			        	{
			        		btnStones[i][i2].setIcon(iiMulde);
			        		btnStones[i][i2].setBounds(i*70, i2*70, 70, 70);
			        	}
				    }
		    	}
		    }
		    
		    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		    // ~~~~~~~~~~~~~~~~~~~~~~ Adden der Buttons und Listener ~~~~~~~~~~~~~~~~~~~~~~~~~
		    // ~~~~~~~~~~~~~~~~~~~~~ sowie positionieren der selbigen ~~~~~~~~~~~~~~~~~~~~~~~~		
		    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~		

		    // Spielsteuerung Buttons adden
		    frame.add(btnRegistrieren);
    		frame.add(btnAnAbmelden);
    		frame.add(btnNeuesSpiel);
    		frame.add(btnRegeln);
    		frame.add(btnHighscore);
    		// Spielanzeige Labels adden
    		frame.add(lbTimer);
    		frame.add(lbSteine);
    		frame.add(lbCopyright);
    		
    		// Spielstuerungs Buttons positionieren
    		btnRegistrieren.setBounds(510, 190, 120, 30);
    		btnAnAbmelden  .setBounds(510, 150, 120, 30);
    		btnNeuesSpiel  .setBounds(510, 230, 120, 30);
    		btnRegeln      .setBounds(510, 270, 120, 30);
    		
    		// Spielanzeige Labels positionieren 
    		lbSteine   .setBounds(510, 310, 120, 30);
    		lbCopyright.setBounds(580, 450, 120, 50);
    		// Spielanzeige Labels centrieren
    		lbTimer.setHorizontalAlignment(JTextField.CENTER); 
    		lbSteine.setHorizontalAlignment(JTextField.CENTER); 

    		// Spielsteuerungs Buttons Action listener adden
    		btnRegistrieren.addActionListener(new myRegistrierenButton());
    		btnAnAbmelden.addActionListener(new myMeldenButton());
    		btnNeuesSpiel.addActionListener(new myRestartButton());
    		btnRegeln.addActionListener(new myRegelnButton());
    		
    		frame.add(label);
			label.setBounds(0, 0, 650, 525);
			label.setLayout(null);
			label.setOpaque(true);
			
		    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		    // ~~~~~~~~~~~~~~~~~~~~~~ Spielstein  ~~~~~~~~~~~~~~~~~~~~~~~~~
		    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~		

			// frame sichtbar machen
			frame.setVisible(true);
		  }
		
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			xziel = 0;
			yziel = 0;
			
			klick = klick +1;
	        koordinate = Integer.parseInt(e.getActionCommand());
	        System.out.println(koordinate);
	        
	        steine = 0;	        
	        
			for (int i=0; i<7;i++)
			{
		    for (int i2=0;i2<7;i2++)
		    {
				if (stonesbt[i][i2]==true)
				{
					steine=(steine+1);
					lbSteine.setText(""+steine);
					if ((steine==1)&&(i==7)&&(stonesbt[3][3]==true))
					{
						JOptionPane.showMessageDialog(null, "", "Gewonnwn", 1, iiWinner);
					}
				}
		    }
		}
	
			if (Start==1&&(Ziel==1))
			{			
    			// Start waehlen
			    if (Start==0)
			    {
			        xstart = koordinate/10;
	    			System.out.println("x start "+xstart);
			        ystart = koordinate%10;
			    	System.out.println("y start "+ystart);
		    		if (stonesbt[xstart][ystart]==true)
		    		{
				    	Start = 1;
			    		btnStones[xstart][ystart].setIcon(iiStone2);
				    	System.out.println("start "+Start+" x "+xstart+" y "+ystart);
		    		}
		    		else
		    		{
						xstart = 0;
						ystart = 0;
		    		}
			    }
			    else 
			    {
	    			// Ziel waehlen
			    	if (Ziel==0)
			    	{
				        xziel= koordinate/10;
				    	System.out.println("X ziel "+xziel);
				        yziel= koordinate%10;
				    	System.out.println("y Ziel "+yziel);
			    		if (stonesbt[xziel][yziel]==false)
			    		{
					    	Ziel = 1;
					    	System.out.println("ziel "+Ziel+" x "+xziel+" y "+yziel);

			    		// X +2 *** Sprung von Links nach Rechts
				    	if (xstart+2<7)
				    	{
				    	  if ((xstart+2==xziel)
				    	   && (stonesbt[xstart+1][ystart]==true)
				    	   && (stonesbt[xstart+2][ystart]==false))
				    	  {
					    	stonesbt [xstart]  [ystart]=false;
				    		stonesbt [xstart+1][ystart]=false;
				    		stonesbt [xziel]   [yziel] =true;
				    		btnStones[xstart]  [ystart].setIcon(iiMulde);	
				    		btnStones[xstart+1][ystart].setIcon(iiMulde);	
				    		btnStones[xziel]   [yziel] .setIcon(iiStone);
				    		Start = 0;
				    		Ziel  = 0;
				    	  }
				    	  else
				    	  {
					    		btnStones[xstart][ystart].setIcon(iiStone);	
				    	  }
				    	}
				    	
				    	// X-2 *** Sprung von Rechts nach Links
				    	if (xstart-2>=0)
				    	{
				    		if ((xstart-2==xziel)
				    		&& (stonesbt[xstart-1][ystart]==true)
						  	&& (stonesbt[xstart-2][ystart]==false))
				    			{
				    			stonesbt [xstart]  [ystart]=false;
				    			stonesbt [xstart-1][ystart]=false;
				    			stonesbt [xziel]   [yziel] =true;
				    			btnStones[xstart]  [ystart].setIcon(iiMulde);
				    			btnStones[xstart-1][ystart].setIcon(iiMulde);
				    			btnStones[xziel]   [yziel] .setIcon(iiStone);
					    		Start = 0;
					    		Ziel  = 0;
				    			}
				    		else
				    		{
					    		btnStones[xstart][ystart].setIcon(iiStone);	
				    		}
				    	}
				    	
				    	// Y+2 *** Sprung von Oben nach Unten
				    	if (ystart+2<=7)
				    	{
				    		if ((ystart+2==yziel)
						      &&(stonesbt[xstart][ystart+1]==true)
							  &&(stonesbt[xstart][ystart+2]==false))
				    			{
				    			stonesbt [xstart][ystart]  =false;
				    			stonesbt [xstart][ystart+1]=false;
				    			stonesbt [xziel] [yziel]   =true;
				    			btnStones[xstart][ystart]  .setIcon(iiMulde);
				    			btnStones[xstart][ystart+1].setIcon(iiMulde);
				    			btnStones[xziel] [yziel]   .setIcon(iiStone);
					    		Start = 0;
					    		Ziel  = 0;
				    			}
				    		else
				    		{
					    		btnStones[xstart][ystart].setIcon(iiStone);	
				    		}
				    	}
				    	
				    	// Y-2 *** Sprung von Unten nach Oben
				    	if (ystart-2>=0)
				    	{
				    		if 	((ystart-2==yziel)
							   &&(stonesbt[xstart][ystart-1]==true)
							   &&(stonesbt[xstart][ystart-2]==false))
				    			{
				    			stonesbt [xstart][ystart]  =false;
				    			stonesbt [xstart][ystart-1]=false;
				    			stonesbt [xziel] [yziel]   =true;
				    			btnStones[xstart][ystart]  .setIcon(iiMulde);
				    			btnStones[xstart][ystart-1].setIcon(iiMulde);
				    			btnStones[xziel] [yziel]   .setIcon(iiStone);
					    		Start = 0;
					    		Ziel  = 0;
				    			}
				    		else
				    		{
					    		btnStones[xstart][ystart].setIcon(iiStone);	
				    		}
				    	}
			    		}	
			    		else
			    		{
			    			xziel = 0;
			    			yziel = 0;
			    		}
			    		System.out.println("start "+Start);
			    		System.out.println("ziel "+Ziel);
			    		}
					    }
			    	}
			    }

//		for (int i=0; i<7;i++)
//		{
//			for (int i2=0;i2<7;i2++)
//			{
//				if  (( (stonesbt[i][i2]==true)
//			    	&& (stonesbt[i-1][i2-1]==true)
//					&& (stonesbt[i-2][i2-2]==false))
//					||
//					(   (stonesbt[i][i2]==true)
//					 && (stonesbt[i-10][i2-10]==true)
//					 && (stonesbt[i-20][i2-20]==false))
//					||
//					(   (stonesbt[i][i2]==true)
//					 && (stonesbt[i+1][i2+1]==true)
//					 && (stonesbt[i+2][i2+2]==false))
//					||
//					(   (stonesbt[i][i2]==true)
//					 && (stonesbt[i+10][i2+10]==true)
//					 && (stonesbt[i+20][i2+20]==false)))
//						{
//							break;
//						}
//				else
//				{
//					JOptionPane.showMessageDialog(null, "", "kein Zug mehr möglich", 1, iiLoser);
//				}
//			}
//		}
		

	

		public class myMeldenButton implements ActionListener {
			@Override
			public void actionPerformed(ActionEvent e) 
			{
//				ArrayList<String> daten = new ArrayList<String>();
//				
//				JPanel pnMessage = new JPanel();
//				JLabel lbUser = new JLabel("Benutzername:");
//				JLabel lbPass = new JLabel("Passwort:");
//				JTextField txUser = new JTextField(10);
//				JPasswordField passwordField = new JPasswordField(10);
//				
//				pnMessage.setLayout(new GridLayout(4, 1));
//				pnMessage.add(lbUser);
//				pnMessage.add(txUser);
//				pnMessage.add(lbPass);
//				pnMessage.add(passwordField);
//
//				passwordField.setEchoChar('*');
//				JOptionPane.showMessageDialog(null, pnMessage,
//				"Anmeldung", JOptionPane.PLAIN_MESSAGE);
//
//				String inputUser = txUser.getText();
//				String inputPW = String.valueOf(passwordField.getPassword());
//				System.out.println(inputUser +", " +inputPW);
//				
//				
//				InputStreamReader isr = new InputStreamReader(System.in);
//			    BufferedReader br = new BufferedReader(isr);
////			    System.out.print("Gib was ein: ");
//			    String eingabe = ""+inputUser+" "+inputPW;
//				
//				try
//					{
//						eingabe = br.readLine();
//					} catch (IOException ex)
//					{
//						System.out.println("Fehler beim Lesen!");
//					}
//				    System.out.println("Du hast " + eingabe + " eingegeben.");
					
					
					
//					try
//					{
//						System.out.println(System.getProperty("user.dir"));
//						frUser = new FileReader("./txt/Solitaerpw.txt"); 
//			
//					frUser.read(daten());
//					frUser.close();
//					while ((c = frUser.read()) != -1){
//						System.out.print((char) daten()); 
//					}
//					
//					
//					
//					frUser.read(inputUser+" "+inputPW);
//					frUser.close();
//				}
//				catch (IOException er)
//				{ 
//					er.printStackTrace();
//					System.out.println("Error");
//				}
				

				
			}
		}
		
		
	    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Registrieren Listener ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		
		
		public class myRegistrierenButton implements ActionListener {
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				JPanel pnMessage = new JPanel();
				
				JLabel lbUser = new JLabel("Benutzername:");
				JLabel lbPass = new JLabel("Passwort:");
				
				JTextField txUser = new JTextField(10);
				JPasswordField passwordField = new JPasswordField(10);
				
				pnMessage.setLayout(new GridLayout(4, 1));
				pnMessage.add(lbUser);
				pnMessage.add(txUser);
				pnMessage.add(lbPass);
				pnMessage.add(passwordField);

				passwordField.setEchoChar('*');
				JOptionPane.showMessageDialog(null, pnMessage,
				"Registrierung", JOptionPane.PLAIN_MESSAGE);

				String inputUser = txUser.getText();
				String inputPW = String.valueOf(passwordField.getPassword());
				System.out.println(inputUser +", " +inputPW);
				
				try
				{
					System.out.println(System.getProperty("user.dir"));
					fwUser = new FileWriter("./txt/Solitaerpw.txt", true);
					fwUser.write(inputUser+" "+inputPW+" ");
					fwUser.close();
				}
				catch (IOException er)
				{ 
					er.printStackTrace();
//					System.out.println("error");
				}
			}
		}
		
	    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Restart Listener ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		
		public class myRestartButton implements ActionListener {
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				
			    for (int i=0; i<7;i++)
			    {
				    for (int i2=0;i2<7;i2++){
					    if (stonesbt[i][i2]==true)
					    {
					    btnStones[i][i2].setIcon(iiStone);
					    }
					    else
					    {
				        	if (!((i==3)&&(i2==3)))
				        	{
				        		btnStones[i][i2].setEnabled(false);
				        		btnStones[i][i2].setVisible(false);
				        		
				        	}
				        	if ((i==3)&&(i2==3))
				        	{
				        		btnStones[i][i2].setIcon(iiMulde);
				        	}
					    }
			    	}
			    }
			    
			    for (int y = 0; y < 7; y++)
			    {
			      if (y < 2 || y > 4)
			      {
			        for (int x = 2; x < 5; x++)
			        {
			        	stonesbt[x][y]=true;
			        }
			      }
			      else
			      {
			        for (int x = 0; x < 7; x++)
			        {
			        	if (!((x==3)&&(y==3)))
			        	{
				        stonesbt[x][y]=true;
			        }
			      }
			    }   
			  }
				

			}
		}
		
	    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Regel Listener ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		
		public class myRegelnButton implements ActionListener {
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				JOptionPane.showMessageDialog(null, "<html><body><center><h1>Regeln</h1></center><br><p>Es geht darum, pro Sprung einen daneben liegenden Spielstein<br>zu überspringen, der dadurch aus dem Spiel entfernt wird.</p><p></p><p>Steine dürfen nur <u>senkrecht oder waagerecht</u>, nicht jedoch<br>diagonal springen. Am Ende soll nur ein Stein soll übrig<br>bleiben, und dies an einem vorgegebenen Platz, in der<br>Grundeinstellung in der  Brettmitte.</p><br></body></html>"
						,"Regeln", JOptionPane.PLAIN_MESSAGE);
			}
		}
		
		public static void main(String[] args)
		{
			new setzenST();
		}
}
 
Zuletzt bearbeitet von einem Moderator:

x22

Bekanntes Mitglied
google?

Java:
// BoardSolitaire.java

import ch.aplu.jgamegrid.*;
import java.awt.Point;
import java.util.*;

public class BoardSolitaire extends GameGrid implements GGMouseTouchListener
{
  final int DOWN = 0;
  final int UP = 1;
  Actor draggedMarble;
  private ArrayList<Location> boardPatternLocations = new ArrayList<Location>();
  private Location initialMarbleLocation;

  public BoardSolitaire()
  {
    super(7, 7, 70, null, "sprites/solitaire.png", false);
    setBgColor(255, 166, 0);
    setTitle("Solitaire");
    loadMarbleLocations();
    loadMarbles();
    show();
    setSimulationPeriod(20);
    doRun();
  }

  public void act()
  {
    refresh();
  }

  public void mouseTouched(Actor touchedMarble, GGMouse mouse, Point spot)
  {
    Location mouseLoc = toLocation(mouse.getX(), mouse.getY());
    Point mousePoint = new Point(mouse.getX(), mouse.getY());
    switch (mouse.getEvent())
    {
      case GGMouse.lPress:
        //define draggedMarble:
        if (draggedMarble == null)
        {
          draggedMarble = touchedMarble;
          initialMarbleLocation = touchedMarble.getLocation();
          draggedMarble.show(UP);
          draggedMarble.setOnTop();
        }
        else
        {
          draggedMarble.show(DOWN);
          draggedMarble.setLocationOffset(new Point(0, 0));
          draggedMarble.setLocation(initialMarbleLocation);
          draggedMarble = null;
        }
        break;
      case GGMouse.lDrag:
        if (draggedMarble != null)
          draggedMarble.setPixelLocation(mousePoint);
        break;

      case GGMouse.lRelease:
        if (draggedMarble != null)
        {
          draggedMarble.setLocationOffset(new Point(0, 0));
          if (isValidJumpLocation(mouseLoc, initialMarbleLocation, true)
            && jumpedMarbleExists(mouseLoc, initialMarbleLocation))
          {
            draggedMarble.setLocation(mouseLoc);
            Actor jumpedMarble = getJumpedMarble(mouseLoc, initialMarbleLocation);
            removeActor(jumpedMarble);
          }
          else
          {
            draggedMarble.setLocation(initialMarbleLocation);
          }
          draggedMarble.show(DOWN);
          draggedMarble = null;
          isGameOver();
        }
        break;
    }
  }

  private boolean jumpedMarbleExists(Location loc, Location initialLoc)
  {
    return getJumpedMarble(loc, initialLoc) != null;
  }

  private Actor getJumpedMarble(Location loc, Location initialLoc)
  {
    Double jumpDirection = loc.getDirectionTo(initialLoc);
    Location overJumpedLoc = loc.getNeighbourLocation(jumpDirection);
    return getOneActorAt(overJumpedLoc, Marble.class);
  }

  private void loadMarbleLocations()
  {
    for (int y = 0; y < 7; y++)
    {
      if (y < 2 || y > 4)
      {
        for (int x = 2; x < 5; x++)
          boardPatternLocations.add(new Location(x, y));
      }
      else
      {
        for (int x = 0; x < 7; x++)
          boardPatternLocations.add(new Location(x, y));
      }
    }
  }

  // Initializes marbles on the board
  private void loadMarbles()
  {
    for (Location loc : boardPatternLocations)
    {
      Marble marble = new Marble(DOWN);
      marble.addMouseTouchListener(this, GGMouse.lPress | GGMouse.lDrag | GGMouse.lRelease);
      addActorNoRefresh(marble, loc);
    }
    this.removeActorsAt(new Location(3, 3)); //make hole in middle
  }

  // check if location is a valid jump location
  private boolean isValidJumpLocation(Location loc, Location previousLoc, boolean isDragged)
  {
    int expectedMarbleNr;
    if (isDragged)
      expectedMarbleNr = 1;
    else
      expectedMarbleNr = 0;
    ArrayList<Location> validJumpLocs = new ArrayList<Location>();
    for (int possibleDir = 0; possibleDir < 360; possibleDir += 90)
      validJumpLocs.add(previousLoc.getAdjacentLocation(possibleDir, 2));
    return (boardPatternLocations.contains(loc) && validJumpLocs.contains(loc)
      && getActorsAt(loc, Marble.class).size() == expectedMarbleNr);
  }

  private void isGameOver()
  {
    ArrayList<Actor> leftMarbles = getActors(Marble.class);
    // One left => you win
    if (leftMarbles.size() == 1)
    {
      addActor(new Actor("sprites/you_win.gif"), new Location(3, 3));
      restart();
    }
    else
    {
      //check if there are any valid moves left
      for (Actor a : leftMarbles)
      {
        if (hasOrthagonalJumpableNeighbours(a))
          return;
      }
      //no more valid jumps possible => you lose!
      addActor(new Actor("sprites/gameover.gif"), new Location(3, 3));
      restart();
    }
  }

  private boolean hasOrthagonalJumpableNeighbours(Actor a)
  {
    Location marbleLoc = a.getLocation();
    for (Location loc : marbleLoc.getNeighbourLocations(0.5))
    {
      double locDir = marbleLoc.getDirectionTo(loc);
      Location jumpLoc = loc.getNeighbourLocation(locDir);
      if (getActorsAt(loc, Marble.class).size() != 0
        && isValidJumpLocation(jumpLoc, marbleLoc, false))
        return true;
    }
    return false;
  }

  // restart game
  private void restart()
  {
    delay(5000);
    removeAllActors();
    loadMarbles();
  }

  public static void main(String[] args)
  {
    new BoardSolitaire();
  }
}

// -----------           class Marble    ------------------------
class Marble extends Actor
{

  public Marble(int imgID)
  {
    super("sprites/marble.png", 2);
    show(imgID);
  }
}
 

ARadauer

Top Contributor
Mhn ich sehe leider kein Fragezeichen... hast du eine Frage?

Ich denke nicht, dass sich jeman einen Code im Detail anschaun wird, aber ich kann dir bei den Compile Fehler helfen...

public setzenST() -> Eine Methode braucht einen Rückgabetyp. Wenn sie keinen hat dann void nehmen
public void setzenST()

new setzenST(); -> du kannst nicht einfach in der main eine Methode des Objekts aufrufen. Du musst vorher ein Objekt machen und kannst dann die Methode aufrufen...
new Copy2ofsetzenST().setzenST();

Ich würd nochmal bei dem Fragen, von dem du den Code kopiert hast...
 

HimBromBeere

Top Contributor
Genereller Tipp: deine Methoden sind allesamt viel zu lang. Lagere sich wiederholende Teile in andere Methoden (und u.U. Klassen) aus, dann ist das Durchlesen und Warten sehr viel einfacher. In den ActionListener-Methoden sollte man nach Möglichkeit nur wenig Inhalt schreiben, am besten dort nur Funktionsaufrufe.
 

Neue Themen


Oben