What programming language you start with really all depends on where you want to go with programming/coding. The great thing about this field is that there are an absolute abundance of smaller fields that you can go into, all using programming in their own unique ways. For web applications, a good start would be with HTML and later moving your way through CSS, JavaScript, JQuery, PHP, SQL, and any of the JavaScript libraries. Ruby is also a popular choice, so I would recommend checking that out too. For more scientific fields or areas with more machine learning and A.I., Python is generally a great place to start as it is widely used in that field of study. C++ is also a very useful language to know for that, but it can be a little more challenging for beginners. For game and application design, languages such as C#, C, Swift, Kotlin, and Java are most often used for that.
Description
Cricket is a bat-and-ball game played between two teams of 11 players each on a field at the centre of which is a rectangular pitch. The game is played by 120 million players in many countries, making it the world's second most popular sport!
There are 2 batsmen standing on two bases and the ball is played to the strike base. Batsmen run between their bases to increases their 'runs'. If a batsman gets out, a new batsman arrives to their base.
This is only a simplified version of the rules
Let's look at a typical score: 1.2wW6.2b34
There are 5 types of characters:
(number) - The player on strike acquires this many runs to his name.
'.' - A dot ball. No runs.
'b' - A bye, 1 run to the team but not to any particular batsman.
'w' - A wide, 1 run to the team but not to any particular batsman.
The difference between 'w' and 'b' is that a 'b' counts as a ball but 'w' is not a legal ball.
'W' - Strike batsman is out. A new player takes his place, if there is any player left to play out of 11 available. If not, the innings is complete.
Additional Rules:
If the striker scores an odd number, that means he reaches the other base and the batsmen have exchanged their base. If he scores 2, he runs twice and comes back to the same base.
The batsmen exchange their bases after 6 legal balls called an over, that means a 'w' doesn't count as a ball. So a score like '1..2.www' is still one over as it has only 5 legal balls.
Formal Inputs & Outputs
Input description
Ball by Ball Score, a line of string. For example:
1.2wW6.2b34
Output description
Individual scores of batsman that have played and number of extras. For example:
P1: 7
P2: 2
P3: 9
Extras: 2
Explanation : P1 hits a 1, P2 a dot ball, P2 hits a 2, Wide, P2 is Out (P3 in place on P2), P3 hits a 6, P3 a dot ball, New Over (P1 on strike), P1 hits a 2, Bye (P3 on strike), P3 hits a 3, P1 hits a 4
Challenge input
WWWWWWWWWW
1..24.w6
Solution
in C++
#include <iostream>
#include <array>
#include <string>
constexpr int number_of_players_on_team = 11;
class GameState
{
struct Player
{
std::string name;
int score;
Player() : score{ 0 } {}
};
std::array<Player, number_of_players_on_team> players;
std::size_t striking_player_index;
std::size_t non_striking_player_index;
int ball;
int extras;
std::size_t latest_in() const
{
return std::max(striking_player_index, non_striking_player_index);
}
std::pair<std::size_t,bool> next_in() const
{
if (latest_in() == (players.size() - 1))
{
// No more players can take the crease.
return std::make_pair(0xeff0eff0, false);
}
else
{
return std::make_pair(latest_in() + 1, true);
}
}
void switch_ends()
{
std::swap(striking_player_index, non_striking_player_index);
}
bool is_in(std::size_t player_index) const
{
return (player_index == striking_player_index) || (player_index == non_striking_player_index);
}
bool is_out(std::size_t player_index) const
{
return (player_index <= latest_in()) && !is_in(player_index);
}
void runs_check_ends(int n_runs)
{
if ((n_runs % 2) != 0)
{
switch_ends();
}
}
public:
GameState() : striking_player_index{ 0 }, non_striking_player_index{ 1 }, ball{ 0 }, extras{ 0 } {}
void set_player_name(std::string&& name, std::size_t order) // 1st out, 2nd out, etc... (NOT ZERO INDEXED!)
{
players[order - 1].name = std::move(name);
}
void legal_ball()
{
++ball;
if (ball >= 6)
{
switch_ends();
ball = 0;
}
}
void score_runs(int n_runs)
{
players[striking_player_index].score += n_runs;
runs_check_ends(n_runs);
legal_ball();
}
void dot_ball()
{
score_runs(0);
}
void bye(int n_runs = 1)
{
extras += n_runs;
runs_check_ends(n_runs);
legal_ball();
}
void wide()
{
++extras;
}
// Returns true if another batsman takes the crease, false if this ends the innings.
bool wicket()
{
const auto next = next_in();
striking_player_index = next.first;
legal_ball();
return next.second;
}
void print_score(std::ostream& oss)
{
for (std::size_t i = 0; i < number_of_players_on_team; ++i)
{
if (is_in(i) || is_out(i))
{
oss << players[i].name << ": " << players[i].score;
if (is_in(i))
{
// Indicate which batters are still in.
oss << '*';
}
oss << '\n';
}
}
oss << "Extras: " << extras << '\n';
}
std::string striking_player() const
{
return players[striking_player_index].name;
}
int ball_in_over() const
{
return ball;
}
};
int main()
{
GameState game;
// Set up player names.
for (std::size_t index = 0; index < number_of_players_on_team; ++index)
{
const std::size_t order = index + 1;
game.set_player_name(std::string{ "P" } +std::to_string(order), order);
}
std::string input;
std::getline(std::cin, input);
for (char ball : input)
{
#ifdef DEBUG_INFO
std::cout << game.striking_player() << " is on strike. Ball in over: " << game.ball_in_over() << " Ball: " << ball << "\n";
#endif
switch (ball)
{
case '.':
game.dot_ball();
break;
case 'b':
game.bye();
break;
case 'w':
game.wide();
break;
case 'W':
game.wicket();
break;
default:
game.score_runs(ball - '0');
break;
}
}
game.print_score(std::cout);
std::cin.get();
}
Challenge outputs:
P1: 0
P2: 0
P3: 0
P4: 0
P5: 0
P6: 0
P7: 0
P8: 0*
P9: 0
P10: 0
P11: 0
Extras: 0
and:
P1: 7*
P2: 6*
Extras: 1
Cricket is a bat-and-ball game played between two teams of 11 players each on a field at the centre of which is a rectangular pitch. The game is played by 120 million players in many countries, making it the world's second most popular sport!
There are 2 batsmen standing on two bases and the ball is played to the strike base. Batsmen run between their bases to increases their 'runs'. If a batsman gets out, a new batsman arrives to their base.
This is only a simplified version of the rules
Let's look at a typical score: 1.2wW6.2b34
There are 5 types of characters:
(number) - The player on strike acquires this many runs to his name.
'.' - A dot ball. No runs.
'b' - A bye, 1 run to the team but not to any particular batsman.
'w' - A wide, 1 run to the team but not to any particular batsman.
The difference between 'w' and 'b' is that a 'b' counts as a ball but 'w' is not a legal ball.
'W' - Strike batsman is out. A new player takes his place, if there is any player left to play out of 11 available. If not, the innings is complete.
Additional Rules:
If the striker scores an odd number, that means he reaches the other base and the batsmen have exchanged their base. If he scores 2, he runs twice and comes back to the same base.
The batsmen exchange their bases after 6 legal balls called an over, that means a 'w' doesn't count as a ball. So a score like '1..2.www' is still one over as it has only 5 legal balls.
Formal Inputs & Outputs
Input description
Ball by Ball Score, a line of string. For example:
1.2wW6.2b34
Output description
Individual scores of batsman that have played and number of extras. For example:
P1: 7
P2: 2
P3: 9
Extras: 2
Explanation : P1 hits a 1, P2 a dot ball, P2 hits a 2, Wide, P2 is Out (P3 in place on P2), P3 hits a 6, P3 a dot ball, New Over (P1 on strike), P1 hits a 2, Bye (P3 on strike), P3 hits a 3, P1 hits a 4
Challenge input
WWWWWWWWWW
1..24.w6
Solution
in C++
#include <iostream>
#include <array>
#include <string>
constexpr int number_of_players_on_team = 11;
class GameState
{
struct Player
{
std::string name;
int score;
Player() : score{ 0 } {}
};
std::array<Player, number_of_players_on_team> players;
std::size_t striking_player_index;
std::size_t non_striking_player_index;
int ball;
int extras;
std::size_t latest_in() const
{
return std::max(striking_player_index, non_striking_player_index);
}
std::pair<std::size_t,bool> next_in() const
{
if (latest_in() == (players.size() - 1))
{
// No more players can take the crease.
return std::make_pair(0xeff0eff0, false);
}
else
{
return std::make_pair(latest_in() + 1, true);
}
}
void switch_ends()
{
std::swap(striking_player_index, non_striking_player_index);
}
bool is_in(std::size_t player_index) const
{
return (player_index == striking_player_index) || (player_index == non_striking_player_index);
}
bool is_out(std::size_t player_index) const
{
return (player_index <= latest_in()) && !is_in(player_index);
}
void runs_check_ends(int n_runs)
{
if ((n_runs % 2) != 0)
{
switch_ends();
}
}
public:
GameState() : striking_player_index{ 0 }, non_striking_player_index{ 1 }, ball{ 0 }, extras{ 0 } {}
void set_player_name(std::string&& name, std::size_t order) // 1st out, 2nd out, etc... (NOT ZERO INDEXED!)
{
players[order - 1].name = std::move(name);
}
void legal_ball()
{
++ball;
if (ball >= 6)
{
switch_ends();
ball = 0;
}
}
void score_runs(int n_runs)
{
players[striking_player_index].score += n_runs;
runs_check_ends(n_runs);
legal_ball();
}
void dot_ball()
{
score_runs(0);
}
void bye(int n_runs = 1)
{
extras += n_runs;
runs_check_ends(n_runs);
legal_ball();
}
void wide()
{
++extras;
}
// Returns true if another batsman takes the crease, false if this ends the innings.
bool wicket()
{
const auto next = next_in();
striking_player_index = next.first;
legal_ball();
return next.second;
}
void print_score(std::ostream& oss)
{
for (std::size_t i = 0; i < number_of_players_on_team; ++i)
{
if (is_in(i) || is_out(i))
{
oss << players[i].name << ": " << players[i].score;
if (is_in(i))
{
// Indicate which batters are still in.
oss << '*';
}
oss << '\n';
}
}
oss << "Extras: " << extras << '\n';
}
std::string striking_player() const
{
return players[striking_player_index].name;
}
int ball_in_over() const
{
return ball;
}
};
int main()
{
GameState game;
// Set up player names.
for (std::size_t index = 0; index < number_of_players_on_team; ++index)
{
const std::size_t order = index + 1;
game.set_player_name(std::string{ "P" } +std::to_string(order), order);
}
std::string input;
std::getline(std::cin, input);
for (char ball : input)
{
#ifdef DEBUG_INFO
std::cout << game.striking_player() << " is on strike. Ball in over: " << game.ball_in_over() << " Ball: " << ball << "\n";
#endif
switch (ball)
{
case '.':
game.dot_ball();
break;
case 'b':
game.bye();
break;
case 'w':
game.wide();
break;
case 'W':
game.wicket();
break;
default:
game.score_runs(ball - '0');
break;
}
}
game.print_score(std::cout);
std::cin.get();
}
Challenge outputs:
P1: 0
P2: 0
P3: 0
P4: 0
P5: 0
P6: 0
P7: 0
P8: 0*
P9: 0
P10: 0
P11: 0
Extras: 0
and:
P1: 7*
P2: 6*
Extras: 1
Comments
Post a Comment