~ubuntu-branches/ubuntu/utopic/peg-solitaire/utopic

« back to all changes in this revision

Viewing changes to scr/moviment.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Innocent De Marchi
  • Date: 2011-01-18 18:06:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110118180654-w1yj9he00r65fvxh
Tags: upstream-1.0.2
Import upstream version 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***********************************************************************
 
2
 *
 
3
 * Copyright (C) 2010 Innocent De Marchi <tangram.peces@gmail.com>
 
4
 *
 
5
 * This program is free software: you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation, either version 3 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 *
 
18
 ***********************************************************************/
 
19
#include <QtCore>
 
20
 
 
21
#include "moviment.h"
 
22
#include "tauler.h"
 
23
#include "fitxa.h"
 
24
 
 
25
 
 
26
Moviment::Moviment(int fitxaInicial, int fitxaBotada, int fitxaFinal, int direccioMoviment,  Tauler *taulerJoc):
 
27
    p_fitxaInicial (fitxaInicial),
 
28
    p_fitxaBotada(fitxaBotada),
 
29
    p_fitxaFinal(fitxaFinal),
 
30
    p_direccioMoviment(direccioMoviment),
 
31
    p_taulerJoc(taulerJoc) {
 
32
    p_direccioMoviment=p_taulerJoc->m_fitxes.value(p_fitxaInicial)->tipusMoviment();
 
33
 
 
34
}
 
35
 
 
36
void Moviment::redo(){
 
37
    if (p_direccioMoviment==1){
 
38
        fesMoviment(0,0,1);
 
39
   }
 
40
    else if (p_direccioMoviment==2){        
 
41
        fesMoviment(0,1,1);
 
42
   }
 
43
    else if (p_direccioMoviment==3){
 
44
        fesMoviment(0,0,1);
 
45
   }
 
46
}
 
47
 
 
48
void Moviment::undo(){
 
49
    //Les fitxes vermelles de final de joc es passen a blaves
 
50
    if(p_taulerJoc->p_movimentsUndoStack->index()==
 
51
       p_taulerJoc->p_movimentsUndoStack->count() ){
 
52
         eliminaFitxesVermelles();
 
53
    }
 
54
    //direccció normal
 
55
    if (p_direccioMoviment==1){
 
56
        fesMoviment(1,1,0);
 
57
   }
 
58
    //moviment invers
 
59
    else if (p_direccioMoviment==2){
 
60
        fesMoviment(1,0,0);
 
61
   }
 
62
    //moviments perpendiculars i en diagonal
 
63
    else if (p_direccioMoviment==3){
 
64
        fesMoviment(1,1,0);
 
65
   }
 
66
}
 
67
 
 
68
void Moviment::fesMoviment(int primera, int segona, int tercera){
 
69
       p_taulerJoc->m_fitxes.value(p_fitxaInicial)->setEstat(primera);
 
70
       p_taulerJoc->m_fitxes.value(p_fitxaBotada)->setEstat(segona);
 
71
       p_taulerJoc->m_fitxes.value(p_fitxaFinal)->setEstat(tercera);
 
72
     //  setText(QString("%1 %2 %3").arg(p_fitxaInicial).arg(p_fitxaBotada).arg(p_fitxaFinal));
 
73
       setText(movimentACoordenades());
 
74
}
 
75
 
 
76
void Moviment::eliminaFitxesVermelles(){
 
77
    QHashIterator <int, Fitxa*> i(p_taulerJoc->m_fitxes);
 
78
    while (i.hasNext()) {
 
79
        i.next();
 
80
        if (i.value()->estat()==5){
 
81
            i.value()->setEstat(1);
 
82
        }
 
83
     }
 
84
}
 
85
 
 
86
QString Moviment::movimentACoordenades(){
 
87
    return QString("(%1,%2) -> (%3,%4)").arg(p_fitxaInicial/100).arg(p_fitxaInicial % 100).
 
88
                                arg(p_fitxaFinal/100).arg(p_fitxaFinal % 100);
 
89
}
 
90
 
 
91