Debian Codegraph @

src/computerplayer.cc

raw symbols SCIP
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 #include <QDebug>
21 #include <QApplication>
22 #include <QEvent>
23
24 #include "computerplayer.h"
25 #include "pdn.h"
26 #include "rcheckers.h"
27 #include "echeckers.h"
28 #include "checkers.h"
29
30
31 myComputerPlayer::myComputerPlayer(const QString& name, bool white, int skill)
32 : myPlayer(name, white)
33 {
34 m_game = 0;
35 m_thread = 0;
36 m_skill = skill;
37 }
38
39
40 myComputerPlayer::~myComputerPlayer()
41 {
42 if(m_thread) {
43 m_thread->stop();
44 // delete m_thread
45 }
46 delete m_game;
47 }
48
49
50 void myComputerPlayer::yourTurn(const Checkers* g)
51 {
52 if(m_thread) {
53 qDebug("myComputerPlayer::yourTurn: a thread exists.");
54 }
55
56 // first create it.
57 if(!m_game || m_game->type()!=g->type()) {
58 delete m_game;
59 if(g->type()==RUSSIAN) {
60 m_game = new RCheckers();
61 } else {
62 m_game = new ECheckers();
63 }
64 }
65
66 m_game->setSkill(m_skill);
67 m_game->fromString(g->toString(false));
68
69 m_thread = new myThread(this, m_game);
70 m_thread->start();
71 }
72
73
74 void myComputerPlayer::stop()
75 {
76 if(m_thread) {
77 m_thread->stop();
78 }
79 }
80
81
82 void myComputerPlayer::customEvent(QEvent* ev)
83 {
84 if(ev->type() == QEvent::MaxUser) {
85 m_thread->wait();
86
87 delete m_thread;
88 m_thread = 0;
89
90 emit moveDone(m_game->toString(false));
91 }
92 }
93
94
95
96 /****************************************************************************
97 *
98 *
99 ***************************************************************************/
100 void myThread::run()
101 {
102 m_game->go2();
103 if(!m_aborted) {
104 QEvent* ev = new QEvent(QEvent::MaxUser);
105 QApplication::postEvent(m_player, ev);
106 } else {
107 qDebug("thread.aborted.done.");
108 }
109 }
110
111
112 void myThread::stop()
113 {
114 m_aborted = true;
115 m_game->setSkill(0);
116 }
117