/
ncurses_func.c
253 lines (252 loc) · 6.14 KB
/
ncurses_func.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*ncurses_func.c - This file contains functions related to the graphics part of the program*/
/*****************************************************************************
* Copyright (C) Nakul 'Wenger' Ramakumar. nakulrk95@gmail.com
* College: COEP Batch- Comp 2017 MIS No.- 111303032
* This program was created for the Data Structures and Algorithms course
*
* DICLAIMER:- I do not promote any form of gambling or betting through this
* program.
*
* This program is a game of Blackjack.
* It supports multiple users.
* This is not a gambling or betting application.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include "linkedlist.h"
#include <ncurses.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "ncfunc.h"
#include "blackjack.h"
#include "admin.h"
#define WIDTH 30
#define HEIGHT 10
int startx = 0;
int starty = 0;
char *choices[] = {
"How to Play",
"Login",
"Create Accout",
"Credits",
"Admin login",
"Exit",
};
char *choices2[] = {
"How to Play",
"Play Game",
"Buy Chips",
"Sell Chips",
"Check Balance",
"Change info",
"Logout",
};
int n_choices = sizeof(choices) / sizeof(char *);
int n_choices2 = sizeof(choices2) / sizeof(char *);
/*Description - Prints the main menu in a new window using ncurses library*/
void menu() {
system("clear");
WINDOW *menu_win;
int highlight = 1;
int choice = 0;
int c;
initscr();
clear();
noecho();
cbreak(); /* Line buffering disabled. pass on everything */
startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2;
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
keypad(menu_win, TRUE);
mvprintw(0, 20, "***************BLACKJACK****************");
mvprintw(2, 7, "Use arrow keys to go up and down, Press enter to select a choice");
refresh();
print_menu(menu_win, highlight);
while(1)
{ c = wgetch(menu_win);
switch(c)
{ case KEY_UP:
if(highlight == 1)
highlight = n_choices;
else
--highlight;
break;
case KEY_DOWN:
if(highlight == n_choices)
highlight = 1;
else
++highlight;
break;
case 10:
choice = highlight;
break;
default:
mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c);
refresh();
break;
}
print_menu(menu_win, highlight);
if(choice != 0) /* User did a choice come out of the infinite loop */
break;
}
clrtoeol();
endwin();
switch(choice) {
case 1: /*Guide*/
guide();
menu();
break;
case 2: /*Login*/
login();
break;
case 3: /*New account*/
new_user();
menu();
break;
case 4: /*Credits*/
credits();
menu();
break;
case 5: /*Admin*/
admin_login();
break;
case 6: /*exit*/
exit(0);
break;
}
}
/*Description - Prints the menu tht appears after login in a new window using ncurses library*/
void login_menu() {
system("clear");
WINDOW *menu_win;
int highlight = 1;
int choice = 0;
int c;
initscr();
clear();
noecho();
cbreak(); /* Line buffering disabled. pass on everything */
startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2;
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
keypad(menu_win, TRUE);
mvprintw(0, 20, "***************WELCOME****************");
mvprintw(2, 7, "Use arrow keys to go up and down, Press enter to select a choice");
refresh();
print_menu2(menu_win, highlight);
while(1)
{ c = wgetch(menu_win);
switch(c)
{ case KEY_UP:
if(highlight == 1)
highlight = n_choices2;
else
--highlight;
break;
case KEY_DOWN:
if(highlight == n_choices2)
highlight = 1;
else
++highlight;
break;
case 10:
choice = highlight;
break;
default:
mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c);
refresh();
break;
}
print_menu2(menu_win, highlight);
if(choice != 0) /* User did a choice come out of the infinite loop */
break;
}
clrtoeol();
endwin();
switch(choice) {
case 1: /*Guide*/
guide();
login_menu();
break;
case 2: /*Play Game*/
play_game();
break;
case 3: /*Buy*/
buy_chips();
login_menu();
break;
case 4: /*Sell*/
sell_chips();
login_menu();
break;
case 5: /*Check bal*/
disp_bal();
login_menu();
break;
case 6: /*Change_info*/
change_info();
login_menu();
break;
case 7: /*Log out*/
logout();
menu();
break;
default:
login_menu();
break;
}
}
/* Input - WINDOW pointer, integer highlight
* Description - Prints the box and the contents in the login menu*/
void print_menu2(WINDOW *menu_win, int highlight) {
int x, y, i;
x = 2;
y = 2;
box(menu_win, 0, 0);
for(i = 0; i < n_choices2; ++i) {
if(highlight == i + 1) { /* High light the present choice */
wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices2[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", choices2[i]);
++y;
}
wrefresh(menu_win);
}
/* Input - WINDOW pointer, integer highlight
* Description - Prints the box and the contents in the main menu*/
void print_menu(WINDOW *menu_win, int highlight) {
int x, y, i;
x = 2;
y = 2;
box(menu_win, 0, 0);
for(i = 0; i < n_choices; ++i) {
if(highlight == i + 1) { /* High light the present choice */
wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", choices[i]);
++y;
}
wrefresh(menu_win);
}