Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //we are maintaining a snake board of [5][5] in which snake would grow and move in the directions specified by user
- class SnakeBoard{
- int [][] Board;
- int moves;
- int [][] position=new int[100][2];
- int direction;
- //up 0,left 1,down 2,right 3
- public SnakeBoard(){
- //we are initializing the array to teh starting position when the constructed is called initially
- Board=new int[][]{
- { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 1, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 2, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 3, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 4, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 5, 0, 0, 0, 0 }
- };
- //this keeps the track of moves the snake has taken till present state
- moves=0;
- direction=0;
- //we keep the position of the snake in teh position array,we store teh coordinates of it
- for(int i=0;i<100;i++){
- position[i][0]=-1;position[i][1]=-1;
- }
- position[0][0]= 4 ;position[0][1]=4;
- position[1][0]= 5 ;position[1][1]=4;
- position[2][0]= 6 ;position[2][1]=4;
- position[3][0]= 7 ;position[3][1]=4;
- position[4][0]= 8 ;position[4][1]=4;
- }
- public static int move(string A){
- moves++;
- //if the snake want to move in upward direction
- if(A.equlas("u")){
- if(position[0][0]==0) return -1;
- board[position[0][0]-1][position[0][1]]=1;
- int start=0;
- while(position[start+1][0]!=-1){
- board[position[start][0]][position[start][1]]=start+2;start++;
- }
- if(moves%5==0)
- {
- board[position[start][0]][position[start][1]]=start+2;start++;
- }
- else{
- position[start][0]=-1;position[start][1]=-1;
- }
- //update the coordinates of snake
- while(start>0){
- position[start][0]=position[start-1][0];
- start--;
- }
- position[0][0]=position[0][0]-1;
- }
- if(A.equlas("d")){
- if(position[0][0]==8) return -1;
- board[position[0][0]+1][position[0][1]]=1;
- int start=0;
- while(position[start+1][0]!=-1){
- board[position[start][0]][position[start][1]]=start+2;start++;
- }
- if(moves%5==0)
- {
- board[position[start][0]][position[start][1]]=start+2;start++;
- }
- else{
- position[start][0]=-1;position[start][1]=-1;
- }
- //update the coordinates of snake
- while(start>0){
- position[start][0]=position[start-1][0];
- start--;
- }
- position[0][0]=position[0][0]+1;
- }
- if(A.equlas("l")){
- if(position[0][1]==0) return -1;
- board[position[0][0]][position[0][1]-1]=1;
- int start=0;
- while(position[start+1][0]!=-1){
- board[position[start][0]][position[start][1]]=start+2;start++;
- }
- if(moves%5==0)
- {
- board[position[start][0]][position[start][1]]=start+2;start++;
- }
- else{
- position[start][0]=-1;position[start][1]=-1;
- }
- //update the coordinates of snake
- while(start>0){
- position[start][0]=position[start-1][0];
- start--;
- }
- position[0][1]=position[0][0]-1;
- }
- if(A.equlas("r")){
- if(position[0][1]==8) return -1;
- board[position[0][0]][position[0][1]+1]=1;
- int start=0;
- while(position[start+1][0]!=-1){
- board[position[start][0]][position[start][1]]=start+2;start++;
- }
- if(moves%5==0)
- {
- board[position[start][0]][position[start][1]]=start+2;start++;
- }
- else{
- position[start][0]=-1;position[start][1]=-1;
- }
- //update the coordinates of snake
- while(start>0){
- position[start][0]=position[start-1][0];
- start--;
- }
- position[0][1]=position[0][1]+1;
- }
- return 0;
- }
- void print(){
- for(int i=0;i<9;i++){
- for(int j=0;j<9;j++){
- System.Out.Print("Board[i][j] ");
- if(Board[i][j]<10)
- System.Out.Print(" ");
- }
- System.Out.Println("");
- }
- }
- public static void main(){
- SnakeBoard temp=new SnakeBoard();
- while(1){
- Scanner in = new Scanner(System.in);
- System.println("enter choice:");
- s = in.nextLine();
- if(s.equals("u")||s.equals("d")||s.equals("r")||s.equals("l")||s.equals("q")){
- int t=temp.move(s);
- if(s.equals("q")){
- System.println("Quit , moves:"+temp.moves);return;
- }
- if(t==-1) {
- System.println("Game over , moves:"+temp.moves);return;
- }
- temp.print();
- }
- else {
- System.println("invalid argument");
- }
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement