test_toolkit.pde 973 Bytes
Newer Older
sbalev's avatar
sbalev committed
1
CodeToolkit tk = new CodeToolkit(4, 6);
sbalev's avatar
sbalev committed
2 3 4 5 6 7 8 9

final int PEG_SIZE = 40;
final int GAP = 10;

int code;
int t;

void setup() {
sbalev's avatar
sbalev committed
10
  size(310, 570);
sbalev's avatar
sbalev committed
11
  background(191);
sbalev's avatar
sbalev committed
12 13
  code = int(random(tk.M));
  tk.displayCode(code, GAP, GAP, PEG_SIZE);
sbalev's avatar
sbalev committed
14 15 16
  line(GAP, 2 * GAP + PEG_SIZE, GAP + 4 * PEG_SIZE, 2 * GAP + PEG_SIZE);
  println(tk.codeToString(code));
  println("----");
sbalev's avatar
sbalev committed
17 18 19 20 21
  t = 0;
}

void draw() {
  t++;
sbalev's avatar
sbalev committed
22
  int guess = int(random(tk.M));
sbalev's avatar
sbalev committed
23
  int y = 2 * GAP + (GAP + PEG_SIZE) * t;
sbalev's avatar
sbalev committed
24
  tk.displayCode(guess, GAP, y, PEG_SIZE);
sbalev's avatar
sbalev committed
25
  print(tk.codeToString(guess));
sbalev's avatar
sbalev committed
26
  int f = tk.feedback(code, guess);
sbalev's avatar
sbalev committed
27
  displayFeedback(f, 2 * GAP + tk.N * PEG_SIZE, y + PEG_SIZE / 4);
sbalev's avatar
sbalev committed
28 29 30 31 32 33 34 35 36 37 38 39
  if (f == 0 || t == 10) noLoop();
}

void displayFeedback(int f, int x, int y) {
  int[] bw = new int[2];
  tk.decodeFeedback(f, bw);
  stroke(0);
  for (int k = 0; k < bw[0] + bw[1]; k++) {
    fill(k < bw[0] ? 0: 255);
    rect(x, y, PEG_SIZE / 2, PEG_SIZE / 2);
    x += PEG_SIZE / 2 + GAP;
  }
sbalev's avatar
sbalev committed
40
  println(" ", bw[0], bw[1]);
sbalev's avatar
sbalev committed
41
}