| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2004-2007 Artur Wiebe * |
| 3 | * [email protected] * |
| 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 2 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, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include <QDebug> |
| 22 | |
| 23 | #include "humanplayer.h" |
| 24 | #include "rcheckers.h" |
| 25 | #include "echeckers.h" |
| 26 | #include "pdn.h" |
| 27 | |
| 28 | |
| 29 | myHumanPlayer::myHumanPlayer(const QString& name, bool white, |
| 30 | bool second_player) |
| 31 | : myPlayer(name, white) |
| 32 | { |
| 33 | selected = false; |
| 34 | |
| 35 | m_second = second_player; |
| 36 | m_game = 0; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | myHumanPlayer::~myHumanPlayer() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | |
| 45 | void myHumanPlayer::yourTurn(const Checkers* g) |
| 46 | { |
| 47 | if(!m_game || m_game->type()!=g->type()) { |
| 48 | delete m_game; |
| 49 | if(g->type()==RUSSIAN) { |
| 50 | m_game = new RCheckers(); |
| 51 | } else { |
| 52 | m_game = new ECheckers(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // synchronize |
| 57 | m_game->fromString(g->toString(m_second)); |
| 58 | } |
| 59 | |
| 60 | bool myHumanPlayer::fieldClicked(int field_num, bool* select, bool bottom_is_white, QString& errmsg) |
| 61 | { |
| 62 | if(m_second) { |
| 63 | field_num = 31 - field_num; |
| 64 | } |
| 65 | |
| 66 | switch(m_game->item(field_num)) { |
| 67 | case MAN1: |
| 68 | case KING1: { |
| 69 | Captures* capture = m_game->getPossibleCapture(); |
| 70 | bool must_capture = !capture->isEmpty() && !m_game->canCapture1(field_num); |
| 71 | QString captureStr = m_game->describeCapture(bottom_is_white, capture); |
| 72 | delete capture; |
| 73 | if (must_capture) { |
| 74 | errmsg = tr("You must capture. Available moves are: %1").arg(captureStr); |
| 75 | return false; |
| 76 | } |
| 77 | if(!m_game->canCapture1(field_num) |
| 78 | && !m_game->canMove1(field_num)) { |
| 79 | errmsg = tr("This unit does not have valid moves.");//TODO better text. |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | // Player (re)selects |
| 84 | from = field_num; |
| 85 | fromField = field_num; |
| 86 | selected = true; |
| 87 | *select = true; |
| 88 | return true; |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | case FREE: |
| 93 | if(!selected) { |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | if (!go(field_num)) { |
| 98 | errmsg = tr("This unit cannot be moved to field you pointed."); |
| 99 | return false; // incorrect course |
| 100 | } |
| 101 | |
| 102 | // move done - unselect |
| 103 | if(selected) { |
| 104 | *select = false; |
| 105 | } |
| 106 | selected = false; |
| 107 | |
| 108 | emit moveDone(m_game->toString(m_second)); |
| 109 | break; |
| 110 | |
| 111 | default: |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | bool myHumanPlayer::go(int to) |
| 120 | { |
| 121 | return m_game->go1(from, to); |
| 122 | } |
| 123 |