Broke some functions into smaller pieces, added the initial keyboard interface.
This commit is contained in:
parent
82d9ee9cb4
commit
67f4775dc4
47
lib/game.c
47
lib/game.c
@ -188,36 +188,31 @@ void deal_cards(struct deck *deck) {
|
||||
return;
|
||||
}
|
||||
|
||||
void initialize_game() {
|
||||
struct deck *deck = NULL;
|
||||
int pressed_key;
|
||||
|
||||
void greet_player() {
|
||||
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);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void prepare_game(struct deck **deck) {
|
||||
draw_empty_stacks();
|
||||
allocate_deck(&deck);
|
||||
initialize_deck(deck);
|
||||
set_stacks_coordinates(deck);
|
||||
fill_deck(deck);
|
||||
shuffle_deck(deck);
|
||||
deal_cards(deck);
|
||||
allocate_deck(deck);
|
||||
initialize_deck(*deck);
|
||||
set_stacks_coordinates(*deck);
|
||||
fill_deck(*deck);
|
||||
shuffle_deck(*deck);
|
||||
deal_cards(*deck);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void initialize_game() {
|
||||
struct deck *deck = NULL;
|
||||
|
||||
clear_screen();
|
||||
prepare_game(&deck);
|
||||
draw_game(deck);
|
||||
getchar();
|
||||
end_curses();
|
||||
end_game(deck);
|
||||
return;
|
||||
case 'q':
|
||||
case 'Q':
|
||||
end_curses();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -3,8 +3,6 @@
|
||||
|
||||
#include "deck.h"
|
||||
|
||||
#define KEY_SPACEBAR ' '
|
||||
|
||||
#define NUMBER_OF_CARDS 52
|
||||
|
||||
#define STOCK_STARTING_X 1
|
||||
@ -38,7 +36,9 @@ void set_stacks_coordinates(struct deck *);
|
||||
void fill_deck(struct deck *);
|
||||
void shuffle_deck(struct deck *);
|
||||
void deal_cards(struct deck *);
|
||||
void greet_player();
|
||||
void initialize_game();
|
||||
void prepare_game(struct deck **);
|
||||
void end_game();
|
||||
|
||||
#endif
|
||||
|
10
lib/keyboard.c
Normal file
10
lib/keyboard.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include "keyboard.h"
|
||||
|
||||
int key_event() {
|
||||
int pressed_key;
|
||||
|
||||
pressed_key = getchar();
|
||||
|
||||
return(pressed_key);
|
||||
}
|
8
lib/keyboard.h
Normal file
8
lib/keyboard.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef KEYBOARD_H
|
||||
#define KEYBOARD_H
|
||||
|
||||
#define KEY_SPACEBAR ' '
|
||||
|
||||
int key_event();
|
||||
|
||||
#endif
|
@ -9,6 +9,7 @@ void initialize_curses() {
|
||||
keypad(stdscr, TRUE); /* enable F and arrow keys */
|
||||
start_color(); /* I want colors */
|
||||
curs_set(FALSE); /* invisible cursor */
|
||||
assume_default_colors(COLOR_WHITE, COLOR_GREEN);
|
||||
|
||||
init_pair(1, COLOR_BLACK, COLOR_WHITE);
|
||||
init_pair(2, COLOR_RED, COLOR_WHITE);
|
||||
@ -23,3 +24,10 @@ void end_curses() {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void clear_screen() {
|
||||
clear();
|
||||
refresh();
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -3,5 +3,6 @@
|
||||
|
||||
void initialize_curses();
|
||||
void end_curses();
|
||||
void clear_screen();
|
||||
|
||||
#endif
|
||||
|
@ -1,10 +1,30 @@
|
||||
#include <stdlib.h>
|
||||
#include "../lib/util.h"
|
||||
#include "../lib/game.h"
|
||||
#include "../lib/keyboard.h"
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
int option;
|
||||
|
||||
initialize_curses();
|
||||
|
||||
greet_player();
|
||||
|
||||
while (1) {
|
||||
switch (option = getch()) {
|
||||
case KEY_SPACEBAR:
|
||||
initialize_game();
|
||||
break;
|
||||
case 'q':
|
||||
case 'Q':
|
||||
end_curses();
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
while (1) {
|
||||
key_event();
|
||||
}
|
||||
|
||||
return(0);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user