summaryrefslogtreecommitdiff
path: root/src/game.c
blob: 7bca6e6e998d1de758b2b5d3bb5605a8f40050c3 (plain)
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
#include "game.h"
#include "card.h"
#include "hand.h"
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>
#include <curses.h>
#include "ui.h"

void init_mainstack(card *stack, const uint32_t size)
{
	assert(stack != NULL);

	// assign card values to main stack
	for(uint32_t i=0, val=MIN_CARD; i<size; i++, val++)
		stack[i] = val;

	// shuffle stack
	for(uint32_t i=0; i<3*size; i++)
	{
		uint32_t x = rand() % size;
		uint32_t y = rand() % size;
		card tmp = stack[x];
		stack[x] = stack[y];
		stack[y] = tmp;
	}
}

void start_game(const bool servermode, const char *addr, const uint16_t port)
{
	assert(addr != NULL);

	bool running = true;
	int cards = MAX_CARD - MIN_CARD + 1;
	card mainstack[cards];
	enum gamestate state = STATE_DEALCARDS;

	srand(time(0));

	init_mainstack(mainstack, cards);

	// Example data set for table cards window
	const card_stack_t _cs1 = { { 1, 2, 3, 4, 101 } };
	const card_stack_t _cs2 = { { 6, 7, 53, 0, 0 } };
	const card_stack_t _cs3 = { { 11, 55, 0, 0, 0 } };
	const card_stack_t _cs4 = { { 17, 29, 36, 42, 0 } };
	const table_stacks_t ts = { { _cs1, _cs2, _cs3, _cs4 } };

	// The stack points window uses ts, too, so there is no separate data set

	// Example data set for current state window
	pnoc_t pnoc[10] = {
		{"$you", 10},
		{"1234567890", 23},
		{"baz", 38},
		{"foo_bar", 14},
		{"lolcat", 60},
		{"blablub123", 15},
		{"abcdefg", 103},
		{"hello", 98},
		{"hornoxe", 33},
		{"1337nick", 74}
	};
	pnoc_sort(pnoc, 10);
	const uint8_t num_players = 10;
	const uint32_t score = 10;

	// Example data set for hand cards window
	hand_t h = { {22, 0, 12, 85, 27, 69, 78, 0, 77, 0} };
	hand_sort(&h);

	// Display all windows
	ui_display_wnd_table_cards(&ts, false, 0);
	ui_display_wnd_stack_points(&ts, false, 0);
	ui_display_wnd_current_state(pnoc, num_players, 2, score);
	ui_display_wnd_hand_cards(&h, false, 0);

	// main game loop
	while(running)
	{
		/*
		switch(state)
		{
			case STATE_CLIENT_WAIT_CARDS:
				if(round == 1)
					receive_and_place_table_stacks();
				wait_for_cards_from_server();
				state = STATE_CLIENT_SELECT_OPEN_CARD;
				break;

			case STATE_SERVER_DEAL_CARDS:
				if(round == 1)
					send_and_place_table_stacks();
				deal_cards_to_clients();
				state = STATE_SERVER_WAIT_OPEN_CARDS;
				break;

			case STATE_CLIENT_SELECT_OPEN_CARD:
				select_open_card();
				send_open_card_to_server();
				state = STATE_CLIENT_WAIT_FOR_OPEN_CARDS;
				break;

			case STATE_SERVER_WAIT_FOR_OPEN_CARDS:
				receive_open_cards_from_clients();
				sort_open_card_list(); // in ascending order 
				send_open_card_list_to_clients();
				state = STATE_SERVER_PLAY_CARDS;
				break;

			case STATE_CLIENT_WAIT_FOR_OPEN_CARDS:
				receive_sorted_list_of_open_cards();
				state = STATE_CLIENT_PLAY_CARDS;
				break;

			case STATE_SERVER_PLAY_CARDS:
				foreach(open_card)
				{
					play_lowest_open_card
					{
						determine_stack_for_open_card
						{
							if (stack_has_to_be_picked) {
								receive_stack_from_client();
								send_received_stack_to_other_clients();
								clear_stack(stack_id);
							}
						}
						place_card()
						{
							if(count_stack_cards == 6)
								clear_stack(stack_id);
						}
					}

				}
				if (clients_have_hand_cards)
				{
					state = STATE_SERVER_WAIT_FOR_OPEN_CARDS;
				}
				else
				{
					if (main_stack_has_enough_cards_for_clients)
					{
						send_action_to_client(DEAL_CARDS);
						state = STATE_SERVER_DEAL_CARDS;
					}
					else
					{
						send_action_to_client(GAME_FINISHED);
						state = STATE_SERVER_GAME_FINISHED;
					}
				}
				break;

			case STATE_CLIENT_PLAY_CARDS:
				foreach(open_card)
				{
					play_lowest_open_card
					{
						determine_stack_for_open_card
						{
							if (stack_has_to_be_picked)
							{
								if (we_have_to_pick)
								{
									pick_stack();
									send_stack_to_server();
								}
								else // another client has to pick
								{
									receive_stack();
								}

								clear_stack(stack_id);
							}
						}
						place_card()
						{
							if(count_stack_cards == 6)
								clear_stack(stack_id);
						}
					}

				}
				if (we_have_hand_cards)
				{
					state = STATE_CLIENT_SELECT_OPEN_CARD;
				}
				else
				{
					receive_next_server_action();
					if (server_action == DEAL_CARDS)
					{
						state = STATE_CLIENT_WAIT_CARDS;
					}
					else if (server_action == GAME_FINISHED)
						state = STATE_CLIENT_GAME_FINISHED;
				}
				break;

			default:
				assert(false); // should never happen
		}
		*/
	}
}