Compare commits

...

11 Commits

Author SHA1 Message Date
_N3m0 6f36af553c makefile : correct install 2023-08-15 21:34:03 +02:00
_N3m0 84ca191abb course : ajout delai au passage d'un palier 2023-08-15 21:29:27 +02:00
_N3m0 b596086bcb makefile ajout install 2023-08-15 21:22:16 +02:00
_N3m0 2d0bb6912e course d'as 2023-08-15 20:36:09 +02:00
_N3m0 53606e58c2 correct makfile 2023-08-15 16:32:17 +02:00
_N3m0 71e5ed309d merge 2023-08-15 16:27:35 +02:00
_N3m0 6c9d716b9a menu + machine a sous 2023-08-15 16:25:28 +02:00
_N3m0 c119abf31e ajout sprite 2023-08-14 18:07:29 +02:00
_N3m0 6529635f66 ajout TODO au gitignore 2023-08-14 14:51:58 +02:00
_N3m0 d7c235c365 screen display center 2023-08-14 14:42:42 +02:00
_N3m0 00977ecbb5 add Makefile 2023-07-07 13:50:16 +02:00
13 changed files with 669 additions and 84 deletions

1
.gitignore vendored
View File

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

102
Makefile
View File

@ -1,15 +1,9 @@
PROJECTNAME=MasterMakefile
PROJECTNAME=cli-casino
BIN=build/$(PROJECTNAME)
CC=g++
EXT=cpp
INCFOLDERS=include lib
RECURSIVE_INCLUDE=false
ifeq ($(RECURSIVE_INCLUDE), true)
INCDIRS=$(foreach I,$(INCFOLDERS),$(shell find $(I) -type d 2>/dev/null))
else
INCDIRS=$(INCFOLDERS)
endif
INCDIRS=include lib
# make mode=release
ifeq ($(mode), release)
@ -18,103 +12,67 @@ else
OPT=-Og -g
endif
DEPFLAGS=-MP -MD
MACROS=
FLAGS=-Wall -Wextra $(foreach F,$(INCDIRS),-I$(F)) $(OPT) $(DEPFLAGS) $(foreach M,$(MACROS),-D$(M))
FLAGS=-Wall -Wextra $(foreach F,$(INCDIRS),-I$(F)) $(OPT) $(DEPFLAGS)
SRC=$(shell find . -name "*.$(EXT)" -path "./src/*")
OBJ=$(subst ./src/,./build/,$(SRC:.$(EXT)=.o))
LIB=$(shell find . -name "*.$(EXT)" -path "./lib/*")
LIBO=$(subst ./lib/,./build/,$(LIB:.$(EXT)=.o))
DEP=$(OBJ:.o=.d)
TEST=$(shell find . -name "*.$(EXT)" -path "./test/*")
TESTO=$(subst ./test/,./build/,$(TEST:.$(EXT)=.t))
$(shell mkdir -p build)
all : $(BIN)
$(BIN) : $(OBJ) $(LIBO)
$(BIN) : $(OBJ)
$(CC) $(FLAGS) -o $@ $^
-include $(OBJ:.o=.d) $(LIBO:.o=.d)
-include $(DEP)
build/%.o : src/%.$(EXT)
@mkdir -p $(@D)
$(CC) $(FLAGS) -o $@ -c $<
build/%.o : lib/%.$(EXT)
@mkdir -p $(@D)
$(CC) $(FLAGS) -o $@ -c $<
# make test file=testGenID.cpp
test : build/$(file).t
./build/$(file).t
alltest : $(TESTO)
@for i in $(TESTO); do echo $$i; $$i; done
build/%.t : test/%.$(EXT)
@mkdir -p $(@D)
$(CC) $(FLAGS) -o $@ $<
asm : $(OBJ:.o=.s) $(BIN).s
build/%.s : src/%.$(EXT)
@mkdir -p $(@D)
$(CC) $(FLAGS) -S $< -o $@
$(BIN).s : $(BIN)
objdump -drwC -Mintel -S $< > $<.s
preprocess : $(OBJ:.o=.i)
build/%.i : src/%.$(EXT)
@mkdir -p $(@D)
$(CC) $(FLAGS) -E $< -o $@
gigall : $(BIN) $(TESTO) asm preprocess
# make run input="program input"
run : $(BIN)
./$< $(input)
run : all
./$(BIN)
clean :
rm -rf build/*
# make test file=testGenID.cpp
test : $(OBJ)
$(CC) $(FLAGS) -o build/$(file) test/$(file).$(EXT)
./build/$(file)
alltest :
@for f in $(subst ./test/,,$(TEST)); do make -s test file=$$f; done
# unzip : mkdir exemple && tar -xvf exemple.tgz -C exemple
dist : clean
$(info /!\ project folder has to be named $(PROJECTNAME) /!\ )
cd .. && tar zcvf $(PROJECTNAME)/build/$(PROJECTNAME).tgz $(PROJECTNAME) >/dev/null
install : dist $(BIN)
cp build/$(PROJECTNAME).tgz $$HOME/dev/opt/archive
mv $(BIN) $$HOME/dev/opt/built
check :
cppcheck --enable=all --suppress=missingIncludeSystem $(foreach I,$(INCDIRS),-I$(I)) .
flawfinder .
debug : $(BIN)
gdb $< $(input)
# unzip : tar -xvf exemple.tgz
dist : clean
$(info /!\ project folder has to be named $(PROJECTNAME) /!\ )
cd .. && tar zcvf $(PROJECTNAME)/build/$(PROJECTNAME).tgz $(PROJECTNAME) >/dev/null
push :
git push bbsrv
git push gh
install : dist
cp Makefile ../script
mv build/$(PROJECTNAME).tgz ../opt/archive
cd .. && rm -rf $(PROJECTNAME)
info :
$(info put what ever)
@echo you want
push :
git push gh
git push bbsrv
# alias
r : run
t : test
c : check
p : push
d : debug
.PHONY : all test t alltest asm preprocess gigall run r clean check c debug d dist push p install info
.PHONY : all run clean test alltest dist install check info push

View File

@ -27,6 +27,8 @@ enum CCarte{
struct carte{
VCarte valeur;
CCarte couleur;
}CARTERETOURNE = {VCarte(0), CCarte(0)};
};
static carte CARTERETOURNE = {VCarte(0), CCarte(0)};
#endif

6
include/course.hpp Normal file
View File

@ -0,0 +1,6 @@
#ifndef COURSE_HPP
#define COURSE_HPP
void course();
#endif

View File

@ -17,7 +17,7 @@ class JDC{
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].couleur = (CCarte)(i+1);
this->jdc[index].valeur = (VCarte)(y+2);
}
}

6
include/machine.hpp Normal file
View File

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

View File

@ -3,6 +3,9 @@
#include <iostream>
#include "jdc.hpp"
#include <sys/ioctl.h>
#include <termios.h>
//#include <unistd.h>
template <typename T>
struct point{
@ -21,6 +24,8 @@ class Screen{
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;
@ -35,28 +40,38 @@ class Screen{
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';
//this->screen[i][y] = 'X';
}
}
};
void display(){
std::cout << "\033[1;48;5;22m" << std::endl;
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++){
std::cout << this->screen[i][y];
if (this->screen[i][y]) std::cout << this->screen[i][y];
else std::cout << ' ';
}
std::cout << std::endl;
printf("\033[%d;%dH", coord.y+i+1, coord.x);
}
std::cout << "\033[0m" << std::endl;
puts("\033[0m"); // reset
}
void addCard(point<int> p, carte c){
@ -77,15 +92,31 @@ class Screen{
this->screen[p.x+1][p.y+2] = JDC::couleur(c);
};
//void addSprite(point<int> p, sprite s){};
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;

227
src/course.cpp Normal file
View File

@ -0,0 +1,227 @@
#include <iostream>
#include <unistd.h>
#include <cstring>
#include "course.hpp"
#include "screen.hpp"
#include "jdc.hpp"
#include "carte.hpp"
carte tirer(JDC* j){
carte c = j->pop();
if (c.valeur == A) return tirer(j);
return c;
}
static void spriteDel(sprite* s){
for (int i=0; i<s->row; i++){
delete s->data[i];
}
delete s->data;
}
bool victoire(Screen* s, char* c){
char ligne[34] = " VICTOIRE ";
strcat(ligne, c);
strcpy(s->screen[s->ligne()-1], ligne);
return true;
}
void course(){
int HCASE = 4;
Screen sc(24, 34);
strcpy(sc.screen[0], "Appuyer sur * pour tirer une carte");
strcpy(sc.screen[1], "Appuyer sur q pour quitter ");
strcpy(sc.screen[2], " ");
strcpy(sc.screen[sc.ligne()-1], " ");
strcpy(sc.screen[sc.ligne()-2], " ");
for (int i=0; i<4; i++){
strcpy(sc.screen[3+i*4], " | | | | ");
strcpy(sc.screen[4+i*4], " | | | | ");
strcpy(sc.screen[5+i*4], " | | | | ");
strcpy(sc.screen[6+i*4], "----+----+----+----+---- ");
}
strcpy(sc.screen[19], " | | | | ");
strcpy(sc.screen[20], " | | | | ");
strcpy(sc.screen[21], " | | | | ");
constexpr point<int> Ppal1 = {20, 3};
constexpr point<int> Ppal2 = {20, 7};
constexpr point<int> Ppal3 = {20, 11};
constexpr point<int> Ppal4 = {20, 15};
sc.addCard(Ppal1, CARTERETOURNE);
sc.addCard(Ppal2, CARTERETOURNE);
sc.addCard(Ppal3, CARTERETOURNE);
sc.addCard(Ppal4, CARTERETOURNE);
point<int> Ppioche = {29, 19};
sc.addCard(Ppioche, CARTERETOURNE);
point<int> Ppic = {0, 19};
point<int> Pcoeur = {5, 19};
point<int> Pcarreau = {10, 19};
point<int> Ptrefle = {15, 19};
sc.addCard(Ppic, {A, PIC});
sc.addCard(Pcoeur, {A, COEUR});
sc.addCard(Pcarreau, {A, CARREAU});
sc.addCard(Ptrefle, {A, TREFLE});
sprite bar = {NULL, 3, 4};
bar.data = new char*[bar.row];
for (int i=0; i<bar.row; i++){
bar.data[i] = new char[bar.col];
}
strcpy(bar.data[0], "\\ ");
strcpy(bar.data[1], " \\ ");
strcpy(bar.data[2], " \\ ");
sc.addSprite({20, 19}, bar);
sc.display();
sprite vide = {NULL, 3, 4};
vide.data = new char*[vide.row];
for (int i=0; i<vide.row; i++){
vide.data[i] = new char[vide.col];
}
strcpy(vide.data[0], " ");
strcpy(vide.data[1], " ");
strcpy(vide.data[2], " ");
JDC j;
j.init();
j.shuffle();
bool vic = false;
char input;
std::cin >> input;
int palier = 15;
while (input != 'q' && !vic){
carte c = tirer(&j);
sc.addCard(Ppioche, c);
switch (c.couleur){
case PIC:
if (Ppic.x <= 3){
vic = victoire(&sc, "PIC");
}else{
sc.addSprite(Ppic, bar);
Ppic.x -= HCASE;
sc.addCard(Ppic, {A, PIC});
}
break;
case COEUR:
if (Pcoeur.x <= 3){
vic = victoire(&sc, "COEUR");
}else{
sc.addSprite(Pcoeur, bar);
Pcoeur.x -= HCASE;
sc.addCard(Pcoeur, {A, COEUR});
}
break;
case CARREAU:
if (Pcarreau.x <= 3){
vic = victoire(&sc, "CARREAU");
}else{
sc.addSprite(Pcarreau, bar);
Pcarreau.x -= HCASE;
sc.addCard(Pcarreau, {A, CARREAU});
}
break;
case TREFLE:
if (Ptrefle.x <= 3){
vic = victoire(&sc, "TREFLE");
}else{
sc.addSprite(Ptrefle, bar);
Ptrefle.x -= HCASE;
sc.addCard(Ptrefle, {A, TREFLE});
}
break;
}
if (Ppic.x <= palier && Pcoeur.x <= palier && Pcarreau.x <= palier && Ptrefle.x <= palier){
carte p = tirer(&j);
switch(palier){
case Ppal1.x:
sc.addCard(Ppal1, p);
break;
case Ppal2.x:
sc.addCard(Ppal2, p);
break;
case Ppal3.x:
sc.addCard(Ppal3, p);
break;
case Ppal4.x:
sc.addCard(Ppal4, p);
break;
}
sc.display();
sleep(1);
switch (p.couleur){
case PIC:
sc.addSprite(Ppic, vide);
Ppic.x += HCASE;
sc.addCard(Ppic, {A, PIC});
break;
case COEUR:
sc.addSprite(Pcoeur, vide);
Pcoeur.x += HCASE;
sc.addCard(Pcoeur, {A, COEUR});
break;
case CARREAU:
sc.addSprite(Pcarreau, vide);
Pcarreau.x += HCASE;
sc.addCard(Pcarreau, {A, CARREAU});
break;
case TREFLE:
sc.addSprite(Ptrefle, vide);
Ptrefle.x += HCASE;
sc.addCard(Ptrefle, {A, TREFLE});
break;
}
palier -= HCASE;
}
sc.display();
//manche(&sc, &j);
std::cin >> input;
}
spriteDel(&bar);
spriteDel(&vide);
}
//Appuyer sur * pour tirer une carte
//Appuyer sur q pour quitter
//
//+--+| | | |+--+
//|AP|| | | ||??|
//+--+| | | |+--+
//----+----+----+----+----
// |+--+| |+--+|+--+
// ||AC|| ||AT|||??|
// |+--+| |+--+|+--+
//----+----+----+----+----
// | |+--+| |+--+
// | ||AK|| ||TT|
// | |+--+| |+--+
//----+----+----+----+----
// | | | |+--+ +--+
// | | | ||TT| |??|
// | | | |+--+ +--+
//----+----+----+----+----
// | | | |
// | | | |
// | | | |
// VICTOIRE CARREAU
// 4 esp
// 17 34

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);
}

129
src/main.cpp Normal file
View File

@ -0,0 +1,129 @@
#ifdef _WIN32
#include <windows.h>
#endif
#include <iostream>
#include <unistd.h>
#include <cstring>
#include "screen.hpp"
#include "jdc.hpp"
#include "machine.hpp"
#include "course.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();
break;
case '2':
//roulette();
break;
case '3':
machine();
break;
case '4':
course();
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;
}

View File

@ -1,4 +1,5 @@
#include <iostream>
#include <unistd.h>
#include "screen.hpp"
#include "jdc.hpp"
@ -6,18 +7,26 @@ int main(){
int row = 5;
int col = 11;
Screen s(row, col);
s.background();
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;
}

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;
}