forked from ItsShunya/Dominoes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
de9527a
commit e7e4976
Showing
30 changed files
with
675 additions
and
623 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.