×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Creating Tic-Tac-Toe using QWidgets

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix ArticleMetaData and RevieweApproval)
hamishwillee (Talk | contribs)
m (Hamishwillee - Tidy wiki text)
Line 1: Line 1:
 +
[[Category:QWidget UI]][[Category:Code Examples]][[Category:Games]]
 +
{{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.}}
 
{{Archived|timestamp=20120209041352|user=[[User:Hamishwillee|<br />----]]|[[: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.}}
 
{{Archived|timestamp=20120209041352|user=[[User:Hamishwillee|<br />----]]|[[: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.}}
[[Category:QWidget UI]]
 
 
 
__NOTOC__
 
__NOEDITSECTION__
 
 
{{ArticleMetaData <!-- v1.1 -->
 
{{ArticleMetaData <!-- v1.1 -->
 
|sourcecode= [[Media:tictactoe.zip]]  
 
|sourcecode= [[Media:tictactoe.zip]]  
Line 13: Line 10:
 
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
+
|signing= Self-Signed
|capabilities= <!-- Capabilities required (e.g. Location, NetworkServices. -->)
+
|capabilities= None
 
|keywords= QPushButton
 
|keywords= QPushButton
|id= ...
 
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->
Line 27: Line 23:
 
|creationdate= 20090203
 
|creationdate= 20090203
 
|author= [[User:James1980]]
 
|author= [[User:James1980]]
<!-- The following are not in current metadata -->
 
|subcategory= Game
 
|category= Qt
 
 
}}
 
}}
 
 
==Overview==
 
 
This article demonstrates how to create a simple tic-tac-toe game in Qt. All of you might be familiar with this game so there is no point in explaining the rules here. And the code of  the game is also self explanatory.
 
 
 
==Preconditions==
 
 
* Install the [[Qt SDK]]
 
 
  
 
== Source File ==
 
== Source File ==
Line 341: Line 323:
  
 
==Related links==
 
==Related links==
[[How to use QPushButton in Qt]] [[Category:Code Examples]]
+
[[How to use QPushButton in Qt]]

Revision as of 07:38, 9 February 2012

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.

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.

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 (09 Feb 2012)

Contents

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();
}
 
}

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

Screenshots

Tictactoe1.jpg

Tictactoe2.jpg


Download

Related links

How to use QPushButton in Qt

140 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×