Skip to content

Instantly share code, notes, and snippets.

@Gi133
Created February 22, 2012 14:33
Show Gist options
  • Select an option

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

Select an option

Save Gi133/1885357 to your computer and use it in GitHub Desktop.
Darts Functions
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;
}
}
};
//Darts 301
//Function Version
#define games 100000
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <Windows.h>
//Local includes
#include "bull.h"
#include "throw.h"
int main()
{
// 0 = Joe, 1 = Sid
bool Player = rand()%2;
//int Joe_score = 301;
//int Sid_score = 301;
int Sid_accuracy = 72;
int Joe_accuracy = 70;
int Joe_wins = 0;
int Sid_wins = 0;
int throws[7] = {0}; // 7,8,9,10,11,12,13 Throws
srand ( time(NULL) );
// Some GUI set up stuff
std::cout << "Darts 301! (With functions)\n";
std::cout << "\n";
std::cout << "---------------------------------------------------------\n\n";
if (Player == 0)
std::cout << "Joe goes first.\n";
else
std::cout << "Sid goes first.\n";
for (int i = 0; i < games; i++)
{
int Joe_score = 301;
int Sid_score = 301;
int throw_number = 0;
while ((Joe_score > 0) && (Sid_score > 0))
{
throw_number++;
//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;
}
else
Joe_score = 50;
// And the result
//std::cout << "Joe's new score is :" << Joe_score << ".\n\n";
//getchar();
//Sleep(300);
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;
}
else
Sid_score = 50;
// And the result
//std::cout << "Sid's new score is :" << Sid_score << ".\n\n";
//getchar();
//Sleep(300);
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";
Joe_wins ++;
if (throw_number == 7)
throws[0] += 1;
else if (throw_number == 8)
throws[1] += 1;
else if (throw_number == 9)
throws[2] += 1;
else if (throw_number == 10)
throws[3] += 1;
else if (throw_number == 11)
throws[4] += 1;
else if (throw_number == 12)
throws[5] += 1;
else
throws[6] += 1;
}
else
{
//std::cout << "Sid wins!\n";
Sid_wins++;
/*if (throw_number == 7)
throws[0] += 1;
else if (throw_number == 8)
throws[1] += 1;
else if (throw_number == 9)
throws[2] += 1;
else if (throw_number == 10)
throws[3] += 1;
else if (throw_number == 11)
throws[4] += 1;
else if (throw_number == 12)
throws[5] += 1;
else
throws[6] += 1;*/
}
//End of loop
}
//Compute win frequency
float joe_frequency = float(Joe_wins)/games * 100;
float sid_frequency = float(Sid_wins)/games * 100;
//Print Frequencies
std::cout << "Joe won "<< Joe_wins << " (" << joe_frequency << "%)\n";
std::cout << "Sid won "<< Sid_wins << " (" << sid_frequency << "%)\n";
for (int z = 0; z < 7; z++)
{
float frequency = (float(throws[z])/games)*100;
std::cout <<"Number of games that took "<< z+7 << " throws: " << throws[z] << " ("<< frequency << "%)\n";
}
//Pause before exiting.
getchar();
return 0;
};
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 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()%2;
if (chance == 0)
score -= board[right_position];
else
score -= board[left_position];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment