Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 43 additions & 38 deletions snake.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,91 +11,96 @@
using namespace std;
using std::chrono::system_clock;
using namespace std::this_thread;
char direction='r';

char direction = 'r';

void input_handler(){
void input_handler() {
// change terminal settings
struct termios oldt, newt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
// turn off canonical mode and echo
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);

map<char, char> keymap = {{'d', 'r'}, {'a', 'l'}, {'w', 'u'}, {'s', 'd'}, {'q', 'q'}};
while (true) {
char input = getchar();
if (keymap.find(input) != keymap.end()) {
// This now correctly modifies the single, shared 'direction' variable
direction = keymap[input];
}else if (input == 'q'){
} else if (input == 'q') {
exit(0);
}
// You could add an exit condition here, e.g., if (input == 'q') break;
}
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
}


void render_game(int size, deque<pair<int, int>> &snake, pair<int, int> food){
for(size_t i=0;i<size;i++){
for(size_t j=0;j<size;j++){
if (i == food.first && j == food.second){
cout << "🍎";
}else if (find(snake.begin(), snake.end(), make_pair(int(i), int(j))) != snake.end()) {
void render_game(int size, deque<pair<int, int>> &snake, pair<int, int> food) {
for (size_t i = 0; i < size; i++) {
for (size_t j = 0; j < size; j++) {
if (i == food.first && j == food.second) {
cout << "🍒"; // changed from apple 🍎 to cherry 🍒
} else if (find(snake.begin(), snake.end(), make_pair(int(i), int(j))) != snake.end()) {
cout << "🐍";
}else{
} else {
cout << "⬜";
}
}
cout << endl;
}
cout << endl;
}
}

pair<int,int> get_next_head(pair<int,int> current, char direction){
pair<int, int> next;
if(direction =='r'){
next = make_pair(current.first,(current.second+1) % 10);
}else if (direction=='l')
{
next = make_pair(current.first, current.second==0?9:current.second-1);
}else if(direction =='d'){
next = make_pair((current.first+1)%10,current.second);
}else if (direction=='u'){
next = make_pair(current.first==0?9:current.first-1, current.second);
}
pair<int, int> get_next_head(pair<int, int> current, char direction) {
pair<int, int> next;
if (direction == 'r') {
next = make_pair(current.first, (current.second + 1) % 10);
} else if (direction == 'l') {
next = make_pair(current.first, current.second == 0 ? 9 : current.second - 1);
} else if (direction == 'd') {
next = make_pair((current.first + 1) % 10, current.second);
} else if (direction == 'u') {
next = make_pair(current.first == 0 ? 9 : current.first - 1, current.second);
}
return next;

}



void game_play(){
void game_play() {
// Welcome screen
cout << "==============================" << endl;
cout << " Welcome to Snake Game 🐍 " << endl;
cout << " Use W/A/S/D to move, Q to quit" << endl;
cout << "==============================" << endl;
sleep_for(1s); // pause so player can read
system("clear");

deque<pair<int, int>> snake;
snake.push_back(make_pair(0,0));
snake.push_back(make_pair(0, 0));

pair<int, int> food = make_pair(rand() % 10, rand() % 10);
for(pair<int, int> head=make_pair(0,1);; head = get_next_head(head, direction)){
for (pair<int, int> head = make_pair(0, 1);; head = get_next_head(head, direction)) {
// send the cursor to the top
cout << "\033[H";

// check self collision
if (find(snake.begin(), snake.end(), head) != snake.end()) {
system("clear");
cout << "Game Over" << endl;
cout << "Game Over 😢" << endl;
cout << "Final length of snake: " << snake.size() << endl;
exit(0);
}else if (head.first == food.first && head.second == food.second) {
} else if (head.first == food.first && head.second == food.second) {
// grow snake
food = make_pair(rand() % 10, rand() % 10);
snake.push_back(head);
}else{
snake.push_back(head);
} else {
// move snake
snake.push_back(head);
snake.pop_front();
}

render_game(10, snake, food);
cout << "length of snake: " << snake.size() << endl;
cout << "Length of snake: " << snake.size() << endl;

sleep_for(500ms);
}
}