Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

This post has been edited 1 times, last edit by "Amleto" (Jan 28th 2012, 6:15pm)
This post has been edited 1 times, last edit by "Amleto" (Jan 28th 2012, 6:15pm)
This post has been edited 2 times, last edit by "Amleto" (Jan 28th 2012, 6:47pm)
This post has been edited 2 times, last edit by "Amleto" (Jan 28th 2012, 8:07pm)
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
/*
* File: spielbrett.cpp
* Author: Reini
*
* Created on 07. Dezember 2011, 17:17
*/
//#include <algorithm
#include <iostream>
#include "karte.h"
#include "spielbrett.h"
using std::cout;
using std::endl;
spielbrett::spielbrett(int length1, int height1): length(length1), height(height1), size(length1*height1){
Karte* card1 = new Karte(this);
card1->setStyleSheet("border-image: url(:/new/prefix1/beer.jpg);");
card1->move(length,height);
}
spielbrett::spielbrett(const spielbrett& orig) {
}
spielbrett::~spielbrett() {
}
void spielbrett::fillSpielbrett()
{
Karte* card1 = new Karte(this);
card1->setStyleSheet("border-image: url(:/new/prefix1/beer.jpg);");
card1->move(50,50);
// delete[] card1;
int idSize = length*height;
//I. Karten erstellen + cardOnBoard true setzen
for(int i = 1; i<=idSize; i++)
{
cards.push_back(new Karte(this));
cards.at(i-1)->setCardOnBoard(true);
}
//II. karten Ids füllen + StringNamen
srand( (unsigned)time( NULL ) );
int picIDSize = (length*height)/2;
int count= 1;
for(int i = 0; i < idSize; i++){
if(count > picIDSize)
count = 1;
cards.at(i)->setPicID(count);
cards.at(i)->setID(i+1);
switch(count){
case 1: cards.at(i)->setCardName("beer.jpg)");
break;
case 2: cards.at(i)->setCardName("patrick.jpg)");
break;
case 3: cards.at(i)->setCardName("pikachu.jpg)");
break;
case 4: cards.at(i)->setCardName("sonne.jpg)");
break;
case 5: cards.at(i)->setCardName("spongebob.jpg)");
break;
case 6: cards.at(i)->setCardName("thaddäus.jpg)");
break;
case 7: cards.at(i)->setCardName("thaddäus2.jpg)");
break;
case 8: cards.at(i)->setCardName("Basketball.png)");
break;
}
count++;
}
//karten ids müssen jetzt durchgemischt werden
//jede karte wird mit anderer karte vertauscht
for(int i = 0; i < idSize; i++){
int number = createRandomValues(i,idSize-1);
cout <<"nummer zufällig: " << number<<endl;
swapCards(cards.at(i),cards.at(number));
count++;
}
//III: fill coordsAndCards Vector
//größe = länge * breite
//for in for loop
int brettSize = -1;
for(int i = 1; i <= length; i++)
{
//i = xcoord, y = ycoord
//brettSize geht von 0 bis lenght*height
for(int j = 1; j<= height; j++)
{
brettSize++;
//vector.at(x) = 1.pair stelle
//vector.at(x).first.first = xCoord
//vector.at(x).first.second = yCoord
//vector.at(x).second = Karte*
allCoordsAndCards.push_back(std::make_pair(std::make_pair(i,j), cards.at(brettSize)) );
}
}
//IV: fill CardsBrettCoords
int x = 30;
int y = -10;
brettSize = -1;
for(int i = 1; i <= length; i++)
{
//i = xcoord, y = ycoord
//brettSize geht von 0 bis lenght*height
y+=40;
for(int j = 1; j<= height; j++)
{
if(x>300)
x= 30;
brettSize++;
//vector.at(x) = 1.pair stelle
//vector.at(x).first.first = xCoord
//vector.at(x).first.second = yCoord
//vector.at(x).second = Karte*
CardsBrettCoords.push_back(std::make_pair(x,y));
x+= 90;
}
cout <<"blub";
}
}
bool spielbrett::checkSlotEmpty(int slotPlace)
{
if(allCoordsAndCards.at(slotPlace).second->getCardOnBoard() == false)
return true;
else
return false;
}
vector<pair<pair<int,int>,Karte*> > spielbrett::getAllCoordsAndCards()
{
return allCoordsAndCards;
}
int spielbrett::getSize()
{
return size;
}
vector<Karte*> spielbrett::getCards()
{
return cards;
}
int spielbrett::createRandomValues(int min, int max){
//srand( (unsigned)time( NULL ) );
repaint();
int blubb = time(NULL) * 0.77;
if(min>=max)
return max;
return ( blubb % (max-min)) + min;
}
int spielbrett::getLength(){
return length;
}
int spielbrett::getFoundPlace1()
{
return foundPlace1;
}
int spielbrett::getFoundPlace2()
{
return foundPlace2;
}
void spielbrett::setFoundPlace1(int place){
foundPlace1 = place;
}
void spielbrett::setFoundPlace2(int place){
foundPlace2 = place;
}
bool spielbrett::checkCardsLeft(){
for(int i = 0; i < allCoordsAndCards.size(); i++)
{
if(checkSlotEmpty(i) ==false)
return true;
}
return false;
}
//alle Karten mit gespeichertem String werden auf ihrem
//Platz angezeigt von allCoordsAndCardsVector
//setStylesheet von allen Karten
void spielbrett::showAllCardsOnBrett()
{
cout <<"function: showAllCards";
// QString borderTag = "border-image: url(:/";
for(int i = 0; i < allCoordsAndCards.size(); i++)
{
//jedes Kartenbild anzeigen lassen
// QString cardName = cards.at(i)->getCardName();
// cards.at(i)->setStyleSheet("border-image: url(:/beer.jpg)");
// cards.at(i)->move(CardsBrettCoords.at(i).first, CardsBrettCoords.at(i).second);
//position:1,1 1,2 1,3 etc...
//Karte an richtiger Stelle anzeigen lassen
}
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
/*
* File: spielbrett.h
* Author: Reini
*
* Created on 07. Dezember 2011, 17:17
*/
#ifndef SPIELBRETT_H
#define SPIELBRETT_H
#include <time.h>
#include "Karte.h"
#include "mainwindow.h"
#include <string>
#include "qwidget.h"
#include <vector>
using std::vector;
using std::pair;
using std::string;
class spielbrett:public MainWindow{
public:
spielbrett(int lenght1, int height1);
spielbrett(const spielbrett& orig);
virtual ~spielbrett();
int getCurrentPos(int x, int y);
bool checkSlotEmpty(int slotPlace);
void fillSpielbrett(); // mit x*y karten
bool removeCardPair(pair<Karte*,Karte*> p);
bool checkCardsLeft();//falls alle cards vom brett sind
vector<pair<pair<int,int>,Karte*> > getAllCoordsAndCards();
int getSize();
vector<Karte*> getCards();
void showAllCardsOnBrett();
void swapCards(Karte* card1, Karte* card2)
{
int picIdTemp = card1->getPicID();
int IDTemp = card1->getID();
card1->setPicID(card2->getPicID());
card1->setID(card2->getID());
card2->setPicID(picIdTemp);
card2->setID(IDTemp);
}
int createRandomValues(int min, int max);
int getLength();
void setFoundPlace1(int place);
void setFoundPlace2(int place);
int getFoundPlace1();
int getFoundPlace2();
private:
int length;
int height;
int size;
int foundPlace1;
int foundPlace2;
vector<Karte*> cards;
vector<pair<pair<int,int>,Karte*> > allCoordsAndCards;
vector<pair<int,int> > CardsBrettCoords;
};
#endif /* SPIELBRETT_H */
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <QtGui/QApplication>
//#include "mainwindow.h"
#include "spielbrett.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
spielbrett* w = new spielbrett(4,4);
w->show();
w->fillSpielbrett();
return a.exec();
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
/*
* File: Karte.h
* Author: Reini
*
* Created on 23. November 2011, 01:10
*/
#ifndef KARTE_H
#define KARTE_H
#include "qpushbutton.h"
#include "mainwindow.h"
using std::string;
class Karte: public QPushButton{
public:
Karte(MainWindow* w);
Karte(const Karte& orig);
virtual ~Karte();
void setPicID(int id);
int getPicID();
void setID(int id);
int getID();
void setCardOnBoard(bool b);
bool getCardOnBoard();
void setCardName(QString name);
QString getCardName();
private:
int picId;
int ID;
bool cardOnBoard;
QString cardName; //example "beer.jpg)"
};
#endif /* KARTE_H */
|