~ubuntu-branches/ubuntu/oneiric/psi/oneiric

« back to all changes in this revision

Viewing changes to src/plugins/generic/chess/gameboard.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
68
68
                        case GameBoard::WHITE_KING:
69
69
                        case GameBoard::WHITE_QUEEN:
70
70
                        case GameBoard::WHITE_KNIGHT:
71
 
                                res = TRUE;
 
71
                                res = true;
72
72
                                break;
73
73
                        default:
74
 
                                res = FALSE;
 
74
                                res = false;
75
75
                }
76
76
        else if (gt == GameBoard::BLACK)
77
77
                switch (map[n]) {
82
82
                        case GameBoard::BLACK_KING:
83
83
                        case GameBoard::BLACK_QUEEN:
84
84
                        case GameBoard::BLACK_KNIGHT:
85
 
                                res = TRUE;
 
85
                                res = true;
86
86
                                break;
87
87
                        default:
88
 
                                res = FALSE;
 
88
                                res = false;
89
89
                }
90
90
        else
91
 
                res = FALSE;
 
91
                res = false;
92
92
 
93
93
        return (res);
94
94
}
214
214
                map[f] = GameBoard::NONE;
215
215
                if (mirror) {
216
216
                        vl.resize(0);
217
 
                        t = checkKing(gt, map, mirror, vl, FALSE);
 
217
                        t = checkKing(gt, map, mirror, vl, false);
218
218
                        switch (t) {
219
219
                                case 1:
220
220
                                        res |= 0x10;
296
296
                                if (p != pk)
297
297
                                        map1[p] = GameBoard::DUMMY;
298
298
                        }
299
 
                        if (checkKing(gt, map1, mirror, vl, TRUE) != 0) {
 
299
                        if (checkKing(gt, map1, mirror, vl, true) != 0) {
300
300
                                vl.resize(0);
301
301
                                moveListKing(vl, mytype, map, xk, yk, !mirror);
302
302
                                memmove(map1, map, sizeof(map1));
306
306
                                        map1[p] = myking;
307
307
                                        map1[pk] = GameBoard::NONE;
308
308
                                        if (checkKing(gt, map1, mirror,
309
 
                                                tmp, TRUE) == 1)
 
309
                                                tmp, true) == 1)
310
310
                                                ++y;
311
311
                                        map1[pk] = map[pk];
312
312
                                        map1[p] = map[p];
587
587
 
588
588
        x1 = x - 1; x2 = x + 1;
589
589
        y1 = y - 1; y2 = y + 1;
590
 
        res = FALSE;
 
590
        res = false;
591
591
 
592
592
        if (validPoint(gt, map, x1, y2, mirror))
593
593
                res = (hasEnemyFigure(gt, map, x1, y2, mirror) == 2);
614
614
Figure::hasPoint(const Q3PointArray &vl, int x, int y)
615
615
{
616
616
        int     i, xp, yp, cnt;
617
 
        bool    res = FALSE;
 
617
        bool    res = false;
618
618
 
619
619
        cnt = vl.count();
620
620
        for (i = 0; i < cnt; ++i) {
621
621
                vl.point(i, &xp, &yp);
622
622
                if ((xp == x) && (yp == y)) {
623
 
                        res = TRUE;
 
623
                        res = true;
624
624
                        break;
625
625
                }
626
626
        }
668
668
 
669
669
        sock = new Q3Socket(this);
670
670
        drw = new Drawer(map, &gt, this);
671
 
        drw->setEnabled(FALSE);
 
671
        drw->setEnabled(false);
672
672
        drw->setFocusPolicy(Qt::NoFocus);
673
673
        box = new Q3GroupBox(tr("Game chat"), this);
674
674
        lst = new Q3ListBox(box);
676
676
        lst->setVScrollBarMode(Q3ScrollView::AlwaysOff);
677
677
        lst->setSelectionMode(Q3ListBox::NoSelection);
678
678
        edt = new QLineEdit(box);
679
 
        edt->setEnabled(FALSE);
 
679
        edt->setEnabled(false);
680
680
        setFocusProxy(edt);
681
681
        hist = new Q3GroupBox(tr("History"), this);
682
682
        hist->setAlignment(Qt::AlignHCenter);
744
744
        connect(protocol,SIGNAL(sendData(const QString&)), this, SIGNAL(sendData(const QString&)));
745
745
        sock = new Q3Socket(this);
746
746
        drw = new Drawer(map, &gt, this);
747
 
        drw->setEnabled(FALSE);
 
747
        drw->setEnabled(false);
748
748
        drw->setFocusPolicy(Qt::NoFocus);
749
749
        box = new Q3GroupBox(tr("Game chat"), this);
750
750
        lst = new Q3ListBox(box);
818
818
        delete box;
819
819
        delete drw;
820
820
        delete sock;
821
 
        delete map;
 
821
        delete[] map;
822
822
        delete protocol;
823
823
}
824
824
 
950
950
        my_stat = tr("Connected to the host");
951
951
        emit showStatus(my_stat);
952
952
        protocol->setGameType(sock, gt);
953
 
        edt->setEnabled(TRUE);
 
953
        edt->setEnabled(true);
954
954
        qDebug("sockConnected");
955
955
}
956
956
 
995
995
        if (s == "game") {
996
996
                s = lst[1].lower();
997
997
                if (s == "mate") {
998
 
                        updateHistory(GAMEOVER_TXT, TRUE);
 
998
                        updateHistory(GAMEOVER_TXT, true);
999
999
                        gt = NOGAME;
1000
1000
                        gameover(0);
1001
1001
                        close();
1010
1010
                        } else if (s == "black") {
1011
1011
                                gt = WHITE;
1012
1012
                                s = tr("Black");
1013
 
                                drw->setEnabled(TRUE);
 
1013
                                drw->setEnabled(true);
1014
1014
                                setCursor(QCursor(Qt::ArrowCursor));
1015
1015
                        }
1016
1016
                        s += ' ' + tr("game from") + ' ';
1020
1020
                                hst = sock->peerAddress().toString() + ':' +
1021
1021
                                        QString::number(sock->peerPort());
1022
1022
                        initMap();
1023
 
                        drw->repaint(TRUE);
 
1023
                        drw->repaint(true);
1024
1024
                        protocol->acceptGame(sock);
1025
1025
                        setCaption(s + hst);
1026
1026
                        my_stat += hst;
1027
1027
                        emit showStatus(my_stat);
1028
1028
                } else if (gt == WHITE) {
1029
 
                        drw->setEnabled(TRUE);
 
1029
                        drw->setEnabled(true);
1030
1030
                        setCursor(QCursor(Qt::ArrowCursor));
1031
1031
                }
1032
1032
        } else if (s == "move") {
1033
1033
                if (!drw->isEnabled()) {
1034
 
                        drw->setEnabled(TRUE);
 
1034
                        drw->setEnabled(true);
1035
1035
                        s = lst[1].lower();
1036
 
                        updateHistory(s, TRUE);
 
1036
                        updateHistory(s, true);
1037
1037
                        drw->makeMove(s);
1038
1038
                        setCursor(QCursor(Qt::ArrowCursor));
1039
1039
                        my_stat = tr("Your move...");
1049
1049
                s = lst[1].lower();
1050
1050
                id = lst[2].toInt();
1051
1051
                drw->newFigure(s, id);
1052
 
                updateHistory(id, TRUE);
 
1052
                updateHistory(id, true);
1053
1053
        }
1054
1054
}
1055
1055
 
1059
1059
 
1060
1060
        protocol->sendMove(sock, str);
1061
1061
        emit sendData(str);
1062
 
        drw->setEnabled(FALSE);
 
1062
        drw->setEnabled(false);
1063
1063
        setCursor(QCursor(Qt::WaitCursor));
1064
 
        updateHistory(str, FALSE);
 
1064
        updateHistory(str, false);
1065
1065
        sock_tout = SOCK_WAIT;
1066
1066
        my_stat = tr("Waiting a move...");
1067
1067
        emit showStatus(my_stat);
1211
1211
        }
1212
1212
        if (id != -1) {
1213
1213
                protocol->sendFigure(sock, coo, id);
1214
 
                updateHistory(id, FALSE);
 
1214
                updateHistory(id, false);
1215
1215
        }
1216
1216
}
1217
1217
 
1256
1256
void
1257
1257
GameBoard::gameover(int type)
1258
1258
{
1259
 
        bool    save = FALSE;
 
1259
        bool    save = false;
1260
1260
        QString s('\n' + tr("Do you want to save the image?")),
1261
1261
                yes(tr("Yes, save")),
1262
1262
                no(tr("No, don't save")),
1266
1266
                save = (QMessageBox::question(this, go,
1267
1267
                        tr("You scored the game") + s, yes, no) == 0);
1268
1268
        } else if (type == 2) {
1269
 
                updateHistory(GAMEOVER_TXT, FALSE);
 
1269
                updateHistory(GAMEOVER_TXT, false);
1270
1270
                protocol->sendGameover(sock, "MATE");
1271
1271
                save = (QMessageBox::question(this, go,
1272
1272
                        tr("You have a mate.\nYou lost the game.") + s,
1291
1291
        int             i;
1292
1292
 
1293
1293
        map = ft; gt = g;
1294
 
        kk = rcm = lcm = km = FALSE;
 
1294
        kk = rcm = lcm = km = false;
1295
1295
        cs = cell_size * 8;
1296
1296
        top_margin = 5;
1297
1297
        for (left_margin = 0, i = 0; i < 8; i++)
1461
1461
                if (hasTakenFigure()) {
1462
1462
                        if ((tfx == x) && (tfy == y)) {
1463
1463
                                tfx = tfy = -1;
1464
 
                                repaint(FALSE);
 
1464
                                repaint(false);
1465
1465
                        } else
1466
 
                                makeMove(*gt, tfx, tfy, x, y, FALSE, FALSE);
 
1466
                                makeMove(*gt, tfx, tfy, x, y, false, false);
1467
1467
                } else if (canTake(x, y)) {
1468
1468
                        takeFigure(x, y);
1469
1469
                        emit touchFigure(x, y);
1476
1476
Drawer::canTake(int x, int y)
1477
1477
{
1478
1478
 
1479
 
        return (Figure::hasMyFigure(*gt, map, x, y, FALSE));
 
1479
        return (Figure::hasMyFigure(*gt, map, x, y, false));
1480
1480
}
1481
1481
 
1482
1482
 
1523
1523
                tfx = x;
1524
1524
                tfy = y;
1525
1525
        }
1526
 
        repaint(FALSE);
 
1526
        repaint(false);
1527
1527
}
1528
1528
 
1529
1529
 
1544
1544
        ft = GameBoard::NONE; n = -1;
1545
1545
        Figure::str2map(coo, &x, &y);
1546
1546
        if (*gt == GameBoard::WHITE) {
1547
 
                n = Figure::map2map(GameBoard::BLACK, x, y, TRUE);
 
1547
                n = Figure::map2map(GameBoard::BLACK, x, y, true);
1548
1548
                switch (id) {
1549
1549
                        case 3:
1550
1550
                                ft = GameBoard::BLACK_BISHOP;
1562
1562
                                ft = GameBoard::NONE;
1563
1563
                }
1564
1564
        } else if (*gt == GameBoard::BLACK) {
1565
 
                n = Figure::map2map(GameBoard::WHITE, x, y, TRUE);
 
1565
                n = Figure::map2map(GameBoard::WHITE, x, y, true);
1566
1566
                switch (id) {
1567
1567
                        case 3:
1568
1568
                                ft = GameBoard::WHITE_BISHOP;
1583
1583
 
1584
1584
        if (ft != GameBoard::NONE) {
1585
1585
                map[n] = ft;
1586
 
                repaint(FALSE);
 
1586
                repaint(false);
1587
1587
        }
1588
1588
}
1589
1589
 
1602
1602
                et = GameBoard::NOGAME;
1603
1603
        if (txt == LONG_XCHG) {
1604
1604
                if (et == GameBoard::BLACK)
1605
 
                        makeMove(et, 1, 8, 4, 8, TRUE, TRUE);
 
1605
                        makeMove(et, 1, 8, 4, 8, true, true);
1606
1606
                else if (et == GameBoard::WHITE)
1607
 
                        makeMove(et, 1, 1, 4, 1, TRUE, TRUE);
 
1607
                        makeMove(et, 1, 1, 4, 1, true, true);
1608
1608
        } else if (txt == SHORT_XCHG) {
1609
1609
                if (et == GameBoard::BLACK)
1610
 
                        makeMove(et, 8, 8, 6, 8, TRUE, TRUE);
 
1610
                        makeMove(et, 8, 8, 6, 8, true, true);
1611
1611
                else if (et == GameBoard::WHITE)
1612
 
                        makeMove(et, 8, 1, 6, 1, TRUE, TRUE);
 
1612
                        makeMove(et, 8, 1, 6, 1, true, true);
1613
1613
        } else {
1614
1614
                Figure::str2map(txt.left(2), &fx, &fy);
1615
1615
                Figure::str2map(txt.right(2), &tx, &ty);
1616
 
                makeMove(et, fx, fy, tx, ty, TRUE, FALSE);
 
1616
                makeMove(et, fx, fy, tx, ty, true, false);
1617
1617
        }
1618
1618
}
1619
1619
 
1637
1637
        res = Figure::validMove(gt, map, fx, fy, tx, ty, mirror);
1638
1638
        if (res) {
1639
1639
                if (!mirror) {
1640
 
                        x = FALSE;
 
1640
                        x = false;
1641
1641
                        if (gt == GameBoard::WHITE)
1642
1642
                                et = GameBoard::BLACK;
1643
1643
                        else if (gt == GameBoard::BLACK)
1644
1644
                                et = GameBoard::WHITE;
1645
 
                        if (Figure::checkKing(et, map, mirror, vl, TRUE) !=
 
1645
                        if (Figure::checkKing(et, map, mirror, vl, true) !=
1646
1646
                                0) {
1647
1647
                                map[nf] = map[nt];
1648
1648
                                map[nt] = old;
1653
1653
                                        "is in check") + '.');
1654
1654
                                goto HAXEP;
1655
1655
                        } else
1656
 
                                kk = FALSE;
 
1656
                                kk = false;
1657
1657
                        if (!km && (!lcm || !rcm) && !kk)
1658
1658
                                x = xchg(fo, map[nt], fx, fy, tx, ty);
1659
1659
                        else
1660
 
                                x = TRUE;
 
1660
                                x = true;
1661
1661
                        if (x)
1662
1662
                                emit moved(Figure::map2str(fx, fy) +
1663
1663
                                        Figure::map2str(tx, ty));
1672
1672
                        tfx = tfy = -1;
1673
1673
                } else if (xc) {
1674
1674
                        if (gt == GameBoard::BLACK)
1675
 
                                checkBlackCastle(fx, fy, tx, ty, TRUE);
 
1675
                                checkBlackCastle(fx, fy, tx, ty, true);
1676
1676
                        else if (gt == GameBoard::WHITE)
1677
 
                                checkWhiteCastle(fx, fy, tx, ty, TRUE);
 
1677
                                checkWhiteCastle(fx, fy, tx, ty, true);
1678
1678
                }
1679
1679
                if (mirror && (res & 0x10)) {
1680
 
                        kk = TRUE;
 
1680
                        kk = true;
1681
1681
                } else if (res & 0x20) {
1682
 
                        repaint(FALSE);
 
1682
                        repaint(false);
1683
1683
                        emit gameover(2);
1684
1684
                        return;
1685
1685
                } else if (res & 0x40) {
1686
 
                        repaint(FALSE);
 
1686
                        repaint(false);
1687
1687
                        emit gameover(3);
1688
1688
                        return;
1689
1689
                }
1690
1690
                HAXEP:
1691
 
                repaint(FALSE);
 
1691
                repaint(false);
1692
1692
        }
1693
1693
}
1694
1694
 
1697
1697
Drawer::xchg(GameBoard::FigureType o, GameBoard::FigureType n,
1698
1698
        int fx, int fy, int tx, int ty)
1699
1699
{
1700
 
        bool    ret = TRUE;
 
1700
        bool    ret = true;
1701
1701
 
1702
1702
        if (*gt == GameBoard::WHITE) {
1703
1703
                km = ((o == n) && (o == GameBoard::WHITE_KING));
1704
1704
                if (!km && ((o == n) && (o == GameBoard::WHITE_CASTLE)))
1705
 
                        ret = checkWhiteCastle(fx, fy, tx, ty, FALSE);
 
1705
                        ret = checkWhiteCastle(fx, fy, tx, ty, false);
1706
1706
        } else if (*gt == GameBoard::BLACK) {
1707
1707
                km = ((o == n) && (o == GameBoard::BLACK_KING));
1708
1708
                if (!km && ((o == n) && (o == GameBoard::BLACK_CASTLE)))
1709
 
                        ret = checkBlackCastle(fx, fy, tx, ty, FALSE);
 
1709
                        ret = checkBlackCastle(fx, fy, tx, ty, false);
1710
1710
        }
1711
1711
 
1712
1712
        return (ret);
1717
1717
Drawer::checkWhiteCastle(int fx, int fy, int tx, int ty, bool mirror)
1718
1718
{
1719
1719
        int     n1, n2;
1720
 
        bool    ret = TRUE;
 
1720
        bool    ret = true;
1721
1721
 
1722
1722
        n1 = n2 = -1;
1723
1723
        if ((fx == 1) && (fy == 1)) {
1724
1724
                if ((tx == 4) && (ty == 1))
1725
1725
                        if (mirror) {
1726
 
                                n1 = Figure::map2map(*gt, 5, 1, FALSE);
1727
 
                                n2 = Figure::map2map(*gt, 3, 1, FALSE);
 
1726
                                n1 = Figure::map2map(*gt, 5, 1, false);
 
1727
                                n2 = Figure::map2map(*gt, 3, 1, false);
1728
1728
                        } else if (!lcm) {
1729
1729
                                if (makeXchg()) {
1730
 
                                        n1 = Figure::map2map(*gt, 5, 1, FALSE);
1731
 
                                        n2 = Figure::map2map(*gt, 3, 1, FALSE);
 
1730
                                        n1 = Figure::map2map(*gt, 5, 1, false);
 
1731
                                        n2 = Figure::map2map(*gt, 3, 1, false);
1732
1732
                                        emit moved(LONG_XCHG);
1733
 
                                        ret = FALSE;
1734
 
                                        rcm = TRUE;
 
1733
                                        ret = false;
 
1734
                                        rcm = true;
1735
1735
                                }
1736
 
                                lcm = TRUE;
 
1736
                                lcm = true;
1737
1737
                        }
1738
1738
        } else if ((fx == 8) && (fy == 1)) {
1739
1739
                if ((tx == 6) && (ty == 1))
1740
1740
                        if (mirror) {
1741
 
                                n1 = Figure::map2map(*gt, 5, 1, FALSE);
1742
 
                                n2 = Figure::map2map(*gt, 7, 1, FALSE);
 
1741
                                n1 = Figure::map2map(*gt, 5, 1, false);
 
1742
                                n2 = Figure::map2map(*gt, 7, 1, false);
1743
1743
                        } else if (!rcm) {
1744
1744
                                if (makeXchg()) {
1745
 
                                        n1 = Figure::map2map(*gt, 5, 1, FALSE);
1746
 
                                        n2 = Figure::map2map(*gt, 7, 1, FALSE);
 
1745
                                        n1 = Figure::map2map(*gt, 5, 1, false);
 
1746
                                        n2 = Figure::map2map(*gt, 7, 1, false);
1747
1747
                                        emit moved(SHORT_XCHG);
1748
 
                                        ret = FALSE;
1749
 
                                        lcm = TRUE;
 
1748
                                        ret = false;
 
1749
                                        lcm = true;
1750
1750
                                }
1751
 
                                rcm = TRUE;
 
1751
                                rcm = true;
1752
1752
                        }
1753
1753
        }
1754
1754
        if (n1 != n2) {
1764
1764
Drawer::checkBlackCastle(int fx, int fy, int tx, int ty, bool mirror)
1765
1765
{
1766
1766
        int     n1, n2;
1767
 
        bool    ret = TRUE;
 
1767
        bool    ret = true;
1768
1768
 
1769
1769
        n1 = n2 = -1;
1770
1770
        if ((fx == 1) && (fy == 8)) {
1771
1771
                if ((tx == 4) && (ty == 8)) {
1772
1772
                        if (mirror) {
1773
 
                                n1 = Figure::map2map(*gt, 5, 8, FALSE);
1774
 
                                n2 = Figure::map2map(*gt, 3, 8, FALSE);
 
1773
                                n1 = Figure::map2map(*gt, 5, 8, false);
 
1774
                                n2 = Figure::map2map(*gt, 3, 8, false);
1775
1775
                        } else if (!rcm) {
1776
1776
                                if (makeXchg()) {
1777
 
                                        n1 = Figure::map2map(*gt, 5, 8, FALSE);
1778
 
                                        n2 = Figure::map2map(*gt, 3, 8, FALSE);
 
1777
                                        n1 = Figure::map2map(*gt, 5, 8, false);
 
1778
                                        n2 = Figure::map2map(*gt, 3, 8, false);
1779
1779
                                        emit moved(LONG_XCHG);
1780
 
                                        ret = FALSE;
 
1780
                                        ret = false;
1781
1781
                                }
1782
 
                                rcm = TRUE;
 
1782
                                rcm = true;
1783
1783
                        }
1784
1784
                }
1785
1785
        } else if ((fx == 8) && (fy == 8)) {
1786
1786
                if ((tx == 6) && (ty == 8))
1787
1787
                        if (mirror) {
1788
 
                                n1 = Figure::map2map(*gt, 5, 8, FALSE);
1789
 
                                n2 = Figure::map2map(*gt, 7, 8, FALSE);
 
1788
                                n1 = Figure::map2map(*gt, 5, 8, false);
 
1789
                                n2 = Figure::map2map(*gt, 7, 8, false);
1790
1790
                        } else if (!lcm) {
1791
1791
                                if (makeXchg()) {
1792
 
                                        n1 = Figure::map2map(*gt, 5, 8, FALSE);
1793
 
                                        n2 = Figure::map2map(*gt, 7, 8, FALSE);
 
1792
                                        n1 = Figure::map2map(*gt, 5, 8, false);
 
1793
                                        n2 = Figure::map2map(*gt, 7, 8, false);
1794
1794
                                        emit moved(SHORT_XCHG);
1795
 
                                        ret = FALSE;
 
1795
                                        ret = false;
1796
1796
                                }
1797
 
                                lcm = TRUE;
 
1797
                                lcm = true;
1798
1798
                        }
1799
1799
        }
1800
1800
        if (n1 != n2) {