Results 1 to 8 of 8
  1. #1
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default snake not biting itself

    So a couple of days ago i figured i should try making snake (genius) with my noooby programming skills.
    After posting here 2 times and reading about 10 tutorials i am here with my last (?) question.
    How would you detect if the snake is "biting" itself, i tried 2 things (which both failed totally)the latest one waschecking if there was any identical coordinates to the head.

    Java Code:
    package xyexs.snake.src;
    
    import java.awt.BorderLayout;
    import java.util.ArrayList;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class Main {
    	// Variables
    	static int direction = 0;
    	JFrame window = new JFrame(Reference.PROGRAM_NAME + " " + Reference.VERSION);
    	static Panel panel = new Panel();
    	static Score scoreboard = new Score();
    	static int score = 0;
    	public static ArrayList<Integer> chainX = new ArrayList<Integer>();
    	public static ArrayList<Integer> chainY = new ArrayList<Integer>();
    	static Boolean doGrow = false;
    
    	// Main
    	public static void main(String[] args) {
    		chainX.add(0, 500);
    		chainY.add(0, 350);
    		chainX.add(0, 500);
    		chainY.add(0, 300);
    		chainX.add(0, 500);
    		chainY.add(0, 250);
    		JFrame window = new JFrame(Reference.PROGRAM_NAME + " " + Reference.VERSION);
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setResizable(false);
    		window.add(panel, BorderLayout.CENTER);
    		window.add(scoreboard, BorderLayout.SOUTH);
    		window.setSize(Reference.WINDOW_X, Reference.WINDOW_Y);
    		window.pack();
    		window.setVisible(true);
    		int growCounter = 0;
    		int moveCounter = 0;
    		while (true) {
    			try {
    				Thread.sleep(Reference.TICK_TIME);
    			}
    			catch (InterruptedException e) {
    				System.err.println("Sleep interrupted ( " + e + " )");
    			}
    			++growCounter;
    			++moveCounter;
    			if (growCounter == Reference.GROW_TICK) {
    				growCounter = 0;
    				doGrow = true;
    			}
    
    			if (moveCounter == Reference.MOVE_TICK) {
    				Boolean isByTop = (chainY.get(0) == 0);
    				Boolean isByBot = (chainY.get(0) + Reference.PIXEL_SIZE == Reference.WINDOW_Y);
    				Boolean isByLeft = (chainX.get(0) == 0);
    				Boolean isByRight = (chainX.get(0) + Reference.PIXEL_SIZE == Reference.WINDOW_X);
    				Boolean facingSnake = false;
    				for (int i = 1; i < chainX.size() ; i++) {
    					if (chainX.get(0) == chainX.get(i) && chainY.get(0)== chainY.get(i)) {
    						facingSnake = true;
    						System.out.println("Dont bite yourself! Breaking");
    						break;
    					}
    				}
    
    				if (facingSnake == true) {
    					break;
    
    				}
    				if (isByTop == true && direction == 0) {
    					break;
    				}
    				else if (isByBot == true && direction == 2) {
    					break;
    				}
    				else if (isByRight == true && direction == 1) {
    					break;
    				}
    				else if (isByLeft == true && direction == 3) {
    					break;
    				}
    				else {
    					if (direction == 0) {
    						chainX.add(0, chainX.get(0));
    						chainY.add(0, chainY.get(0) - Reference.PIXEL_SIZE);
    					}
    					else if (direction == 1) {
    						chainX.add(0, chainX.get(0) + Reference.PIXEL_SIZE);
    						chainY.add(0, chainY.get(0));
    					}
    					else if (direction == 2) {
    						chainX.add(0, chainX.get(0));
    						chainY.add(0, chainY.get(0) + Reference.PIXEL_SIZE);
    					}
    					else if (direction == 3) {
    						chainX.add(0, chainX.get(0) - Reference.PIXEL_SIZE);
    						chainY.add(0, chainY.get(0));
    					}
    					if (doGrow == false) {
    						chainY.remove(chainX.size() - 1);
    						chainX.remove(chainX.size() - 1);
    					}
    					else {
    						doGrow = false;
    					}
    					panel.repaint();
    				}
    				moveCounter = 0;
    			}
    			score = score + Reference.SCORE_INCREMENT;
    			scoreboard.setText("Score: " + score);
    		}
    
    		ImageIcon image = new ImageIcon("\\C:\\Java\\Snake\\resources\\Lost.png");
    		JLabel imageLabel = new JLabel();
    		imageLabel.setIcon(image);
    		imageLabel.setVisible(true);
    		window.remove(panel);
    		window.add(imageLabel, BorderLayout.CENTER);
    		window.pack();
    	}
    
    	// Public Functions
    	public static int getDir() {
    		return direction;
    	}
    
    	public static void setDir(int dir) {
    		direction = dir;
    	}
    }
    This works sometimes but not close to always... help?
    EDIT: never works..
    Last edited by Xyexs; 08-24-2013 at 05:20 PM.

  2. #2
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: snake not biting itself

    bump

  3. #3
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: snake not biting itself

    EDITED OUT:
    i thought i solved it but i didn't.
    It works sometimes, better than nothing...
    latest attempt:
    Java Code:
    package xyexs.snake.src;
    
    import java.awt.BorderLayout;
    import java.util.ArrayList;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class Main {
    	// Variables
    	static int direction = 0;
    	JFrame window = new JFrame(Reference.PROGRAM_NAME + " " + Reference.VERSION);
    	static Panel panel = new Panel();
    	static Score scoreboard = new Score();
    	static int score = 0;
    	public static ArrayList<Integer> chainX = new ArrayList<Integer>();
    	public static ArrayList<Integer> chainY = new ArrayList<Integer>();
    	static Boolean doGrow = false;
    
    	// Main
    	public static void main(String[] args) {
    		chainX.add(0, 500);
    		chainY.add(0, 400);
    		chainX.add(0, 500);
    		chainY.add(0, 350);
    		chainX.add(0, 500);
    		chainY.add(0, 300);
    		chainX.add(0, 500);
    		chainY.add(0, 250);
    		JFrame window = new JFrame(Reference.PROGRAM_NAME + " " + Reference.VERSION);
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setResizable(false);
    		window.add(panel, BorderLayout.CENTER);
    		window.add(scoreboard, BorderLayout.SOUTH);
    		window.setSize(Reference.WINDOW_X, Reference.WINDOW_Y);
    		window.pack();
    		window.setVisible(true);
    		int growCounter = 0;
    		int moveCounter = 0;
    		while (true) {
    			try {
    				Thread.sleep(Reference.TICK_TIME);
    			}
    			catch (InterruptedException e) {
    				System.err.println("Sleep interrupted ( " + e + " )");
    			}
    			++growCounter;
    			++moveCounter;
    			if (growCounter == Reference.GROW_TICK) {
    				growCounter = 0;
    				doGrow = true;
    			}
    
    			if (moveCounter == Reference.MOVE_TICK) {
    				Boolean isByTop = (chainY.get(0) == 0);
    				Boolean isByBot = (chainY.get(0) + Reference.PIXEL_SIZE == Reference.WINDOW_Y);
    				Boolean isByLeft = (chainX.get(0) == 0);
    				Boolean isByRight = (chainX.get(0) + Reference.PIXEL_SIZE == Reference.WINDOW_X);
    				Boolean facingSnake = false;
    
    				for (int i = 1; i < chainX.size(); i++) {
    					if (direction == 0) {
    						if (chainX.get(0) == chainX.get(i) && chainY.get(0) - Reference.PIXEL_SIZE == chainY.get(i)) {
    							facingSnake = true;
    							System.out.println("OUCH! You bit me! (facing north)");
    							break;
    						}
    					}
    					else if (direction == 1) {
    						if (chainX.get(0) + Reference.PIXEL_SIZE == chainX.get(i) && chainY.get(0) == chainY.get(i)) {
    							facingSnake = true;
    							System.out.println("OUCH! You bit me! (facing east)");
    							break;
    						}
    					}
    					else if (direction == 2) {
    						if (chainX.get(0) == chainX.get(i) && chainY.get(0) + Reference.PIXEL_SIZE == chainY.get(i)) {
    							facingSnake = true;
    							System.out.println("OUCH! You bit me! (facing south)");
    							break;
    						}
    					}
    					else if (direction == 3) {
    						if (chainX.get(0) - Reference.PIXEL_SIZE == chainX.get(i) && chainY.get(0) == chainY.get(i)) {
    							facingSnake = true;
    							System.out.println("OUCH! You bit me! (facing west)");
    							break;
    						}
    					}
    				}
    				if (facingSnake == true) {
    					break;
    				}
    				if (isByTop == true && direction == 0) {
    					break;
    				}
    				else if (isByBot == true && direction == 2) {
    					break;
    				}
    				else if (isByRight == true && direction == 1) {
    					break;
    				}
    				else if (isByLeft == true && direction == 3) {
    					break;
    				}
    				else {
    					if (direction == 0) {
    						chainX.add(0, chainX.get(0));
    						chainY.add(0, chainY.get(0) - Reference.PIXEL_SIZE);
    					}
    					else if (direction == 1) {
    						chainX.add(0, chainX.get(0) + Reference.PIXEL_SIZE);
    						chainY.add(0, chainY.get(0));
    					}
    					else if (direction == 2) {
    						chainX.add(0, chainX.get(0));
    						chainY.add(0, chainY.get(0) + Reference.PIXEL_SIZE);
    					}
    					else if (direction == 3) {
    						chainX.add(0, chainX.get(0) - Reference.PIXEL_SIZE);
    						chainY.add(0, chainY.get(0));
    					}
    					if (doGrow == false) {
    						chainY.remove(chainX.size() - 1);
    						chainX.remove(chainX.size() - 1);
    					}
    					else {
    						doGrow = false;
    					}
    					panel.repaint();
    				}
    				moveCounter = 0;
    			}
    			score = score + Reference.SCORE_INCREMENT;
    			scoreboard.setText("Made by Xyexs        Score: " + score+ "   Length: " + chainX.size());
    		}
    
    		ImageIcon image = new ImageIcon("\\C:\\Java\\Snake\\resources\\Lost.png");
    		JLabel imageLabel = new JLabel();
    		imageLabel.setIcon(image);
    		imageLabel.setVisible(true);
    		window.remove(panel);
    		window.add(imageLabel, BorderLayout.CENTER);
    		window.pack();
    	}
    
    	// Public Functions
    	public static int getDir() {
    		return direction;
    	}
    
    	public static void setDir(int dir) {
    		direction = dir;
    	}
    }
    Last edited by Xyexs; 08-24-2013 at 06:07 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,651
    Rep Power
    5

    Default Re: snake not biting itself

    Have you tried deriving line segments from the points that make up the snake and seeing when the
    line segments intersect with other line segments. See Line2D (Java Platform SE 7 )

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: snake not biting itself

    I didn't understand a word :P

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,651
    Rep Power
    5

    Default Re: snake not biting itself

    Well, I am not certain how your snake is constructed so I will pretend it is a series of coordinates that slither around. Let's say the head starts at (x1,y1), the next section at (x2,y2), then (x3, y3), and (x4.y4). If you want to see if the segment of the snake (x1,y1,x2,y2) crosses some other segment (say x45,y45,x46,y46) then if the two segments cross each other (intersect) then the snake crossed itself. There is a method in the link I provided earlier which has methods to detect the intersection of two line segments.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: snake not biting itself

    It's based off 2 arraylists (chainX and chainY) that contain them like {50,100,150 (pixel_size is 50, its kind of a "part" of the snake)} and {200,200,200}

    id did this:
    Java Code:
    for (int i = 1; i < chainX.size(); i++) {
    	if (direction == 0) {
    		if (chainX.get(0) == chainX.get(i) && chainY.get(0) - Reference.PIXEL_SIZE == chainY.get(i)) {
    			facingSnake = true;
    	   	        System.out.println("OUCH! You bit me! (facing north)");
    			Break;
    		}
    	}
    	else if (direction == 1) {
    	     if (chainX.get(0) + Reference.PIXEL_SIZE == chainX.get(i) && chainY.get(0) == chainY.get(i)) {
                    facingSnake = true;
    		System.out.println("OUCH! You bit me! (facing east)");
    		break;
    	     }
    	}
    	else if (direction == 2) {
    		if (chainX.get(0) == chainX.get(i) && chainY.get(0) + Reference.PIXEL_SIZE == chainY.get(i)) {
    			FacingSnake = true;
    			System.out.println("OUCH! You bit me! (facing south)");
    			break;
    		}
    	}
    	else if (direction == 3) {
    		if (chainX.get(0) - Reference.PIXEL_SIZE == chainX.get(i) && chainY.get(0) == chainY.get(i)) {
    		        facingSnake = true;
    		        System.out.println("OUCH! You bit me! (facing west)");
    			break;
    		}
    	}
    }
    Well, im to tired to do this now... it's 2 pm here... :P

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: snake not biting itself

    If you had used Points for the coordinates of the snake segments, a snake would look like this:

    Java Code:
    List<Point> snake= ...;
    Assuming the head of the snake is at position #0, your check reduces to just this:

    Java Code:
    if (snake.lastIndexOf(snake.get(0)) > 0)
       // snake has bitten itself.
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Snake Game
    By FrankElliot9 in forum New To Java
    Replies: 5
    Last Post: 12-03-2012, 05:01 AM
  2. altered snake game
    By dmp in forum Android
    Replies: 1
    Last Post: 04-17-2012, 05:49 AM
  3. Snake Game
    By LuluMM in forum New To Java
    Replies: 0
    Last Post: 03-08-2012, 06:48 AM
  4. snake and ladder
    By angela in forum Java Applets
    Replies: 2
    Last Post: 04-09-2011, 05:28 PM
  5. Snake Game
    By mustachMan in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 10:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •