-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.cpp
115 lines (97 loc) · 3.01 KB
/
play.cpp
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
#include <iostream>
#include "functions_definitions.h"
#include "conio.h"
#include "interfaces.h"
#include "board.h"
bool prepare_game(Player* player_1, Player* player_2, int& player_index, Board* table_s,
int& background_color, int& star_x, int& star_y, int& zn, int& zero)
{
if ((player_1->dice1 == 0 && player_1->dice2 == 0 && player_index == 1) ||
(player_2->dice1 == 0 && player_2->dice2 == 0 && player_index == 2))
{
// It means that player cannot make another moves
return true;
}
// Print table
print_table(table_s);
// Print interface for removed pawns
print_removed_pawns_interface(player_1, player_2);
// Set some visual attributes
textbackground(background_color);
// Put our star on the screen
gotoxy(star_x, star_y);
putch('*');
zero = 0;
zn = getch();
background_color = 0; // Reset background color
textbackground(background_color);
return false;
}
void game_loop(Player* player_1, Player* player_2, int& player_index, Board* table_s, int& background_color,
int& star_x, int& star_y, int& zero, int& zn, bool& inserting_element, bool& start_a_new_game, char& player_sign)
{
do
{
if (prepare_game(player_1, player_2, player_index, table_s, background_color, star_x, star_y, zn, zero))
break;
// Determine player pawn sign ("R" or "B")
player_sign = player_index == 1 ? player_1->pawn_char : player_2->pawn_char;
move(table_s, zn, star_x, star_y, background_color,
zero, inserting_element, player_sign, player_1, player_2);
// After making some move check if the player won the game
if (player_index == 1)
{
if (check_if_player_won(player_1))
{
start_a_new_game = true;
return;
}
}
else
{
if (check_if_player_won(player_2))
{
start_a_new_game = true;
return;
}
}
} while (zn != 'F' && zn != 'Q');
clrscr();
}
void di(Player* player_1, Player* player_2, Board* table_s, int& player_index, int& dice1, int& dice2, char& player_sign)
{
get_random_numbers(&dice1, &dice2);
int dice_sum = dice1 + dice2;
// Update dice values
if (player_index == 1)
{
player_1->dice1 = dice1;
player_1->dice2 = dice2;
}
else
{
player_2->dice1 = dice1;
player_2->dice2 = dice2;
}
// Print the entire interface for player
print_player_interface(player_index, player_1, player_2);
}
bool play(Player* player_1, Player* player_2, Board* table_s, int player_index, bool& start_a_new_game)
{
textcolor(LIGHTGRAY);
// Generate two random numbers and display them
int dice1, dice2;
char player_sign;
di(player_1, player_2, table_s, player_index, dice1, dice2, player_sign);
int zn = 0, background_color = 0, zero = 0;
// Initialize start coordinates for our '*' pointer, roughly in the middle of the board
int star_x = 55, star_y = 16;
// Print the top interface
print_top_interface(player_1, player_2, true);
bool inserting_element = false;
game_loop(player_1, player_2, player_index, table_s, background_color, star_x, star_y,
zero, zn, inserting_element, start_a_new_game, player_sign);
// Stop playing the game
if (zn == 'Q') return true;
return false;
}