Skip to content

Instantly share code, notes, and snippets.

@eliaperantoni
Last active October 14, 2017 06:38
Show Gist options
  • Save eliaperantoni/1915357cf2c7513858df715053c2cf1b to your computer and use it in GitHub Desktop.
Save eliaperantoni/1915357cf2c7513858df715053c2cf1b to your computer and use it in GitHub Desktop.
Specchi C++
// TestCPP.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
enum Parete{SOPRA, DESTRA, SOTTO, SINISTRA};
struct Foro {
Parete parete;
int posizione;
};
int rm, cm;
vector<vector<int>> mat;
int directionX, directionY;
void print() {
for (int i = 0;i < rm + 2;i++) {
for (int j = 0;j < cm + 2;j++) {
cout << mat.at(i).at(j) << " ";
}
cout << endl;
}
cout << endl;
}
void print(int x, int y) {
for (int i = 0;i < rm + 2;i++) {
for (int j = 0;j < cm + 2;j++) {
if (i == x && y == j) {
cout << "@" << " ";
}
else {
cout << mat.at(i).at(j) << " ";
}
}
cout << endl;
}
cout << endl;
}
void inizia(int r, int c) {
rm = r; cm = c;
for (int i = 0;i < r+2;i++) {
vector<int> temp;
for (int j = 0;j < c+2;j++) {
int x = 0;
if (i == 0 || i == r + 1 || j == 0 || j == c + 1) {
x = 2;
}
temp.push_back(x);
}
mat.push_back(temp);
}
}
void aggiungi(int r, int c, char diagonale) {
int x;
switch (diagonale) {
case '/':
x = 1;
break;
case '\\':
x = -1;
break;
}
mat[r+1][c+1] = x;
}
Foro calcola(Foro ingresso) {
int x, y;
switch (ingresso.parete) {
case SOPRA:
x = 0;
y = ingresso.posizione+1;
directionX = 1;
directionY = 0;
break;
case DESTRA:
y = cm + 1;
x = ingresso.posizione + 1;
directionX = 0;
directionY = 1;
break;
case SOTTO:
x = rm + 1;
y = ingresso.posizione + 1;
directionX = -1;
directionY = 0;
break;
case SINISTRA:
y = 0;
x = ingresso.posizione + 1;
directionX = 0;
directionY = -1;
break;
}
do {
x += directionX;
y += directionY;
print(x,y);
if (mat[x][y] == 1) {
int tempx = directionX;
int tempy = directionY;
directionX = -tempy;
directionY = -tempx;
}
else if (mat[x][y] == -1) {
int tempx = directionX;
int tempy = directionY;
directionX = tempy;
directionY = tempx;
}
} while (x != 0 && y != 0 && x != rm+1 && y != cm+1);
Foro f = Foro();
return f;
}
int main()
{
inizia(2, 2);
aggiungi(0, 0, '/');
Foro x;
x.parete = SOPRA;
x.posizione = 0;
calcola(x);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment