×
Namespaces

Variants
Actions

Archived:Como criar um jogo da velha, usando Qt

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngAquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

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

Este artigo mostra com criar um jogo da velha simples, usando Qt. Todos vocês, podem está familiarizados com este jogo então, assim sendo não existe pontos de explicação sobre as regras do mesmo aqui. E o código é ainda auto explicativo.

Article Metadata

Testado com
Aparelho(s): Emulator

Compatibilidade
Plataforma(s): S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
Symbian

Platform Security
Tipo de assinatura requerido: Self-Signed
Capabilities: None

Artigo
Palavras-chave: QPushButton
Tradução:
Por valderind4
Última alteração feita por hamishwillee em 11 Oct 2012

Contents

Código fonte

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

Arquivo de cabeçalho(Header)

#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

Captura de tela

Tictactoe1.jpg

Tictactoe2.jpg


Downloads

  • Você pode fazer o download do exemplo neste link: tictactoe.zip

Links relacionados

Archived:Como usar um QPushButton

This page was last modified on 11 October 2012, at 01:13.
73 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.

×