day 2 task 1
This commit is contained in:
parent
032f129cb2
commit
cf920586b9
35
2/main.cpp
Normal file
35
2/main.cpp
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
constexpr char ROCK = 1;
|
||||||
|
constexpr char PAPER = 2;
|
||||||
|
constexpr char SCISSORS = 3;
|
||||||
|
|
||||||
|
inline int get_result(const char& myChoice, const char& opponentChoice) {
|
||||||
|
if (myChoice == SCISSORS and opponentChoice == ROCK) { return 0; }
|
||||||
|
else if (myChoice == ROCK and opponentChoice == SCISSORS) { return 6; }
|
||||||
|
else if (myChoice == opponentChoice) { return 3; }
|
||||||
|
else if (myChoice > opponentChoice) { return 6; }
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int score = 0;
|
||||||
|
auto is = std::ifstream("scores.txt");
|
||||||
|
std::string line;
|
||||||
|
if (is.is_open()) {
|
||||||
|
for (std::string line; std::getline(is, line); ) {
|
||||||
|
char other = line.at(0) + 1 - 'A'; // convert to 1,2,3;
|
||||||
|
char mine = line.at(2) + 1 - 'X'; // convert to 1,2,3
|
||||||
|
score += mine + get_result(mine, other);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::cerr << "Could not open file scores.txt" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
std::cout << "My score is " << score << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
2500
2/scores.txt
Normal file
2500
2/scores.txt
Normal file
File diff suppressed because it is too large
Load Diff
3
2/scores.txt__
Normal file
3
2/scores.txt__
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
A Y
|
||||||
|
B X
|
||||||
|
C Z
|
Loading…
Reference in New Issue
Block a user