This commit is contained in:
_N3m0 2023-08-15 16:27:35 +02:00
commit 71e5ed309d
12 changed files with 730 additions and 6 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
build/
.wakatime-project
TODO

34
include/carte.hpp Normal file
View File

@ -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

126
include/jdc.hpp Normal file
View File

@ -0,0 +1,126 @@
#ifndef JDC_HPP
#define JDC_HPP
#include <iostream>
#include <cstdlib>
#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; i<this->len; 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

6
include/machine.hpp Normal file
View File

@ -0,0 +1,6 @@
#ifndef MACHINE_HPP
#define MACHINE_HPP
void machine();
#endif

125
include/screen.hpp Normal file
View File

@ -0,0 +1,125 @@
#ifndef SCREEN_HPP
#define SCREEN_HPP
#include <iostream>
#include "jdc.hpp"
#include <sys/ioctl.h>
#include <termios.h>
//#include <unistd.h>
template <typename T>
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; i<row; i++){
this->screen[i] = new char[col];
}
};
~Screen(){
for (int i=0; i<row; i++){
delete this->screen[i];
}
delete this->screen;
puts("\033[?1049l"); // ~ alt screen
puts("\033[?25h"); // ~ inv cursor
};
void background(){
for (int i=0; i<row; i++){
for (int y=0; y<col; y++){
if (this->screen[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<int> 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; i<row; i++){
for (int y=0; y<col; y++){
if (this->screen[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<int> 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<int> p, sprite s){
for (int i=0; i<s.row; i++){
for (int y=0; y<s.col; y++){
if (!s.data[i][y]) continue;
this->screen[p.x+i][p.y+y] = s.data[i][y];
}
}
};
int ligne(){
return this->row;
};
int colone(){
return this->col;
};
static point<int> size(){
point<int> 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

93
src/machine.cpp Normal file
View File

@ -0,0 +1,93 @@
#include <iostream>
#include <unistd.h>
#include <cstring>
#include "machine.hpp"
#include "screen.hpp"
#include "jdc.hpp"
void spriteDel(sprite* s){
for (int i=0; i<s->row; 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<sp.row; i++){
sp.data[i] = new char[sp.col];
}
strcpy(sp.data[0], "+-----+-----+-----+");
strcpy(sp.data[1], "| - - | - - | - - |");
strcpy(sp.data[2], "+-----+-----+-----+");
sprite t = {NULL, 2, 19};
t.data = new char*[t.row];
for (int i=0; i<t.row; i++){
t.data[i] = new char[t.col];
}
strcpy(t.data[0], "Pour jouer : * ");
strcpy(t.data[1], "Pour quitter : q ");
sm.addSprite({0, 2}, sp);
sm.addSprite({0, 0}, t);
jack(&sm);
sm.display();
// game
char input;
std::cin >> input;
while (input != 'q'){
manche(&sm, input);
std::cin >> input;
}
spriteDel(&sp);
spriteDel(&t);
}

View File

@ -1,6 +1,131 @@
#include <iostream>
int main(){
std::cout << "Hello World!" << std::endl;
return 0;
}
#ifdef _WIN32
#include <windows.h>
#endif
#include <iostream>
#include <unistd.h>
#include <cstring>
#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<sp.row; i++){
sp.data[i] = new char[sp.col];
}
strcpy(sp.data[0], "Choisisez un jeu :");
strcpy(sp.data[1], " ");
strcpy(sp.data[2], "1 - Blackjack ");
strcpy(sp.data[3], "2 - Roulette ");
strcpy(sp.data[4], "3 - Machine a sous");
strcpy(sp.data[5], "4 - Course d'as ");
strcpy(sp.data[6], "q - Quitter ");
s.addSprite({0, 0}, sp);
s.display();
}
void selectGame(){
char input;
std::cin >> 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;
}

9
test/cartest.cpp Normal file
View File

@ -0,0 +1,9 @@
#include <iostream>
#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;
}

32
test/scroon.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <iostream>
#include <unistd.h>
#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<int> 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;
}

94
test/sprot.cpp Normal file
View File

@ -0,0 +1,94 @@
#include <iostream>
#include <unistd.h>
#include <cmath>
#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<int> 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<int> 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<int> 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<int> 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;
}

50
test/testjdc.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <iostream>
#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;
}

29
test/testou.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <iostream>
#include <unistd.h>
#include <cmath>
int main(){
int size = 21;
char** s = new char*[size];
for (int i=0; i<size; i++){
s[i] = new char[size];
}
for (float i=0; i<2*3.14; i+=0.1){
float x = (std::cos(i)+1) * 10;
float y = (std::sin(i)+1) * 10;
std::cout << "cos : " << x << " sin : " << y << std::endl;
std::cout << (int)x << ';' << (int)y << std::endl;
s[(int)x][(int)y] = '@';
}
for (int i=0; i<size; i++){
for (int y=0; y<size; y++){
if (!s[i][y]) std::cout << ' ';
std::cout << s[i][y];
}
std::cout << std::endl;
}
return 0;
}