Heute war es immer noch einfach, aber schon kniffliger. Dieses Puzzle hab ich in C++ gelöst.
Spoiler anzeigen
C-Quellcode
-
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- enum class Shapes
- {
- Rock = 1,
- Paper = 2,
- Scissors = 3,
- };
- class GameItem
- {
- public:
- Shapes Shape;
- GameItem(Shapes shape) : Shape(shape) {}
- GameItem(char input)
- {
- switch (input)
- {
- case 'A':
- case 'X':
- Shape = Shapes::Rock;
- break;
- case 'B':
- case 'Y':
- Shape = Shapes::Paper;
- break;
- case 'C':
- case 'Z':
- Shape = Shapes::Scissors;
- break;
- default:
- throw ("Illegal char");
- }
- }
- bool DefeatsShape(GameItem target)
- {
- switch (Shape)
- {
- case Shapes::Rock:
- return target.Shape == Shapes::Scissors;
- case Shapes::Paper:
- return target.Shape == Shapes::Rock;
- case Shapes::Scissors:
- return target.Shape == Shapes::Paper;
- default:
- return false;
- }
- }
- };
- int GetPoints(GameItem myItem, GameItem opponentItem)
- {
- if (myItem.Shape == opponentItem.Shape)
- {
- return (int)myItem.Shape + 3;
- }
- if (myItem.DefeatsShape(opponentItem))
- {
- return (int)myItem.Shape + 6;
- }
- return (int)myItem.Shape;
- }
- void ChangeShapeForForcedEnd(GameItem* myItem, GameItem opponentItem)
- {
- switch (myItem->Shape)
- {
- case Shapes::Paper://draw
- myItem->Shape = opponentItem.Shape;
- break;
- case Shapes::Rock: //lose
- switch (opponentItem.Shape)
- {
- case Shapes::Rock:
- myItem->Shape = Shapes::Scissors;
- break;
- case Shapes::Paper:
- myItem->Shape = Shapes::Rock;
- break;
- case Shapes::Scissors:
- myItem->Shape = Shapes::Paper;
- break;
- }
- break;
- case Shapes::Scissors://win
- switch (opponentItem.Shape)
- {
- case Shapes::Rock:
- myItem->Shape = Shapes::Paper;
- break;
- case Shapes::Paper:
- myItem->Shape = Shapes::Scissors;
- break;
- case Shapes::Scissors:
- myItem->Shape = Shapes::Rock;
- break;
- }
- break;
- }
- }
- int main()
- {
- std::ifstream inputFile("input.txt");
- if (!inputFile.is_open())
- {
- cout << "Can't open the file";
- return -1;
- }
- int myPointsPuzzle1 = 0;
- int myPointsPuzzle2 = 0;
- for (string line; getline(inputFile, line);)
- {
- GameItem opponentItem(line[0]);
- GameItem myItem(line[2]);
- myPointsPuzzle1 += GetPoints(myItem, opponentItem);
- ChangeShapeForForcedEnd(&myItem, opponentItem);
- myPointsPuzzle2 += GetPoints(myItem, opponentItem);
- }
- cout << myPointsPuzzle1 << endl << myPointsPuzzle2;
- return 0;
- }