Skip to content

Commit

Permalink
1.0: Makefile
Browse files Browse the repository at this point in the history
zvezdochiot committed Dec 11, 2021
1 parent de9527a commit e7e4976
Showing 30 changed files with 675 additions and 623 deletions.
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
PROJECT = dominoes
CC = gcc
CFLAGS = -Wall
LDFLAGS = -s
PREFIX = /usr/local
SRCS = src
INSTALL = install
RM = rm -f

.PHONY: all clean install

all: $(PROJECT)

$(PROJECT): $(SRCS)/game.c $(SRCS)/main.c $(SRCS)/move.c $(SRCS)/player.c $(SRCS)/questions.c $(SRCS)/random.c $(SRCS)/sleep.c $(SRCS)/stack.c $(SRCS)/table.c $(SRCS)/tile.c
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)

clean:
$(RM) $(PROJECT) $(PROJECT).exe

install: $(PROJECT)
$(INSTALL) -d $(PREFIX)/games
$(INSTALL) -m 0755 $(PROJECT) $(PREFIX)/games
$(INSTALL) -d $(PREFIX)/share/doc/$(PROJECT)
$(INSTALL) -m 0644 LICENSE README.md $(PREFIX)/share/doc/$(PROJECT)
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
![GitHub release (latest by date)](https://img.shields.io/github/v/release/World-of-Domino/dominoes)
![GitHub Release Date](https://img.shields.io/github/release-date/World-of-Domino/dominoes)
![GitHub repo size](https://img.shields.io/github/repo-size/World-of-Domino/dominoes)
![GitHub all releases](https://img.shields.io/github/downloads/World-of-Domino/dominoes/total)
![GitHub](https://img.shields.io/github/license/World-of-Domino/dominoes)

# Dominoes
Single-player command line dominoes game with up to three machine players. It can be played displaying 2D tiles

Single-player command line dominoes game with up to three machine players. It can be played displaying 2D tiles

## Board

```sh
===>>> HA GANADO EL J1! :/ <<<===

FINAL STATE OF THE GAME:

Pila: La pila esta vacia
Table:
3|___ ___ ___ 0|___ ___ ___ ___ 6|___ ___ 4|___ ___ 2|___ ___ ___ ___ ___ 1|___ ___
-|3:5|5:1|1:0|-|0:2|2:4|4:3|3:6|-|6:0|0:4|-|4:5|5:2|-|2:6|6:1|1:3|3:2|2:1|-|1:4|4:6|
3| 0| 6| 4| 2| 1|

J0 5:5|Humano
J1 Robot
J2 0:3|Robot
J3 0:5|5:6|Robot
```
27 changes: 0 additions & 27 deletions game.h

This file was deleted.

21 changes: 0 additions & 21 deletions main.c

This file was deleted.

122 changes: 0 additions & 122 deletions move.c

This file was deleted.

117 changes: 0 additions & 117 deletions player.c

This file was deleted.

41 changes: 0 additions & 41 deletions questions.c

This file was deleted.

26 changes: 0 additions & 26 deletions random.c

This file was deleted.

54 changes: 30 additions & 24 deletions game.c → src/game.c
Original file line number Diff line number Diff line change
@@ -22,14 +22,9 @@ typedef struct
// if num_passes == n_players the game ends.
} t_game;

void initialize_game(t_game *p_pa);
void print_game_state(t_game pa);
void make_play(t_game *p_pa);
int has_the_game_ended(t_game pa);

void initialize_game(t_game *p_pa)
{
int i, j, p, k, nj, nf;
int i, j, nj, nf;

char s1[50] = "Number of players?", s2[50] = "Will there be a human player?", s3[50] = "Do you wish to activate omniscience?", s4[50] = "Do you wish to see the table in 2D?";

@@ -50,7 +45,8 @@ void initialize_game(t_game *p_pa)
p_pa->js.j[i].f[j] = pick_from_stack(&(p_pa->pi));

if (p_pa->js.a_human == TRUE)
{ // Initialize players.
{
// Initialize players.
p_pa->js.j[0].type = T_HUMAN;
p_pa->js.j[0].n_tiles = 7;
for (i = 1; i < p_pa->js.n_players; i++)
@@ -88,16 +84,15 @@ void initialize_game(t_game *p_pa)

void print_game_state(t_game pa)
{
int i;
print_stack(pa.pi, pa.visible);
print_table(pa.m, pa.dim);
printf("\n");
print_players(pa.js, pa.visible);
print_turn(pa.js);
}
void print_final_game_state(t_game pa)
{ // Prints the game at the time of the ending move.
int i;
{
// Prints the game at the time of the ending move.
sleep_a_bit(10);
printf("\tFINAL STATE OF THE GAME:\n\n");
pa.visible = TRUE;
@@ -117,11 +112,13 @@ void make_play(t_game *p_pa)
ts = calculate_possible_moves(p_pa->js.j[p_pa->js.turn], p_pa->m); // We calculate the possible moves.

while (ts.n_moves == 0 && p_pa->pi.n_tiles != 0)
{ // If the player cannot move, he has to pick a tile from the stack.
{
// If the player cannot move, he has to pick a tile from the stack.
p_pa->js.j[p_pa->js.turn].f[p_pa->js.j[p_pa->js.turn].n_tiles] = pick_from_stack(&(p_pa->pi));

if (p_pa->visible == TRUE)
{ // Omniscience active.
{
// Omniscience active.
if (p_pa->js.j[p_pa->js.turn].type == T_HUMAN) // If it is human, the tile is shown.
printf("You take tiles from the stack! Picked a %d:%d ;P\n", p_pa->js.j[p_pa->js.turn].f[p_pa->js.j[p_pa->js.turn].n_tiles].num1, p_pa->js.j[p_pa->js.turn].f[p_pa->js.j[p_pa->js.turn].n_tiles].num2);
else
@@ -143,9 +140,11 @@ void make_play(t_game *p_pa)
print_moves(ts, p_pa->js.j[p_pa->js.turn]);

if (p_pa->js.j[p_pa->js.turn].type == T_HUMAN)
{ // Human plays.
{
// Human plays.
if (ts.n_moves == 0 && p_pa->pi.n_tiles == 0)
{ // Human cannot move and passes turn.
{
// Human cannot move and passes turn.
printf("You pass turn! :(\n\n");
p_pa->num_passes++;
}
@@ -167,14 +166,16 @@ void make_play(t_game *p_pa)
}
printf("\n");
if ((ts.count_dobles > 1) && (tirada == ts.n_moves))
{ // Dobledobla is when the counter has 2 moves with doubles and the player picks it.
{
// Dobledobla is when the counter has 2 moves with doubles and the player picks it.
play_from_right(&(p_pa->m), p_pa->js.j[p_pa->js.turn].f[ts.t[ts.dobleleft].n_tile]); // Double from the right.
play_from_left(&(p_pa->m), p_pa->js.j[p_pa->js.turn].f[ts.t[ts.dobleright].n_tile]); // Double from the left.
for (i = ts.t[ts.dobleright].n_tile; i < p_pa->js.j[p_pa->js.turn].n_tiles - 1; i++) // Deletes a double from the tiles vector of the player.
p_pa->js.j[p_pa->js.turn].f[i] = p_pa->js.j[p_pa->js.turn].f[i + 1];
p_pa->js.j[p_pa->js.turn].n_tiles--;
if (ts.t[ts.dobleleft].n_tile > ts.t[ts.dobleright].n_tile)
{ // Deletes the other double from the tiles vector depending where it is found respecting the first one.
{
// Deletes the other double from the tiles vector depending where it is found respecting the first one.
for (i = ts.t[ts.dobleleft - 1].n_tile; i < p_pa->js.j[p_pa->js.turn].n_tiles - 1; i++)
p_pa->js.j[p_pa->js.turn].f[i] = p_pa->js.j[p_pa->js.turn].f[i + 1];
p_pa->js.j[p_pa->js.turn].n_tiles--;
@@ -187,7 +188,8 @@ void make_play(t_game *p_pa)
}
}
if (tirada != ts.n_moves)
{ // If no dobledobla
{
// If no dobledobla
if (ts.t[tirada].rotated == TRUE) // Rotates the tile if necessary.
turn_tile(&(p_pa->js.j[p_pa->js.turn].f[ts.t[tirada].n_tile]));
if (ts.t[tirada].corner == 'd')
@@ -220,14 +222,16 @@ void make_play(t_game *p_pa)
ts.n_moves++; // Dobledobla counts as an extra move.
tirada = random_number(ts.n_moves); // Picks a random move.
if ((ts.count_dobles > 1) && (tirada == ts.n_moves))
{ // Dobledobla is when the counter has 2 moves with doubles and the player picks it.
{
// Dobledobla is when the counter has 2 moves with doubles and the player picks it.
play_from_right(&(p_pa->m), p_pa->js.j[p_pa->js.turn].f[ts.t[ts.dobleleft].n_tile]); // Double from the right.
play_from_left(&(p_pa->m), p_pa->js.j[p_pa->js.turn].f[ts.t[ts.dobleright].n_tile]); // Double from the left.
for (i = ts.t[ts.dobleright].n_tile; i < p_pa->js.j[p_pa->js.turn].n_tiles - 1; i++) // Deletes a double from the tiles vector of the player.
p_pa->js.j[p_pa->js.turn].f[i] = p_pa->js.j[p_pa->js.turn].f[i + 1];
p_pa->js.j[p_pa->js.turn].n_tiles--;
if (ts.t[ts.dobleleft].n_tile > ts.t[ts.dobleright].n_tile)
{ // Deletes the other double from the tiles vector depending where it is found respecting the first one.
{
// Deletes the other double from the tiles vector depending where it is found respecting the first one.
for (i = ts.t[ts.dobleleft - 1].n_tile; i < p_pa->js.j[p_pa->js.turn].n_tiles - 1; i++)
p_pa->js.j[p_pa->js.turn].f[i] = p_pa->js.j[p_pa->js.turn].f[i + 1];
p_pa->js.j[p_pa->js.turn].n_tiles--;
@@ -274,7 +278,8 @@ int has_the_game_ended(t_game pa)
pa.js.turn = pa.js.n_players - 1;

if (pa.js.j[pa.js.turn].n_tiles == 0)
{ // Winning condition.
{
// Winning condition.
if (pa.js.j[pa.js.turn].type == T_HUMAN)
{
printf("===>>> HAS GANADO! :) <<<===\n\n");
@@ -291,7 +296,8 @@ int has_the_game_ended(t_game pa)
printf("===>>> JUEGO BLOQUEADO <<<===\n\n");
aux = TRUE;
for (i = 0; i < pa.js.n_players; i++)
{ // Calculates the weight of the tiles.
{
// Calculates the weight of the tiles.
pa.js.j[i].weight = 0;
for (j = 0; j < pa.js.j[i].n_tiles; j++)
{
@@ -308,7 +314,8 @@ int has_the_game_ended(t_game pa)
}
}
for (i = 0; i < pa.js.n_players; i++)
{ // Draw.
{
// Draw.
if (minweight == pa.js.j[i].weight && i != jugminweight)
{
empate = TRUE;
@@ -323,6 +330,5 @@ int has_the_game_ended(t_game pa)
printf("\n===>>> HA GANADO EL J%d POR MINIMO weight(%d de weight) <<<===\n\n\n", jugminweight, minweight);
}
}
else
return (aux);
return (aux);
}
27 changes: 27 additions & 0 deletions src/game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef GAME_H
#define GAME_H

#include "player.h"
#include "table.h"
#include "stack.h"
#include "move.h"

typedef struct
{
t_players js;
t_table m; // Tiles played.
t_stack pi; // Tiles not played and not belonging to any player.
int visible; // Stack of tiles and visible tiles from a bot.
int table_2d; // See table in 2D (versus 1D).
// visible and table_2d can be TRUE o FALSE.
int num_passes; // Consecutive times a player passes turn.
// if num_passes == n_players the game ends.
} t_game;

void initialize_game(t_game *p_pa);
void print_game_state(t_game pa);
void print_final_game_state(t_game pa);
void make_play(t_game *p_pa);
int has_the_game_ended(t_game pa);

#endif // GAME_H
21 changes: 21 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "game.h"

#define TRUE 1
#define FALSE 0

int main()
{
int end;
t_game game;
initialize_game(&game);
make_play(&game);
end = FALSE;
while (end == FALSE) // Enters the loop every time there are moves to play.
{
print_game_state(game);
make_play(&game);
end = has_the_game_ended(game);
}

print_final_game_state(game);
}
129 changes: 129 additions & 0 deletions src/move.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#define MOVE_H

#include <stdio.h>
#include "tile.h"
#include "player.h"
#include "table.h"

#define MAX_MOVES_PLAYER NUM_TILES_DOMINO

typedef struct
{
int n_tile; // Number of tiles in the tiles vector of the player
char corner; // Left or right
int rotated; // True or false
int dobledobla;
} t_tirada;

typedef struct
{
int n_moves;
t_tirada t[MAX_MOVES_PLAYER];
int count_dobles, dobleleft, dobleright;

} t_tiradas;

void print_move(t_tirada t, t_player j)
{
// Always visible independent of omniscience
int aux;
aux = t.n_tile;
if (j.type == T_ROBOT)
printf("Move: %c|%d:%d|\n\n", t.corner, j.f[aux].num1, j.f[aux].num2);
}

void print_moves(t_tiradas ts, t_player j, int visible)
{
int i;

if ((visible == TRUE && j.type == T_ROBOT) || j.type == T_HUMAN)
{
// Visible if omniscience is active or the player is human
printf("Possible moves: ");
if (ts.n_moves > 0)
{
// If the player can move
for (i = 0; i < ts.n_moves; i++)
{
printf("%d(%c%d:%d) ", i, ts.t[i].corner, j.f[ts.t[i].n_tile].num1, j.f[ts.t[i].n_tile].num2);
}

if ((ts.count_dobles > 1) && (j.f[ts.t[ts.dobleleft].n_tile].num1 != j.f[ts.t[ts.dobleright].n_tile].num2))
{
i = ts.n_moves;
printf("%d(%c%d:%d|%c%d:%d) ", i, ts.t[ts.dobleleft].corner, j.f[ts.t[ts.dobleleft].n_tile].num1, j.f[ts.t[ts.dobleleft].n_tile].num2, ts.t[ts.dobleright].corner, j.f[ts.t[ts.dobleright].n_tile].num1, j.f[ts.t[ts.dobleright].n_tile].num2);
}
printf("\n");
}
else
printf("There are no possible movements!\n"); // If the player cannot move
}
}

t_tiradas calculate_possible_moves(t_player j, t_table m)
{
t_tiradas ts;
int izq, der, i, k = 0, doble;
izq = table_leftmost(m); // Gets the leftmost tile on the table
der = table_rightmost(m); // Gets the rightmost tile on the table
ts.count_dobles = 0;
for (i = 0; i < j.n_tiles; i++)
{
doble = 0;
if (j.f[i].num1 == j.f[i].num2)
doble = TRUE; // Check if it is a double

if (j.f[i].num2 == izq)
{
// Move from the left without rotating
if (doble == TRUE)
{
ts.count_dobles++;
ts.dobleleft = k;
}
ts.t[k].n_tile = i;
ts.t[k].corner = 'i';
ts.t[k].rotated = 0;
k++;
}
if (j.f[i].num1 == izq)
{
// Move from the left rotating
if (doble == FALSE)
{
ts.t[k].n_tile = i;
ts.t[k].corner = 'i';
ts.t[k].rotated = 1;
k++;
}
}
if (j.f[i].num1 == der)
{
// Move from the right without rotating
if (doble == TRUE)
{
ts.count_dobles++;
ts.dobleright = k;
}
ts.t[k].n_tile = i;
ts.t[k].corner = 'd';
ts.t[k].rotated = 0;
k++;
}
if (j.f[i].num2 == der)
{
// Move from the right rotating
if (doble == FALSE)
{
ts.t[k].n_tile = i;
ts.t[k].corner = 'd';
ts.t[k].rotated = 1;
k++;
}
}

ts.n_moves = k; // Number of movements
}

return (ts);
}
14 changes: 7 additions & 7 deletions move.h → src/move.h
Original file line number Diff line number Diff line change
@@ -9,17 +9,17 @@

typedef struct
{
int n_tile; // Number of tiles in the tiles vector of the player
char corner; // Left or right
int rotated; // True or false
int dobledobla;
int n_tile; // Number of tiles in the tiles vector of the player
char corner; // Left or right
int rotated; // True or false
int dobledobla;
} t_tirada;

typedef struct
{
int n_moves;
t_tirada t[MAX_MOVES_PLAYER];
int count_dobles, dobleright, dobleleft;
int n_moves;
t_tirada t[MAX_MOVES_PLAYER];
int count_dobles, dobleright, dobleleft;
} t_tiradas;

void print_move(t_tirada ts, t_player j);
117 changes: 117 additions & 0 deletions src/player.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include "tile.h"
#include <stdio.h>

#define MIN_PLAYERS 2
#define MAX_PLAYERS 4

#define T_HUMAN 1
#define T_ROBOT 2

#define TRUE 1
#define FALSE 0

typedef struct
{
int n_tiles;
t_tile f[NUM_TILES_DOMINO]; // Tiles owned.
int type;
int weight;
} t_player;

typedef struct
{
int n_players;
int a_human; // TRUE or FALSE.
int turn;
t_player j[MAX_PLAYERS]; // Each player in players.
} t_players;

void print_player(t_player j, int visible)
{
int i;

if (j.type == T_ROBOT)
{
// Bot.
if (visible == FALSE)
{
// Omniscience off.
for (i = 0; i < j.n_tiles; i++)
{
printf("?:?|");
}
}
else
{
// Omniscience on.
for (i = 0; i < j.n_tiles; i++)
{
printf("%d:%d|", j.f[i].num1, j.f[i].num2);
}
}
}
else
{
// Human.
for (i = 0; i < j.n_tiles; i++)
{
printf("%d:%d|", j.f[i].num1, j.f[i].num2);
}
}
}

void print_players(t_players js, int visible)
{
int i;
// We do it for every player.
for (i = 0; i < js.n_players; i++)
{
printf("J%d ", i);
print_player(js.j[i], visible);
if (js.j[i].type == T_ROBOT)
{
// Robot.
printf("Robot\n");
}
else
{
// Human.
printf("Humano\n");
}
}
}

void who_has_higher_doble(t_players js, int *p_nj, int *p_nf)
{
int i, j, max = 0, encontrado = 0;

for (i = 0; i < js.n_players; i++)
{
// Check which player has the highst double.
for (j = 0; j < js.j[i].n_tiles; j++)
{
if (js.j[i].f[j].num1 == js.j[i].f[j].num2 && js.j[i].f[j].num1 > max)
{
max = js.j[i].f[j].num1;
*p_nj = i;
*p_nf = j;
encontrado = 1;
}
}
}
if (encontrado == 0)
{
*p_nj = 0;
*p_nf = 0;
}
}

void skip_turn(t_players *p_js)
{
p_js->turn = ((p_js->turn) + 1) % (p_js->n_players); // Adds 1 to turns unless its maximum, in this case returns to the initial 0.
}

void print_turn(t_players js)
{
printf("turn: %d\n", js.turn);
}
16 changes: 8 additions & 8 deletions player.h → src/player.h
Original file line number Diff line number Diff line change
@@ -11,18 +11,18 @@

typedef struct
{
int n_tiles;
t_tile f[NUM_TILES_DOMINO]; // tiles que tiene
int type;
int weight;
int n_tiles;
t_tile f[NUM_TILES_DOMINO]; // tiles que tiene
int type;
int weight;
} t_player;

typedef struct
{
int n_players;
int a_human; // TRUE or FALSE
int turn;
t_player j[MAX_PLAYERS]; // tiles que tiene y otras cosas
int n_players;
int a_human; // TRUE or FALSE
int turn;
t_player j[MAX_PLAYERS]; // tiles que tiene y otras cosas
} t_players;

void print_player(t_player j, int visible);
43 changes: 43 additions & 0 deletions src/questions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdio.h>

int out_of_range(int n, int min, int max)
{
return ((n < min) || (n > max));
}

void to_lower_caps(char *c)
{
if ((*c >= 'A') && (*c <= 'Z'))
{
*c = *c - 'A' + 'a';
}
}

int ask_n_in_range(char *s, int min, int max)
{
int n;

do
{
printf("%s [%d-%d]: ", s, min, max);
scanf("%d%*c", &n);
}
while (out_of_range(n, min, max));

return n;
}

int ask_yes_or_no(char *s)
{
char c;

do
{
printf("%s [s/n]: ", s);
scanf("%c%*c", &c);
to_lower_caps(&c);
}
while (c != 's' && c != 'n');

return (c == 's');
}
File renamed without changes.
26 changes: 26 additions & 0 deletions src/random.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <time.h> // time()
#include <stdlib.h> // rand(), srand()
#include <stdio.h> // printf()
#include "random.h"

/*
* initialize_random() has to be executed once at the beginning of main
* to start the generation of random numbers
*/
//void initialize_random() {
/*
* To ensure that the sequence of random numbers is the samePara que la sequencia de numeros aleatorios sea igual en cada
* in each execution use srand(0) instead srand(time).
*/
//srand(0);
// srand( (unsigned)time( NULL ) );
//}

/*
* random_number(max) returna a random integer number between 0 and max-1.
*/
int random_number(int max)
{
srand(time(NULL));
return (rand() % max);
}
File renamed without changes.
22 changes: 11 additions & 11 deletions sleep.c → src/sleep.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <time.h>
#include "sleep.h"

#define ONE_SECOND 1 // 1 second
#define ONE_SECOND 1 // 1 second
#define HALF_SECOND 500000000 // 0.5 seconds

/*
@@ -10,11 +10,11 @@
*/
void sleep_n_seconds(long segundos)
{
struct timespec tim, tim2;
tim.tv_sec = segundos;
tim.tv_nsec = 0;
struct timespec tim, tim2;
tim.tv_sec = segundos;
tim.tv_nsec = 0;

nanosleep(&tim, &tim2);
nanosleep(&tim, &tim2);
}

/*
@@ -23,11 +23,11 @@ void sleep_n_seconds(long segundos)
*/
void sleep_n_nanoseconds(long nanosegundos)
{
struct timespec tim, tim2;
tim.tv_sec = 0;
tim.tv_nsec = nanosegundos;
struct timespec tim, tim2;
tim.tv_sec = 0;
tim.tv_nsec = nanosegundos;

nanosleep(&tim, &tim2);
nanosleep(&tim, &tim2);
}

/*
@@ -36,7 +36,7 @@ void sleep_n_nanoseconds(long nanosegundos)
*/
void sleep_a_bit()
{
sleep_n_seconds(ONE_SECOND);
sleep_n_seconds(ONE_SECOND);
}

/*
@@ -45,5 +45,5 @@ void sleep_a_bit()
*/
void sleep_a_nano_bit()
{
sleep_n_nanoseconds(HALF_SECOND);
sleep_n_nanoseconds(HALF_SECOND);
}
File renamed without changes.
72 changes: 72 additions & 0 deletions src/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <stdio.h>
#include "random.h"
#include "tile.h"
#define NUM_TILES_DOMINO 28
#define FALSE 0
#define TRUE 1

typedef struct
{
int n_tiles;
t_tile f[NUM_TILES_DOMINO];
} t_stack;

void initialize_stack(t_stack *pi)
{
int i = 0, j, k;

pi->n_tiles = NUM_TILES_DOMINO; // At the start all the tiles belong to the stack.
for (j = 0; j < 7; j++)
for (k = j; k < 7; k++)
{
initialize_tile(&pi->f[i], j, k); // Initialize the 28 tiles.
i++;
}
}

void print_stack(t_stack pi, int visible)
{
int i;

if (visible == TRUE)
{
// Omniscience on.
printf("Pila:\t");
for (i = 0; i < pi.n_tiles; i++)
print_tile(pi.f[i], visible);
}

else
{
// Omnisciencia off.
printf("Pila:\t");
for (i = 0; i < pi.n_tiles; i++)
printf("?:?|");
}
if (pi.n_tiles == 0)
printf("La pila esta vacia");
}

int is_stack_empty(t_stack pi)
{
if (pi.n_tiles == 0)
return (TRUE);

else
return (FALSE);
}

t_tile pick_from_stack(t_stack *pi)
{
int i, pos;
t_stack aux;
pos = random_number(pi->n_tiles); // Picks a random tile.
aux.f[pos] = pi->f[pos];
for (i = pos; i < (pi->n_tiles - 1); i++)
{
// Deletes the picked tile from the stack.
pi->f[i] = pi->f[i + 1];
}
pi->n_tiles--;
return (aux.f[pos]); // returns the picked tile.
}
4 changes: 2 additions & 2 deletions stack.h → src/stack.h
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@

typedef struct
{
int n_tiles;
t_tile f[NUM_TILES_DOMINO];
int n_tiles;
t_tile f[NUM_TILES_DOMINO];
} t_stack;

void initialize_stack(t_stack *pi);
102 changes: 102 additions & 0 deletions src/table.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <stdio.h>
#include "tile.h"
#define NUM_TILES_DOMINO 28

typedef struct
{
int n_tiles;
t_tile f[NUM_TILES_DOMINO];
} t_table;

void initialize_table(t_table *p_m)
{
p_m->n_tiles = 0; // No tiles on the table at the beginning.
}

void print_table(t_table m, int dim)
{
int i;

printf("\nTable: ");
if (dim == FALSE)
{
// No 2D table.
for (i = 0; i < m.n_tiles; i++)
printf("%d:%d|", m.f[i].num1, m.f[i].num2);
}
else
{
// 2D table
printf("\n");

for (i = 0; i < m.n_tiles; i++)
{
if (m.f[i].num1 == m.f[i].num2)
{
// If double, place vertically.
printf("%d|", m.f[i].num1);
}
else
{
printf("___ ");
}
}
printf("\n");
for (i = 0; i < m.n_tiles; i++)
{
if (m.f[i].num1 == m.f[i].num2)
{
// If double, place vertically.
printf("-|");
}
else
{
printf("%d:%d|", m.f[i].num1, m.f[i].num2);
}
}
printf("\n");
for (i = 0; i < m.n_tiles; i++)
{
if (m.f[i].num1 == m.f[i].num2)
{
// If double, place vertically.
printf("%d|", m.f[i].num1);
}
else
{
printf(" ");
}
}
printf("\n");
}
}

void play_from_right(t_table *p_m, t_tile f)
{
p_m->f[p_m->n_tiles] = f; // Rightmost position :n_tiles because it is the first empty position in the vector.
p_m->n_tiles++;
}
void play_from_left(t_table *p_m, t_tile f)
{
int i;
p_m->n_tiles++;
for (i = p_m->n_tiles; i > 0; i--)
{
// Shift all the table one place to the right
p_m->f[i] = p_m->f[i - 1];
}
p_m->f[0] = f; // Introduces the new tile on position 0.
}

int table_leftmost(t_table m)
{
int left;
left = m.f[0].num1;
return (left);
}
int table_rightmost(t_table m)
{
int right;
right = m.f[m.n_tiles - 1].num2;
return (right);
}
4 changes: 2 additions & 2 deletions table.h → src/table.h
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@

typedef struct
{
int n_tiles;
t_tile f[NUM_TILES_DOMINO];
int n_tiles;
t_tile f[NUM_TILES_DOMINO];
} t_table;

void initialize_table(t_table *p_m);
27 changes: 27 additions & 0 deletions src/tile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdio.h>

typedef struct
{
int num1, num2;
} t_tile;

void print_tile(t_tile tile, int visible)
{
printf("%d:%d|", tile.num1, tile.num2);
}

void turn_tile(t_tile *tile)
{
// Here num1 is swapped for num2 and vice versa.
int aux;

aux = tile->num1;
tile->num1 = tile->num2;
tile->num2 = aux;
}

void initialize_tile(t_tile *tile, int i, int j)
{
tile->num2 = j;
tile->num1 = i;
}
File renamed without changes.
74 changes: 0 additions & 74 deletions stack.c

This file was deleted.

98 changes: 0 additions & 98 deletions table.c

This file was deleted.

42 changes: 0 additions & 42 deletions tile.c

This file was deleted.

0 comments on commit e7e4976

Please sign in to comment.