Skip to content

Commit

Permalink
Now the game works
Browse files Browse the repository at this point in the history
  • Loading branch information
FLAK-ZOSO committed Feb 10, 2022
1 parent dcc88cb commit 254027a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 37 deletions.
56 changes: 36 additions & 20 deletions game.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <iostream>
#include <cstdlib>
#include "time.h"
#include "cleanScreen.hpp"


struct Game {
char matrix[50][80]; // Il campo di gioco
char matrix[20][50]; // Il campo di gioco
unsigned points; // Punteggio del giocatore (unsigned = int ma senza i negativi)
std::string name; // Nome del giocatore
char skin; // Iniziale del giocatore, da usare come pedina
Expand All @@ -13,27 +15,35 @@ struct Game {
};


void updateMatrix(Game game_) {
void updateMatrix(Game &game_) {
// Scaliamo tutto di una riga
for (int i = 1; i < 50; i++) {
for (int j = 0; j < 80; j++)
for (int i = 1; i < 20; i++) {
for (int j = 0; j < 50; j++)
game_.matrix[i-1][j] = game_.matrix[i][j];
}

// Trasformiamo la pedina vecchia in uno spazio
for (int i = 0; i < 80; i++) {
if (game_.matrix[23][i] == game_.skin) {
game_.matrix[23][i] = '-';
for (int i = 0; i < 50; i++) {
if (game_.matrix[2][i] == game_.skin) {
game_.matrix[2][i] = ' ';
break;
}
}
game_.matrix[3][game_.position] = game_.skin;

// Generiamo casualmente la nuova riga
char newLine[80];
for (int i = 0; i < 80; i++)
newLine[i] = '-';
for (int i = 0; i < 80; i++)
game_.matrix[49][i] = newLine[i];
char newLine[50];
for (int i = 0; i < 50; i++)
newLine[i] = ' ';
int cloudBeginning = rand() % 49;
int cloudWidth = rand() % 5;
int cloudEnd = cloudBeginning + cloudWidth;
for (int i = cloudBeginning; i < cloudEnd+1; i++)
newLine[i] = '*';
newLine[0] = '#';
newLine[49] = '#';
for (int i = 0; i < 50; i++)
game_.matrix[19][i] = newLine[i];

game_.points++;
}
Expand All @@ -43,15 +53,21 @@ void printMatrix(Game game_) {
system("cls");

// Stampo la matrice
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 80; j++)
for (int i = 0; i < 50; i++)
std::cout << '#';
std::cout << std::endl;
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 50; j++)
std::cout << game_.matrix[i][j];
std::cout << std::endl;
}
for (int i = 0; i < 50; i++)
std::cout << '#';
std::cout << std::endl << game_.points << std::endl;
}


void processMove(Game game_, std::string input) {
void processMove(Game &game_, std::string input) {
if (input == "s" or input == "S")
game_.position--;
if (input == "d" or input == "D")
Expand All @@ -62,16 +78,16 @@ void processMove(Game game_, std::string input) {
game_.position += 2;

if (game_.position < 0)
game_.position = 0;
if (game_.position > 79)
game_.position = 79;
game_.position = 1;
if (game_.position > 48)
game_.position = 48;
}


bool checkMatrix(Game game_) {
if (game_.matrix[25][game_.position] == 'X')
if (game_.matrix[4][game_.position] == '*')
return true; // Il giocatore ha perso
if (game_.matrix[25][game_.position] == '$')
if (game_.matrix[4][game_.position] == '$')
game_.points += 10;
return false;
}
Binary file added game.hpp.gch
Binary file not shown.
41 changes: 24 additions & 17 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,36 @@

int game(std::string name) { // Ritorna il numero di punti
Game myGame;
myGame.points = 0;
srand(time(0));

// Riempio la matrice, che in partenza è vuota
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 80; j++)
myGame.matrix[i][j] = '-';
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 50; j++)
myGame.matrix[i][j] = ' ';
myGame.matrix[i][0] = '#';
myGame.matrix[i][49] = '#';
}

// Il personaggio del giocatore sarà la lettera maiuscola della sua iniziale
myGame.name = name;
myGame.skin = name[0];
myGame.position = 39;
myGame.matrix[24][39] = myGame.skin;
myGame.position = 9;
myGame.matrix[3][24] = myGame.skin;

// Enable standard literals as 2s and ""s.
using namespace std::literals;

// Execute lambda asyncronously.
auto f = std::async(std::launch::async, [] {
std::string move;
if (std::cin >> move) return move;
});


while (true) {
// Execute lambda asyncronously.
auto f = std::async(std::launch::async, [] {
std::string move;
if (std::cin >> move) return move;
});

// Continue execution in main thread.
while (f.wait_for(0.5s) != std::future_status::ready) {
while (f.wait_for(0.3s) != std::future_status::ready) {
// Controlliamo se ha perso
if (checkMatrix(myGame)) { // The user lost
goto end;
Expand All @@ -44,19 +49,21 @@ int game(std::string name) { // Ritorna il numero di punti
processMove(myGame, f.get());
}
end:
int x;
std::cin >> x;
updateMatrix(myGame);
printMatrix(myGame);
std::cout << "You lost... ";
return myGame.points;
}


int main() {
// Chiediamo le varie informazioni all'utente
std::string name = "Asia";
// std::cin >> name;
// Salviamo tutti i dati in dei file .txt (di testo)

while (true) {
game("Ciao");

// Diciamo quanti punti ha fatto
int points = game(name);

// Chiediamo se vuole rigiocare
}
Expand Down
Binary file modified main.exe
Binary file not shown.

0 comments on commit 254027a

Please sign in to comment.