diff --git a/.gitignore b/.gitignore index 6884326..84027d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/ .wakatime-project +TODO diff --git a/include/carte.hpp b/include/carte.hpp new file mode 100644 index 0000000..012293b --- /dev/null +++ b/include/carte.hpp @@ -0,0 +1,34 @@ +#ifndef CARTE_HPP +#define CARTE_HPP + +enum VCarte{ + II = 2, + III, + IV, + V, + VI, + VII, + VIII, + IX, + T, + J, + Q, + K, + A, +}; + +enum CCarte{ + PIC = 1, + CARREAU, + COEUR, + TREFLE, +}; + +struct carte{ + VCarte valeur; + CCarte couleur; +}; + +static carte CARTERETOURNE = {VCarte(0), CCarte(0)}; + +#endif diff --git a/include/jdc.hpp b/include/jdc.hpp new file mode 100644 index 0000000..66374fa --- /dev/null +++ b/include/jdc.hpp @@ -0,0 +1,126 @@ +#ifndef JDC_HPP +#define JDC_HPP + +#include +#include +#include "carte.hpp" + +class JDC{ + public: + JDC(){ + this->len = 0; + }; + + void init(){ + this->len = 52; + int index; + for (int i=0; i<4; i++){ + for (int y=0; y<13; y++){ + index = (i*13)+y; + this->jdc[index].couleur = (CCarte)i; + this->jdc[index].valeur = (VCarte)(y+2); + } + } + } + + void shuffle(){ + srand(time(0)); + for (int i=0; i<52; i++){ + int randomIndex = rand()%52; + carte swap = this->jdc[randomIndex]; + + this->jdc[randomIndex] = this->jdc[i]; + this->jdc[i] = swap; + } + }; + + void display(){ + for (int i=0; ilen; i++){ + if (!(i%13)) std::cout << std::endl; + std::cout << i << ':' << this->jdc[i].valeur << '+' << this->jdc[i].couleur << '\t'; + } + std::cout << std::endl; + }; + + carte top(){ + if (this->len <= 0) throw "jdc empty"; + return this->jdc[this->len-1]; + } + + carte pop(){ + if (this->len <= 0) throw "jdc empty"; + this->len--; + return this->jdc[this->len]; + } + + carte push(carte c){ + if (this->len == 52) throw "jdc full"; + this->jdc[this->len] = c; + this->len++; + return c; + } + + carte get(int index){ + if (index < 0 || index > this->len) throw "jdc out of bound"; + return this->jdc[index]; + } + + static char valeur(carte c){ + if ((int)c.valeur >= 1 && (int)c.valeur < 10) return char(c.valeur+48); + switch(c.valeur){ + case T: + return 'T'; + case J: + return 'J'; + case Q: + return 'Q'; + case K: + return 'K'; + case A: + return 'A'; + default: + return '?'; + } + } + + static char couleur(carte c){ + switch(c.couleur){ + case PIC: + return 'P'; + case COEUR: + return 'C'; + case TREFLE: + return 'T'; + case CARREAU: + return 'K'; + default: + return '?'; + } + }; + + //std::string* ascii(carte c){ + // std::string* s = new std::string[3]; + // //if (c == NULL){ + // // s[0] = "\033[38;5;235m╭──╮"; + // // s[1] = "│ȸȹ│"; + // // s[2] = "╰──╯\033[0m"; + // // return s; + // //} + // std::string co = this->couleur(c); + // s[0] = "\033[38;5;235m╭──╮"; + // if (co == "♥" || co == "♦") s[1] = "│\033[31m"+this->valeur(c)+co+"\033[38;5;235m│"; + // else s[1] = "│\033[30m"+this->valeur(c)+co+"\033[38;5;235m│"; + // s[2] = "╰──╯"; + // return s; + //}; + + int length(){ + return this->len; + } + + private: + carte jdc[52]; + int len; +}; + +#endif diff --git a/include/machine.hpp b/include/machine.hpp new file mode 100644 index 0000000..49612cc --- /dev/null +++ b/include/machine.hpp @@ -0,0 +1,6 @@ +#ifndef MACHINE_HPP +#define MACHINE_HPP + +void machine(); + +#endif diff --git a/include/screen.hpp b/include/screen.hpp new file mode 100644 index 0000000..dfd4087 --- /dev/null +++ b/include/screen.hpp @@ -0,0 +1,125 @@ +#ifndef SCREEN_HPP +#define SCREEN_HPP + +#include +#include "jdc.hpp" +#include +#include +//#include + +template +struct point{ + T y; + T x; +}; + +struct sprite{ + char** data; + int row; + int col; +}; + +class Screen{ + public: + char** screen; + + Screen(int row, int col){ + puts("\033[?1049h\033[H"); // alt screen + puts("\033[?25l"); // inv cursor + this->col = col; + this->row = row; + + this->screen = new char*[row]; + for (int i=0; iscreen[i] = new char[col]; + } + }; + + ~Screen(){ + for (int i=0; iscreen[i]; + } + delete this->screen; + puts("\033[?1049l"); // ~ alt screen + puts("\033[?25h"); // ~ inv cursor + }; + + void background(){ + for (int i=0; iscreen[i][y]) continue; + if (y%2 && i%2) this->screen[i][y] = 'X'; + else if (!(i%2) && !(y%2)) this->screen[i][y] = 'X'; + else this->screen[i][y] = 'O'; + } + } + }; + + void display(){ + puts("\033[2J"); // clear screen + puts("\033[1;48;5;22m"); // bold + background green + + point coord = this->size(); + coord.x = coord.x/2 - col/2; + coord.y = coord.y/2 - row/2; + printf("\033[%d;%dH", coord.y, coord.x); + + for (int i=0; iscreen[i][y]) std::cout << this->screen[i][y]; + else std::cout << ' '; + } + printf("\033[%d;%dH", coord.y+i+1, coord.x); + } + puts("\033[0m"); // reset + } + + void addCard(point p, carte c){ + this->screen[p.x][p.y] = '+'; + this->screen[p.x][p.y+3] = '+'; + this->screen[p.x+2][p.y+3] = '+'; + this->screen[p.x+2][p.y] = '+'; + + this->screen[p.x][p.y+1] = '-'; + this->screen[p.x][p.y+2] = '-'; + this->screen[p.x+2][p.y+1] = '-'; + this->screen[p.x+2][p.y+2] = '-'; + + this->screen[p.x+1][p.y] = '|'; + this->screen[p.x+1][p.y+3] = '|'; + + this->screen[p.x+1][p.y+1] = JDC::valeur(c); + this->screen[p.x+1][p.y+2] = JDC::couleur(c); + }; + + void addSprite(point p, sprite s){ + for (int i=0; iscreen[p.x+i][p.y+y] = s.data[i][y]; + } + } + }; + + int ligne(){ + return this->row; + }; + + int colone(){ + return this->col; + }; + + static point size(){ + point p; + struct winsize size; + ioctl(1, TIOCGWINSZ, &size); + p.x = size.ws_col; + p.y = size.ws_row; + return p; + }; + private: + int row; + int col; +}; + +#endif diff --git a/src/machine.cpp b/src/machine.cpp new file mode 100644 index 0000000..fc8fad7 --- /dev/null +++ b/src/machine.cpp @@ -0,0 +1,93 @@ +#include +#include +#include + +#include "machine.hpp" +#include "screen.hpp" +#include "jdc.hpp" + +void spriteDel(sprite* s){ + for (int i=0; irow; i++){ + delete s->data[i]; + } + delete s->data; +} + +void jack(Screen* s){ + strcpy(s->screen[5], " J A C K P O T "); +} + +void manche(Screen* s, char in){ + strcpy(s->screen[5], " "); + strcpy(s->screen[3], "| - - | - - | - - |"); + s->display(); + + srand(time(0)); + + char symbole[] = "OP@7AX"; + int symb_l = 6; + + // cheat & + if (in == '&'){ + char c = symbole[(rand()%symb_l)]; + + s->screen[3][3] = c; + s->screen[3][9] = c; + s->screen[3][15] = c; + jack(s); + + s->display(); + return; + } + + for (int i=0; i<15; i++){ + for (int y=0; y<(i%3)+1; y++){ + s->screen[3][3+6*y] = symbole[(rand()%symb_l)]; + } + s->display(); + usleep(200000); + } + + if (s->screen[3][3] == s->screen[3][9] && s->screen[3][9] == s->screen[3][15]) jack(s); + else strcpy(s->screen[5], " P E R D U "); + s->display(); +}; + +void machine(){ + Screen sm(6, 19); + + sprite sp = {NULL, 3, 19}; + sp.data = new char*[sp.row]; + for (int i=0; i> input; + while (input != 'q'){ + manche(&sm, input); + std::cin >> input; + } + + spriteDel(&sp); + spriteDel(&t); +} diff --git a/src/main.cpp b/src/main.cpp index 6fadfc2..e40c507 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,131 @@ -#include - -int main(){ - std::cout << "Hello World!" << std::endl; - return 0; -} +#ifdef _WIN32 +#include +#endif + +#include +#include +#include + +#include "screen.hpp" +#include "jdc.hpp" +#include "machine.hpp" + +void menu(){ + static Screen s(7, 18); + + sprite sp = {NULL, 7, 18}; + sp.data = new char*[sp.row]; + for (int i=0; i> input; + + switch (input){ + case '1': + //blackjack(); + std::cout << "blackjack" << std::endl; + break; + case '2': + //roulette(); + std::cout << "roulette" << std::endl; + break; + case '3': + machine(); + break; + case '4': + //course(); + std::cout << "course" << std::endl; + break; + } + if (input != 'q'){ + menu(); + selectGame(); + } +} + +void setup(){ + #ifndef _WIN32 + // echo off + struct termios term; + tcgetattr(1, &term); + term.c_lflag &= ~ECHO; + tcsetattr(1, TCSANOW, &term); + + // canon off + struct termios term2; + tcgetattr(1, &term2); + term2.c_lflag &= ~ICANON; + tcsetattr(1, TCSANOW, &term2); + + #else + + // active couleur sur windows + DWORD outMode = 0; + stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE); + if(stdoutHandle == INVALID_HANDLE_VALUE){ + exit(GetLastError()); + } + if(!GetConsoleMode(stdoutHandle, &outMode)){ + exit(GetLastError()); + } + outModeInit = outMode; + + outMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; + if(!SetConsoleMode(stdoutHandle, outMode)){ + exit(GetLastError()); + } + + #endif +} + +void tearDown(){ + std::cout << "\033[0m \033[?25h \033[?1049l\033[?1049l\033[?1049l" << std::endl; + puts("\033[2J"); + puts("\033[0;0H"); + + #ifndef _WIN32 + // echo on + struct termios term; + tcgetattr(1, &term); + term.c_lflag |= ECHO; + tcsetattr(1, TCSANOW, &term); + + // canon on + struct termios term2; + tcgetattr(1, &term2); + term2.c_lflag |= ICANON; + tcsetattr(1, TCSANOW, &term2); + + #else + + // restore la console sur windows + if(!SetConsoleMode(stdoutHandle, outModeInit)){ + exit(GetLastError()); + } + #endif +} + +int main(){ + + setup(); + menu(); + selectGame(); + tearDown(); + + return 0; +} diff --git a/test/cartest.cpp b/test/cartest.cpp new file mode 100644 index 0000000..457535a --- /dev/null +++ b/test/cartest.cpp @@ -0,0 +1,9 @@ +#include +#include "carte.hpp" + +int main(){ + carte c = {III, TREFLE}; + if (c.valeur == 3 && c.couleur == TREFLE) std::cout << "\033[32mOK\033[0m" << std::endl; + + return 0; +} diff --git a/test/scroon.cpp b/test/scroon.cpp new file mode 100644 index 0000000..0b01e63 --- /dev/null +++ b/test/scroon.cpp @@ -0,0 +1,32 @@ +#include +#include +#include "screen.hpp" +#include "jdc.hpp" + +int main(){ + int row = 5; + int col = 11; + Screen s(row, col); + s.display(); + sleep(1); + + JDC jdc; + jdc.init(); + jdc.shuffle(); + + point p = {0, 0}; + s.addCard(p, jdc.top()); + s.display(); + sleep(1); + + s.background(); + s.display(); + sleep(1); + + p = {col-4, row-3}; + s.addCard(p, CARTERETOURNE); + s.display(); + sleep(1); + + return 0; +} diff --git a/test/sprot.cpp b/test/sprot.cpp new file mode 100644 index 0000000..099ea02 --- /dev/null +++ b/test/sprot.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include "screen.hpp" + +int main(){ + Screen s(50, 50); + s.display(); + sleep(1); + + sprite sp = {NULL, 5, 5}; + sp.data = new char*[5]; + for (int i=0; i<5; i++){ + sp.data[i] = new char[5]; + for (int y=0; y<5; y++){ + sp.data[i][y] = '+'; + } + } + + point p = {22, 22}; + + s.addSprite(p, sp); + s.display(); + sleep(1); + + sprite sp2 = {NULL, 50, 50}; + sp2.data = new char*[50]; + for (int i=0; i<50; i++){ + sp2.data[i] = new char[50]; + for (int y=0; y<50; y+=2){ + sp2.data[i][y] = 'x'; + } + } + + point p2 = {0, 0}; + + s.addSprite(p2, sp2); + s.display(); + sleep(1); + + sprite sp3 = {NULL, 50, 50}; + sp3.data = new char*[50]; + for (int i=0; i<50; i+=2){ + sp3.data[i] = new char[50]; + sp3.data[i+1] = new char[50]; + for (int y=0; y<50; y++){ + sp3.data[i][y] = 'o'; + } + } + + point p3 = {0, 0}; + + s.addSprite(p3, sp3); + s.display(); + sleep(1); + + sprite sp4 = {NULL, 21, 21}; + sp4.data = new char*[21]; + for (int i=0; i<21; i++){ + sp4.data[i] = new char[21]; + } + + for (float i=0; i<2*3.14; i+=0.1){ + float x = (std::cos(i)+1) * 5; + float y = (std::sin(i)+1) * 10; + sp4.data[(int)x][(int)y] = '@'; + } + + point p4 = {14, 2}; + + s.addSprite(p4, sp4); + s.display(); + sleep(1); + + sprite sp5 = {NULL, 50, 50}; + sp5.data = new char*[50]; + for (int i=0; i<50; i++){ + sp5.data[i] = new char[50]; + for (int y=0; y<50; y++){ + sp5.data[i][y] = ' '; + } + } + + s.addSprite(p2, sp5); + s.display(); + sleep(1); + + s.addSprite({15, 20}, sp4); + s.display(); + sleep(1); + + sleep(3); + return 0; +} diff --git a/test/testjdc.cpp b/test/testjdc.cpp new file mode 100644 index 0000000..15a2a1c --- /dev/null +++ b/test/testjdc.cpp @@ -0,0 +1,50 @@ +#include +#include "jdc.hpp" + +int main(){ + JDC jdc; + jdc.init(); + + jdc.shuffle(); + jdc.display(); + jdc.shuffle(); + jdc.display(); + + carte c = jdc.top(); + std::cout << "c : " << c.valeur << '+' << c.couleur << std::endl; + std::cout << jdc.length() << std::endl; + c = jdc.pop(); + std::cout << "c : " << c.valeur << '+' << c.couleur << std::endl; + std::cout << jdc.length() << std::endl; + + c = jdc.get(0); + std::cout << "c : " << c.valeur << '+' << c.couleur << std::endl; + std::cout << jdc.length() << std::endl; + + jdc.display(); + std::cout << jdc.length() << std::endl; + + jdc.push({K, COEUR}); + jdc.display(); + std::cout << jdc.length() << std::endl; + + c = jdc.get(0); + std::cout << "bottom = " << jdc.valeur(c) << jdc.couleur(c) << std::endl; + c = jdc.get(2); + std::cout << "2 = " << jdc.valeur(c) << jdc.couleur(c) << std::endl; + + c = jdc.top(); + std::cout << "top = " << jdc.valeur(c) << jdc.couleur(c) << std::endl; + //std::cout << jdc.couleur(jdc.top()).length() << std::endl; + + //std::cout << "\033[1;48;5;22m" << std::endl; + //for (int y=0; y<52; y++){ + // std::string* ss = jdc.ascii(jdc.get(y)); + // for (int i=0; i<3; i++){ + // std::cout << "\033[1;48;5;22;38;5;235m" << ss[i] << std::endl; + // } + //} + //std::cout << "\033[0m" << std::endl; + + return 0; +} diff --git a/test/testou.cpp b/test/testou.cpp new file mode 100644 index 0000000..1db1f6b --- /dev/null +++ b/test/testou.cpp @@ -0,0 +1,29 @@ +#include +#include +#include + +int main(){ + int size = 21; + char** s = new char*[size]; + for (int i=0; i