public void move(string direction) { Position currentHeadPosition = body[0]; Position newHeadPosition = null; switch (direction) { case "UP": if (currentHeadPosition.getRowNo() > mainBoard.getMinRowNo()) { newHeadPosition = new Position(currentHeadPosition.getRowNo() - 1, currentHeadPosition.getColNo()); } break; case "DOWN": if (currentHeadPosition.getRowNo() < mainBoard.getMaxRowNo()) { newHeadPosition = new Position(currentHeadPosition.getRowNo() + 1, currentHeadPosition.getColNo()); } break; case "LEFT": if (currentHeadPosition.getColNo() > mainBoard.getMinColNo()) { newHeadPosition = new Position(currentHeadPosition.getRowNo(), currentHeadPosition.getColNo() - 1); } break; case "RIGHT": if (currentHeadPosition.getColNo() < mainBoard.getMaxColNo()) { newHeadPosition = new Position(currentHeadPosition.getRowNo(), currentHeadPosition.getColNo() + 1); } break; default: //Do nothing break; } if (newHeadPosition != null) { body.Insert(0, newHeadPosition); //Add a new head positon 0 body.RemoveAt(body.Count - 1); //Remove the last position } }
public Rewards(int size, Board mainBoard) { this.mainBoard = mainBoard; applelist = new List <Position>(); for (int i = 0; i < size; i++) { int rowNo, colNo; //Generate an apple at random position but not duplicated do { //Generate a random number between 1 and MaxRowNo rowNo = (new Random()).Next(1, mainBoard.getMaxRowNo() + 1); //Generate a random number between 1 and MaxColNo colNo = (new Random()).Next(1, mainBoard.getMaxColNo() + 1); } while (isDuplicate(rowNo, colNo) == true); applelist.Add(new Position(rowNo, colNo)); } }