-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_table_from_file.cpp
More file actions
109 lines (95 loc) · 2.9 KB
/
Copy pathload_table_from_file.cpp
File metadata and controls
109 lines (95 loc) · 2.9 KB
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
#include "functions_definitions.h"
#include "conio.h"
#include "board.h"
#include <stdio.h>
#include <iostream>
void br_case(char character, int first_value, int second_value, Board* table)
{
if (character == 'B' || character == 'R')
{
table->pawns[first_value][second_value] = character;
}
}
void p_case(char character, int first_value, int second_value, Player* player_1, Player* player_2)
{
// Set the points for players
if (character == 'P')
{
player_1->points = first_value;
player_2->points = second_value;
}
}
void d_case(char character, int first_value, int second_value, Player* player_1, Player* player_2)
{
if (character == 'D')
{
player_1->number_of_removed_pawns = first_value;
player_2->number_of_removed_pawns = second_value;
// Set removed pawns for first player
for (size_t i = 0; i < first_value; ++i)
{
player_1->removed_pawns[i] = 'B';
}
// Set removed pawns for second player
for (size_t i = 0; i < first_value; ++i)
{
player_2->removed_pawns[i] = 'R';
}
}
}
void a_case(char character, int first_value, int second_value, Board* table)
{
if (character == 'A')
{
// Set pawns on bar for first player
for (size_t i = 0; i < first_value; ++i)
{
table->player_1_bar[i] = 'B';
}
// Set pawns on bar for second player
for (size_t i = 0; i < second_value; ++i)
{
table->player_2_bar[i] = 'R';
}
}
}
void process(Board* table, char character, int first_value, int second_value, Player* player_1, Player* player_2)
{
br_case(character, first_value, second_value, table);
p_case(character, first_value, second_value, player_1, player_2);
d_case(character, first_value, second_value, player_1, player_2);
a_case(character, first_value, second_value, table);
}
void clear(Board* table)
{
// We need to clear all the pawns that are initialized by default
for (size_t i = 0; i < N_COLUMNS; ++i)
{
for (size_t j = 0; j < NUMBER_OF_ROWS_IN_COLUMN; ++j)
{
table->pawns[i][j] = 'E'; // E means empty
}
}
}
Board* load_table_from_file(Player* player_1, Player* player_2)
{
gotoxy(1, 1);
Board* table = new Board();
initialize_table(table);
clear(table);
FILE* file;
if (fopen_s(&file, "game_to_load.txt", "r") != 0) {
printf("Error opening the file.\n");
return table;
}
unsigned int first_value, second_value;
char character;
// Read and process the content of the file
while (fscanf_s(file, " %c %d %d", &character, sizeof(character), &first_value, &second_value) == 3) {
// Set the pawns on the board
process(table, character, first_value, second_value, player_1, player_2);
}
// Close the file
fclose(file);
return table;
}