/// <summary> /// Method that adds new Join to snake's tail /// </summary> /// <param name="j">Join that should be added</param> public void addJoinToTail(Join j) { if (j != null) { tail.setNext(j); j.setPrev(tail); tail = j; length++; } }
/// <summary> /// Method that moves snake's head by adding new Join to it. Snake is following received direction /// </summary> /// <param name="direction">direction in whitch snake is moving</param> /// <returns></returns> public Coordinates moveHead(int direction) { //creates next bone and connects it before snake's head. That imitates that head was moved Bone newBone = new Bone(direction, head.getBone().getCoord().getX(), head.getBone().getCoord().getY()); Join j = new Join(newBone, null, head); head.setPrev(j); //direction of ex-head should be changed. 8-up, 2-down, 4-left, 6-right. 1,7,3,9 - corners. //use numeric keypad to visualise direction better int z = head.getBone().getDirection(); if (direction != z) {//if direction of nex head and ex-head are different, then change diretion of ex-head to hold corner if (direction == 8) { head.getBone().setDirection(z == 4 ? 1 : 3); j.getBone().getCoord().setY(newBone.getCoord().getY() - 1); } else if (direction == 6) { head.getBone().setDirection(z == 8 ? 7 : 1); j.getBone().getCoord().setX(newBone.getCoord().getX() + 1); } else if (direction == 2) { head.getBone().setDirection(z == 4 ? 7 : 9); j.getBone().getCoord().setY(newBone.getCoord().getY() + 1); } else if (direction == 4) { head.getBone().setDirection(z == 8 ? 9 : 3); j.getBone().getCoord().setX(newBone.getCoord().getX() - 1); } } else { switch (direction) {// correction of coordinates of a new head case 2: j.getBone().getCoord().setY(newBone.getCoord().getY() + 1); break; case 4: j.getBone().getCoord().setX(newBone.getCoord().getX() - 1); break; case 6: j.getBone().getCoord().setX(newBone.getCoord().getX() + 1); break; case 8: j.getBone().getCoord().setY(newBone.getCoord().getY() - 1); break; } } setHead(j); length++; return(head.getBone().getCoord()); }