Moved code into 'game' and 'util', removed unnecessary headers.
This commit is contained in:
parent
9459fc8242
commit
e65e4e2875
@ -1,4 +1,3 @@
|
||||
#include <ncurses.h>
|
||||
#include <malloc.h>
|
||||
#include "card.h"
|
||||
|
||||
|
185
lib/deck.c
185
lib/deck.c
@ -1,6 +1,4 @@
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <time.h>
|
||||
#include "deck.h"
|
||||
|
||||
void allocate_deck(struct deck **deck) {
|
||||
@ -66,186 +64,3 @@ void delete_deck(struct deck *deck) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void set_deck_stacks_coordinates(struct deck *deck) {
|
||||
set_frame(deck->stock->card->frame,
|
||||
STOCK_STARTING_Y,
|
||||
STOCK_STARTING_X);
|
||||
set_frame(deck->waste_pile->card->frame,
|
||||
WASTE_PILE_STARTING_Y,
|
||||
WASTE_PILE_STARTING_X);
|
||||
set_frame(deck->foundation_0->card->frame,
|
||||
FOUNDATION_0_STARTING_Y,
|
||||
FOUNDATION_0_STARTING_X);
|
||||
set_frame(deck->foundation_1->card->frame,
|
||||
FOUNDATION_1_STARTING_Y,
|
||||
FOUNDATION_1_STARTING_X);
|
||||
set_frame(deck->foundation_2->card->frame,
|
||||
FOUNDATION_2_STARTING_Y,
|
||||
FOUNDATION_2_STARTING_X);
|
||||
set_frame(deck->foundation_3->card->frame,
|
||||
FOUNDATION_3_STARTING_Y,
|
||||
FOUNDATION_3_STARTING_X);
|
||||
set_frame(deck->maneuvre_0->card->frame,
|
||||
MANEUVRE_0_STARTING_Y,
|
||||
MANEUVRE_0_STARTING_X);
|
||||
set_frame(deck->maneuvre_1->card->frame,
|
||||
MANEUVRE_1_STARTING_Y,
|
||||
MANEUVRE_1_STARTING_X);
|
||||
set_frame(deck->maneuvre_2->card->frame,
|
||||
MANEUVRE_2_STARTING_Y,
|
||||
MANEUVRE_2_STARTING_X);
|
||||
set_frame(deck->maneuvre_3->card->frame,
|
||||
MANEUVRE_3_STARTING_Y,
|
||||
MANEUVRE_3_STARTING_X);
|
||||
set_frame(deck->maneuvre_4->card->frame,
|
||||
MANEUVRE_4_STARTING_Y,
|
||||
MANEUVRE_4_STARTING_X);
|
||||
set_frame(deck->maneuvre_5->card->frame,
|
||||
MANEUVRE_5_STARTING_Y,
|
||||
MANEUVRE_5_STARTING_X);
|
||||
set_frame(deck->maneuvre_6->card->frame,
|
||||
MANEUVRE_6_STARTING_Y,
|
||||
MANEUVRE_6_STARTING_X);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void fill_deck(struct deck *deck) {
|
||||
struct card *card[NUMBER_OF_CARDS];
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||
allocate_card(&(card[i]));
|
||||
}
|
||||
set_card(card[0], TWO, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[1], TWO, SPADES, COVERED, 1, 1);
|
||||
set_card(card[2], TWO, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[3], TWO, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[4], THREE, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[5], THREE, SPADES, COVERED, 1, 1);
|
||||
set_card(card[6], THREE, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[7], THREE, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[8], FOUR, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[9], FOUR, SPADES, COVERED, 1, 1);
|
||||
set_card(card[10], FOUR, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[11], FOUR, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[12], FIVE, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[13], FIVE, SPADES, COVERED, 1, 1);
|
||||
set_card(card[14], FIVE, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[15], FIVE, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[16], SIX, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[17], SIX, SPADES, COVERED, 1, 1);
|
||||
set_card(card[18], SIX, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[19], SIX, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[20], SEVEN, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[21], SEVEN, SPADES, COVERED, 1, 1);
|
||||
set_card(card[22], SEVEN, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[23], SEVEN, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[24], EIGHT, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[25], EIGHT, SPADES, COVERED, 1, 1);
|
||||
set_card(card[26], EIGHT, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[27], EIGHT, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[28], NINE, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[29], NINE, SPADES, COVERED, 1, 1);
|
||||
set_card(card[30], NINE, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[31], NINE, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[32], TEN, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[33], TEN, SPADES, COVERED, 1, 1);
|
||||
set_card(card[34], TEN, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[35], TEN, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[36], JACK, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[37], JACK, SPADES, COVERED, 1, 1);
|
||||
set_card(card[38], JACK, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[39], JACK, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[40], QUEEN, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[41], QUEEN, SPADES, COVERED, 1, 1);
|
||||
set_card(card[42], QUEEN, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[43], QUEEN, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[44], KING, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[45], KING, SPADES, COVERED, 1, 1);
|
||||
set_card(card[46], KING, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[47], KING, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[48], ACE, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[49], ACE, SPADES, COVERED, 1, 1);
|
||||
set_card(card[50], ACE, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[51], ACE, CLUBS, COVERED, 1, 1);
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||
push(&(deck->stock), card[i]);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void shuffle_deck(struct deck *deck) {
|
||||
struct stack **stack = NULL;
|
||||
struct stack tmp;
|
||||
int random;
|
||||
|
||||
stack = malloc(NUMBER_OF_CARDS * sizeof(*stack));
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||
stack[i] = pop(&(deck->stock));
|
||||
}
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS - 1; i++) {
|
||||
random = i + (rand() % (NUMBER_OF_CARDS) - i);
|
||||
tmp = (*stack[i]);
|
||||
(*stack[i]) = (*stack[random]);
|
||||
(*stack[random]) = tmp;
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||
push(&(deck->stock), stack[i]->card);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void deal_cards(struct deck *deck) {
|
||||
move_card(&(deck->stock), &(deck->maneuvre_0));
|
||||
expose_card(deck->maneuvre_0->card);
|
||||
move_card(&(deck->stock), &(deck->maneuvre_1));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_2));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_3));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_4));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_5));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_6));
|
||||
|
||||
move_card(&(deck->stock), &(deck->maneuvre_1));
|
||||
expose_card(deck->maneuvre_1->card);
|
||||
move_card(&(deck->stock), &(deck->maneuvre_2));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_3));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_4));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_5));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_6));
|
||||
|
||||
move_card(&(deck->stock), &(deck->maneuvre_2));
|
||||
expose_card(deck->maneuvre_2->card);
|
||||
move_card(&(deck->stock), &(deck->maneuvre_3));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_4));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_5));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_6));
|
||||
|
||||
move_card(&(deck->stock), &(deck->maneuvre_3));
|
||||
expose_card(deck->maneuvre_3->card);
|
||||
move_card(&(deck->stock), &(deck->maneuvre_4));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_5));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_6));
|
||||
|
||||
move_card(&(deck->stock), &(deck->maneuvre_4));
|
||||
expose_card(deck->maneuvre_4->card);
|
||||
move_card(&(deck->stock), &(deck->maneuvre_5));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_6));
|
||||
|
||||
move_card(&(deck->stock), &(deck->maneuvre_5));
|
||||
expose_card(deck->maneuvre_5->card);
|
||||
move_card(&(deck->stock), &(deck->maneuvre_6));
|
||||
|
||||
move_card(&(deck->stock), &(deck->maneuvre_6));
|
||||
expose_card(deck->maneuvre_6->card);
|
||||
|
||||
return;
|
||||
}
|
||||
|
33
lib/deck.h
33
lib/deck.h
@ -3,35 +3,6 @@
|
||||
|
||||
#include "stack.h"
|
||||
|
||||
#define NUMBER_OF_CARDS 52
|
||||
|
||||
#define STOCK_STARTING_X 1
|
||||
#define STOCK_STARTING_Y 1
|
||||
#define WASTE_PILE_STARTING_X 9
|
||||
#define WASTE_PILE_STARTING_Y 1
|
||||
#define FOUNDATION_0_STARTING_X 25
|
||||
#define FOUNDATION_0_STARTING_Y 1
|
||||
#define FOUNDATION_1_STARTING_X 33
|
||||
#define FOUNDATION_1_STARTING_Y 1
|
||||
#define FOUNDATION_2_STARTING_X 41
|
||||
#define FOUNDATION_2_STARTING_Y 1
|
||||
#define FOUNDATION_3_STARTING_X 49
|
||||
#define FOUNDATION_3_STARTING_Y 1
|
||||
#define MANEUVRE_0_STARTING_X 1
|
||||
#define MANEUVRE_0_STARTING_Y 7
|
||||
#define MANEUVRE_1_STARTING_X 9
|
||||
#define MANEUVRE_1_STARTING_Y 7
|
||||
#define MANEUVRE_2_STARTING_X 17
|
||||
#define MANEUVRE_2_STARTING_Y 7
|
||||
#define MANEUVRE_3_STARTING_X 25
|
||||
#define MANEUVRE_3_STARTING_Y 7
|
||||
#define MANEUVRE_4_STARTING_X 33
|
||||
#define MANEUVRE_4_STARTING_Y 7
|
||||
#define MANEUVRE_5_STARTING_X 41
|
||||
#define MANEUVRE_5_STARTING_Y 7
|
||||
#define MANEUVRE_6_STARTING_X 49
|
||||
#define MANEUVRE_6_STARTING_Y 7
|
||||
|
||||
struct deck {
|
||||
struct stack *stock;
|
||||
struct stack *waste_pile;
|
||||
@ -53,9 +24,5 @@ struct deck {
|
||||
void allocate_deck(struct deck **);
|
||||
void initialize_deck(struct deck *);
|
||||
void delete_deck(struct deck *);
|
||||
void set_deck_stacks_coordinates(struct deck *);
|
||||
void fill_deck(struct deck *);
|
||||
void shuffle_deck(struct deck *);
|
||||
void deal_cards(struct deck *);
|
||||
|
||||
#endif
|
||||
|
127
lib/display.c
127
lib/display.c
@ -1,103 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <ncurses.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <locale.h>
|
||||
#include "deck.h"
|
||||
#include "display.h"
|
||||
|
||||
void initialize_curses() {
|
||||
setlocale(LC_ALL, ""); /* supporting unicode characters */
|
||||
initscr(); /* initialize the terminal in curses mode */
|
||||
raw(); /* disable line buffers */
|
||||
noecho(); /* character echo is unnecessary */
|
||||
keypad(stdscr, TRUE); /* enable F and arrow keys */
|
||||
start_color(); /* I want colors */
|
||||
curs_set(FALSE); /* invisible cursor */
|
||||
|
||||
init_pair(1, COLOR_BLACK, COLOR_WHITE);
|
||||
init_pair(2, COLOR_RED, COLOR_WHITE);
|
||||
init_pair(3, COLOR_WHITE, COLOR_BLUE);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void end_curses() {
|
||||
endwin();
|
||||
puts("Game finished.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void initialize_game() {
|
||||
struct deck *deck = NULL;
|
||||
int pressed_key;
|
||||
|
||||
mvprintw(11, 27, "Welcome to tty-solitaire.");
|
||||
mvprintw(12, 19, "Press the space bar to play or q to quit.");
|
||||
while (1) {
|
||||
switch (pressed_key = getch()) {
|
||||
case KEY_SPACEBAR:
|
||||
clear();
|
||||
refresh();
|
||||
assume_default_colors(COLOR_WHITE, COLOR_GREEN);
|
||||
draw_empty_stacks();
|
||||
allocate_deck(&deck);
|
||||
initialize_deck(deck);
|
||||
set_deck_stacks_coordinates(deck);
|
||||
fill_deck(deck);
|
||||
shuffle_deck(deck);
|
||||
deal_cards(deck);
|
||||
draw_game(deck);
|
||||
getchar();
|
||||
end_curses();
|
||||
end_game(deck);
|
||||
return;
|
||||
case 'q':
|
||||
case 'Q':
|
||||
end_curses();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void end_game(struct deck *deck) {
|
||||
delete_deck(deck);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void draw_empty_stacks() {
|
||||
WINDOW **empty_stack;
|
||||
|
||||
empty_stack = malloc(EMPTY_STACKS_NUMBER * sizeof(**empty_stack));
|
||||
|
||||
empty_stack[0] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 1);
|
||||
empty_stack[1] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 9);
|
||||
empty_stack[2] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 25);
|
||||
empty_stack[3] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 33);
|
||||
empty_stack[4] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 41);
|
||||
empty_stack[5] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 49);
|
||||
empty_stack[6] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 1);
|
||||
empty_stack[7] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 9);
|
||||
empty_stack[8] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 17);
|
||||
empty_stack[9] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 25);
|
||||
empty_stack[10] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 33);
|
||||
empty_stack[11] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 41);
|
||||
empty_stack[12] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 49);
|
||||
|
||||
for (int i = 0; i < EMPTY_STACKS_NUMBER; i++) {
|
||||
box(empty_stack[i], 0, 0);
|
||||
wrefresh(empty_stack[i]);
|
||||
delwin(empty_stack[i]);
|
||||
}
|
||||
|
||||
free(empty_stack);
|
||||
|
||||
return;
|
||||
}
|
||||
#include "game.h"
|
||||
|
||||
char *card_suit(enum suit suit) {
|
||||
char *card_suit;
|
||||
@ -140,6 +45,36 @@ char *card_value(enum value value) {
|
||||
return(card_value);
|
||||
}
|
||||
|
||||
void draw_empty_stacks() {
|
||||
WINDOW **empty_stack;
|
||||
|
||||
empty_stack = malloc(EMPTY_STACKS_NUMBER * sizeof(**empty_stack));
|
||||
|
||||
empty_stack[0] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 1);
|
||||
empty_stack[1] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 9);
|
||||
empty_stack[2] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 25);
|
||||
empty_stack[3] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 33);
|
||||
empty_stack[4] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 41);
|
||||
empty_stack[5] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 1, 49);
|
||||
empty_stack[6] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 1);
|
||||
empty_stack[7] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 9);
|
||||
empty_stack[8] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 17);
|
||||
empty_stack[9] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 25);
|
||||
empty_stack[10] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 33);
|
||||
empty_stack[11] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 41);
|
||||
empty_stack[12] = newwin(FRAME_HEIGHT, FRAME_WIDTH, 7, 49);
|
||||
|
||||
for (int i = 0; i < EMPTY_STACKS_NUMBER; i++) {
|
||||
box(empty_stack[i], 0, 0);
|
||||
wrefresh(empty_stack[i]);
|
||||
delwin(empty_stack[i]);
|
||||
}
|
||||
|
||||
free(empty_stack);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void draw_value(struct card *card) {
|
||||
mvwprintw(card->frame->shape, 0, 0, card_value(card->value));
|
||||
mvwprintw(card->frame->shape, 4,
|
||||
|
@ -1,11 +1,8 @@
|
||||
#ifndef DISPLAY_H
|
||||
#define DISPLAY_H
|
||||
|
||||
#include "card.h"
|
||||
#include "deck.h"
|
||||
|
||||
#define KEY_SPACEBAR ' '
|
||||
|
||||
#define EMPTY_STACKS_NUMBER 13
|
||||
|
||||
#define DIAMONDS_SYMBOL "\u2666"
|
||||
@ -17,13 +14,9 @@
|
||||
#define RED_ON_WHITE 2
|
||||
#define WHITE_ON_BLUE 3
|
||||
|
||||
void initialize_curses();
|
||||
void end_curses();
|
||||
void initialize_game();
|
||||
void end_game();
|
||||
void draw_empty_stacks();
|
||||
char *card_suit(enum suit);
|
||||
char *card_value(enum value);
|
||||
void draw_empty_stacks();
|
||||
void draw_value(struct card *);
|
||||
void draw_suit(struct card *);
|
||||
void draw_front(struct card *);
|
||||
|
229
lib/game.c
Normal file
229
lib/game.c
Normal file
@ -0,0 +1,229 @@
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <time.h>
|
||||
#include "display.h"
|
||||
#include "util.h"
|
||||
#include "game.h"
|
||||
|
||||
void set_stacks_coordinates(struct deck *deck) {
|
||||
set_frame(deck->stock->card->frame,
|
||||
STOCK_STARTING_Y,
|
||||
STOCK_STARTING_X);
|
||||
set_frame(deck->waste_pile->card->frame,
|
||||
WASTE_PILE_STARTING_Y,
|
||||
WASTE_PILE_STARTING_X);
|
||||
set_frame(deck->foundation_0->card->frame,
|
||||
FOUNDATION_0_STARTING_Y,
|
||||
FOUNDATION_0_STARTING_X);
|
||||
set_frame(deck->foundation_1->card->frame,
|
||||
FOUNDATION_1_STARTING_Y,
|
||||
FOUNDATION_1_STARTING_X);
|
||||
set_frame(deck->foundation_2->card->frame,
|
||||
FOUNDATION_2_STARTING_Y,
|
||||
FOUNDATION_2_STARTING_X);
|
||||
set_frame(deck->foundation_3->card->frame,
|
||||
FOUNDATION_3_STARTING_Y,
|
||||
FOUNDATION_3_STARTING_X);
|
||||
set_frame(deck->maneuvre_0->card->frame,
|
||||
MANEUVRE_0_STARTING_Y,
|
||||
MANEUVRE_0_STARTING_X);
|
||||
set_frame(deck->maneuvre_1->card->frame,
|
||||
MANEUVRE_1_STARTING_Y,
|
||||
MANEUVRE_1_STARTING_X);
|
||||
set_frame(deck->maneuvre_2->card->frame,
|
||||
MANEUVRE_2_STARTING_Y,
|
||||
MANEUVRE_2_STARTING_X);
|
||||
set_frame(deck->maneuvre_3->card->frame,
|
||||
MANEUVRE_3_STARTING_Y,
|
||||
MANEUVRE_3_STARTING_X);
|
||||
set_frame(deck->maneuvre_4->card->frame,
|
||||
MANEUVRE_4_STARTING_Y,
|
||||
MANEUVRE_4_STARTING_X);
|
||||
set_frame(deck->maneuvre_5->card->frame,
|
||||
MANEUVRE_5_STARTING_Y,
|
||||
MANEUVRE_5_STARTING_X);
|
||||
set_frame(deck->maneuvre_6->card->frame,
|
||||
MANEUVRE_6_STARTING_Y,
|
||||
MANEUVRE_6_STARTING_X);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void fill_deck(struct deck *deck) {
|
||||
struct card *card[NUMBER_OF_CARDS];
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||
allocate_card(&(card[i]));
|
||||
}
|
||||
set_card(card[0], TWO, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[1], TWO, SPADES, COVERED, 1, 1);
|
||||
set_card(card[2], TWO, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[3], TWO, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[4], THREE, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[5], THREE, SPADES, COVERED, 1, 1);
|
||||
set_card(card[6], THREE, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[7], THREE, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[8], FOUR, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[9], FOUR, SPADES, COVERED, 1, 1);
|
||||
set_card(card[10], FOUR, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[11], FOUR, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[12], FIVE, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[13], FIVE, SPADES, COVERED, 1, 1);
|
||||
set_card(card[14], FIVE, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[15], FIVE, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[16], SIX, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[17], SIX, SPADES, COVERED, 1, 1);
|
||||
set_card(card[18], SIX, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[19], SIX, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[20], SEVEN, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[21], SEVEN, SPADES, COVERED, 1, 1);
|
||||
set_card(card[22], SEVEN, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[23], SEVEN, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[24], EIGHT, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[25], EIGHT, SPADES, COVERED, 1, 1);
|
||||
set_card(card[26], EIGHT, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[27], EIGHT, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[28], NINE, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[29], NINE, SPADES, COVERED, 1, 1);
|
||||
set_card(card[30], NINE, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[31], NINE, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[32], TEN, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[33], TEN, SPADES, COVERED, 1, 1);
|
||||
set_card(card[34], TEN, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[35], TEN, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[36], JACK, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[37], JACK, SPADES, COVERED, 1, 1);
|
||||
set_card(card[38], JACK, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[39], JACK, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[40], QUEEN, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[41], QUEEN, SPADES, COVERED, 1, 1);
|
||||
set_card(card[42], QUEEN, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[43], QUEEN, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[44], KING, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[45], KING, SPADES, COVERED, 1, 1);
|
||||
set_card(card[46], KING, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[47], KING, CLUBS, COVERED, 1, 1);
|
||||
set_card(card[48], ACE, DIAMONDS, COVERED, 1, 1);
|
||||
set_card(card[49], ACE, SPADES, COVERED, 1, 1);
|
||||
set_card(card[50], ACE, HEARTS, COVERED, 1, 1);
|
||||
set_card(card[51], ACE, CLUBS, COVERED, 1, 1);
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||
push(&(deck->stock), card[i]);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void shuffle_deck(struct deck *deck) {
|
||||
struct stack **stack = NULL;
|
||||
struct stack tmp;
|
||||
int random;
|
||||
|
||||
stack = malloc(NUMBER_OF_CARDS * sizeof(*stack));
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||
stack[i] = pop(&(deck->stock));
|
||||
}
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS - 1; i++) {
|
||||
random = i + (rand() % (NUMBER_OF_CARDS) - i);
|
||||
tmp = (*stack[i]);
|
||||
(*stack[i]) = (*stack[random]);
|
||||
(*stack[random]) = tmp;
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
|
||||
push(&(deck->stock), stack[i]->card);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void deal_cards(struct deck *deck) {
|
||||
move_card(&(deck->stock), &(deck->maneuvre_0));
|
||||
expose_card(deck->maneuvre_0->card);
|
||||
move_card(&(deck->stock), &(deck->maneuvre_1));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_2));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_3));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_4));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_5));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_6));
|
||||
|
||||
move_card(&(deck->stock), &(deck->maneuvre_1));
|
||||
expose_card(deck->maneuvre_1->card);
|
||||
move_card(&(deck->stock), &(deck->maneuvre_2));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_3));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_4));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_5));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_6));
|
||||
|
||||
move_card(&(deck->stock), &(deck->maneuvre_2));
|
||||
expose_card(deck->maneuvre_2->card);
|
||||
move_card(&(deck->stock), &(deck->maneuvre_3));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_4));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_5));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_6));
|
||||
|
||||
move_card(&(deck->stock), &(deck->maneuvre_3));
|
||||
expose_card(deck->maneuvre_3->card);
|
||||
move_card(&(deck->stock), &(deck->maneuvre_4));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_5));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_6));
|
||||
|
||||
move_card(&(deck->stock), &(deck->maneuvre_4));
|
||||
expose_card(deck->maneuvre_4->card);
|
||||
move_card(&(deck->stock), &(deck->maneuvre_5));
|
||||
move_card(&(deck->stock), &(deck->maneuvre_6));
|
||||
|
||||
move_card(&(deck->stock), &(deck->maneuvre_5));
|
||||
expose_card(deck->maneuvre_5->card);
|
||||
move_card(&(deck->stock), &(deck->maneuvre_6));
|
||||
|
||||
move_card(&(deck->stock), &(deck->maneuvre_6));
|
||||
expose_card(deck->maneuvre_6->card);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void initialize_game() {
|
||||
struct deck *deck = NULL;
|
||||
int pressed_key;
|
||||
|
||||
mvprintw(11, 27, "Welcome to tty-solitaire.");
|
||||
mvprintw(12, 19, "Press the space bar to play or q to quit.");
|
||||
while (1) {
|
||||
switch (pressed_key = getch()) {
|
||||
case KEY_SPACEBAR:
|
||||
clear();
|
||||
refresh();
|
||||
assume_default_colors(COLOR_WHITE, COLOR_GREEN);
|
||||
draw_empty_stacks();
|
||||
allocate_deck(&deck);
|
||||
initialize_deck(deck);
|
||||
set_stacks_coordinates(deck);
|
||||
fill_deck(deck);
|
||||
shuffle_deck(deck);
|
||||
deal_cards(deck);
|
||||
draw_game(deck);
|
||||
getchar();
|
||||
end_curses();
|
||||
end_game(deck);
|
||||
return;
|
||||
case 'q':
|
||||
case 'Q':
|
||||
end_curses();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void end_game(struct deck *deck) {
|
||||
delete_deck(deck);
|
||||
|
||||
return;
|
||||
}
|
44
lib/game.h
Normal file
44
lib/game.h
Normal file
@ -0,0 +1,44 @@
|
||||
#ifndef GAME_H
|
||||
#define GAME_H
|
||||
|
||||
#include "deck.h"
|
||||
|
||||
#define KEY_SPACEBAR ' '
|
||||
|
||||
#define NUMBER_OF_CARDS 52
|
||||
|
||||
#define STOCK_STARTING_X 1
|
||||
#define STOCK_STARTING_Y 1
|
||||
#define WASTE_PILE_STARTING_X 9
|
||||
#define WASTE_PILE_STARTING_Y 1
|
||||
#define FOUNDATION_0_STARTING_X 25
|
||||
#define FOUNDATION_0_STARTING_Y 1
|
||||
#define FOUNDATION_1_STARTING_X 33
|
||||
#define FOUNDATION_1_STARTING_Y 1
|
||||
#define FOUNDATION_2_STARTING_X 41
|
||||
#define FOUNDATION_2_STARTING_Y 1
|
||||
#define FOUNDATION_3_STARTING_X 49
|
||||
#define FOUNDATION_3_STARTING_Y 1
|
||||
#define MANEUVRE_0_STARTING_X 1
|
||||
#define MANEUVRE_0_STARTING_Y 7
|
||||
#define MANEUVRE_1_STARTING_X 9
|
||||
#define MANEUVRE_1_STARTING_Y 7
|
||||
#define MANEUVRE_2_STARTING_X 17
|
||||
#define MANEUVRE_2_STARTING_Y 7
|
||||
#define MANEUVRE_3_STARTING_X 25
|
||||
#define MANEUVRE_3_STARTING_Y 7
|
||||
#define MANEUVRE_4_STARTING_X 33
|
||||
#define MANEUVRE_4_STARTING_Y 7
|
||||
#define MANEUVRE_5_STARTING_X 41
|
||||
#define MANEUVRE_5_STARTING_Y 7
|
||||
#define MANEUVRE_6_STARTING_X 49
|
||||
#define MANEUVRE_6_STARTING_Y 7
|
||||
|
||||
void set_stacks_coordinates(struct deck *);
|
||||
void fill_deck(struct deck *);
|
||||
void shuffle_deck(struct deck *);
|
||||
void deal_cards(struct deck *);
|
||||
void initialize_game();
|
||||
void end_game();
|
||||
|
||||
#endif
|
25
lib/util.c
Normal file
25
lib/util.c
Normal file
@ -0,0 +1,25 @@
|
||||
#include <ncurses.h>
|
||||
#include <locale.h>
|
||||
|
||||
void initialize_curses() {
|
||||
setlocale(LC_ALL, ""); /* supporting unicode characters */
|
||||
initscr(); /* initialize the terminal in curses mode */
|
||||
raw(); /* disable line buffers */
|
||||
noecho(); /* character echo is unnecessary */
|
||||
keypad(stdscr, TRUE); /* enable F and arrow keys */
|
||||
start_color(); /* I want colors */
|
||||
curs_set(FALSE); /* invisible cursor */
|
||||
|
||||
init_pair(1, COLOR_BLACK, COLOR_WHITE);
|
||||
init_pair(2, COLOR_RED, COLOR_WHITE);
|
||||
init_pair(3, COLOR_WHITE, COLOR_BLUE);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void end_curses() {
|
||||
endwin();
|
||||
puts("Game finished.");
|
||||
|
||||
return;
|
||||
}
|
7
lib/util.h
Normal file
7
lib/util.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
void initialize_curses();
|
||||
void end_curses();
|
||||
|
||||
#endif
|
@ -1,7 +1,5 @@
|
||||
#include <ncurses.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include "../lib/display.h"
|
||||
#include "../lib/util.h"
|
||||
#include "../lib/game.h"
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
initialize_curses();
|
||||
|
Loading…
Reference in New Issue
Block a user