Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
nerbivol authored Sep 3, 2021
1 parent ea7ed78 commit 32666c0
Show file tree
Hide file tree
Showing 30 changed files with 2,475 additions and 0 deletions.
41 changes: 41 additions & 0 deletions TowerofHanoi.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

TARGET = TowerofHanoi
TEMPLATE = app


SOURCES += \
hanoi.cpp \
main.cpp\
disk.cpp \
menuwindow.cpp \
move.cpp \
space.cpp

HEADERS += \
disk.h \
hanoi.h \
menuwindow.h \
move.h \
space.h

FORMS += \
hanoi.ui \
menuwindow.ui

RESOURCES += \
resource.qrc
296 changes: 296 additions & 0 deletions TowerofHanoi.pro.user

Large diffs are not rendered by default.

316 changes: 316 additions & 0 deletions TowerofHanoi.pro.user.367c2af

Large diffs are not rendered by default.

336 changes: 336 additions & 0 deletions TowerofHanoi.pro.user.67c1419.4.8-pre1

Large diffs are not rendered by default.

336 changes: 336 additions & 0 deletions TowerofHanoi.pro.user.68742aa

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions disk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "space.h"
#include "disk.h"
#include "ui_hanoi.h"
#include <QPainter>

disk::disk(int size, space* on,int pos, QWidget* parent)
{
this->size = size;
this->on = on;
this->pos = pos;
this->setParent(parent);
show();
}

void disk::setPos(space* down, int p){

if(On() != down){
on = down;
setParent(down->parentWidget());
show();
}
pos = p;
repaint();
}

void disk::Lift(){
pos = 12;
repaint();
}

void disk::paintEvent(QPaintEvent *event){

Q_UNUSED(event)
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QColor edgeDiskColor(0, 0, 0);
painter.setPen(edgeDiskColor);

int width = size*5+size*3 + 40;
QColor colors[] = {QColor(35,110,150), QColor(21,178,211),QColor(255,215,0), QColor(243,135,47), QColor(255,89,143), QColor(51, 102, 255), QColor(102,0,255), QColor(204,0,255)};
QColor diskTop = colors[size-1];
QColor diskShadow = diskTop.darker(140);
painter.setBrush(diskShadow);
painter.drawEllipse(QPointF(130,355-23*(pos+1)),width-1,22+size*4);

painter.setBrush(diskTop);
painter.drawEllipse(QPointF(130,343-23*(pos+1)),width,24+size*4);

diskShadow = diskTop.darker(230);
painter.setBrush(diskShadow);
painter.drawEllipse(QPointF(130,345-23*(pos+1)),width/4,8);

}

21 changes: 21 additions & 0 deletions disk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<QWidget>
class space;
class disk: public QWidget
{
private:

int size;
space* on;
int Size(){return size;}
int pos;

public:
void setPos(space*down, int p);
void Lift();
void paintEvent(QPaintEvent * event);
disk(int size, space* on,int pos, QWidget*);
space* On(){return on;}
friend class space;
};


237 changes: 237 additions & 0 deletions hanoi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
#include "hanoi.h"
#include "ui_hanoi.h"
#include <QPainter>
#include <QPixmap>
#include <QInputDialog>
#include <iostream>
#include <QMessageBox>

disk*moving = NULL;

Hanoi::Hanoi(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Hanoi){
ui->setupUi(this);

ui->pushButton_1->setStyleSheet("QPushButton {background: None; border: 0}\
QPushButton:hover {background: None}\
QPushButton:pressed {background: None}");
ui->pushButton_2->setStyleSheet("QPushButton {background: None; border: 0}\
QPushButton:hover {background: None}\
QPushButton:pressed {background: None}");
ui->pushButton_3->setStyleSheet("QPushButton {background: None; border: 0}\
QPushButton:hover {background: None}\
QPushButton:pressed {background: None}");

value = 4;

spaces[0] = new space(0, value,ui->pushButton_1);
spaces[1] = new space(1, 0,ui->pushButton_2);
spaces[2] = new space(2, 0,ui->pushButton_3);
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(delayedAction()));
autoplay= false;
ui->label_4->setNum(stack.size());
QMessageBox::information(this, "Старт", "Ваше завдання полягає в тому, щоб перенести диски з першої вежі до третьої. При цьому не можна класти більший диск на менший. Удачі!");

}

void Hanoi::delayedAction(){
if(autoplay){
if(!queue.empty()){
Move autom = queue.dequeue();
moving = spaces[autom.getFrom()]->take();
spaces[autom.getTo()]->put(moving, value);
moving = NULL;
stack.push(autom);
ui->label_4->setNum(stack.size());
}else{
autoplay = false;
ui->pushButton_1->setEnabled(true);
ui->pushButton_2->setEnabled(true);
ui->pushButton_3->setEnabled(true);
timer->stop();
}

}else{
if(stack.empty()){
ui->pushButton_1->setEnabled(true);
ui->pushButton_2->setEnabled(true);
ui->pushButton_3->setEnabled(true);
timer->stop();
}else{
on_action_Undo_triggered();
}
}
}

void Hanoi::paintEvent(QPaintEvent *event){
Q_UNUSED(event);
QPixmap pix(":/fon/img/PlatformforHunaiTower.tif");
int w = ui->label_3->width();
int h = ui->label_3->height();
ui->label_3->setPixmap(pix.scaled(w,h+100, Qt::KeepAspectRatio));
QPixmap pix1(":/fon/img/Fantasy World Wallpapers HD.jpg");
w = ui->label_5->width();
h = ui->label_5->height();
ui->label_5->setPixmap(pix1.scaled(w,h, Qt::KeepAspectRatio));
}

Hanoi::~Hanoi()
{
delete ui;
for (int i=0;i<3;i++){
delete spaces[i];
}
moving = NULL;
stack.clear();
queue.clear();
}


void Hanoi::on_pushButton_clicked(space* position){
if(!moving){
moving = position->take();
}else{
int from = moving->On()->getIndex();
if(position->put(moving, value)){
Move mov(from,position->getIndex());
stack.push(mov);
moving = NULL;
ui->label_4->setNum(stack.size());
}else{
moving->On()->put(moving, value);
ui->label_4->setNum(stack.size());
moving = NULL;
}
}
}


void Hanoi::CalculateAutoplay(int disks, int from, int to, int temp){
if(disks == 0){
return;
}else{
CalculateAutoplay(disks-1,from,temp,to);
queue.enqueue(Move(from,to));
CalculateAutoplay(disks-1,temp,to,from);
}
}

void Hanoi::on_pushButton_1_clicked()
{
on_pushButton_clicked(spaces[0]);
}

void Hanoi::on_pushButton_2_clicked()
{
on_pushButton_clicked(spaces[1]);
}

void Hanoi::on_pushButton_3_clicked()
{
on_pushButton_clicked(spaces[2]);
}

void Hanoi::on_action_New_triggered()
{

for (int i = 0;i < 3; i++){
delete spaces[i];
}
if(moving){
delete moving;
}
moving = NULL;
stack.clear();
queue.clear();
ui->label_4->setNum(stack.size());

spaces[0] = new space(0, value,ui->pushButton_1);
spaces[1] = new space(1, 0,ui->pushButton_2);
spaces[2] = new space(2, 0,ui->pushButton_3);
}


void Hanoi::on_action_Undo_triggered()
{
if(!stack.empty()){
Move mUndo = stack.pop();
space* to = NULL;
space* from = NULL;
to = spaces[mUndo.getTo()];
from = spaces[mUndo.getFrom()];
if(moving){
moving->On()->put(moving, value);
moving = NULL;
}
moving = to->take();
from->put(moving, value);
moving = NULL;
ui->label_4->setNum(stack.size());
}
}

void Hanoi::on_action_Undo_All_triggered()
{
ui->pushButton_1->setEnabled(false);
ui->pushButton_2->setEnabled(false);
ui->pushButton_3->setEnabled(false);

timer->start(100);
}

void Hanoi::on_actionAuto_Play_triggered()

{
ui->pushButton_1->setEnabled(false);
ui->pushButton_2->setEnabled(false);
ui->pushButton_3->setEnabled(false);
autoplay = true;
on_action_New_triggered();
CalculateAutoplay(spaces[0]->getNdisks(), 0,2,1);
timer->start(100);
}

void Hanoi::on_action_2_triggered()
{
value = 2;
ui->action_New->trigger();
}

void Hanoi::on_action_3_triggered()
{
value = 3;
ui->action_New->trigger();
}

void Hanoi::on_action_4_triggered()
{
value = 4;
ui->action_New->trigger();
}

void Hanoi::on_action_5_triggered()
{
value = 5;
ui->action_New->trigger();
}

void Hanoi::on_action_6_triggered()
{
value = 6;
ui->action_New->trigger();
}

void Hanoi::on_action_7_triggered()
{
value = 7;
ui->action_New->trigger();
}

void Hanoi::on_action_8_triggered()
{
value = 8;
ui->action_New->trigger();
}

Loading

0 comments on commit 32666c0

Please sign in to comment.