Skip to content

Instantly share code, notes, and snippets.

@buyoh
Last active July 29, 2018 23:09
Show Gist options
  • Save buyoh/7cfb73b1c8f57d27f22224f7b3e335d6 to your computer and use it in GitHub Desktop.
Save buyoh/7cfb73b1c8f57d27f22224f7b3e335d6 to your computer and use it in GitHub Desktop.
summerpronama2018.pde (processing 3.4)
import java.awt.Point;
import java.util.*;
boolean redrawFlg = true;
int rate = 80; // 与えられる回答が正しい確率
int N = 6;
String[] questions;
boolean[] answers;
int totalScore = 0;
LinkedList<ArrayList<Point>> historyPens = new LinkedList<ArrayList<Point>>();
LinkedList<Boolean> historyPenColor = new LinkedList<Boolean>();
ArrayList<Point> penpoints = new ArrayList<Point>();
int randomInt(int high){ return floor(random(high)); }
int randomInt(int low, int high){ return floor(random(low, high)); }
float diffangle(float x, float y, float mod) {
float a1 = (y-x)%mod, a2 = (y-x+mod)%mod, a3 = (y-x-mod)%mod;
return abs(a2) < abs(a1) ? (abs(a3) < abs(a2) ? a3 : a2) : (abs(a3) < abs(a1) ? a3 : a1);
}
// - - - - - - - - - - - - - - - - -
void setup(){
size(480, 480); textSize(48);
initializeQuestions();
}
void loop(){
redraw();
}
void draw(){
if (!redrawFlg) return;
redrawFlg = false;
drawScreenGame();
}
// - - - - - - - - - - - - - - - - -
String out_createNewQuestion_question;
boolean out_createNewQuestion_answer;
void createNewQuestion(){
int x = randomInt(1, 10);
int y = randomInt(1, 10);
int op = randomInt(0, 3);
boolean maru = randomInt(100) < rate;
int ans;
String q;
switch (op){
case 0:{
ans = x + y;
if (!maru){
if (randomInt(2) > 0) ans += randomInt(1,4)*(randomInt(2)*2-1);
else ans = x*y;
}
q = "" + x + " + " + y + " = " + ans;
maru = (x + y == ans); break;
}
case 1:{
if (x < y) {x^=y; y^=x; x^=y;}
ans = x - y;
if (!maru){
if (randomInt(2) > 0) ans += randomInt(1,4);
else ans = x+y;
}
q = "" + x + " - " + y + " = " + ans;
maru = (x - y == ans); break;
}
default:{
ans = x * y;
if (!maru){
if (randomInt(2) > 0) ans += randomInt(1,4)*(randomInt(2)*2-1);
else ans = x+y;
}
q = "" + x + " x " + y + " = " + ans;
maru = (x * y == ans); break;
}
}
out_createNewQuestion_question = q;
out_createNewQuestion_answer = maru;
}
// - - - - - - - - - - - - - - - - -
void initializeQuestions(){
questions = new String[N];
answers = new boolean[N];
for (int i = 2; i < N; ++i){
createNewQuestion();
questions[i] = out_createNewQuestion_question;
answers[i] = out_createNewQuestion_answer;
}
questions[0] = ""; questions[1] = "";
}
// - - - - - - - - - - - - - - - - -
void drawPen(ArrayList<Point> points, float offsetx, float offsety){
Point last = points.get(0);
for (int i = 1; i < points.size(); ++i){
Point p = points.get(i);
line(last.x+offsetx, last.y+offsety, p.x+offsetx, p.y+offsety);
last = p;
}
}
void drawScreenGame(){
background(255);
fill(0);
for (int i = 0; i < N; ++i){
if (i == 2) fill(0);
else fill (160);
text(questions[i], 30, 70 + i*90);
}
stroke(255,10,5); strokeWeight(4);
if (penpoints.size() > 0)
drawPen(penpoints, 0, 0);
for (int i = 0; i < historyPens.size(); ++i){
if (historyPenColor.get(i).booleanValue())
stroke(255,10,5, 48);
else
stroke(5,10,255, 48);
drawPen(historyPens.get(i), 0, -90*(historyPens.size()-i));
}
}
// - - - - - - - - - - - - - - - - -
boolean checkPen(ArrayList<Point> points){
float cx, cy;
cx = cy = 0;
for (int i = 0; i < points.size(); ++i){
Point p = points.get(i);
cx += p.x; cy += p.y;
}
cx /= points.size(); cy /= points.size();
Point p0 = points.get(0);
float d = 0, dmin = 0, dmax = 0, alast;
alast = atan2(p0.y-cy, p0.x-cx);
for (int i = 0; i < points.size(); ++i){
Point p = points.get(i);
float a = atan2(p.y-cy, p.x-cx);
d += diffangle(alast, a, PI*2);
dmin = min(dmin, d);
dmax = max(dmax, d);
alast = a;
}
return dmax-dmin > 5;
}
// - - - - - - - - - - - - - - - - -
void judgePen(){
boolean chk = checkPen(penpoints);
totalScore += chk == answers[2] ? 1 : -10;
historyPens.add(penpoints);
historyPenColor.add(new Boolean(chk == answers[2]));
while (historyPens.size() > 5) {historyPens.poll(); historyPenColor.poll();}
surface.setTitle("score:"+totalScore);
}
void goNextQuestion(){
for (int i = 0; i < N-1; ++i){
questions[i] = questions[i+1];
answers[i] = answers[i+1];
}
createNewQuestion();
questions[N-1] = out_createNewQuestion_question;
answers[N-1] = out_createNewQuestion_answer;
penpoints = new ArrayList<Point>();
}
// - - - - - - - - - - - - - - - - -
void mousePressed(){
}
void mouseDragged(){
penpoints.add(new Point(mouseX, mouseY));
redrawFlg = true;
redraw();
}
void mouseReleased(){
if (penpoints.size() <= 2){
penpoints = new ArrayList<Point>();
redraw = true; redraw(); return;
}
judgePen();
goNextQuestion();
redrawFlg = true; redraw();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment