-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.cpp
74 lines (53 loc) · 1.26 KB
/
snake.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<bits/stdc++.h>
using namespace std ;
class Snake {
unordered_map<int,int> snakeMapping ;
protected:
bool isValidBlock(int blockNumber){
return blockNumber>=1 && blockNumber<=100 ;
}
public:
Snake(){
int defaultSnakes[10][2]={
{16,6},
{49,11},
{47,26},
{56,53},
{64,60},
{87,24},
{93,73},
{95,75},
{98,78},
} ;
for(int i=0 ; i<10 ; i++){
int from = defaultSnakes[i][0];
int to = defaultSnakes[i][1] ;
snakeMapping[from] = to ;
}
}
Snake(int customSnakes[][2] , int noOfSnakes){
for(int i=0 ;i<noOfSnakes ; i++){
int from = customSnakes[i][0] ;
int to = customSnakes[i][1] ;
snakeMapping[from] = to ;
}
}
bool isSnake(int blockNumber){
if(!isValidBlock(blockNumber))
return false ;
return snakeMapping[blockNumber] ;
}
int snakeBite(int blockNumber){
if(isValidBlock(blockNumber) && isSnake(blockNumber))
return snakeMapping[blockNumber] ;
return 0 ;
}
void addSnake(int from , int to){
if(isValidBlock(from) && isValidBlock(to))
snakeMapping[from] = to ;
}
void removeSnake(int blockNumber){
if(isValidBlock(blockNumber) && isSnake(blockNumber))
snakeMapping[blockNumber] =0 ;
}
} ;