×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Creating Tic-Tac-Toe using QWidgets

From Nokia Developer Wiki
Jump to: navigation, search
croozeus (Talk | contribs)
(Formatted the article)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(27 intermediate revisions by 10 users not shown)
Line 1: Line 1:
[[Category:Qt for S60]][[Category:How To]][[Category:Code Examples]]
+
{{Archived|timestamp=20120209041352|user=[[User:Hamishwillee|&lt;br /&gt;----]]|[[:Category:Qt Quick|Qt Quick]] should be used for all UI development on mobile devices. The approach described in this article (based on {{Qapiname|QWidget}}) is deprecated.}}
__NOTOC__
+
[[Category:Qt C++ UI]][[Category:Code Examples]][[Category:Games]]
__NOEDITSECTION__
+
{{Abstract|This article demonstrates how to create a simple tic-tac-toe game using QWidgets. The game rules are well known and the implementation is self-explanatory - hence these are not explained here.}}
{{CodeSnippet
+
{{ArticleMetaData <!-- v1.2 -->
|id=...
+
|sourcecode= [[Media:tictactoe.zip]]
|platform=S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= Emulator
 
|devices= Emulator
|category=Qt for S60
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|subcategory=Game
+
|platform= S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
|creationdate=03 February 2009
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|keywords=QPushButton
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= Self-Signed
 +
|capabilities= None
 +
|keywords= QPushButton
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20090203
 +
|author= [[User:James1980]]
 
}}
 
}}
 
 
==Overview==
 
 
This code snippet demonstrates how to create a simple tic-tac-toe game in [[Qt for S60]]. All of you might be familiar with this game so I am not explaining the rules for this game. And the code of  the game is also  self explaining.
 
 
 
==Preconditions==
 
 
* Install Qt for S60 Temple release from here: [http://pepper.troll.no/s60prereleases/ Qt for S60 "Temple" pre-release]
 
* Check this link for installation guide: [http://pepper.troll.no/s60prereleases/doc/install-s60.html How to install the package].
 
* Go through this article: [[Getting started with Qt for S60]]
 
<br>
 
  
 
== Source File ==
 
== Source File ==
  
<code cpp>
+
<code cpp-qt>
 
+
 
+
 
#include "tictactoe.h"
 
#include "tictactoe.h"
 
#include <QPushButton>
 
#include <QPushButton>
Line 38: Line 36:
 
     : QWidget(parent)
 
     : QWidget(parent)
 
{
 
{
layout = new QVBoxLayout;
+
layout = new QVBoxLayout(this);
rw1 = new QHBoxLayout;
+
rw1 = new QHBoxLayout(this);
rw2 = new QHBoxLayout;
+
rw2 = new QHBoxLayout(this);
rw3 = new QHBoxLayout;
+
rw3 = new QHBoxLayout(this);
 
flag=1;
 
flag=1;
 
 
button11 = new QPushButton;
+
button11 = new QPushButton(this);
button12 = new QPushButton;
+
button12 = new QPushButton(this);
button13 = new QPushButton;
+
button13 = new QPushButton(this);
button21 = new QPushButton;
+
button21 = new QPushButton(this);
button22 = new QPushButton;
+
button22 = new QPushButton(this);
button23 = new QPushButton;
+
button23 = new QPushButton(this);
button31 = new QPushButton;
+
button31 = new QPushButton(this);
button32 = new QPushButton;
+
button32 = new QPushButton(this);
button33 = new QPushButton;
+
button33 = new QPushButton(this);
 
setStyleSheet("* { background-color:rgb(199,147,88); padding: 7px ; color:rgb(255,255,255)}");
 
setStyleSheet("* { background-color:rgb(199,147,88); padding: 7px ; color:rgb(255,255,255)}");
 
 
Line 83: Line 81:
 
tictactoe::~tictactoe()
 
tictactoe::~tictactoe()
 
{
 
{
 +
    // No need to delete any object that got a parent that is properly deleted.
  
 
}
 
}
Line 236: Line 235:
 
if ((b11==1 && b12==1 && b13==1)||(b21==1 && b22==1 && b23==1)||(b31==1 && b32==1 && b33==1)||(b31==1 && b22==1 && b13==1)||(b11==1 && b22==1 && b33==1)||(b31==1 && b21==1 && b11==1)||(b12==1 && b32==1 && b22==1)||(b13==1 && b23==1 && b33==1))
 
if ((b11==1 && b12==1 && b13==1)||(b21==1 && b22==1 && b23==1)||(b31==1 && b32==1 && b33==1)||(b31==1 && b22==1 && b13==1)||(b11==1 && b22==1 && b33==1)||(b31==1 && b21==1 && b11==1)||(b12==1 && b32==1 && b22==1)||(b13==1 && b23==1 && b33==1))
 
{
 
{
msgbox = new QMessageBox;
+
msgbox = new QMessageBox(this);
 
msgbox->setText("Player 1 Win ");
 
msgbox->setText("Player 1 Win ");
 
msgbox->setStyleSheet("* { background-color:rgb(199,147,88); padding: 7px ; color:rgb(255,255,255)}");
 
msgbox->setStyleSheet("* { background-color:rgb(199,147,88); padding: 7px ; color:rgb(255,255,255)}");
Line 243: Line 242:
 
}
 
}
 
if ((b11==0 && b12==0 && b13==0)||(b21==0 && b22==0 && b23==0)||(b31==0 && b32==0 && b33==0)||(b31==0 && b22==0 && b13==0)||(b11==0 && b22==0 && b33==0)||(b31==0 && b21==0 && b11==0)||(b12==0 && b32==0 && b22==0)||(b13==0 && b23==0 && b33==0))
 
if ((b11==0 && b12==0 && b13==0)||(b21==0 && b22==0 && b23==0)||(b31==0 && b32==0 && b33==0)||(b31==0 && b22==0 && b13==0)||(b11==0 && b22==0 && b33==0)||(b31==0 && b21==0 && b11==0)||(b12==0 && b32==0 && b22==0)||(b13==0 && b23==0 && b33==0))
{msgbox = new QMessageBox;
+
{msgbox = new QMessageBox(this);
 
msgbox->setText("Player 2 Win ");
 
msgbox->setText("Player 2 Win ");
 
msgbox->setStyleSheet("* { background-color:rgb(199,147,88); padding: 7px ; color:rgb(255,255,255)}");
 
msgbox->setStyleSheet("* { background-color:rgb(199,147,88); padding: 7px ; color:rgb(255,255,255)}");
Line 255: Line 254:
 
== Header File ==
 
== Header File ==
  
<code cpp>
+
<code cpp-qt>
  
 
#ifndef TICTACTOE_H
 
#ifndef TICTACTOE_H
Line 314: Line 313:
 
== Screenshots ==
 
== Screenshots ==
  
[[Image:tictactoe1.jpg]]
+
[[File:tictactoe1.jpg]]
  
[[Image:tictactoe2.jpg]]
+
[[File:tictactoe2.jpg]]
  
  
 
== Download ==
 
== Download ==
  
* Download the working Example from this link: [[Media:tictactoe.zip|tictactoe.zip]]
+
* Download the working example from this link: [[Media:tictactoe.zip|tictactoe.zip]]
 +
 
 +
==Related links==
 +
[[Archived:How to use QPushButton]][[Category:MeeGo Harmattan]] [[Category:Symbian]]
 +
<!-- Translation --> [[pt:Archived:Como criar um jogo da velha, usando Qt]]

Latest revision as of 04:14, 11 October 2012

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.

This article demonstrates how to create a simple tic-tac-toe game using QWidgets. The game rules are well known and the implementation is self-explanatory - hence these are not explained here.

Article Metadata
Code Example
Source file: Media:tictactoe.zip
Tested with
Devices(s): Emulator
Compatibility
Platform(s): S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
Symbian
Platform Security
Signing Required: Self-Signed
Capabilities: None
Article
Keywords: QPushButton
Created: james1980 (03 Feb 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

[edit] Source File

#include "tictactoe.h"
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
 
tictactoe::tictactoe(QWidget *parent)
: QWidget(parent)
{
layout = new QVBoxLayout(this);
rw1 = new QHBoxLayout(this);
rw2 = new QHBoxLayout(this);
rw3 = new QHBoxLayout(this);
flag=1;
 
button11 = new QPushButton(this);
button12 = new QPushButton(this);
button13 = new QPushButton(this);
button21 = new QPushButton(this);
button22 = new QPushButton(this);
button23 = new QPushButton(this);
button31 = new QPushButton(this);
button32 = new QPushButton(this);
button33 = new QPushButton(this);
setStyleSheet("* { background-color:rgb(199,147,88); padding: 7px ; color:rgb(255,255,255)}");
 
connect(button11,SIGNAL(clicked()),this,SLOT(mark_button11()));
connect(button12,SIGNAL(clicked()),this,SLOT(mark_button12()));
connect(button13,SIGNAL(clicked()),this,SLOT(mark_button13()));
connect(button21,SIGNAL(clicked()),this,SLOT(mark_button21()));
connect(button22,SIGNAL(clicked()),this,SLOT(mark_button22()));
connect(button23,SIGNAL(clicked()),this,SLOT(mark_button23()));
connect(button31,SIGNAL(clicked()),this,SLOT(mark_button31()));
connect(button32,SIGNAL(clicked()),this,SLOT(mark_button32()));
connect(button33,SIGNAL(clicked()),this,SLOT(mark_button33()));
rw1->addWidget(button11);
rw1->addWidget(button12);
rw1->addWidget(button13);
rw2->addWidget(button21);
rw2->addWidget(button22);
rw2->addWidget(button23);
rw3->addWidget(button31);
rw3->addWidget(button32);
rw3->addWidget(button33);
layout->addLayout(rw1);
layout->addLayout(rw2);
layout->addLayout(rw3);
 
setLayout(layout);
 
}
 
tictactoe::~tictactoe()
{
// No need to delete any object that got a parent that is properly deleted.
 
}
void tictactoe::mark_button11()
{
if(flag==1)
{flag=0;
button11->setText("X");
b11=1;
}
else
{flag=1;
b11=0;
button11->setText("O");
}
button11->setDisabled(1);
check_winner();
 
}
 
void tictactoe::mark_button12()
{
if(flag==1)
{flag=0;
b12=1;
button12->setText("X");
}
else
{flag=1;
button12->setText("O");
b12=0;
}
check_winner();
button12->setDisabled(1);
}
 
void tictactoe::mark_button13()
{
if(flag==1)
{flag=0;
b13=1;
button13->setText("X");
}
else
{flag=1;
button13->setText("O");
b13=0;
 
}
check_winner();
button13->setDisabled(1);
}
 
void tictactoe::mark_button21()
{
if(flag==1)
{flag=0;
button21->setText("X");
b21=1;
}
else
{flag=1;
button21->setText("O");
b21=0;
}
check_winner();
button21->setDisabled(1);
}
 
void tictactoe::mark_button22()
{
if(flag==1)
{flag=0;
button22->setText("X");
b22=1;
}
else
{flag=1;
b22=0;
button22->setText("O");
}
check_winner();
button22->setDisabled(1);
}
 
void tictactoe::mark_button23()
{
if(flag==1)
{flag=0;
b23=1;
button23->setText("X");
}
else
{flag=1;
b23=0;
button23->setText("O");
}
check_winner();
button23->setDisabled(1);
}
 
void tictactoe::mark_button31()
{
if(flag==1)
{flag=0;
b31=1;
button31->setText("X");
}
else
{flag=1;
b31=0;
button31->setText("O");
}
check_winner();
button31->setDisabled(1);
}
 
void tictactoe::mark_button32()
{
if(flag==1)
{flag=0;
b32=1;
button32->setText("X");
}
else
{flag=1;
b32=0;
 
button32->setText("O");
}
check_winner();
button32->setDisabled(1);
}
 
void tictactoe::mark_button33()
{
if(flag==1)
{flag=0;
b33=1;
button33->setText("X");
}
else
{flag=1;
b33=0;
button33->setText("O");
}
check_winner();
button33->setDisabled(1);
}
 
void tictactoe::check_winner()
{
if ((b11==1 && b12==1 && b13==1)||(b21==1 && b22==1 && b23==1)||(b31==1 && b32==1 && b33==1)||(b31==1 && b22==1 && b13==1)||(b11==1 && b22==1 && b33==1)||(b31==1 && b21==1 && b11==1)||(b12==1 && b32==1 && b22==1)||(b13==1 && b23==1 && b33==1))
{
msgbox = new QMessageBox(this);
msgbox->setText("Player 1 Win ");
msgbox->setStyleSheet("* { background-color:rgb(199,147,88); padding: 7px ; color:rgb(255,255,255)}");
msgbox->exec();
qApp->quit();
}
if ((b11==0 && b12==0 && b13==0)||(b21==0 && b22==0 && b23==0)||(b31==0 && b32==0 && b33==0)||(b31==0 && b22==0 && b13==0)||(b11==0 && b22==0 && b33==0)||(b31==0 && b21==0 && b11==0)||(b12==0 && b32==0 && b22==0)||(b13==0 && b23==0 && b33==0))
{msgbox = new QMessageBox(this);
msgbox->setText("Player 2 Win ");
msgbox->setStyleSheet("* { background-color:rgb(199,147,88); padding: 7px ; color:rgb(255,255,255)}");
msgbox->exec();
qApp->quit();
}
 
}

[edit] Header File

#ifndef TICTACTOE_H
#define TICTACTOE_H
 
#include <QtGui/QWidget>
#include "ui_tictactoe.h"
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>
 
class tictactoe : public QWidget
{
Q_OBJECT
 
public:
tictactoe(QWidget *parent = 0);
bool flag;
int b11,b12,b13,b21,b22,b23,b31,b32,b33;
~tictactoe();
private slots:
void mark_button11();
void mark_button12();
void mark_button13();
 
void mark_button21();
void mark_button22();
void mark_button23();
 
void mark_button31();
void mark_button32();
void mark_button33();
 
void check_winner();
private:
QVBoxLayout *layout;
QHBoxLayout *rw1;
QHBoxLayout *rw2;
QHBoxLayout *rw3;
QMessageBox *msgbox;
QPushButton *button11;
QPushButton *button12;
QPushButton *button13;
QPushButton *button21;
QPushButton *button22;
QPushButton *button23;
QPushButton *button31;
QPushButton *button32;
QPushButton *button33;
 
 
};
 
#endif // TICTACTOE_H

[edit] Screenshots

Tictactoe1.jpg

Tictactoe2.jpg


[edit] Download

[edit] Related links

Archived:How to use QPushButton

This page was last modified on 11 October 2012, at 04:14.
230 page views in the last 30 days.
×