#include "NXCDefs.h" #define BLUE 0 #define YELLOW 1 #define RED 2 #define PAMAX 8 int score = 0; int happy = 0; int cp0 = 0, cp1 = 0, cp2 = 0, cp3 = 0, cp4 = 0; int pp0 = 0, pp1 = 0, pp2 = 0, pp3 = 0, pp4 = 0; int pa0 = 0, pa1 = 0, pa2 = 0, pa3 = 0, pa4 = 0; int pr0 = 0, pr1 = 0, pr2 = 0, pr3 = 0, pr4 = 0; int besta = 0, bestp = 0; int pos = BLUE; int predict() { pr0 = random(3); /* make a random prediction */ pr1 = cp1; /* predict the previous choice */ pr2 = cp2; /* ... or the 2nd previous ... */ pr3 = cp3; /* ... or the 3rd ... */ pr4 = cp4; /* ... or the 4th ... */ if (bestp == 0) return pr0; else if (bestp == 1) return pr1; else if (bestp == 2) return pr2; else if (bestp == 3) return pr3; else return pr4; } void show_prediction(int prediction) { int i; i = (random(3) + 1) * 360 + (prediction - pos) * 120; RotateMotor(OUT_A, 50, i); pos = prediction; } void update(int choice, int prediction) { int i; if (choice == prediction) { score += 2; // RotateMotor(OUT_B, 25, -120); // monkey climbs, says "I thought so!" } else { score -= 1; // monkey descends // RotateMotor(OUT_B, 25, 60); } if (score > 9 && happy < 1) { // monkey celebrates happy = 1; } else if (score < -9 && happy > -1) { // he's a sad, sad monkey ... happy = -1; } cp3 = cp2; cp2 = cp1; cp1 = cp0; cp0 = choice; pp3 = pp2; pp2 = pp1; pp1 = pp0; pp0 = prediction; if (pr0 == choice) pa0 += 2; else pa0--; if (pr1 == choice) pa1 += 2; else pa1--; if (pr2 == choice) pa2 += 2; else pa2--; if (pr3 == choice) pa3 += 2; else pa3--; if (pr4 == choice) pa4 += 2; else pa4--; besta = pa0; bestp = 0; if (pa1 >= besta) { besta = pa1; bestp = 1; } if (pa2 > besta) { besta = pa2; bestp = 2; } if (pa3 > besta) { besta = pa3; bestp = 3; } if (pa4 > besta) { besta = pa4; bestp = 4; } if (pa0 > PAMAX) pa0 = PAMAX; if (pa1 > PAMAX) pa1 = PAMAX; if (pa2 > PAMAX) pa2 = PAMAX; if (pa3 > PAMAX) pa3 = PAMAX; if (pa4 > PAMAX) pa4 = PAMAX; } int get_result() { int left, right; TextOut(0, LCD_LINE3, true, "Was I right?"); TextOut(5, LCD_LINE8, false, "No Yes"); while (1) { left = ButtonPressed(BTNLEFT, true); right = ButtonPressed(BTNRIGHT, true); Wait(500); if (left) return (0); if (right) return (1); } } void ask_for_choice() { TextOut(0, LCD_LINE3, true, "Choose a color"); TextOut(0, LCD_LINE5, false, "Press the orange"); TextOut(0, LCD_LINE6, false, "button when you"); TextOut(0, LCD_LINE7, false, "are ready"); if (score) NumOut(0, LCD_LINE1, false, score); PlayTone(440, 500); Wait(500); while (1) { if (ButtonPressed(BTNCENTER, true)) break; } } void celebrate() { TextOut(0, LCD_LINE3, true, "I thought so!"); RotateMotor(OUT_B, 25, -120); /* monkey ascends */ // PlayTone(1760, 500); Wait(1000); } void raspberry() { TextOut(0, LCD_LINE3, true, "I was close ..."); RotateMotor(OUT_B, 25, 60); /* monkey descends */ // PlayTone(220, 500); // Wait(1000); } void turn() { int choice, prediction, p2, p3, pt, result; ask_for_choice(); prediction = predict(); show_prediction(prediction); result = get_result(); if (result) { choice = prediction; celebrate(); } else { raspberry(); p2 = prediction + 1; if (p2 > 2) p2 = 0; p3 = prediction - 1; if (p3 < 0) p3 = 2; if (random(2) < 1) { pt = p2; p2 = p3; p3 = pt; } show_prediction(p2); result = get_result(); if (result) { choice = p2; celebrate(); } else { choice = p3; raspberry(); } } update(choice, prediction); } task main() { while (-10 < score && score < 10) turn(); if (score >= 10) { TextOut(0, LCD_LINE1, true, "I am very smart"); TextOut(0, LCD_LINE2, false, "or don't you"); TextOut(0, LCD_LINE3, false, "think so?"); TextOut(0, LCD_LINE4, false, "With my tiny"); TextOut(0, LCD_LINE5, false, "brain, I can"); TextOut(0, LCD_LINE6, false, "guess what your"); TextOut(0, LCD_LINE7, false, "ENORMOUS brain"); TextOut(0, LCD_LINE8, false, "is thinking!"); } else { TextOut(0, LCD_LINE1, true, "I am very sad"); TextOut(0, LCD_LINE2, false, "because I can't"); TextOut(0, LCD_LINE3, false, "guess what you"); TextOut(0, LCD_LINE4, false, "are thinking."); TextOut(0, LCD_LINE6, false, "I will"); TextOut(0, LCD_LINE7, false, "probably be"); TextOut(0, LCD_LINE8, false, "disassembled!"); } Wait(10000); }