The header file
#ifndef DRAUGHTS2_H
#define DRAUGHTS2_H
#include <SFML\Graphics.hpp>
#include "TwoPlayerGame.h"
#include <iostream>
enum state{intro, playing, gameover}; //the states found within the game
enum squareValue{green, red, blank}; //the draught square values on the board
class Input
{
public:
Input();
void setInput(unsigned int,unsigned int);
unsigned int getX();
unsigned int getY();
bool inputStatus();
protected:
unsigned int x;
unsigned int y;
bool inputReceived;
};
class draughtPiece
{
public:
draughtPiece();
draughtPiece(double,double,double, int);//Draught(X, Y and radius)
//set the positions of the draughts
void setPosX(double);
void setPosY(double);
void setRadius(double);
//void setPieceNum(int);
//get positions, velocity and radius of the draughts
double getPosX();
double getPosY();
double getRadius();
//int getPieceNum();
private:
double posX;//x coord
double posY;//y coord
double radius;//draught radius
//double pieceNum;
};
class Draughts : public TurnBasedGame
{
public:
Draughts(Player*,Player*, sf::RenderWindow&); //the constructor
~Draughts();
void init(); //initialise the game
void draw(); //draws the draughts
void getInput(); //gets input from the player
void update(); //transfers update to the model (constantly updates the drawing of the draughts)
float getMoveIncrement(); //how far the draughts move on keypress or mouse press
void setMoveIncrement(float); //sets how far the draughts move on key press
void play(); //the game loop
//bool virtual gameOver();
protected:
draughtPiece g[12]; //array of green draughts
draughtPiece r[12]; //array of red draughts
sf::Image greenDraught; //the green draught image
sf::Sprite greenSprite; //the green draught sprite created from the green draught image
sf::Image redDraught; //the red draught image
sf::Sprite redSprite; //the red draught sprite created from the red draught image
sf::Image Board; //the board image
sf::Sprite BoardSprite; //the board sprite created from the board image
sf::Key::Code currentKey; //transfers info from getInput to the update function
sf::RenderWindow& myApp; //the window to draw to
float moveIncrement; //how far the ball moves when a key is pressed
unsigned int width; //the original window size - width
unsigned int height; //the original window size - height
sf::Clock Clock; //for the timing of movement and display
draughtPiece* pMseTacheddraught; //for recording the draught piece when attached to the mouse
bool mouseAttached; //for recording whether the draught is attached to the mouse
bool mouseReleased; //for recording whether the draught has been released
state gameState; //the current gamestate
squareValue board[8][8]; //the board
Input in;
bool virtual inputIsValid();
void virtual processPlayerInput();
private:
double dist(double, double, double, double); //the distance between points
int widthNormalise(int); //used to correct the values returned for mouse location by SFML input class
int heightNormalise(int); //used to correct the values returned for mouse location by SFML input class
void drawSymbols(); //function to draw the symbols on the board
};
#endif
The cpp file
#include <iostream>
#include "draughts2.h"
#include "TwoPlayerGame.h"
#include <string>
using namespace std;
Input::Input()
{
inputReceived=false;//no input received at construction
}
void Input::setInput(unsigned int xC,unsigned int yC)
{
x=xC;
y=yC;
//set inputRecieved
inputReceived=true;
}
unsigned int Input::getX()
{
return x;
}
unsigned int Input::getY()
{
return y;
}
bool Input::inputStatus()
{
return inputReceived;
}
draughtPiece::draughtPiece(){posX=0;posY=0;radius=0;}
//getters and setter for the draughtPiece class
void draughtPiece::setPosX(double d)
{
posX=d;
}
void draughtPiece::setPosY(double d)
{
posY=d;
}
void draughtPiece::setRadius(double d)
{
radius=d;
}
//void draughtPiece::setPieceNum(int d) //for setting the piece numbers
//{
// pieceNum=d;
//}
double draughtPiece::getPosX()
{
return posX;
}
double draughtPiece::getPosY()
{
return posY;
}
double draughtPiece::getRadius()
{
return radius;
}
//int draughtPiece::getPieceNum()
//{
// return pieceNum;
//}
Draughts::Draughts(Player* p1, Player* p2, sf::RenderWindow& win) : myApp(win), TurnBasedGame(p1,p2)
{
//sets the dimensions for the current window and allows for resizing
width=myApp.GetWidth();
height=myApp.GetHeight();
//sets the start for the mouse
pMseTacheddraught=0;
mouseAttached=false;
gameState=intro; //sets the intial game state
//initialise the board
int i, j;
//the board needs initialising
for (i=0; i<8;i++)
{
for(int j=0;j<8;j++)
{
board[i][j]=blank;
}
}
//sets the boards square values to be green depending on the modulus of j/2 (basically every other square)
for(i=0; i<3; i++)
{
for(j=0; j<8; j++)
{
if(i==0 && j%2==0)
{
board[i][j]=green;
}
else if(i==1 &&j%2==1)
{
board[i][j]=green;
}
else if(i==2 &&j%2==0)
{
board[i][j]=green;
}
}
}
//sets the boards square values to be red depending on the modulus of j/2 (basically every other square)
for(int i=5;i<8; i++)
{
for(j=0; j<8; j++)
{
if(i==5 && j%2==1)
{
board[i][j]=red;
}
else if(i==6 && j%2==0)
{
board[i][j]=red;
}
else if(i==7 && j%2==1)
{
board[i][j]=red;
}
}
}
}
Draughts::~Draughts(){}
void Draughts::init()
{
greenDraught.LoadFromFile("green_draught.png"); //loads the green draught image
greenSprite=sf::Sprite(greenDraught); //sets the green draught image as a sprite
greenSprite.SetCenter(greenSprite.GetSize().x/2,greenSprite.GetSize().y/2); //sets the centre for the green draught sprites
greenSprite.SetScale(0.2, 0.2); //scales the sprite to be smaller than the original image
redDraught.LoadFromFile("reddraught.bmp"); //loads the red draught image
redSprite =sf::Sprite(redDraught); //sets the red draught image as a sprite
redSprite.SetCenter(redSprite.GetSize().x/2,redSprite.GetSize().y/2); //sets the centre for the green draught sprites
redSprite.SetScale(0.2, 0.2); //scales the sprite to be smaller than the original image
Board.LoadFromFile("Draughts_board.GIF"); //loads the board image
BoardSprite=sf::Sprite(Board); //sets the board image as a sprite
BoardSprite.SetScale(1.23, 1.23); //sets the board to fit the window (for the time being)
int a=0, b=0; //the numbers of each draught
//sets the green draught objects the correct x and y positions
for(int i=0; i<3; i++)
{
for(int j=0; j<8; j++)
{
if(i==0 && j%2==0)
{
g[a].setRadius(greenSprite.GetSize().x/2); //sets the radius from green sprite
g[a].setPosX((width/8)*j+width/16);
g[a].setPosY((height/8)*i+height/16);
a++; //number increases giving the next draught piece a new number i.e. draught0, draught 1 etc.
}
else if(i==1 && j%2==1)
{
g[a].setRadius(greenSprite.GetSize().x/2); //sets the radius from green sprite
g[a].setPosX((width/8)*j+width/16);
g[a].setPosY((height/8)*i+height/16);
a++;
}
else if(i==2 && j%2==0)
{
g[a].setRadius(greenSprite.GetSize().x/2); //sets the radius from green sprite
g[a].setPosX((width/8)*j+width/16);
g[a].setPosY((height/8)*i+height/16);
a++;
}
}
}
//red draughts, sets each draught the correct get x and y positions
for(int i=5; i<8; i++)
{
for(int j=0; j<8; j++)
{
if(i==5 && j%2==0)
{
r[b].setRadius(redSprite.GetSize().x/2); //sets the radius from green sprite
r[b].setPosX((width/8)*j+width/16);
r[b].setPosY((height/8)*i+height/16);
b++;
}
else if(i==6 && j%2==1)
{
r[b].setRadius(redSprite.GetSize().x/2); //sets the radius from green sprite
r[b].setPosX((width/8)*j+width/16);
r[b].setPosY((height/8)*i+height/16);
b++;
}
else if(i==7 && j%2==0)
{
r[b].setRadius(redSprite.GetSize().x/2); //sets the radius from green sprite
r[b].setPosX((width/8)*j+width/16);
r[b].setPosY((height/8)*i+height/16);
b++;
}
}
}
}
void Draughts::draw()
{
myApp.Clear();
}
void Draughts::getInput()
{
unsigned int mouseX=0, mouseY=0; //to hold the mouse poisition
sf::Event Event;
while(myApp.GetEvent(Event))
{
//close the window
if(Event.Type==sf::Event::Closed)
myApp.Close();
//if the escape key is pressed the above close is executed
if((Event.Type==sf::Event::KeyPressed) && (Event.Key.Code==sf::Key::Escape))
myApp.Close();
//user input, if the P key is pressed the game state is changed to playing
if((Event.Type==sf::Event::KeyPressed) && (Event.Key.Code==sf::Key::P))
gameState=playing;
//used for testing the states at the moment
if((Event.Type==sf::Event::KeyPressed) && (Event.Key.Code==sf::Key::G))
gameState=gameover;
//user input, play again
if((Event.Type==sf::Event::KeyPressed) && (Event.Key.Code==sf::Key::A))
gameState=intro;
//mouse clicked function for the green draught
if(myApp.GetInput().IsMouseButtonDown(sf::Mouse::Left) && !mouseAttached && gameState==playing) //if the left mouse button is pressed and it is not currently attached
{
for(int i=0; i<12; i++)
{
if(dist((myApp.GetInput().GetMouseX()),(myApp.GetInput().GetMouseY()),g[i].getPosX(),g[i].getPosY())< g[i].getRadius()) //if the mouse is over or touching the green draught
{
mouseAttached=true;
pMseTacheddraught=&g[i];
}
}
}
//mouse clicked functionf or the red draught
if(myApp.GetInput().IsMouseButtonDown(sf::Mouse::Left) && !mouseAttached && gameState==playing) //if the left mouse button is pressed and it is not currently attached
{
for(int i=0; i<12; i++)
{
if(dist((myApp.GetInput().GetMouseX()),(myApp.GetInput().GetMouseY()),r[i].getPosX(),r[i].getPosY())< r[i].getRadius()) //if the mouse is over or touching the green draught
{
mouseAttached=true;
pMseTacheddraught=&r[i];
}
}
}
}
}
void Draughts::update()
{
unsigned int mouseX=0, mouseY=0;
//update based on velocity of the ball
float elapsedTime=Clock.GetElapsedTime();
if(elapsedTime>1.0F/30.0F)//30 frames per second
{
Clock.Reset();
if(mouseAttached)
{ //here is the draught tracking, constantly updates which square the draught is in
mouseX=myApp.GetInput().GetMouseX();
mouseY=myApp.GetInput().GetMouseY();
in.setInput(mouseY/((myApp.GetHeight()/8)+1),mouseX/((myApp.GetWidth()/8+1)));
cout<<"X "<<in.getY()<<" ";
cout<<"Y "<<in.getX()<<endl;
pMseTacheddraught->setPosX((myApp.GetInput().GetMouseX()));
pMseTacheddraught->setPosY((myApp.GetInput().GetMouseY()));
//this allows for the draught to only be moved on the squares but at the moment it stacks each draught to the same position
THIS IS THE BIT IM HAVING TROUBLE WITH
for(int i=0; i<12; i++)
{
g[i].setPosX((width/8)*in.getY()+width/16);
g[i].setPosY((height/8)*in.getX()+height/16);
}
}
//this did not work in the getInput function...works here though
if(!myApp.GetInput().IsMouseButtonDown(sf::Mouse::Left)) //if the mouse is not being pressed
{
mouseAttached=false;
pMseTacheddraught=0;
}
}
}
at the moment i need to get each draught to only move on the grid squares.
i have set it so that the draughts can only move on the grid squares but it deletes the rest except for 1
any ideas how to fix this?
or could it be a problem with how im accessing them, because ive got it in a loop is it that its telling each draught object (g by the way) to go to the same location.
is there a way to say access any array element of the draught?
would be great to get some tips or some help!