summaryrefslogtreecommitdiff
path: root/noncore/games/backgammon/moveengine.cpp
blob: a4145cc2d39439f53def8e2617bc6062e89c959b (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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#include "moveengine.h"

#include <qtimer.h>
#include <stdlib.h>

MoveEngine::MoveEngine()
        : QObject()
{
    int offset=7;
    int a=0; //counter variable
    int xfill[]={210,185,170,155,140,125,110,85,70,55,40,25,10,10,25,40,55,70,85,110,125,140,155,170,185,210};
    for(a=0;a<26;a++)
    {
        x_coord[a]=xfill[a];
    }

    int yfill[]={10,25,40,55,70,10+offset,25+offset,40+offset,55+offset,25,40,55, 25+offset,40+offset,40};
    int zfill[]={1,1,1,1,1,2,2,2,2,3,3,3,4,4,5};
    for(a=0;a<15;a++)
    {
        yup_coord[a]=yfill[a];
        ylow_coord[a]=185-(yfill[a]);
        z_coord[a]=zfill[a];
    }
    for(a=0;a<5;a++)
    {
        if(a<3)
        {
            x_fin1[a]=65+a*15;
            x_fin2[a]=155-a*15;
        }
        y_fin[a]=225-a*5;
    }
    z_fin=1;

    reset();
}

MoveEngine::~MoveEngine()
{}

void MoveEngine::position(Pieces& pieces,bool non_qte)
{
    int player1_counter=0;
    int player2_counter=0;

    //non qte styles are smaller !!
    int offset=(non_qte) ? 5 : 0;

    for(int a=0;a<28;a++)
    {
        for(int b=0;b<abs(population[a].total);b++)
        {
            if(population[a].total>0) //player 1 pieces
            {
                pieces.player1[player1_counter].x=x_coord[a]-offset;
                if(a>=0 && a<13)
                {
                    pieces.player1[player1_counter].y=yup_coord[b]-offset;
                    pieces.player1[player1_counter].z=z_coord[b];
                    pieces.player1[player1_counter].side=false;
                    player1_counter++;
                }
                else if(a>12 && a<26)
                {
                    pieces.player1[player1_counter].y=ylow_coord[b]-offset;
                    pieces.player1[player1_counter].z=z_coord[b];
                    pieces.player1[player1_counter].side=false;
                    player1_counter++;
                }
                else if(a==26)
                {
                    if(b<5)
                    {
                        pieces.player1[player1_counter].x=x_fin1[0]-offset;
                        pieces.player1[player1_counter].y=y_fin[b]-offset;
                        pieces.player1[player1_counter].z=z_fin;
                    }
                    else if(b>=5 && b<10)
                    {
                        pieces.player1[player1_counter].x=x_fin1[1]-offset;
                        pieces.player1[player1_counter].y=y_fin[b-5]-offset;
                        pieces.player1[player1_counter].z=z_fin;
                    }
                    else
                    {
                        pieces.player1[player1_counter].x=x_fin1[2]-offset;
                        pieces.player1[player1_counter].y=y_fin[b-10]-offset;
                        pieces.player1[player1_counter].z=z_fin;
                    }
                    pieces.player1[player1_counter].side=true;
                    player1_counter++;

                }
            }

            else if(population[a].total<0) //player 2 pieces
            {
                pieces.player2[player2_counter].x=x_coord[a]-offset;
                if(a>=0 && a<13)
                {
                    pieces.player2[player2_counter].y=yup_coord[b]-offset;
                    pieces.player2[player2_counter].z=z_coord[b];
                    pieces.player2[player2_counter].side=false;
                    player2_counter++;
                }
                else if(a>12 && a<26)
                {
                    pieces.player2[player2_counter].y=ylow_coord[b]-offset;
                    pieces.player2[player2_counter].z=z_coord[b];
                    pieces.player2[player2_counter].side=false;
                    player2_counter++;
                }
                else if(a==27)
                {
                    if(b<5)
                    {
                        pieces.player2[player2_counter].x=x_fin2[0]-offset;
                        pieces.player2[player2_counter].y=y_fin[b]-offset;
                        pieces.player2[player2_counter].z=z_fin;
                    }
                    else if(b>=5 && b<10)
                    {
                        pieces.player2[player2_counter].x=x_fin2[1]-offset;
                        pieces.player2[player2_counter].y=y_fin[b-5]-offset;
                        pieces.player2[player2_counter].z=z_fin;
                    }
                    else
                    {
                        pieces.player2[player2_counter].x=x_fin2[2]-offset;
                        pieces.player2[player2_counter].y=y_fin[b-10]-offset;
                        pieces.player2[player2_counter].z=z_fin;
                    }
                    pieces.player2[player2_counter].side=true;
                    player2_counter++;

                }
            }
        }
    }
}

void MoveEngine::diceroll(const int& newplayer,const int& face1,const int& face2,const int& face3,const int& face4,bool computer)
{
    checkstate();
    player=newplayer;
    otherplayer=(player==1) ? 2 : 1;
    dice[0]=face1;
    dice[1]=face2;
    dice[2]=face3;
    dice[3]=face4;
    marker_current=-1;
    if(getPossibleMoves()==0)
    {
        emit nomove();
        return; // player will be changed
    }
    if(!computer)
        return; //human intervention required

    QTimer::singleShot(2000,this,SLOT(automove()));
}


void MoveEngine::automove()
{
    //the maximimum possibility
    int maxpos=0;
    //the position in the moves array
    int from=-1;
    int to=-1;
    //dice1 or dice 2 ??
    int index_dice=0;
    for(int counter=0;counter<26;counter++)
    {
        int a=(player==1) ? counter : 25-counter;
        for(int b=0;b<4;b++)
        {
            if(moves[a].weight[b]>maxpos)
            {
                maxpos=moves[a].weight[b];
                from=a;
                to=moves[a].to[b];
                index_dice=b+1;
            }
        }
    }
    move(from,to,index_dice);
}


void MoveEngine::boardpressed(const int& x,const int& y,Marker& marker,bool non_qte)
{
    //get the position of the mouse click
    bool upper=true;
    bool found=false;

    int offset=(non_qte) ? 5 : 0;

    if(y<=85) // board slots 0 to 12
        marker.y_current=0;
    else if(y>=105) //board slots 13 to 25
    {
        marker.y_current=195-2*offset;
        upper=false;
    }

    int index=13; // the clicked board slot

    while(index<25 && !found)
    {
        if(x>=x_coord[index] && x<x_coord[index+1])
        {
            marker.x_current=x_coord[index];
            found=true;
            ;
        }
        else
        {
            index++;
        }
    }
    if(!found)
    {
        marker.x_current=x_coord[25];
        index=25;
    }
    if(upper)
    {
        index=25-index;
    }

    int a=0;
    int usedice=-1;
    int dice_value=7;
    for(a=0;a<4;a++)
    {
        if(index==marker_next[a] && marker_next[a]!=-1 && dice_value>dice[a])
        {
          usedice=a;
          dice_value=dice[0];
         }        
    }  
    if(usedice!=-1)
    {
        move(marker_current,marker_next[usedice],usedice+1);
        nomarker(marker);
        return;

    }


    if(dice[0]==7 && dice[1]==7 && dice[2]==7 && dice[3]==7) //no dice rolled
    {
        nomarker(marker);
        return;
    }
    else if(fieldColor(index)==player)
    {
        marker.visible_current=true;
        marker_current=index;
    }
    else
    {
        nomarker(marker);
        return;
    }

    for(a=0;a<4;a++)
    {
        if(moves[index].weight[a]>0)
        {
            int nextfield=moves[index].to[a];
            marker.x_next[a]=x_coord[nextfield];
            marker_next[a]=nextfield;
            if(nextfield<13) //upper half
                marker.y_next[a]=0;
            else //lower half
                marker.y_next[a]=195-2*offset;
            marker.visible_next[a]=true;
        }
        else
        {
            marker.x_next[a]=0;
            marker.y_next[a]=0;
            marker_next[a]=-1;
            marker.visible_next[a]=false;
        }
    }
    return;
}

void MoveEngine::reset()
{
    int a=0;
    for(a=0;a<28;a++)
    {
        population[a].total=0;
    }

    int p1_index[]={1,1,12,12,12,12,12,17,17,17,19,19,19,19,19};
    int p2_index[]={24,24,13,13,13,13,13,8,8,8,6,6,6,6,6};
    //int p1_index[]={19,20,21,22,22,23,23,18,18,23,24,24,24,24,24};
    //int p2_index[]={6,5,4,3,3,2,2,2,2,2,1,7,7,1,1};
    for(a=0;a<15;a++)
    {
        population[p1_index[a]].total++;
        population[p2_index[a]].total--;
    }

    player=0;
    dice[0]=7;
    dice[1]=7;
    dice[2]=7;
    dice[3]=7;

    marker_current=-1;
    marker_next[0]=-1;
    marker_next[1]=-1;
    marker_next[2]=-1;
    marker_next[3]=-1;
    //allclear[0]==false;
    allclear[1]=false;
    allclear[2]=false;
    last_piece[1]=0;
    last_piece[2]=25;
}

void MoveEngine::loadGame(const LoadSave& load)
{
    for(int a=0;a<28;a++)
    {
        population[a].total=load.pop[a].total;
    }
    checkstate();
}

LoadSave MoveEngine::saveGame()
{
    LoadSave save;
    for(int a=0;a<28;a++)
    {
        save.pop[a].total=population[a].total;
    }
    return save;
}

AISettings MoveEngine::getAISettings()
{
    return ai;
}

void MoveEngine::setAISettings(const AISettings& new_ai)
{
    ai=new_ai;
}

void MoveEngine::setRules(Rules rules)
{
  move_with_pieces_out=rules.move_with_pieces_out;
  nice_dice=rules.generous_dice;
}


int MoveEngine::getPossibleMoves()
{
    int homezone[]={0,25,0};
    int lastToHomeZone=abs(last_piece[player]-homezone[player]);
    for(int field=0;field<26;field++)
    {
       
        for(int b=0;b<4;b++)
        {            
            int dice_tmp=dice[b];
            if(dice[b]!=7 && dice[b]> lastToHomeZone)
              dice_tmp=lastToHomeZone;

            int nextfield=(player==1) ? field+dice_tmp : field-dice_tmp;
           
            if(nice_dice)
            {
              if(player==1 && nextfield>homezone[1])
                nextfield=homezone[1];
              else if(player==2 && nextfield<homezone[2])
                nextfield=homezone[2];
            }

            moves[field].weight[b]=0;
            moves[field].to[b]=nextfield;

            int out_of_board[]={-1,0,25};
            if(!move_with_pieces_out && field!=out_of_board[player] && pieces_out[player])
            {
              continue;
            }
           

        
            if(dice[b]!=7 && fieldColor(field)==player )  //player can only move his own pieces
            {
                if((player==1 && nextfield > homezone[1]) || (player==2 && nextfield < homezone[2]))
                {
                  moves[field].weight[b]=0; //movement would be far out of board
                }
                else if(nextfield==homezone[player] && !allclear[player])
                {
                  moves[field].weight[b]=0; //can not rescue pieces until all are in the endzone
                }
                else if(nextfield==homezone[player] && allclear[player])
                {
                   moves[field].weight[b]=ai.rescue; //rescue your pieces : nuff said ;-)
                }
                else if(fieldColor(nextfield)==otherplayer)
                {
                    if(abs(population[nextfield].total)>1) //can not move to this field
                        moves[field].weight[b]=0;
                    else if(abs(population[nextfield].total)==1) //eliminate opponent : very nice
                        moves[field].weight[b]=ai.eliminate;
                }
                else if(fieldColor(nextfield)==player) //nextfield already occupied by player
                {
                    if(abs(population[field].total)==2) //expose own piece : not diserable
                        moves[field].weight[b]=ai.expose;
                    else if(abs(population[nextfield].total)>1) //own pices already there : safe
                        moves[field].weight[b]=ai.safe;
                    else if(abs(population[nextfield].total)==1) //protect own piece : most importatnt
                        moves[field].weight[b]=ai.protect;
                }
                else if(population[nextfield].total==0) //nextfield empty
                {
                    if(abs(population[field].total)==2) //expose own piece : not diserable
                        moves[field].weight[b]=ai.expose;
                    else
                        moves[field].weight[b]=ai.empty;
                }
                else
                    moves[field].weight[b]=0; //default.
            }



            else
                moves[field].weight[b]=0; //dice already used or field not used by player
        }



    }

    int total=0;
    for(int field=0;field<26;field++)
    {
        total+=moves[field].weight[0]+moves[field].weight[1]+moves[field].weight[2]+moves[field].weight[3];
    }
    return total;
}

void MoveEngine::move(const int& from, int to, const int& dice)
{
    //odebug << "" << player << " moves from " << from << " to " << to << " (" << to-from << ") with dice " << dice << "" << oendl; 

    if(player==1 && to==25)
        to=26;
    if(player==2 && to==0)
        to=27;

    //if space is occupied by enemy move pieces to startzone
    if(fieldColor(to)==otherplayer)
    {
        population[to].total=0;
        if(otherplayer==1)
            population[0].total++;
        else
            population[25].total--;
    }

    if(player==1)
    {
        population[from].total--;
        population[to].total++;
    }
    else  //player=2
    {
        population[from].total++;
        population[to].total--;
    }

    if(dice==1)
        emit done_dice1();
    else if(dice==2)
        emit done_dice2();
    else if(dice==3)
        emit done_dice3();
    else
        emit done_dice4();

    if(abs(population[26].total)==15)
        emit player_finished(1);
    if(abs(population[27].total)==15)
        emit player_finished(2);
}

void MoveEngine::checkstate()
{
    //check if pieces are out
    pieces_out[1]=(population[0].total>0) ? true : false;
    pieces_out[2]=(population[25].total<0) ? true : false;
    
    //check if all pieces are in the endzones
    allclear[1]=true;
    allclear[2]=true;

    last_piece[1]=25;
    bool found_last_piece1=false;
    last_piece[2]=0;
    
    for(int a=0;a<26;a++)
    {
        if(a<19 && population[a].total>0)
            allclear[1]=false;
        if(a>6 && population[a].total<0)
            allclear[2]=false;
            
        if(population[a].total>0 && !found_last_piece1)
        {
          last_piece[1]=a;
          found_last_piece1=true;
        }
        if(population[a].total<0)
          last_piece[2]=a;
    }
}

void MoveEngine::nomarker(Marker& marker)
{
    marker.visible_current=false;
    marker_current=-1;
    for(int a=0;a<4;a++)
    {
        marker.x_next[a]=0;
        marker.y_next[a]=0;
        marker_next[a]=-1;
        marker.visible_next[a]=false;
    }
}

int MoveEngine::fieldColor(const int& index) const
{
    if(population[index].total>0)
        return 1;
    else if(population[index].total<0)
        return 2;
    else
        return 0;
}