Skip to content

Instantly share code, notes, and snippets.

@Gi133
Created February 1, 2012 14:55
Show Gist options
  • Select an option

  • Save Gi133/1717401 to your computer and use it in GitHub Desktop.

Select an option

Save Gi133/1717401 to your computer and use it in GitHub Desktop.
Darts Coursework
//Darts 301
//Class version
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <Windows.h>
#define BASE_ACCURACY 80 //Accuracy of both players when aiming for anything but the center.
const int board [20] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5}; //Globaly initialize board so it can be used in class functions.
class Player
{
private:
//Class variables and constants
static const int ACCURACY = BASE_ACCURACY;
int bull_accuracy;
//Class functions
int State(); //Based on score remaining, chance state of the player.
int Aim(); //Find the segment to aim for in order to get as close to 50 as possible
int Throw(); //Use the Aim value and try to hit it.
int Score_Update(); //Function to update score for each player.
public:
//Class variables and constants
int score;
short int state;
int score_hit; // What did you hit
int aiming; //What is the player aiming for.
bool won; //Whether the player won or not.
Player(int); //constructor
void Play(); //The function used for the player object to play the game.
};
//Constructor
Player::Player(int accuracy)
{
score = 301; //Initialize the Score at start of the game.
bull_accuracy = accuracy; //The chances of each player to hit the bull.
state = 0; // Set state to Fast Paced since game just started.
aiming = 0; // Player is aiming at nothing.
won = false;
}
//Figuring out the player state (if he's shooting to lower score fast or concentrates to get to exactly 50 as fast as possible).
int Player::State()
{
if (score > 70)
{
state = 0;
}
else if (score > 50 && score <= 70)
{
state = 1;
}
else if ((score == 50) || (score==0))
{
state = 2;
}
return (state);
}
//Player takes aim and returns the value he aimed for for the Throw function to use.
int Player::Aim()
{
if (state == 0) //The player is in a state where he or she try to lower the score fast and will always aim for middle to reduce score fast.
{
if ((score - 50) > 50)
{
aiming = 50;
}
else
{
aiming = 20;
}
}
if (state == 1) // The Player is in a state of concentration where he or she will try to get to 50 best way possible.
{
aiming = score - 50;
}
else if (state == 2) //Player can only aim at the Bull since he has a score of 50
{
aiming = 50;
}
return (aiming);
}
//Player throws a dart at the value the aim function returned.
int Player::Throw()
{
unsigned int chance = 0, aiming_position = 0;
int left_position = 0, right_position = 0;
chance = rand()%100+1; // Number magic follows, look away so you don't go blind!
if (aiming == 50)
{
if (chance <= bull_accuracy) // Congrats you hit what you aimed for.
{
score_hit = aiming;
}
else // You missed what you aimed for but hit something else.
{
aiming_position = rand()%19;
score_hit = board[aiming_position];
}
}
else if (aiming != 50)
{
if (chance <= ACCURACY)// Congrats you hit what you aimed for.
{
score_hit = aiming;
}
else // You missed what you aimed for but hit something else.
{
for (int i = 0; i <= 19; i++)
{
if (board[i] == aiming)
{
aiming_position = i;
}
}
if (aiming_position = 0)
{
left_position = 19;
right_position = aiming_position + 1;
}
else if (aiming_position = 19)
{
left_position = aiming_position - 1;
right_position = 0;
}
chance = rand()%1;
if (chance == 0) // Miss and hit the area to the left of where you aimed at.
{
score_hit = board[left_position];
}
else if (chance == 1) // Miss and hit the area to the right of where you aimed at.
{
score_hit = board[right_position];
}
}
}
return score_hit;
}
//After throwing a dart, update the player's individual score.
int Player::Score_Update()
{
if (state == 0)
{
if ( (score - score_hit) < 50)
{
score = score;
}
else
{
score = score - score_hit;
}
}
if (state == 1)
{
if ((score - score_hit) < 50)
{
score = score;
}
else if ((score - score_hit)>=50)
{
score = score - score_hit;
}
}
if ((state == 2) && (score_hit == 50))
{
score = score - score_hit;
}
else
{
score = score;
}
return score;
}
//Play function to make things secure and simple.
void Player::Play()
{
State();
Aim();
Throw();
Score_Update();
if (score == 0)
won = true;
}
//Simple trace function to test things out.
void trace(std::string message)
{
std::cout << message << "\n";
};
int main()
{
Player joe(70); //Initialize Joe with Bull Accuracy of 72%
Player sid(72); //Initialize Sid with Bull Accuracy of 70%
srand ( time(NULL) );
// Title text and test to make sure some stuff work.
trace("Joe and Sid playing 301 Darts game!");
//The game loop
while ((joe.won == false) && (sid.won == false))
{
if (sid.score != 0)
{
Sleep(550);
std::cout<<"Joe's turn: \n";
std::cout<<"Joe has "<<joe.score<<" points left.\n";
joe.Play();
std::cout<<"Joe aims for "<<joe.aiming<<", he hits "<<joe.score_hit<<"\n\n";
}
if (joe.score != 0)
{
Sleep(550);
std::cout<<"Sid's turn: \n";
std::cout<<"Sid has "<<sid.score<<" points left.\n";
sid.Play();
std::cout<<"Sid aims for "<<sid.aiming<<", he hits "<<sid.score_hit<<"\n\n";
}
}
std::cout<<"\n\n";
if (sid.won == true)
{
std::cout<<"Sid won the game!!\n";
std::cout<<"\n";
}
else
{
std::cout<<"Joe won the game!!\n";
std::cout<<"\n";
}
//Pause before exiting.
system("PAUSE");
return 0;
};
//Darts 301
//Function Version
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <Windows.h>
const int ACCURACY = 80; //Accuracy of both players when aiming for anything but the center.
const int board [20] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5}; //Initialize board.
void Bull(int& score, const int& player_accuracy)
{
// Player aims for 50 (bull) but there's a chance to miss and hit anything ranging from 1 to 20 if he does so.
int chance = (rand()%100)+1;
//State check
if (score == 50)
{
if (chance <= player_accuracy)
{
// If you hit the bull, reduce the player's score by 50.
score = score - 50;
}
else
score = 50;
}
else if (score != 50)
{
if (chance <= player_accuracy)
{
// If you hit the bull, reduce the player's score by 50.
score-=50;
}
else
{
// You have missed, reduce score by a random value from 1 to 20.
score -= (rand()%20)+1;
}
}
};
void Throw(int& score, const int& player_accuracy)
{
unsigned int aim = 0;
unsigned int aim_position = 0;
unsigned int right_position = 0, left_position = 0;
unsigned int chance = (rand()%100)+1;
//Assume score is 99 or below.
if (score > 70)
{
aim = 20;
}
else if (score <=70)
{
aim = score - 50;
}
// Now to find the position of the segment you aimed for.
for (int i = 0; i >=19; i++)
{
if (board[i] == aim)
{
aim_position = i;
}
}
//Making it loop at array edges
if (aim_position = 0)
{
left_position = 19;
right_position = 1;
}
else if (aim_position = 19)
{
left_position = 18;
right_position = 0;
}
// See if the player hit what he was aiming at.
if (chance <= ACCURACY)
{
score -= aim;
}
else
{
chance = rand()%1;
if (chance == 0)
score -= board[right_position];
else
score -= board[left_position];
}
};
int main()
{
bool Player = rand()%1; // 0 = Joe, 1 = Sid
int Joe_score = 301;
int Sid_score = 301;
int Sid_accuracy = 72;
int Joe_accuracy = 70;
srand ( time(NULL) );
// Some GUI set up stuff
std::cout << "Darts 301! (With functions)\n";
if (Player == 0)
std::cout << "Joe goes first.\n";
else
std::cout << "Sid goes first.\n";
std::cout << "\n";
std::cout << "---------------------------------------------------------\n";
while ((Joe_score > 0) && (Sid_score > 0))
{
//Check who's playing, if it's Joe...
if (Player == 0)
{
// More GUI stuff
std::cout << "Joe's turn. \n";
std::cout << "Current Score: " << Joe_score << ".\n";
std::cout << "Joe throws a dart. \n";
if (Joe_score >= 100)
{
Bull(Joe_score, Joe_accuracy);
Player = 1;
}
else if ((Joe_score < 100) && (Joe_score > 50))
{
Throw(Joe_score, Joe_accuracy);
Player = 1;
}
else if (Joe_score == 50)
{
Bull(Joe_score, Joe_accuracy);
Player = 1;
}
// And the result
std::cout << "Joe's new score is :" << Joe_score << ".\n\n";
Sleep(3500);
if (Joe_score == 0)
break;
}
//Check who's playing, if it's Sid...
if (Player == 1)
{
// Same GUI stuff but for Sid.
std::cout << "Sid's turn. \n";
std::cout << "Current Score: " << Sid_score << ".\n";
std::cout << "Sid throws a dart. \n";
if (Sid_score >= 100)
{
Bull(Sid_score, Sid_accuracy);
Player = 0;
}
else if ((Sid_score < 100) && (Sid_score > 50))
{
Throw(Sid_score, Sid_accuracy);
Player = 0;
}
else if (Sid_score == 50)
{
Bull(Sid_score, Sid_accuracy);
Player = 0;
}
// And the result
std::cout << "Sid's new score is :" << Sid_score << ".\n\n";
Sleep(3500);
if(Sid_score == 0)
break;
}
}
//Program get's out of the loop when one of them reaches 0 so announce the winner!
if (Joe_score == 0)
std::cout << "Joe wins!\n";
else
std::cout << "Sid wins!\n";
//Pause before exiting.
system("PAUSE");
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment