Skip to content
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

.vscode/settings.json
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
# Secante
# Snake 20x20
Move with `w`/`a`/`s`/`d` your snake, which at `v0.1.0` still isn't a snake but a bare `a` symbol.

Capture the `O`s which are apples.

# Field
This is how the whole thing looks like.
```cmd
####################
# #
# #
# #
# #
# #
# 0 #
# #
# #
# #
# $ #
# #
# #
# #
# #
# #
# #
# #
# #
####################
```
223 changes: 104 additions & 119 deletions snake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,159 +2,144 @@
#include <thread>
#include <future>
#include <chrono>
#include <cstring>
#include <time.h>
#include <stdlib.h>
using namespace std;
// g++ snake.cpp -o snake.exe -std=c++2a


bool gameover(string matrix[20][20]) {
bool controllo=true;
for (int i = 0; i <19; i++)
{
for (int j = 0; j <19; j++)
if(matrix[i][0] == "a" || matrix[i][19] == "a" || matrix[0][j]=="a" || matrix[19][j]=="a")
{
controllo=false;
// g++ -S snake.cpp -std=c++2a
const char SNAKE = '$';
const char BORDER = '#';
const char APPLE = '0';
const std::string VERSION = "v0.3.0";


void gameover(char matrix[20][20], unsigned long long points) {
for (int i = 0; i <20; i++) {
if (matrix[i][0] == SNAKE || matrix[i][19] == SNAKE || matrix[0][i] == SNAKE || matrix[19][i] == SNAKE) {
std::cout << "Game over!" << std::endl << "Your points: " << points << std::endl;
exit(0);
}
}
return controllo;
}


// Se colpisci una mela allora ritorni true
bool controllomela1(string campo[20][20], int riga, int colonna)
{
if(campo[riga][colonna]=="O")
return true;
else
return false;
}
void nuovamela(string campo[20][20])
{
campo[rand()%18][rand()%18]="O";
// campo[7][12]="O";
bool appleCheck(char field[20][20], int x, int y) {
return (field[x][y] == APPLE);
}


void movimento(string matrix[][20], char direzione, int & x, int & y)
{
// Ripristino il bordo
for (int i = 0; i <20; i++)
{
for (int j = 0; j <20; j++)
{
if(matrix[i][j]=="a")
matrix[i][j]=" ";
}
matrix[i][0] = "#";
matrix[i][19] = "#";
}
matrix[x][y] = " ";
for (int i = 0; i < 20; i++)
{
matrix[0][i]="#";
matrix[19][i]="#";
}
void addApple(char field[20][20], int x, int y) {
int a, b;
do {
a = 1+rand()%17;
b = 1+rand()%17;
} while (a == x && b == y);
field[a][b] = APPLE;
}


// Muovo a seconda della direzione
if(direzione=='w')
{
void move(char matrix[][20], char direction, int & x, int & y, unsigned long long points) {
matrix[x][y] = ' ';
if (direction == 'w')
x--;
if(controllomela1(matrix, x, y)==true)
nuovamela(matrix);
matrix[x][y]="a";
}
else if(direzione=='s')
{
x++;
if(controllomela1(matrix, x, y)==true)
nuovamela(matrix);
matrix[x][y]="a";
}
else if(direzione=='a')
{
else if (direction == 's')
x++;
else if (direction == 'a')
y--;
if(controllomela1(matrix, x, y)==true)
nuovamela(matrix);
matrix[x][y]="a";
}
else if(direzione=='d')
{
else if (direction == 'd')
y++;
if(controllomela1(matrix, x, y)==true)
nuovamela(matrix);
matrix[x][y]="a";
}
if(appleCheck(matrix, x, y))
addApple(matrix, x, y);
matrix[x][y]=SNAKE;
gameover(matrix, points);
}

string inputMossa() {
string mossa;
if (cin >> mossa)
return mossa;

std::string inputMove() {
std::string move;
if (std::cin >> move)
return move;
}


void stampa(string m[][20]) {
void print(char m[][20]) {
system("cls");
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++)
cout << m[i][j];
cout << endl;
std::cout << m[i][j];
std::cout << std::endl;
}
}


void cambiaDirezione(char & direzione, string mossa) {
if (mossa == "w")
direzione = 'w';
else if (mossa == "s")
direzione = 's';
else if (mossa == "a")
direzione = 'a';
else if (mossa == "d")
direzione = 'd';
void changeDirection(char & direction, std::string move) {
if (move == "w")
direction = 'w';
else if (move == "s")
direction = 's';
else if (move == "a")
direction = 'a';
else if (move == "d")
direction = 'd';
}


int main()
{
int main(int argc, char *argv[]) {
float seconds = 0.2;
if (argc == 1) {
std::cout << "Period [seconds]: ";
std::cin >> seconds;
} else if (argc == 2 && !strcmp(argv[1], "--help")) {
std::cout << "Usage: snake [--help] [--version] [--frequency | --delay]" << std::endl << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " --help Show this help message and exit" << std::endl;
std::cout << " --version Show version and exit" << std::endl;
std::cout << " --frequency Set the frame period in seconds" << std::endl;
std::cout << " --delay Set the frame period in seconds" << std::endl;
std::cout << "Examples:" << std::endl;
std::cout << " snake --help" << std::endl;
std::cout << " snake --frequency 0.2" << std::endl;
std::cout << " snake --delay 0.2" << std::endl;
return 0;
} else if (argc == 2 && !strcmp(argv[1], "--version")) {
std::cout << VERSION << std::endl;
return 0;
} else if (argc == 3 && !strcmp(argv[1], "--frequency")) {
seconds = atof(argv[2]);
} else if (argc == 3 && !strcmp(argv[1], "--delay")) {
seconds = atof(argv[2]);
} else if (argc > 2) {
std::cout << "Usage: snake [--help] [--version] [--frequency | --delay]" << std::endl;
return 0;
}
std::chrono::duration<double> period(seconds);

srand(time(NULL));
string campo[20][20];
int x=9;
int y=9;
char direzione='w'; // Variabile che ha il valore w quando si muove in alto, s quando si muove in basso, a quando si muove a sinistra, d quando si muove a destra
for (int i = 0; i <20; i++)
{
unsigned long long points = 0;
char field[20][20];
int x=9, y=9;
char direction = 'w';
for (int i = 0; i <20; i++) {
for (int j = 0; j <20; j++)
campo[i][j]=" ";
field[i][j] = ' ';
}
for(int i=0; i<20;i++)
{
campo[0][i]="#";
campo[i][0]="#";
campo[19][i]="#";
campo[i][19]="#";
campo[x][y]="a";
for (int i=0; i<20; i++) {
field[0][i] = BORDER;
field[i][0] = BORDER;
field[19][i] = BORDER;
field[i][19] = BORDER;
field[x][y] = SNAKE;
}

// Genero la prima mela
nuovamela(campo);
stampa(campo);
string num;

while(gameover(campo)==true) //cin
{
// Eseguo la funzione inputMove in modo asincrono
auto input = std::async(std::launch::async, inputMossa);
system("cls");
using namespace std::literals;
while (input.wait_for(0.2s) != std::future_status::ready) {
movimento(campo, direzione, x, y);
stampa(campo);
addApple(field, x, y);
print(field);

while (true) {
auto input = std::async(std::launch::async, inputMove);
while (input.wait_for(period) != std::future_status::ready) {
move(field, direction, x, y, points);
print(field);
}
num = input.get(); // Assegnamo a num la variabile presa da inputMossa in maniera asincrona
cambiaDirezione(direzione, num);
changeDirection(direction, input.get());
points++;
}
cout<<"gameover";
}
Binary file modified snake.exe
Binary file not shown.
74 changes: 0 additions & 74 deletions snakedef.cpp

This file was deleted.

Binary file removed snakedef.exe
Binary file not shown.