×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Creating Tic-Tac-Toe using QWidgets

From Nokia Developer Wiki
Jump to: navigation, search
savaj (Talk | contribs)
(link updated)
mind_freak (Talk | contribs)
Line 84: Line 84:
 
tictactoe::~tictactoe()
 
tictactoe::~tictactoe()
 
{
 
{
 
+
    if(rw1)
 +
    {
 +
        delete rw1;
 +
    }
 +
    if(rw2)
 +
    {
 +
        delete rw2;
 +
    }
 +
    if(rw3)
 +
    {
 +
        delete rw3;
 +
    }
 +
    if(msgbox)
 +
    {
 +
        delete msgbox;
 +
    }
 +
    if(button11)
 +
    {
 +
        delete button11;
 +
    }
 +
    if(button12)
 +
    {
 +
        delete button12;
 +
    }
 +
    if(button13)
 +
    {
 +
        delete button13;
 +
    }
 +
    if(button21)
 +
    {
 +
        delete button21;
 +
    }
 +
    if(button22)
 +
    {
 +
        delete button22;
 +
    }
 +
    if(button23)
 +
    {
 +
        delete button23;
 +
    }
 +
    if(button31)
 +
    {
 +
        delete button31;
 +
    }
 +
    if(button32)
 +
    {
 +
        delete button32;
 +
    }
 +
    if(button33)
 +
    {
 +
        delete button33;
 +
    }
 +
    if(lay)
 +
    {
 +
        delete lay;
 +
    }
 
}
 
}
 
void tictactoe::mark_button11()
 
void tictactoe::mark_button11()
Line 325: Line 380:
  
 
==Related links==
 
==Related links==
*More about QPushButton[http://wiki.forum.nokia.com/index.php/How_to_use_QPushButton_%28Qt_for_S60%29 QPushButton]
+
[[How to use QPushButton (Qt for S60)]]

Revision as of 20:27, 25 July 2009


Article Metadata
Tested with
Devices(s): Emulator
Compatibility
Platform(s): S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
Symbian
Article
Keywords: QPushButton
Created: (03 Feb 2009)
Last edited: mind_freak (25 Jul 2009)


Overview

This article demonstrates how to create a simple tic-tac-toe game in Qt for S60. 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


Source File

#include "tictactoe.h"
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
 
tictactoe::tictactoe(QWidget *parent)
: QWidget(parent)
{
layout = new QVBoxLayout;
rw1 = new QHBoxLayout;
rw2 = new QHBoxLayout;
rw3 = new QHBoxLayout;
flag=1;
 
button11 = new QPushButton;
button12 = new QPushButton;
button13 = new QPushButton;
button21 = new QPushButton;
button22 = new QPushButton;
button23 = new QPushButton;
button31 = new QPushButton;
button32 = new QPushButton;
button33 = new QPushButton;
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()
{
if(rw1)
{
delete rw1;
}
if(rw2)
{
delete rw2;
}
if(rw3)
{
delete rw3;
}
if(msgbox)
{
delete msgbox;
}
if(button11)
{
delete button11;
}
if(button12)
{
delete button12;
}
if(button13)
{
delete button13;
}
if(button21)
{
delete button21;
}
if(button22)
{
delete button22;
}
if(button23)
{
delete button23;
}
if(button31)
{
delete button31;
}
if(button32)
{
delete button32;
}
if(button33)
{
delete button33;
}
if(lay)
{
delete lay;
}
}
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;
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;
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 (Qt for S60)

113 page views in the last 30 days.
×