-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandMd.c
177 lines (147 loc) · 3.32 KB
/
randMd.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "struct.h"
#include "guilib.h"
#include "mapping.h"
#include "strlib.h"
#include "randMd.h"
#include "offline.h"
#include "labyrinthAPI.h"
/*! \file randMd.c
\brief Rand mode related functions.
\author Maeva Arlandis et Alexis Devillard
\version 6.2
\date 10 janvier 2017
*/
void randMode(Map *L)
//instructions of the random mode of the game
{
t_return_code ret = MOVE_OK; /* indicates the status of the previous move */
t_move* myMove=(t_move*) malloc(sizeof(t_move));
if (myMove==NULL) //test if the allocation is a success
exit(EXIT_FAILURE);
myMove->type = DO_NOTHING;
myMove->value = 0;
t_move* opMove=(t_move*) malloc(sizeof(t_move));
if (opMove==NULL) //test if the allocation is a success
exit(EXIT_FAILURE);
opMove->type = DO_NOTHING;
opMove->value = 0;
dispMap(L);
while(ret==MOVE_OK)
{
if (L->players[0]->turn==1)
{
if(L->offline==0)
{
addStr(L->infoP2[5]," ","");
ret = getMove(opMove);
movement(L,1,opMove);
}
else
{
getMoveOff(L,1,opMove);
ret = movement(L,1,opMove);
}
}
else
{
addStr(L->infoP1[5]," ","");
gene_randmove(L,myMove,0);
if(L->offline==0)
{
movement(L,0,myMove);
sendComment(L->comments[rand()%5]);
ret = sendMove(*myMove);
}
else
ret =movement(L,0,myMove);
}
//int ch = getch();
//endwin();
//printLabyrinth();
dispInfo(L);
dispMap(L);
dispPath(L);
}
if ((L->players[0]->turn==1 && ret == MOVE_WIN) || (L->players[0]->turn==0 && ret == MOVE_LOSE))
{
addStr(L->infoP1[4]," ","");
addStr(L->infoP2[6]," YOU LOOSE","");
addStr(L->infoP1[6]," YOU WIN","");
addStr(L->cases[L->heigth/2]," YOU WIN","");
}
else
{
addStr(L->infoP1[4]," ","");
addStr(L->infoP1[6]," YOU LOOSE","");
addStr(L->infoP2[6]," YOU WIN","");
addStr(L->cases[L->heigth/2]," YOU LOOSE","");
}
if(L->offline==0&&L->players[1]->mode!=1)
/* end the connection, because we are polite */
closeConnection();
free(myMove);
free(opMove);
}
void gene_randmove(Map *L,t_move *move,int P)
{
int type,t=-1;
while(t==-1)
{
type=rand()%9;
if (type==0 || type==1) //row line
{
convert_movetype(type,move);
move->value=rand()%L->heigth;
t=testMoveM(L,P);
}
else if (type==2 || type==3) //row column
{
convert_movetype(type,move);
move->value=rand()%L->width;
t=testMoveM(L,P);
}
else //basic move of the player
{
convert_movetype(type,move);
t=testMoveP(L,P,move);
}
}
}
void convert_movetype(int type, t_move *move)
//convert an int, for example generate by rand, to a t_move type
{
switch (type)
{
case 0:
move->type=ROTATE_LINE_LEFT;
break;
case 1:
move->type= ROTATE_LINE_RIGHT;
break;
case 2:
move->type=ROTATE_COLUMN_UP;
break;
case 3:
move->type=ROTATE_COLUMN_DOWN;
break;
case 4:
move->type=MOVE_LEFT;
break;
case 5:
move->type=MOVE_RIGHT;
break;
case 6:
move->type=MOVE_UP;
break;
case 7:
move->type=MOVE_DOWN;
break;
case 8:
move->type=DO_NOTHING;
break;
}
}