[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-games-devel
Subject:    [Kde-games-devel] [PATCH] highlight last AI move in KSquares
From:       Paolo Capriotti <p.capriotti () gmail ! com>
Date:       2008-02-07 17:21:19
Message-ID: fofemm$235$1 () ger ! gmane ! org
[Download RAW message or body]

Hi all,
while playing KSquares, expecially in the endgame on a large board, I 
often miss the last move of the AI. This means I can't spot the point 
where a square can be completed, so I play elsewhere and lose :(

So I've added a highlighting for the last move played by a computer 
player, to make it easy for us humans to find it.

The patch is attached. Matt, what do you think about it?

Paolo

["ksquares-highlight.patch" (text/x-diff)]

Index: src/gameboardscene.h
===================================================================
--- src/gameboardscene.h	(revision 762396)
+++ src/gameboardscene.h	(working copy)
@@ -52,6 +52,11 @@
 		 */
 		void drawLine(int index, const QColor &colour);
 		/**
+		  * Draw a temporary halo behind a line.
+		  * @param index the line-index of the line
+		  */
+		void highlightLine(int index);
+		/**
 		 * Fill a box to show it is owned be a particular player.
 		 * @param index the square-index of the square
 		 * @param colour the colour fill of the square
@@ -90,11 +95,11 @@
 		 */
 		int indexFromPointPair(const QList<QGraphicsEllipseItem*> &pointPair) \
const;	//given a pointPair, returns the index of the line between them. If not a \
valid line, returns -1  /**
-		 * Takes a line-index and returns a QGraphicsLineItem located at that position
+		 * Takes a line-index and returns a QLineF located at that position
 		 * @param index the line-index
 		 * @return line located at the correct position
 		 */
-		QGraphicsLineItem* lineFromIndex(int index) const;	//all external calls will need \
to be passed through this to convert to local coords +		QLineF lineFromIndex(int \
index) const;	//all external calls will need to be passed through this to convert to \
local coords  
 		///Moves to show where the next line will be drawn
 		QGraphicsLineItem* indicatorLine;
Index: src/highlightanimation.h
===================================================================
--- src/highlightanimation.h	(revision 0)
+++ src/highlightanimation.h	(revision 0)
@@ -0,0 +1,37 @@
+/***************************************************************************
+ *   Copyright (C) 2006 by Paolo Capriotti    <p.capriotti@gmail.com>      *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ ***************************************************************************/
+
+#ifndef HIGHLIGHTANIMATION_H
+#define HIGHLIGHTANIMATION_H
+
+#include <QGraphicsLineItem>
+#include <QTimeLine>
+
+/**
+ * @short Highlight animation when new lines appear.
+ *
+ * Created when a new line is added to the board.
+ * 
+ * @author Paolo Capriotti <p.capriotti@gmail.com>
+ */
+class HighlightAnimation : public QObject, public QGraphicsLineItem
+{
+	Q_OBJECT
+	
+	public:
+		HighlightAnimation(const QLineF &line);
+	
+	private:
+		QTimeLine timeline;
+		
+	private slots:
+		void setOpacity(int opacity);
+};
+
+#endif // HIGHLIGHTANIMATION_H
Index: src/CMakeLists.txt
===================================================================
--- src/CMakeLists.txt	(revision 762396)
+++ src/CMakeLists.txt	(working copy)
@@ -10,6 +10,7 @@
 	ksquaresdemowindow.cpp
 	themegraphicsitems.cpp
 	dots_client.cpp
+	highlightanimation.cpp
 )
 
 include_directories(${CMAKE_SOURCE_DIR}/libkdegames/highscore)
Index: src/ksquaresgame.h
===================================================================
--- src/ksquaresgame.h	(revision 762396)
+++ src/ksquaresgame.h	(working copy)
@@ -143,7 +143,8 @@
 		bool anotherGo;
 		/// is there currently a game in progress
 		bool gameInProgress;
-		
+		/// last line added
+		int lastLine;
 	signals:
 		///A player's turn has started. This allows you to use AI/networking etc.
 		void takeTurnSig(KSquaresPlayer*);	//emit the new curent player
@@ -153,6 +154,8 @@
 		void drawLine(int,QColor);	//int == lineList index
 		///Emits the index and colour of the square
 		void drawSquare(int,QColor);	//int == squareOwnerTable index
+		///Emitted when the last move in a series is played by the AI
+		void highlightMove(int);
 };
 
 #endif // KSQUARESGAME_H
Index: src/gameboardscene.cpp
===================================================================
--- src/gameboardscene.cpp	(revision 762396)
+++ src/gameboardscene.cpp	(working copy)
@@ -10,6 +10,7 @@
 #include "gameboardscene.h"
 
 #include "dots_client.h"
+#include "highlightanimation.h"
 
 #include <math.h>
 
@@ -82,7 +83,7 @@
 
 void GameBoardScene::drawLine(int index, const QColor &colour)
 {
-	QGraphicsLineItem* line = lineFromIndex(index);
+	QGraphicsLineItem* line = new QGraphicsLineItem(lineFromIndex(index));
 	line->setZValue(10);
 	line->setPen(QPen(QBrush(colour), 2.5));
 	addItem(line);	//draw new line
@@ -91,6 +92,13 @@
 	update(line->boundingRect());
 }
 
+void GameBoardScene::highlightLine(int index)
+{
+	HighlightAnimation* anim = new HighlightAnimation(lineFromIndex(index));
+	anim->setZValue(9);
+	addItem(anim);
+}
+
 void GameBoardScene::drawSquare(int index, const QColor &colour)
 {
 	QBrush brush(colour, Qt::SolidPattern);
@@ -138,7 +146,7 @@
 	return index;
 }
 
-QGraphicsLineItem* GameBoardScene::lineFromIndex(int index) const
+QLineF GameBoardScene::lineFromIndex(int index) const
 {
 	int index2 = index % ((2*width) + 1);
 	enum{HORIZONTAL, VERTICAL} dir;
@@ -164,7 +172,7 @@
 			xCoordEnd = xCoordStart;
 			break;
 	}
-	return new QGraphicsLineItem(QLineF(xCoordStart, yCoordStart, xCoordEnd, \
yCoordEnd)); +	return QLineF(xCoordStart, yCoordStart, xCoordEnd, yCoordEnd);
 }
 
 bool GameBoardScene::isLineAlready(const QList<QGraphicsEllipseItem*> &pointPair) \
                const //TODO does this work?
Index: src/prefs_display.ui
===================================================================
--- src/prefs_display.ui	(revision 762396)
+++ src/prefs_display.ui	(working copy)
@@ -5,29 +5,14 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>250</width>
-    <height>118</height>
+    <width>245</width>
+    <height>214</height>
    </rect>
   </property>
   <property name="windowTitle" >
    <string>Display Settings</string>
   </property>
   <layout class="QVBoxLayout" >
-   <property name="spacing" >
-    <number>6</number>
-   </property>
-   <property name="leftMargin" >
-    <number>9</number>
-   </property>
-   <property name="topMargin" >
-    <number>9</number>
-   </property>
-   <property name="rightMargin" >
-    <number>9</number>
-   </property>
-   <property name="bottomMargin" >
-    <number>9</number>
-   </property>
    <item>
     <widget class="QGroupBox" name="groupBox_2" >
      <property name="whatsThis" >
@@ -37,24 +22,22 @@
       <string>Colors</string>
      </property>
      <layout class="QGridLayout" >
-      <property name="leftMargin" >
-       <number>9</number>
-      </property>
-      <property name="topMargin" >
-       <number>9</number>
-      </property>
-      <property name="rightMargin" >
-       <number>9</number>
-      </property>
-      <property name="bottomMargin" >
-       <number>9</number>
-      </property>
-      <property name="horizontalSpacing" >
-       <number>6</number>
-      </property>
-      <property name="verticalSpacing" >
-       <number>6</number>
-      </property>
+      <item row="0" column="0" >
+       <widget class="QLabel" name="label" >
+        <property name="whatsThis" >
+         <string>Color of the drawn lines</string>
+        </property>
+        <property name="text" >
+         <string>Standard line color:</string>
+        </property>
+        <property name="buddy" >
+         <cstring>kcfg_LineColor</cstring>
+        </property>
+       </widget>
+      </item>
+      <item row="0" column="1" >
+       <widget class="KColorButton" name="kcfg_LineColor" />
+      </item>
       <item row="1" column="0" >
        <widget class="QLabel" name="label_2" >
         <property name="whatsThis" >
@@ -71,22 +54,22 @@
       <item row="1" column="1" >
        <widget class="KColorButton" name="kcfg_IndicatorLineColor" />
       </item>
-      <item row="0" column="1" >
-       <widget class="KColorButton" name="kcfg_LineColor" />
-      </item>
-      <item row="0" column="0" >
-       <widget class="QLabel" name="label" >
+      <item row="2" column="0" >
+       <widget class="QLabel" name="label_3" >
         <property name="whatsThis" >
-         <string>Color of the drawn lines</string>
+         <string>Color of the indicator lines</string>
         </property>
         <property name="text" >
-         <string>Standard line color:</string>
+         <string>Highlight color:</string>
         </property>
         <property name="buddy" >
-         <cstring>kcfg_LineColor</cstring>
+         <cstring>kcfg_IndicatorLineColor</cstring>
         </property>
        </widget>
       </item>
+      <item row="2" column="1" >
+       <widget class="KColorButton" name="kcfg_HighlightColor" />
+      </item>
      </layout>
     </widget>
    </item>
@@ -98,7 +81,7 @@
      <property name="sizeHint" >
       <size>
        <width>232</width>
-       <height>16</height>
+       <height>71</height>
       </size>
      </property>
     </spacer>
Index: src/highlightanimation.cpp
===================================================================
--- src/highlightanimation.cpp	(revision 0)
+++ src/highlightanimation.cpp	(revision 0)
@@ -0,0 +1,37 @@
+/***************************************************************************
+ *   Copyright (C) 2006 by Paolo Capriotti    <p.capriotti@gmail.com>      *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ ***************************************************************************/
+
+#include "highlightanimation.h"
+
+#include "settings.h"
+
+#include <QBrush>
+#include <QPen>
+#include <QTimer>
+
+HighlightAnimation::HighlightAnimation(const QLineF &line) : \
QGraphicsLineItem(line), timeline(1000) +{
+	setPen(QPen(Settings::highlightColor(), 8.0, Qt::SolidLine, Qt::RoundCap));
+	timeline.setUpdateInterval(10);
+	timeline.setFrameRange(255, 0);
+	connect(&timeline, SIGNAL(frameChanged(int)), this, SLOT(setOpacity(int)));
+	connect(&timeline, SIGNAL(finished()), this, SLOT(deleteLater()));
+	QTimer::singleShot(1000, &timeline, SLOT(start()));
+}
+
+void HighlightAnimation::setOpacity(int opacity)
+{
+	QPen p = pen();
+	QColor c = p.color();
+	c.setAlpha(opacity);
+	p.setColor(c);
+	setPen(p);
+	
+	update();
+}
Index: src/ksquares.kcfg
===================================================================
--- src/ksquares.kcfg	(revision 762396)
+++ src/ksquares.kcfg	(working copy)
@@ -48,5 +48,10 @@
 			<whatsthis></whatsthis>
 			<default>255,255,0</default>
 		</entry>
+		<entry name="HighlightColor" type="Color">
+			<label>Highlight Color</label>
+			<whatsthis></whatsthis>
+			<default>220,100,100</default>
+		</entry>
 	</group>
 </kcfg>
Index: src/ksquareswindow.cpp
===================================================================
--- src/ksquareswindow.cpp	(revision 762396)
+++ src/ksquareswindow.cpp	(working copy)
@@ -192,6 +192,7 @@
 	connect(m_scene, SIGNAL(lineDrawn(int)), sGame, SLOT(addLineToIndex(int)));
 	connect(m_scene, SIGNAL(signalMoveRequest(const msg&)), SLOT(slotMoveRequest(const \
msg&)));  connect(sGame, SIGNAL(drawLine(int,QColor)), m_scene, \
SLOT(drawLine(int,QColor))); +	connect(sGame, SIGNAL(highlightMove(int)), m_scene, \
SLOT(highlightLine(int)));  connect(sGame, SIGNAL(drawSquare(int,QColor)), m_scene, \
SLOT(drawSquare(int,QColor)));  
 	if (Settings::quickStart() == 2)
Index: src/ksquaresgame.cpp
===================================================================
--- src/ksquaresgame.cpp	(revision 762396)
+++ src/ksquaresgame.cpp	(working copy)
@@ -102,6 +102,10 @@
 	else
 	{
 		kDebug() << "- - - Go ending";
+		if (!currentPlayer()->isHuman())
+		{
+			emit highlightMove(lastLine);
+		}
 		nextPlayer();
 	}
 }
@@ -118,6 +122,7 @@
 	i_currentPlayerId = -1;
 	anotherGo = false;
 	gameInProgress = false;
+	lastLine = -1;
 }
 
 void KSquaresGame::addLineToIndex(int index)
@@ -129,6 +134,7 @@
 		return;
 	}
 	lineList[index] = true;
+	lastLine = index;
 	
 	emit drawLine(index, Settings::lineColor());
 	
Index: src/ksquaresdemowindow.cpp
===================================================================
--- src/ksquaresdemowindow.cpp	(revision 762396)
+++ src/ksquaresdemowindow.cpp	(working copy)
@@ -83,6 +83,7 @@
 	sGame->createGame(playerList, 15, 10);
 	connect(m_scene, SIGNAL(lineDrawn(int)), sGame, SLOT(addLineToIndex(int)));
 	connect(sGame, SIGNAL(drawLine(int,QColor)), m_scene, SLOT(drawLine(int,QColor)));
+	connect(sGame, SIGNAL(highlightMove(int)), m_scene, SLOT(highlightLine(int)));
 	connect(sGame, SIGNAL(drawSquare(int,QColor)), m_scene, \
SLOT(drawSquare(int,QColor)));  
 	sGame->start();



_______________________________________________
kde-games-devel mailing list
kde-games-devel@kde.org
https://mail.kde.org/mailman/listinfo/kde-games-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic