Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent ba1d3dd862
commit f48d89288f
  1. BIN
      C++ProjectTemplate
  2. 2500
      input
  3. 138
      main.cpp

Binary file not shown.

2500
input

File diff suppressed because it is too large Load Diff

@ -2,88 +2,72 @@
#include "pixelGameEngine.h" #include "pixelGameEngine.h"
#include "olcutils.h" #include "olcutils.h"
using namespace olc; int main()
class Example : public olc::PixelGameEngine
{
public:
Example()
{ {
sAppName = "Example"; std::ifstream file("input");
int points=0;
while (file.good()) {
std::string line;
std::getline(file,line);
/*
A,X = Rock 1
B,Y = Paper 2
C,Z = Scissors 3
X Loss 0
Y Draw 3
Z Wins 6
*/
char s1 = line[0];
char s2 = line[2];
if (s2=='Z') { //Win
switch (s1) {
case 'A':{ //Rock
points+=6+2;
std::cout<<points<<std::endl;
}break;
case 'B':{ //Paper
points+=6+3;
std::cout<<points<<std::endl;
}break;
case 'C':{ //Scissors
points+=6+1;
std::cout<<points<<std::endl;
}break;
} }
public:
bool RayVsRect(const vf2d ray_origin, const vf2d ray_dir, const olc::utils::geom2d::rect<float> target, vf2d&contact_point, vf2d&contact_normal, float&t_hit_near){
contact_normal = { 0, 0 };
contact_point = { 0, 0 };
vf2d t_near = {(target.pos.x - ray_origin.x) / ray_dir.x, (target.pos.y - ray_origin.y) / ray_dir.y};
vf2d t_far = {(target.pos.x + target.size.x - ray_origin.x) / ray_dir.x, (target.pos.y + target.size.y - ray_origin.y) / ray_dir.y};
if (t_near.x > t_far.x) {float b; b = t_near.x; t_near.x = t_far.x; t_far.x = b;};
if (t_near.y > t_far.y) {float b; b = t_near.y; t_near.y = t_far.y; t_far.y = b;};
if (t_near.x > t_far.y || t_near.y > t_far.x) return false;
t_hit_near = fmax(t_near.x, t_near.y);
float t_hit_far = fmin(t_far.x, t_far.y);
if (t_hit_far < 0) return false;
contact_point.x = ray_origin.x + t_hit_near * ray_dir.x;
contact_point.y = ray_origin.y + t_hit_near * ray_dir.y;
if (t_near.x > t_near.y)
if ( 1.0f / ray_dir.x < 0)
contact_normal = { 1, 0 };
else
contact_normal = { -1, 0};
else
if ( t_near.x < t_near.y)
if ( 1.0f / ray_dir.y < 0)
contact_normal = { 0, 1 };
else
contact_normal = { 0, -1 };
return true;
} }
vf2d originPoint={16,16}; if (s2=='Y') { //Draw
bool OnUserCreate() override switch (s1) {
{ case 'A':{ //Rock
// Called once at the start, so create things here points+=3+1;
return true; std::cout<<points<<std::endl;
}break;
case 'B':{ //Paper
points+=3+2;
std::cout<<points<<std::endl;
}break;
case 'C':{ //Scissors
points+=3+3;
std::cout<<points<<std::endl;
}break;
} }
bool OnUserUpdate(float fElapsedTime) override
{
vf2d velocity={(GetKey(D).bHeld-GetKey(A).bHeld)*20*fElapsedTime,(GetKey(S).bHeld-GetKey(W).bHeld)*20*fElapsedTime};
vf2d contact_point;
vf2d contact_normal;
float t_hit_near;
Clear(Pixel(64,64,255));
if (!olc::utils::geom2d::overlaps(olc::utils::geom2d::circle<float>{originPoint+velocity,5},olc::utils::geom2d::rect<float>{{32,32},{64,32}})) {
originPoint+=velocity;
DrawCircle(originPoint,5);
} else {
DrawCircle(originPoint,5,RED);
} }
DrawLine(originPoint,GetMousePos()); if (s2=='X') { //Lose
switch (s1) {
DrawRect({32,32},{64,32},RayVsRect(originPoint, GetMousePos()-originPoint, olc::utils::geom2d::rect<float>{{32,32},{64,32}},contact_point,contact_normal,t_hit_near)&&t_hit_near<1?YELLOW:WHITE); case 'A':{ //Rock
return true; points+=0+3;
std::cout<<points<<std::endl;
}break;
case 'B':{ //Paper
points+=0+1;
std::cout<<points<<std::endl;
}break;
case 'C':{ //Scissors
points+=0+2;
std::cout<<points<<std::endl;
}break;
} }
}; }
}
std::cout<<"Total:"<<points<<std::endl;
int main()
{
Example demo;
if (demo.Construct(128, 120, 8, 8))
demo.Start();
return 0; return 0;
} }

Loading…
Cancel
Save