fork download
  1. class Cols{
  2. int r, g, b;
  3.  
  4. Cols(int r, int g, int b){
  5. this.r = r;
  6. this.g = g;
  7. this.b = b;
  8. }
  9. }
  10. enum ColorType {
  11. Red("赤", 240, 0, 0),
  12. Green("緑", 0, 240, 0),
  13. Blue("青", 0, 0, 240),
  14. Yellow("黄色", 240, 240, 0),
  15. Purple("紫", 240, 0, 240);
  16.  
  17. final String name;
  18. int r, g, b;
  19.  
  20. ColorType(String name, int r, int g, int b) {
  21. this.name = name;
  22. this.r = r;
  23. this.g = g;
  24. this.b = b;
  25. }
  26. String get_name() {
  27. return name;
  28. }
  29. }
  30. abstract class Figure {
  31. ColorType colorType;
  32. color col;
  33. PVector offset;
  34. ArrayList<PVector> pos;
  35.  
  36. Figure(ColorType colorType, PVector offset) {
  37. this.colorType = colorType;
  38. this.col = color(colorType.r, colorType.g, colorType.b);
  39. this.offset = offset;
  40. this.pos = new ArrayList<PVector>();
  41. }
  42. abstract void drawSelf();
  43. void drawHover(){
  44. if (isIn()) {
  45. stroke(0);
  46. strokeWeight(2);
  47. } else {
  48. noStroke();
  49. }
  50. }
  51. void draw() {
  52. drawHover();
  53.  
  54. fill(col);
  55. drawSelf();
  56. }
  57. abstract boolean isIn();
  58. boolean isClicked() {
  59. return isIn();
  60. }
  61. }
  62. class Maru extends Figure {
  63. int d;
  64. int r;
  65.  
  66. Maru(ColorType colorType, PVector offset) {
  67. super(colorType, offset);
  68.  
  69. pos.add(new PVector(250, 300));
  70. pos.get(0).add(offset);
  71.  
  72. d = 150;
  73. r = d/2;
  74. }
  75. void drawSelf() {
  76. ellipse(pos.get(0).x, pos.get(0).y, d, d);
  77. }
  78. boolean isIn() {
  79. float a = mouseX - pos.get(0).x;
  80. float b = mouseY - pos.get(0).y;
  81. return sqrt(a*a + b*b) < r;
  82. }
  83. }
  84. class Sankaku extends Figure {
  85. ArrayList<PVector> es;//edges
  86.  
  87. Sankaku(ColorType colorType, PVector offset) {
  88. super(colorType, offset);
  89.  
  90. PVector center = new PVector(250, 325);
  91.  
  92. for(int i=0; i<3; i++){
  93. float delta = TWO_PI * ((float)i)/3 + radians(30);
  94. PVector r = new PVector(100, 0);
  95. r.rotate(delta);
  96.  
  97. PVector pt = new PVector(center.x, center.y);
  98. pt.add(r);
  99.  
  100. pos.add(pt);
  101. }
  102.  
  103. center.add(offset);
  104. for (PVector p : pos) {
  105. p.add(offset);
  106. }
  107.  
  108. es = new ArrayList<PVector>();
  109. for (int i=0, j=1; i<pos.size(); i++, j++, j%=pos.size()) {
  110. es.add(new PVector(
  111. pos.get(j).x - pos.get(i).x,
  112. pos.get(j).y - pos.get(i).y));
  113. }
  114. }
  115. void drawSelf() {
  116. beginShape();
  117. for (PVector p : pos) {
  118. vertex(p.x, p.y);
  119. }
  120. endShape(CLOSE);
  121. }
  122. boolean isIn() {
  123. for (int i=0; i<es.size(); i++) {
  124. PVector p = pos.get(i);
  125. PVector m = new PVector(
  126. mouseX - p.x,
  127. mouseY - p.y);//mouse
  128.  
  129. PVector e = es.get(i);
  130. if (m.x * e.y - m.y * e.x > 0) {
  131. return false;
  132. }
  133. }
  134. return true;
  135. }
  136. }
  137. class Shikaku extends Figure {
  138. int w;
  139. int h;
  140. PVector s;//start
  141.  
  142. Shikaku(ColorType colorType, PVector offset) {
  143. super(colorType, offset);
  144. w = 150;
  145. h = 150;
  146.  
  147. s = new PVector(200, 225);
  148. pos.add(new PVector(s.x, s.y));
  149. pos.add(new PVector(s.x+w, s.y));
  150. pos.add(new PVector(s.x+w, s.y+h));
  151. pos.add(new PVector(s.x, s.y+h));
  152.  
  153. s.add(offset);
  154. for(PVector p: pos){
  155. p.add(offset);
  156. }
  157. }
  158. void drawSelf() {
  159. beginShape();
  160. for (PVector p : pos) {
  161. vertex(p.x, p.y);
  162. }
  163. endShape(CLOSE);
  164. }
  165. boolean isIn() {
  166. return
  167. s.x < mouseX && mouseX < s.x+w
  168. &&
  169. s.y < mouseY && mouseY < s.y+h;
  170. }
  171. }
  172. class Question {
  173. ColorType correctColor;
  174. ColorType chooseColor;
  175.  
  176. Figure left;
  177. Figure right;
  178.  
  179. Question(ColorType correctColor, Figure left, Figure right) {
  180. this.correctColor = correctColor;
  181. this.left = left;
  182. this.right = right;
  183. }
  184. void draw() {
  185. left.draw();
  186. right.draw();
  187. }
  188. boolean isClicked() {
  189. if (left.isClicked()) {
  190. chooseColor = left.colorType;
  191. return true;
  192. } else if (right.isClicked()) {
  193. chooseColor = right.colorType;
  194. return true;
  195. }
  196. return false;
  197. }
  198. boolean isCorrect() {
  199. return correctColor == chooseColor;
  200. }
  201. String correctColorName() {
  202. return correctColor.get_name();
  203. }
  204. String chooseColorName() {
  205. return chooseColor.get_name();
  206. }
  207. }
  208. enum FigureType {
  209. Maru,
  210. Shikaku,
  211. Sankaku,
  212. }
  213.  
  214. class QuestionMaker {
  215. ArrayList<FigureType> ftypes;
  216. ArrayList<ColorType> cols;
  217.  
  218. ColorType ctypeL;
  219. ColorType ctypeR;
  220. ColorType correct;
  221.  
  222. Figure left;
  223. Figure right;
  224.  
  225. QuestionMaker() {
  226. ftypes = new ArrayList<FigureType>();
  227. ftypes.add(FigureType.Maru);
  228. ftypes.add(FigureType.Shikaku);
  229. ftypes.add(FigureType.Sankaku);
  230.  
  231. cols = new ArrayList<ColorType>();
  232. cols.add(ColorType.Red);
  233. cols.add(ColorType.Green);
  234. cols.add(ColorType.Blue);
  235. cols.add(ColorType.Yellow);
  236. cols.add(ColorType.Purple);
  237. Collections.shuffle(cols);
  238.  
  239. ctypeL = cols.get(0);
  240. ctypeR = cols.get(1);
  241.  
  242. ArrayList<ColorType> corr = new ArrayList<>();
  243. corr.add(ctypeL);
  244. corr.add(ctypeR);
  245. Collections.shuffle(corr);
  246.  
  247. correct = corr.get(0);
  248. PVector offsetL = new PVector(0, 0);
  249. PVector offsetR = new PVector(250, 0);
  250. left = getFigure(ctypeL, offsetL);
  251. right = getFigure(ctypeR, offsetR);
  252. }
  253. Figure getFigure(ColorType ctype, PVector offset) {
  254. Random rand = new Random();
  255. int index = rand.nextInt(ftypes.size());
  256. FigureType ftype = ftypes.get(index);
  257.  
  258. switch(ftype) {
  259. case Maru:
  260. return new Maru(ctype, offset);
  261.  
  262. case Sankaku:
  263. return new Sankaku(ctype, offset);
  264.  
  265. case Shikaku:
  266. return new Shikaku(ctype, offset);
  267.  
  268. default:
  269. return new Maru(ctype, offset);
  270. }
  271. }
  272. ColorType getCorrect() {
  273. return correct;
  274. }
  275. Figure getLeft() {
  276. return left;
  277. }
  278. Figure getRight() {
  279. return right;
  280. }
  281. }
  282. import java.util.Random;
  283. import java.util.Collections;
  284.  
  285. enum State {
  286. Title,
  287. Show1,
  288. Blank,
  289. Show2,
  290. Result,
  291. }
  292.  
  293. State state;
  294.  
  295. PFont font;
  296.  
  297. int startTime;
  298. int period;
  299. String str_elp;
  300.  
  301. Question question;
  302.  
  303.  
  304. void setup() {
  305. size(800, 600);
  306.  
  307. noStroke();
  308.  
  309. state = State.Title;
  310.  
  311. QuestionMaker qm = new QuestionMaker();
  312. question = new Question(qm.getCorrect(), qm.getLeft(), qm.getRight());
  313.  
  314. font = createFont("メイリオ", 40);
  315. textFont(font);
  316. }
  317.  
  318. void draw() {
  319. background(255);
  320.  
  321. switch(state) {
  322. case Title:
  323. fill(0);
  324. text("マウスを押したら実験開始", 100, 300);
  325. text(question.correctColorName() + "の図形を選んでください", 150, 380);
  326. break;
  327.  
  328. case Show1:
  329. question.draw();
  330. break;
  331.  
  332. case Blank:
  333. if ((millis() - startTime) >= period) {
  334. state = State.Show2;
  335. startTime = millis();
  336. }
  337. break;
  338.  
  339. case Show2:
  340. question.draw();
  341. break;
  342.  
  343. case Result:
  344. fill(0);
  345. text("key: ", 30, 40 );
  346. text("RT: ", 30, 80 );
  347. text("Elapsed: " + str_elp, 30, 120);
  348. break;
  349. }
  350. }
  351. void mousePressed() {
  352. switch(state) {
  353. case Title:
  354. state = State.Show1;
  355. break;
  356.  
  357. case Show1:
  358. state = State.Blank;
  359. startTime = millis();
  360. period = (new Random()).nextInt(200) + 1800;
  361. break;
  362.  
  363. case Blank:
  364. break;
  365.  
  366. case Show2:
  367. if (question.isClicked()) {
  368. state = State.Result;
  369. str_elp = nf(millis() - startTime, 4);
  370. }
  371. break;
  372.  
  373. case Result:
  374. {
  375. state = State.Title;
  376. QuestionMaker qm = new QuestionMaker();
  377. question = new Question(qm.getCorrect(), qm.getLeft(), qm.getRight());
  378. }
  379. break;
  380. }
  381. }
  382.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty