Skip to content

Commit

Permalink
added button class, unicode texts and comment after end of game
Browse files Browse the repository at this point in the history
  • Loading branch information
ggawryal committed Aug 13, 2018
1 parent e3198a0 commit 514da57
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 53 deletions.
1 change: 1 addition & 0 deletions Warcaby.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Unit filename="MaxiJumpSequenceFinder.cpp" />
<Unit filename="MoveController.cpp" />
<Unit filename="ResourceManager.cpp" />
<Unit filename="headers/Button.h" />
<Unit filename="headers/Checkboard.h" />
<Unit filename="headers/Checker.h" />
<Unit filename="headers/Grid.h" />
Expand Down
9 changes: 7 additions & 2 deletions Warcaby.depend
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# depslib dependency file v1.0
1534005483 source:d:\programowanie\warcaby\main.cpp
1534166909 source:d:\programowanie\warcaby\main.cpp
<SFML/Graphics.hpp>
<iostream>
<vector>
Expand All @@ -16,6 +16,7 @@
"MouseHandler.h"
"Highlighter.h"
"WindowResizer.h"
"Button.h"

1532364783 d:\programowanie\libs\sfml\include\sfml\graphics.hpp
<SFML/Window.hpp>
Expand Down Expand Up @@ -464,7 +465,7 @@
<cassert>
<iostream>

1533846522 d:\programowanie\warcaby\headers\movecontroller.h
1534165135 d:\programowanie\warcaby\headers\movecontroller.h
<SFML/System.hpp>
"Checker.h"
"Checkboard.h"
Expand Down Expand Up @@ -506,3 +507,7 @@
1533899411 d:\programowanie\warcaby\headers\windowresizer.h
<SFML/Graphics.hpp>

1534166849 d:\programowanie\warcaby\headers\button.h
<SFML/Graphics.hpp>
"ResourceManager.h"

67 changes: 34 additions & 33 deletions Warcaby.layout
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,82 @@
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="headers\MoveController.h" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="MaxiJumpSequenceFinder.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="6769" topLine="158" />
<Cursor1 position="1042" topLine="0" />
</Cursor>
<Folding>
<Collapse line="113" />
</Folding>
</File>
<File name="headers\Checkboard.h" open="1" top="0" tabpos="9" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="headers\Button.h" open="1" top="0" tabpos="11" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="91" topLine="0" />
<Cursor1 position="1440" topLine="31" />
</Cursor>
<Folding>
<Collapse line="33" />
<Collapse line="50" />
</Folding>
</File>
<File name="headers\Checker.h" open="1" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="headers\basics.h" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="914" topLine="0" />
<Cursor1 position="411" topLine="0" />
</Cursor>
</File>
<File name="MaxiJumpSequenceFinder.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="headers\Highlighter.h" open="1" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="1042" topLine="0" />
<Cursor1 position="99" topLine="0" />
</Cursor>
</File>
<File name="headers\basics.h" open="1" top="0" tabpos="8" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="headers\MaxiJumpSequenceFinder.h" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="411" topLine="0" />
<Cursor1 position="228" topLine="84" />
</Cursor>
</File>
<File name="main.cpp" open="1" top="1" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="headers\MouseHandler.h" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="6720" topLine="179" />
<Cursor1 position="1192" topLine="9" />
</Cursor>
</File>
<File name="MoveController.cpp" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="headers\MoveController.h" open="1" top="0" tabpos="12" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="6005" topLine="140" />
<Cursor1 position="3797" topLine="144" />
</Cursor>
<Folding>
<Collapse line="113" />
</Folding>
</File>
<File name="headers\MaxiJumpSequenceFinder.h" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="headers\ResourceManager.h" open="1" top="0" tabpos="9" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="228" topLine="84" />
<Cursor1 position="1821" topLine="35" />
</Cursor>
</File>
<File name="ResourceManager.cpp" open="1" top="0" tabpos="14" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="ResourceManager.cpp" open="1" top="0" tabpos="10" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="705" topLine="0" />
</Cursor>
</File>
<File name="headers\ResourceManager.h" open="1" top="0" tabpos="13" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="MoveController.cpp" open="1" top="0" tabpos="13" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="1781" topLine="44" />
<Cursor1 position="5513" topLine="140" />
</Cursor>
</File>
<File name="headers\WindowResizer.h" open="1" top="0" tabpos="12" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="headers\Checkboard.h" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
<Cursor1 position="91" topLine="0" />
</Cursor>
</File>
<File name="headers\Grid.h" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="headers\Checker.h" open="0" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="914" topLine="0" />
</Cursor>
</File>
<File name="headers\Grid.h" open="0" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="439" topLine="0" />
</Cursor>
</File>
<File name="headers\Highlighter.h" open="1" top="0" tabpos="11" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="headers\WindowResizer.h" open="1" top="0" tabpos="8" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="99" topLine="0" />
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="headers\MouseHandler.h" open="1" top="0" tabpos="10" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="main.cpp" open="1" top="1" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="1192" topLine="0" />
<Cursor1 position="8400" topLine="226" />
</Cursor>
</File>
</CodeBlocks_layout_file>
62 changes: 62 additions & 0 deletions headers/Button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#ifndef BUTTON_H
#define BUTTON_H
#include <SFML/Graphics.hpp>
#include "ResourceManager.h"

class Button : public sf::Drawable{
sf::RectangleShape shape;
sf::Text text;
void fixTextAlignment(){
text.setOrigin(text.getLocalBounds().width/2.0f,text.getLocalBounds().height/2.0f);
sf::Vector2f position = shape.getPosition();
text.setPosition(sf::Vector2f(position.x + shape.getLocalBounds().width/2.0f,position.y + shape.getLocalBounds().height/2.0f));
}
public:
Button(sf::IntRect rect) {
setRect(rect);
text.setFont(FontManager::instance().get("arial"));
text.setCharacterSize(32);
text.setColor(sf::Color(190,190,190));
}

void setPosition(sf::Vector2f position){
shape.setPosition(position);
fixTextAlignment();
}
sf::Vector2f getPosition() const{
return shape.getPosition();
}

void setSize(sf::Vector2f size){
shape.setSize((sf::Vector2f)size);
}
sf::Vector2f getSize() const{
return shape.getSize();
}

void setRect(sf::IntRect rect){
setPosition(sf::Vector2f(rect.left,rect.top));
setSize(sf::Vector2f(rect.width,rect.height));
}

void setBackgroundColor(sf::Color color){
shape.setFillColor(color);
}
void setText(sf::String str){
text.setString(str);
fixTextAlignment();

}
virtual void draw(sf::RenderTarget& target,sf::RenderStates states) const override{
target.draw(shape,states);
target.draw(text,states);
}

bool isPointInside(sf::Vector2i point){
return (point.x >= shape.getPosition().x && point.y >= shape.getPosition().y &&
point.x <= shape.getPosition().x + shape.getSize().x && point.y <= shape.getPosition().y + shape.getSize().y);
}

};

#endif // BUTTON_H
24 changes: 20 additions & 4 deletions headers/MoveController.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ class MoveController{


class Rules{
int end_game_state = -1; // -1 -> still playing, 0 - white wins, 1 - black wins, 2 - draw
int end_game_state = -1; // -1 -> still playing, 0 -> white wins, 1 -> black wins, 2 -> draw
int end_game_cause = -1; // -1 -> other, 0 -> no enemy pawns, 1 -> enemy pawn are blocked, 2 -> 15moves rule
int maximumQueenMovesWithoutDraw = 15;
public:
MoveController & moveController;
Expand All @@ -196,21 +197,34 @@ class Rules{
bool areStillPlaying(){
return end_game_state == -1;
}

int getEndOfGameReason(){
return end_game_cause;
}

virtual bool existAnyCorrectMoveWith(int x1,int y1){
return true;
}
virtual bool isEndOfGame(){
int blackCheckers = moveController.getPawnCount(false), whiteCheckers = moveController.getPawnCount(true);
assert(blackCheckers > 0 || whiteCheckers > 0);
if(blackCheckers == 0)
if(blackCheckers == 0){
end_game_state = 0;
if(whiteCheckers == 0)
end_game_cause = 0;
return end_game_state >= 0;
}
if(whiteCheckers == 0){
end_game_state = 1;
end_game_cause = 0;
return end_game_state >= 0;
}
if(moveController.areAllCheckersBlocked()){
if(moveController.isBlackOnTurn())
end_game_state = 0;
else
end_game_state = 1;
end_game_cause = 1;
return end_game_state >= 0;
}

auto lastMovedCheckers = moveController.getMoveCheckersType();
Expand All @@ -224,8 +238,10 @@ class Rules{
}
}
auto countVect = moveController.getCheckerCountVector();
if(countVect.back() == countVect[countVect.size()-1-2*maximumQueenMovesWithoutDraw] && draw == true)
if(countVect.back() == countVect[countVect.size()-1-2*maximumQueenMovesWithoutDraw] && draw == true){
end_game_state = 2;
end_game_cause = 2;
}
}


Expand Down
70 changes: 56 additions & 14 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "MouseHandler.h"
#include "Highlighter.h"
#include "WindowResizer.h"
#include "Button.h"

using namespace std;

Expand Down Expand Up @@ -142,6 +143,8 @@ int main(){
checkboard.drawer.setPosition(sf::Vector2i(50,50));
checkboard.drawer.setImageSize(700,700);



GridPositioner gp(checkboard.drawer);

MoveController mv(checkboard);
Expand All @@ -161,8 +164,10 @@ int main(){

sf::Event event;
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed)
if (event.type == sf::Event::Closed){
window.close();
exit(0);
}
MouseHandler::instance().handle(event);
}

Expand All @@ -184,20 +189,49 @@ int main(){
window.draw(checkboard.drawer);
window.display();
}
sf::String message = "";
if(rules->blackWon())
message = "Wygraly biale";


sf::String winnerMessage = "";
sf::String endOfGameReason = "";

if(rules->whiteWon())
message = "Wygraly czarne";
winnerMessage = L"Wygrały białe";
if(rules->blackWon())
winnerMessage = L"Wygrały czarne";
if(rules->isDraw())
message = "Remis";
winnerMessage = L"Remis";

if(rules->getEndOfGameReason() == 0)
endOfGameReason = L"Zbito wszystkie\nfigury przeciwnika";

if(rules->getEndOfGameReason() == 1)
endOfGameReason = L"Zablokowano wszystkie\nfigury przeciwnika";

if(rules->getEndOfGameReason() == 2)
endOfGameReason = L"Wykonano po 15 posunięć\nbez bicia ani ruchu pionem";



sf::Text mainText, commentText;
mainText.setFont(FontManager::instance().get("arial"));
commentText.setFont(FontManager::instance().get("arial"));

mainText.setPosition(800,50);
commentText.setPosition(800,150);

mainText.setColor(sf::Color(190,190,190));
commentText.setColor(sf::Color(190,190,190));

mainText.setCharacterSize(64);
commentText.setCharacterSize(25);

mainText.setString(winnerMessage);
commentText.setString(endOfGameReason);

Button button(sf::IntRect(900,650,250,80));
button.setBackgroundColor(sf::Color(0,155,155));
button.setText(L"Wyjście");

sf::Text text;
text.setFont(FontManager::instance().get("arial"));
text.setPosition(800,50);
text.setColor(sf::Color::Black);
text.setCharacterSize(64);
text.setString(message);


while (window.isOpen()){
Expand All @@ -207,9 +241,17 @@ int main(){
window.close();
MouseHandler::instance().handle(event);
}
window.clear(sf::Color(100,100,100));
if(MouseHandler::instance().getButton() == sf::Mouse::Left &&
button.isPointInside(window.mapPixelToStd(MouseHandler::instance().getCurrentMousePosition()))){

window.close();
}
MouseHandler::instance().clear();
window.clear(sf::Color(70,70,70));
window.draw(checkboard.drawer);
window.draw(text);
window.draw(mainText);
window.draw(commentText);
window.draw(button);
window.display();
}

Expand Down

0 comments on commit 514da57

Please sign in to comment.