My first project with SFML 2.0 I just wanted to make something simple to see how stuff works with SFML.
Done in in 3,5 hours.
Download link:
http://www.2shared.com/file/JvIyLmij/TicTacToe.html#include <SFML/Graphics.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Window/Mouse.hpp>
#include <SFML/System/Sleep.hpp>
#include <SFML/Audio/SoundBuffer.hpp>
#include <SFML\Audio\Sound.hpp>
#include <iostream>
#include <sstream>
#include <string>
sf::Time thetime;
sf::Clock elapsed;
std::string IntToString(int d)
{
std::stringstream ss;
ss << d;
return ss.str();
}
std::string GetFrameRate()
{ static int frameCounter = 0;
static int fps = 0;
frameCounter++;
thetime = elapsed.getElapsedTime();
if(thetime.asMilliseconds() > 999)
{
fps = frameCounter;
frameCounter = 0;
elapsed.restart();
}
return IntToString(fps);
}
const int SCREEN_WIDTH=649;
const int SCREEN_HEIGHT=460;
const int SCREEN_BPP=32;
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP),"TicTacToe",!sf::Style::Resize|sf::Style::Close);
char *score[]={"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"};
enum BoardValues{x,o,empty};
BoardValues board[3][3];
BoardValues onPlay;
BoardValues winnerCheck()
{
if (board[0][0]==x && board[0][0]==board[1][1] && board[1][1]==board[2][2]) {return x;}
if (board[2][0]==x && board[2][0]==board[1][1] && board[1][1]==board[0][2]) {return x;}
if (board[0][0]==x && board[0][0]==board[1][0] && board[1][0]==board[2][0]) {return x;}
if (board[0][1]==x && board[0][1]==board[1][1] && board[1][1]==board[2][1]) {return x;}
if (board[0][2]==x && board[0][2]==board[1][2] && board[1][2]==board[2][2]) {return x;}
if (board[0][0]==x && board[0][0]==board[0][1] && board[0][1]==board[0][2]) {return x;}
if (board[1][0]==x && board[1][0]==board[1][1] && board[1][1]==board[1][2]) {return x;}
if (board[2][0]==x && board[2][0]==board[2][1] && board[2][1]==board[2][2]) {return x;}
if (board[0][0]==o && board[0][0]==board[1][1] && board[1][1]==board[2][2]) {return o;}
if (board[2][0]==o && board[2][0]==board[1][1] && board[1][1]==board[0][2]) {return o;}
if (board[0][0]==o && board[0][0]==board[1][0] && board[1][0]==board[2][0]) {return o;}
if (board[0][1]==o && board[0][1]==board[1][1] && board[1][1]==board[2][1]) {return o;}
if (board[0][2]==o && board[0][2]==board[1][2] && board[1][2]==board[2][2]) {return o;}
if (board[0][0]==o && board[0][0]==board[0][1] && board[0][1]==board[0][2]) {return o;}
if (board[1][0]==o && board[1][0]==board[1][1] && board[1][1]==board[1][2]) {return o;}
if (board[2][0]==o && board[2][0]==board[2][1] && board[2][1]==board[2][2]) {return o;}
return empty;
}
bool tie()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if (board[i][j]==empty)
return false;
}
}
return true;
}
inline void pause()
{
sf::Time forSleep;
forSleep=sf::seconds(2.5f);
sf::sleep(forSleep);
}
int main()
{
int xScore=0,oScore=0,tieScore=0;
sf::Text TtieScore(score[tieScore],sf::Font::getDefaultFont(),70);
TtieScore.setColor(sf::Color::Black);
TtieScore.setPosition(515,140);
sf::Text TxScore(score[xScore],sf::Font::getDefaultFont(),70);
TxScore.setColor(sf::Color(2,0,153));
TxScore.setPosition(515,35);
sf::Text ToScore(score[oScore],sf::Font::getDefaultFont(),70);
ToScore.setColor(sf::Color(205,0,0));
ToScore.setPosition(515,300);
while(1)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
board[i][j]=empty;
}
}
sf::Font font;
if(!font.loadFromFile("ABITE.ttf"))
return 1;
sf::Text TxWin("X won",font,70);
TxWin.setColor(sf::Color::Black);
TxWin.setPosition(200,200);
sf::Text ToWin("O won",font,70);
ToWin.setColor(sf::Color::Black);
ToWin.setPosition(200,200);
sf::Text Ttie("Tie",font,70);
Ttie.setColor(sf::Color::Black);
Ttie.setPosition(200,200);
sf::Text TtieText("Tie",font,25);
TtieText.setColor(sf::Color::Black);
TtieText.setPosition(450,132);
sf::Text TplayerX("X Player",font,25);
TplayerX.setColor(sf::Color (2,0,153));
TplayerX.setPosition(450,25);
sf::Text TplayerO("O Player",font,25);
TplayerO.setColor(sf::Color(205,0,0));
TplayerO.setPosition(450,290);
window.setFramerateLimit(50);
sf::SoundBuffer BXsound;
if(!BXsound.loadFromFile("Xsound.wav"))
return 1;
sf::Sound Xsound;
Xsound.setBuffer(BXsound);
sf::SoundBuffer BOsound;
if(!BOsound.loadFromFile("Osound.wav"))
return 1;
sf::Sound Osound;
Osound.setBuffer(BOsound);
sf::SoundBuffer BTieSound;
if(!BTieSound.loadFromFile("TieSound.wav"))
return 1;
sf::Sound TieSound;
TieSound.setBuffer(BTieSound);
sf::Texture Tbackground;
sf::Texture Tx;
sf::Texture To;
if(!Tbackground.loadFromFile("pozadina.jpg") || !Tx.loadFromFile("iks.jpg") || !To.loadFromFile("oks.jpg"))
return 1;
sf::Sprite Sbackground;
sf::Sprite Sx;
sf::Sprite So;
Sx.setTexture(Tx);
So.setTexture(To);
Sbackground.setTexture(Tbackground);
Sbackground.setPosition(0,0);
sf::Sprite SonPlay;
sf::Sprite Sfield[9];
bool mouseClicked[9]={false,false,false,false,false,false,false,false,false};
int turn=1;
while(window.isOpen())
{
window.setTitle("TicTacToe FPS:" + GetFrameRate());
if(turn%2==0)
{
SonPlay=So;
onPlay=o;
}
else
{
SonPlay=Sx;
onPlay=x;
}
sf::Event Event;
while (window.pollEvent(Event))
{
if (Event.type == sf::Event::Closed)
window.close();
}
//input
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
sf::Vector2i mousePosition=sf::Mouse::getPosition(window);
if(mousePosition.x<125 && mousePosition.x>10 && mousePosition.y<130 && mousePosition.y>10 && mouseClicked[0]==false)
{
if(onPlay==x)
Xsound.play();
else if (onPlay==o)
Osound.play();
mouseClicked[0]=true;
Sfield[0]=SonPlay;
Sfield[0].setPosition(10,10);
turn++;
board[0][0]=onPlay;
}//1,1 polje
if(mousePosition.x<280 && mousePosition.x>161 && mousePosition.y<130 && mousePosition.y>10 && mouseClicked[1]==false)
{
if(onPlay==x)
Xsound.play();
else if (onPlay==o)
Osound.play();
mouseClicked[1]=true;
Sfield[1]=SonPlay;
Sfield[1].setPosition(161,10);
turn++;
board[0][1]=onPlay;
}//1,2 polje
if(mousePosition.x<414 && mousePosition.x>314 && mousePosition.y<130 && mousePosition.y>10 && mouseClicked[2]==false)
{
if(onPlay==x)
Xsound.play();
else if (onPlay==o)
Osound.play();
mouseClicked[2]=true;
Sfield[2]=SonPlay;
Sfield[2].setPosition(314,10);
turn++;
board[0][2]=onPlay;
}//1,3 polje
if(mousePosition.x<125 && mousePosition.x>10 && mousePosition.y<291 && mousePosition.y>162 && mouseClicked[3]==false)
{
if(onPlay==x)
Xsound.play();
else if (onPlay==o)
Osound.play();
mouseClicked[3]=true;
Sfield[3]=SonPlay;
Sfield[3].setPosition(10,161);
turn++;
board[1][0]=onPlay;
}//2,1 polje
if(mousePosition.x<280 && mousePosition.x>161 && mousePosition.y<291 && mousePosition.y>162 && mouseClicked[4]==false)
{
if(onPlay==x)
Xsound.play();
else if (onPlay==o)
Osound.play();
mouseClicked[4]=true;
Sfield[4]=SonPlay;
Sfield[4].setPosition(161,170);
turn++;
board[1][1]=onPlay;
}//2,2 polje
if(mousePosition.x<414 && mousePosition.x>341 && mousePosition.y<291 && mousePosition.y>162 && mouseClicked[5]==false)
{
if(onPlay==x)
Xsound.play();
else if (onPlay==o)
Osound.play();
mouseClicked[5]=true;
Sfield[5]=SonPlay;
Sfield[5].setPosition(319,166);
turn++;
board[1][2]=onPlay;
}//2,3 polje
if(mousePosition.x<125 && mousePosition.x>10 && mousePosition.y<438 && mousePosition.y>318 && mouseClicked[6]==false)
{
if(onPlay==x)
Xsound.play();
else if (onPlay==o)
Osound.play();
mouseClicked[6]=true;
Sfield[6]=SonPlay;
Sfield[6].setPosition(10,322);
turn++;
board[2][0]=onPlay;
}//3,1 polje
if(mousePosition.x<280 && mousePosition.x>161 && mousePosition.y<438 && mousePosition.y>318 && mouseClicked[7]==false)
{
if(onPlay==x)
Xsound.play();
else if (onPlay==o)
Osound.play();
mouseClicked[7]=true;
Sfield[7]=SonPlay;
Sfield[7].setPosition(161,322);
turn++;
board[2][1]=onPlay;
}//3,2 polje
if(mousePosition.x<414 && mousePosition.x>314 && mousePosition.y<438 && mousePosition.y>318 && mouseClicked[8]==false)
{
if(onPlay==x)
Xsound.play();
else if (onPlay==o)
Osound.play();
mouseClicked[8]=true;
Sfield[8]=SonPlay;
Sfield[8].setPosition(314,322);
turn++;
board[2][2]=onPlay;
}//3,3 polje
}
window.clear(sf::Color(250,250,250));
if (winnerCheck()==x)
{
xScore++;
TxScore.setString(score[xScore]);
window.draw(TxWin);
window.display();
Xsound.play();
pause();
break;
}
else if(winnerCheck()==o)
{
oScore++;
ToScore.setString(score[oScore]);
window.draw(ToWin);
window.display();
Osound.play();
pause();
break;
}
else if (tie()==true)
{
tieScore++;
TtieScore.setString(score[tieScore]);
window.draw(Ttie);
window.display();
TieSound.play();
pause();
break;
}
window.draw(Sbackground);
for(int i=0;i<9;i++)
window.draw(Sfield[i]);
window.draw(TplayerX);
window.draw(TxScore);
window.draw(TplayerO);
window.draw(TtieScore);
window.draw(TtieText);
window.draw(ToScore);
window.display();
}
}
return 0;
}
What do you think?
Am I doing something wrong?
If you have time check it out
PS.
Turn your speakers