×
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 (Fix typo)
hamishwillee (Talk | contribs)

Revision as of 13:31, 4 April 2011

{{{width}}}


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: hamishwillee (04 Apr 2011)


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


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 (Qt for Symbian)

96 page views in the last 30 days.
×