The main classes will be:
1. Snake
2. Cell
3. Board
4. Game
import [Link];
public class Snake {
private LinkedList<Cell> snakePartList= new LinkedList<>();
private Cell head;
public Snake(Cell initPos){
head = initPos;
[Link](head);
[Link](CellType.SNAKE_NODE);
}
public void grow() { [Link](head); }
public void move(Cell nextCell)
{
[Link]("Snake is moving to + [Link]() + " " + [Link]());
Cell tail = [Link]();
[Link]([Link]);
head = nextCell;
[Link](CellType.SNAKE_NODE);
[Link](head);
}
public boolean checkCrash(Cell nextCell)
{
[Link]("Going to check for Crash");
for (Cell cell : snakePartList) {
if (cell == nextCell) {
return true;
}
}
return false;
}
public LinkedList<Cell> getSnakePartList()
{
return snakePartList;
}
public void
setSnakePartList(LinkedList<Cell> snakePartList){
[Link] = snakePartList;
}
public Cell getHead() { return head; }
public void setHead(Cell head) { [Link] = head; }
}
public class Board {
final int ROW_COUNT, COL_COUNT;
private Cell[][] cells;
public Board(int rowCount, int columnCount)
{
ROW_COUNT = rowCount;
COL_COUNT = columnCount;
cells = new Cell[ROW_COUNT][COL_COUNT];
for (int row = 0; row < ROW_COUNT; row++) {
for (int column = 0; column < COL_COUNT;
column++) {
cells[row][column] = new Cell(row, column);
}
}
}
public Cell[][] getCells() { return cells; }
public void setCells(Cell[][] cells)
{
[Link] = cells;
}
public void generateFood()
{
[Link]("Going to generate food");
int row = 0, column = 0;
while (true) {
row = (int)([Link]() * ROW_COUNT);
column = (int)([Link]() * COL_COUNT);
if (cells[row][column].getCellType()
!= CellType.SNAKE_NODE)
break;
}
cells[row][column].setCellType([Link]);
[Link]("Food is generated at: " + row
+ " " + column);
}
}
// To represent Snake Game
public class Game {
public static final int DIRECTION_NON= 0,
DIRECTION_RIGHT = 1, DIRECTION_LEFT = -1,
DIRECTION_UP = 2, DIRECTION_DOWN = -2;
private Snake snake;
private int direction;
private boolean gameOver;
public Game(Snake snake, Board board)
{
[Link] = snake;
public Snake getSnake() {
return snake; }
public void setSnake(Snake snake){
[Link] = snake;
}
public Board getBoard() { return board; }
public void setBoard(Board board)
{
[Link] = board;
}
public boolean isGameOver() { return gameOver; }
public void setGameOver(boolean gameOver)
{
[Link] = gameOver;
}
public int getDirection() { return direction; }
public void setDirection(int direction){
[Link] = direction;
}
public void update()
{
[Link]("Going to update the game");
if (gameOver) {
if (direction != DIRECTION_NONE) {
Cell nextCell= getNextCell([Link]());
if ([Link](nextCell)) {
setDirection(DIRECTION_NONE);
gameOver = true;
}
else {
[Link](nextCell);
if ([Link]()== [Link]) {
[Link]();
[Link]();
}
}
}
}
}
private Cell getNextCell(Cell currentPosition)
{
[Link]("Going to find next cell");
row = [Link]();
col = [Link]();
if (direction == DIRECTION_RIGHT) {
col++;
}
else if (direction == DIRECTION_LEFT) {
col--;
}
else if (direction == DIRECTION_UP) {
row--;
}
else if (direction == DIRECTION_DOWN) {
row++;
}
Cell nextCell = [Link]()[row][col];
return nextCell;
}
public static void main(String[] args)
{
[Link]("Going to start game");
Cell initPos = new Cell(0, 0);
Snake initSnake = new Snake(initPos);
Board board = new Board(10, 10);
Game newGame = new Game(initSnake, board);
[Link] = true;
[Link] = DIRECTION_RIGHT;
for (int i = 0; i < 5; i++) {
if (i == 2)
[Link]();
[Link]();
if (i == 3)
[Link] = DIRECTION_RIGHT;
if ([Link] == true)
break;
}
}
}