Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lukka
- import java.util.*;
- public class SnakeGame {
- public static char[][] gameBoard = new char[8][8];
- public static void printBoard(){
- int i,j;
- for(i=0;i<8;++i){
- for(j=0;j<8;++j){
- System.out.print(gameBoard[i][j]+" ");
- }
- System.out.print("\n");
- }
- System.out.print("\n");
- }
- public static boolean checkCollision(Snake snake1, Snake snake2){
- for(int i=0;i<snake1.getLength();++i){
- for(int j=0;j<snake2.getLength();++j){
- if(snake1.snakeBody.get(i).equals(snake2.snakeBody.get(j)))
- return true;
- }
- }
- return false;
- }
- public static void updateBoard(Snake snake){
- gameBoard[snake.snakeBody.get(0).x][snake.snakeBody.get(0).y]='@';
- for(int i=1;i<snake.getLength();++i){
- gameBoard[snake.snakeBody.get(i).x][snake.snakeBody.get(i).y]='o';
- }
- }
- public static void clearBoard(){
- int i,j;
- for(i=0;i<8;++i){
- for(j=0;j<8;++j){
- gameBoard[i][j] = '.';
- }
- }
- }
- public static void main(String args[]){
- clearBoard();
- Snake snake1 = new Snake(new Point(2,2),new Point(2,1),new Point(2,0));
- Snake snake2 = new Snake(new Point(5,5),new Point(5,6),new Point(5,7));
- updateBoard(snake1);
- updateBoard(snake2);
- while(true){
- printBoard();
- try{
- Thread.sleep(3000);
- }
- catch (InterruptedException e){};
- if(!snake1.newMove()){
- System.out.println("0-1");
- break;
- };
- if(!snake2.newMove()){
- System.out.println("1-0");
- break;
- }
- if(checkCollision(snake1,snake2)){
- System.out.println("0-0");
- break;
- }
- clearBoard();
- updateBoard(snake1);
- updateBoard(snake2);
- }
- }
- }
- class Point{
- public int x,y;
- public Point(){
- this.x = 0;
- this.y = 0;
- }
- public Point(int x, int y){
- this.x = x;
- this.y = y;
- }
- public void makeEqual(Point a){
- this.x = a.x;
- this.y = a.y;
- }
- }
- class Snake{
- ArrayList<Point> snakeBody = new ArrayList<>(3);
- private int moveNumber = 0;
- public Snake(Point a, Point b, Point c){
- this.snakeBody.add(a);
- this.snakeBody.add(b);
- this.snakeBody.add(c);
- }
- public int getLength(){
- return this.snakeBody.size();
- }
- public boolean timeToIncrease(){
- return (this.moveNumber % 3 == 0)? true: false;
- }
- private boolean checkPointCollision(Point a){
- if(a.x<0 || a.x>7 || a.y<0 ||a.y>7)
- return true;
- for(int i=0;i<this.getLength();++i){
- if(a.equals(this.snakeBody.get(i)))
- return true;
- }
- return false;
- }
- public boolean newMove(){
- if(!timeToIncrease()) {
- (this.snakeBody.get(this.getLength())).makeEqual(new Point(-1,-1));
- }
- Point newHeadPosition = new Point();
- int spareDirections = 4;
- int[] Directions = new int[4];
- do {
- newHeadPosition.makeEqual(this.snakeBody.get(0));
- Random rnd = new Random();
- int moveNumber = rnd.nextInt(4);
- switch (moveNumber) {
- case 0:
- newHeadPosition.x--;
- break;
- case 1:
- newHeadPosition.y--;
- break;
- case 2:
- newHeadPosition.x++;
- break;
- case 3:
- newHeadPosition.y++;
- break;
- }
- if(Directions[moveNumber]==0){
- spareDirections--;
- Directions[moveNumber]=1;
- }
- if(spareDirections==0)
- return false;
- }while(this.checkPointCollision(newHeadPosition));
- if(timeToIncrease()){
- this.snakeBody.add(new Point(-1,-1));
- }
- for(int i=this.getLength()-1;i>0;--i){
- this.snakeBody.get(i).makeEqual(this.snakeBody.get(i-1));
- }
- (this.snakeBody.get(0)).makeEqual(newHeadPosition);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement