How did the snake move without collision with the wall of the seed?
I'm confused
BasicSnake.java
public interface BasicSnake
{
int move(final Field field);
}
Direction.java
public enum Direction
{
LEFT,RIGHT,UP,DOWN;
}
Field.java
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Random;
public final class Field
{
private ArrayList<Snake> SnakeList;
private ArrayList<Wall> wallList;
private ArrayList<Snake> tempList;
private int width = 800;
private int height = 600;
private Position seed;
private int time=0;
public ArrayList<Snake> getSnakeList() {
ArrayList<Snake> result = new ArrayList<Snake>();
for(Snake s:SnakeList)
{
Snake t= new Snake(s.ID,s.name);
t.clone(s);
result.add(t);
}
return result;
}
public ArrayList<Wall> getWallList()
{
ArrayList<Wall> result = new ArrayList<Wall>();
for(Wall w:wallList)
result.add(new Wall(w));
return result;
}
public int getWidth() {
return width;
}
public int getHeight()
{
return height;
}
public Position getSeed()
{
return new Position(seed);
}
public int getTime() {
return time;
}
public Field(int width,int height)
{
this.width = width;
this.height= height;
initSnakeList();
initWallList();
initSeed();
}
public void canEat()
{
for(Snake s:SnakeList)
if(s.head.equals(seed))
{
eat(s);
System.out.println(s+" score: "+ s.score+" t: "+time);
}
}
@SuppressWarnings("unused")
public void removeHit()
{
ArrayList<Snake> removedList = new ArrayList<Snake>();
ListIterator<Snake> itr = this.SnakeList.listIterator();
for(;itr.hasNext();)/>
{
Snake s1= (Snake) itr.next();
ListIterator<Snake> itr2 = this.SnakeList.listIterator();
for(;itr2.hasNext();)/>
{
Snake s2= (Snake) itr2.next();
if(s1==s2)
continue;
switch(s1.isHitByHeadToSnake(s2))
{
case 2:
System.out.println(s1+" & "+s2+" hit together t: "+time );
removedList.add(s1);
removedList.add(s2);
break;
case 1:
System.out.println(s1+" hit to "+s2+ " t: "+time);
removedList.add(s1);
break;
}
}
}
itr = this.SnakeList.listIterator();
ListIterator<Snake> itr2 = this.tempList.listIterator();
for(;itr.hasNext();)/>
{
Snake s= itr.next();
Snake temp= itr2.next();
for(Snake t:removedList)
if(t==s)
{
itr.remove();
itr2.remove();
break;
}
}
itr = this.SnakeList.listIterator();
itr2 = this.tempList.listIterator();
for(;itr.hasNext();)/>
{
Snake s3= itr.next();
Snake temp= itr2.next();
if(s3.isCollided2AnyWall(wallList)!=-1)
{
System.out.println(s3+" hit the wall t:"+time );
itr.remove();
itr2.remove();
}
}
itr = this.SnakeList.listIterator();
itr2 = this.tempList.listIterator();
for(;itr.hasNext();)/>
{
Snake s= itr.next();
Snake temp= itr2.next();
if(s.isInterCollision())
{
System.out.println(s+" hit to itself t:"+time);
itr.remove();
itr2.remove();
}
}
}
public void updateAllSnakes()
{
for(int i=0;i<this.SnakeList.size();i++)
{
Snake s =this.SnakeList.get(i);
Snake temp =tempList.get(i);
temp.clone(s);
s.score++;
int action = temp.move(this);
switch(action)
{
default:
case 0:
s.moveRight(width);
break;
case 1:
s.moveUp(height);
break;
case 2:
s.moveLeft(width);
break;
case 3:
s.moveDown(height);
break;
}
}
}
public void update()
{
time++;
this.removeHit();
this.canEat();
this.updateAllSnakes();
}
private void eat(Snake s)
{
//s.body.add(0, new Position(s.head));
//s.head.clone(seed);
s.score+=10;
nextSeed();
}
public void updateWalls()
{
for(int i=0;i<wallList.size();i++)
if((wallList.get(i).duration)--==0)
{
wallList.remove(i);
this.insertWalls();
}
}
public void insertWalls()
{
Random r = new Random();
int s= r.nextInt(10-wallList.size())+1;
for(int i =0;i<s;)
{
Wall temp = new Wall();
temp.duration = r.nextInt(5)*20;
temp.generateWall(new Position(r.nextInt(this.width),r.nextInt(this.height)), r.nextInt(10), r.nextInt(10));
if(temp.isWallonAnySnake(SnakeList)==-1)
{
if(!temp.location.contains(seed))
wallList.add(temp);
i++;
}
}
}
private void initSeed()
{
Random rnd = new Random();
seed= new Position(rnd.nextInt(width),rnd.nextInt(height));
}
public void nextSeed()
{
Random rnd = new Random();
seed.set(rnd.nextInt(width),rnd.nextInt(height));
}
private void initSnakeList()
{
SnakeList = new ArrayList<Snake>();
tempList = new ArrayList<Snake>();
SnakeList.add(new G1(0,"G1"));
SnakeList.add(new G2(1,"G2"));
SnakeList.add(new G3(2,"G3"));
SnakeList.add(new G4(3,"G4"));
SnakeList.add(new G5(4,"G5"));
SnakeList.add(new G6(5,"G6"));
SnakeList.add(new G7(6,"G7"));
SnakeList.add(new G8(7,"G8"));
tempList.add(new G1(0,"G1"));
tempList.add(new G2(1,"G2"));
tempList.add(new G3(2,"G3"));
tempList.add(new G4(3,"G4"));
tempList.add(new G5(4,"G5"));
tempList.add(new G6(5,"G6"));
tempList.add(new G7(6,"G7"));
tempList.add(new G8(7,"G8"));
for(int i=0;i<SnakeList.size();)/>
{
Snake s=SnakeList.get(i);
initSnake(s,30);
boolean done=true;
for(int j=0;j<i;j++)
{
Snake t=SnakeList.get(j);
if(s.isCollided2Snake(t))
done = false;
}
if(done == true)
i++;
}
}
private void initWallList()
{
wallList = new ArrayList<Wall>();
this.insertWalls();
}
public void initSnake(Snake s,int size)
{
s.body.clear();
Random rnd = new Random();
s.head.set(Math.abs(rnd.nextInt(width-1)), Math.abs(rnd.nextInt(height-1)));
int direction = Math.abs(rnd.nextInt(4));
Position h = new Position(s.head);
for(int i=0;i<size-1;i++)
{
switch(direction)
{
case 0:
h.left(width);
break;
case 1:
h.right(width);
break;
case 2:
h.up(height);
break;
case 3:
h.down(height);
}
s.body.add(new Position(h));
}
}
}
G1.java
import java.util.Random;
public class G1 extends Snake
{
public G1(int ID, String name) {
super(ID, name);
// TODO Auto-generated constructor stub
}
@Override
public int move(final Field field)
{
Snake temp = new G1(-1, "temp");
Random r= new Random();
boolean found= false;
int numberOfTry =0;
int d= r.nextInt(3);
while(!found && numberOfTry<10)
{
numberOfTry++;
temp.clone(this);
d= r.nextInt(4);
switch(d)
{
case 3:
temp.moveDown(field.getHeight());
break;
case 1:
temp.moveUp(field.getHeight());
break;
case 2:
temp.moveLeft(field.getWidth());
break;
case 0:
temp.moveRight(field.getWidth());
break;
}
if(!temp.isInterCollision())
found = true;
}
//this.clone(temp);
return d;
}
}
Position.java
public class Position
{
private int x,y;
public Position() {
super();
this.x = 0;
this.y = 0;
}
public Position(int x, int y) {
super();
this.x = x;
this.y = y;
}
public Position(Position t)
{
super();
this.clone(t);
}
public Position right(int width)
{
++(this.x);
if(this.x>=width)
this.x-= width;
return this;
}
public Position left(int width)
{
--this.x;
if(this.x<0)
this.x+=width;
return this;
}
public Position up(int height)
{
this.y--;
if(this.y<0)
this.y+= height;
return this;
}
public Position down(int height)
{
this.y++;
if(this.y>=height)
this.y-= height;
return this;
}
public void set(int x, int y)
{
this.x =x;
this.y= y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public double distanceTo(Position t)
{
return Math.sqrt(Math.pow(t.x-this.x, 2)+Math.pow(t.y-this.y, 2));
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Position other = (Position) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
@Override
public String toString() {
return " (" + x + ", " +y+ ")";
}
public void show()
{
System.out.println(this.toString());
}
public void clone(Position t)
{
this.x= t.x;
this.y= t.y;
}
}
Seed.java
import java.awt.Color;
public class Seed
{
public Position location;
public Color color;
}
Snake.java
import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
public class Snake implements BasicSnake
{
public Position head;
public ArrayList<Position> body;
public int ID;
public String name;
public Color headColor;
public Color bodyColor;
public int score=0;
public Snake(int ID, String name)
{
Random r= new Random();
this.head = new Position();
this.body= new ArrayList<Position>();
this.ID = ID;
this.name = name;
this.headColor = new Color(100,11,3);
this.bodyColor = new Color(50+r.nextInt(100), 150+r.nextInt(80), 30+r.nextInt(200));
}
public Snake(int ID, String name,Color headColor,Color bodyColor)
{
this.head = new Position();
this.body= new ArrayList<Position>();
this.ID = ID;
this.name = name;
this.headColor = new Color(headColor.getRGB());
this.bodyColor = new Color(bodyColor.getRGB());
}
public final int size()
{
if(head!=null)
return body.size()+1;
else
return 0;
}
public final void moveRight(int width)
{
for(int i=this.body.size()-1;i>0;i--)
body.get(i).clone(body.get(i-1));
if(body.size()!=0)
body.get(0).clone(head);
head.right(width);
}
public final void moveLeft(int width)
{
for(int i=this.body.size()-1;i>0;i--)
body.get(i).clone(body.get(i-1));
if(body.size()!=0)
body.get(0).clone(head);
head.left(width);
}
public final void moveUp(int height)
{
for(int i=this.body.size()-1;i>0;i--)
body.get(i).clone(body.get(i-1));
if(body.size()!=0)
body.get(0).clone(head);
head.up(height);
}
public final void moveDown(int height)
{
for(int i=this.body.size()-1;i>0;i--)
body.get(i).clone(body.get(i-1));
if(body.size()!=0)
body.get(0).clone(head);
head.down(height);
}
public final boolean isCollided2Snake(Snake s)
{
boolean result=false;
if(this.head.equals(s.head))
result = true;
if(s.body.contains(this.head))
result = true;
if(this.body.contains(s.head))
result = true;
for(Position p : this.body)
if(s.body.contains(p))
return true;
return result;
}
public final int isHitByHeadToSnake(Snake s)
{
int r=0;
if(this.head.equals(s.head))
r=2;
if(s.body.contains(this.head))
r=1;
return r;
}
public final int isCollided2AnySnake(ArrayList<Snake> s)
{
int result=-1;
for(int i=0;i<s.size();i++)
if(this.isCollided2Snake(s.get(i)))
result = i;
return result;
}
public final boolean isInterCollision()
{
boolean result= false;
if(this.body.contains(this.head))
result = true;
for(int i=0;i<this.body.size()-1;i++)
if(this.body.get(i).equals(this.body.get(i+1)))
result = true;
return result;
}
public final boolean isCollided2Wall(Wall w)
{
boolean result=false;
if(w.location.contains(this.head))
result = true;
for(Position p : this.body)
if(w.location.contains(p))
return true;
return result;
}
public final int isCollided2AnyWall(ArrayList<Wall> w)
{
int result=-1;
for(int i=0;i<w.size();i++)
if(this.isCollided2Wall(w.get(i)))
result = i;
return result;
}
public final Position getHead() {
return head;
}
public final void setHead(Position head) {
this.head = head;
}
public final ArrayList<Position> getBody() {
return body;
}
public final void setBody(ArrayList<Position> body) {
this.body = body;
}
public final int getID() {
return ID;
}
public final void setID(int iD) {
ID = iD;
}
public final String getName() {
return name;
}
public final void setName(String name) {
this.name = name;
}
public final Color getHeadColor() {
return headColor;
}
public final void setHeadColor(Color headColor) {
this.headColor = headColor;
}
public final Color getBodyColor() {
return bodyColor;
}
public final void setBodyColor(Color bodyColor) {
this.bodyColor = bodyColor;
}
public final void clone(Snake t)
{
this.body.clear();
this.score = t.score;
this.head.clone(t.head);
this.headColor = t.headColor;
this.bodyColor = t.bodyColor;
for(Position p: t.body)
{
this.body.add(new Position(p));
}
}
public final int getScore() {
return score;
}
public final void setScore(int score)
{
this.score = score;
}
public final String toString()
{
return "("+this.ID+")";
}
@Override
public int move(Field field)
{
// TODO Auto-generated method stub
return 0;
}
}
Test.java
import java.applet.*;
import java.awt.*;
@SuppressWarnings("serial")
public class Test extends Applet
implements Runnable {
int width, height;
Image backbuffer;
Graphics backg;
Field field;
int countDown;
Color fieldColor;
private Thread clock;
private boolean running= false;
public void init()
{
clock = new Thread(this);
width =800;
height = 600;
countDown= 100;
setSize (width, height);
backbuffer = createImage( width, height );
backg = backbuffer.getGraphics();
initField();
resetField();
field = new Field(width/10, height/10);
running = true;
clock.start();
}
void initField()
{
fieldColor= new Color(140, 190, 220);
}
void resetField()
{
backg.setColor(fieldColor);
backg.fillRect( 0, 0, width, height );
}
void printWall(Wall w)
{
backg.setColor(w.getColor());
for(Position p: w.location)
{
backg.fillRect(p.getX()*10,p.getY()*10,10,10);
}
}
void printAllWalls()
{
field.updateWalls();
for(Wall w:field.getWallList())
printWall(w);
}
void printAllSnakes()
{
for(Snake s:field.getSnakeList())
printSnake(s);
}
void printSnake(Snake s)
{
backg.setColor(s.getHeadColor());
backg.fillOval(s.head.getX()*10,s.head.getY()*10,10,10);
backg.setColor(s.getBodyColor());
for(Position p: s.body)
{
backg.fillOval(p.getX()*10,p.getY()*10,10,10);
}
}
public void printSeed()
{
backg.setColor(new Color(200,100,220));
backg.fillOval(field.getSeed().getX()*10,field.getSeed().getY()*10,10,10);
}
public void printField()
{
resetField();
printSeed();
printAllSnakes();
printAllWalls();
}
public void update( Graphics g )
{
g.drawImage( backbuffer, 0, 0, this );
}
public void paint( Graphics g )
{
update( g );
}
@SuppressWarnings({ "static-access", "deprecation" })
@Override
public void run()
{
String m="";
if(running ==false)
init();
while(running)
{
countDown--;
field.update();
printField();
if(countDown==0)
{
int max = 0;
for(Snake s:field.getSnakeList())
{
if(s.score>max)
max = s.score;
System.out.println(s+"->"+ s.score);
}
m="max: ";
for(Snake s:field.getSnakeList())
{
if(s.score==max)
m += s+"*";
}
clock.stop();
return;
}
if(field.getSnakeList().size()==0 && countDown!=0)
{
m="no One wins !!";
System.out.println(m);
clock.stop();
return;
}
repaint();
try
{
clock.sleep(100L);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
@SuppressWarnings("deprecation")
public void start()
{
clock.resume();
}
@SuppressWarnings("deprecation")
public void stop()
{
clock.suspend();
}
public void destroy()
{
running = false;
clock = null;
}
}
Wall.java
import java.awt.Color;
import java.util.ArrayList;
public class Wall
{
public ArrayList<Position> location;
private Color color;
public int duration;
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Wall()
{
this.color = new Color(50, 10, 80);
location= new ArrayList<Position>();
}
public Wall(Wall w)
{
this.color = new Color(50, 10, 80);
location= new ArrayList<Position>();
for(Position p: w.location)
this.location.add(new Position(p));
}
public void set(Wall w)
{
for(Position p: w.location)
this.location.add(new Position(p));
}
public int size()
{
return location.size();
}
public void generateWall(Position s,Position e)
{
location.clear();
for(int i=s.getX();i<=e.getX();i++)
for(int j=s.getY();j<=e.getY();j++)
location.add(new Position(i,j));
}
public void generateWall(Position s,int width,int height)
{
location.clear();
for(int i=0;i<width;i++)
for(int j=0;j<height;j++)
location.add(new Position(i+s.getX(),j+s.getY()));
}
public boolean isWallOnSnake(Snake s)
{
boolean result=false;
if(location.contains(s.head))
result = true;
for(Position p : s.body)
if(this.location.contains(p))
return true;
return result;
}
public int isWallonAnySnake(ArrayList<Snake> s)
{
int result =-1;
for(int i=0;i<s.size();i++)
if(this.isWallOnSnake(s.get(0)))
result=i;
return result;
}
public boolean isWallOnWall()
{
return false;
}
public static Wall createWall(Position s,Position e,int duration)
{
Wall w= new Wall();
w.generateWall(s, e);
w.duration= duration;
return w;
}
public static Wall createWall(Position s,int width,int height,int duration)
{
Wall w= new Wall();
w.generateWall(s, width,height);
w.duration = duration;
return w;
}
}
Several important:
Move the snake to eat the seeds automatically without keyboard.
Snakes do not hit each other.
8 There is a snake in the program.
Walls appear automatically.

New Topic/Question
Reply



MultiQuote







|