Skip to content
Snippets Groups Projects
Commit 2b2c135e authored by William Billingsley's avatar William Billingsley
Browse files

Added sample solution

parent 0e38fae4
No related branches found
No related tags found
No related merge requests found
package controllers; package controllers;
import model.BCryptExample; import model.BCryptExample;
import model.Captcha;
import play.mvc.Controller; import play.mvc.Controller;
import play.mvc.Result; import play.mvc.Result;
import java.util.Arrays;
public class Application extends Controller { public class Application extends Controller {
public static Result index() { public static Result index() {
return internalServerError("Not implemented yet..."); int arrayLength = 5;
int[] indexes = new int[arrayLength];
for (int i = 0; i < arrayLength; i++) {
indexes[i] = (int)(Captcha.numPhotos() * Math.random());
}
return ok(views.html.application.index.render(indexes));
}
private static int countBeagles(String[] indexes) {
int i = 0;
for (String s : indexes) {
if (Captcha.isCorrect(Integer.valueOf(s))) {
i++;
}
}
return i;
} }
public static Result matches() { public static Result matches() {
return internalServerError("Not implemented yet..."); String[] sent = request().body().asFormUrlEncoded().get("sent");
String[] beagles = request().body().asFormUrlEncoded().get("beagle");
int numBeagles = countBeagles(sent);
int numFound = countBeagles(beagles);
return ok(views.html.application.matches.render(numBeagles, numFound));
} }
} }
...@@ -58,7 +58,7 @@ public class Captcha { ...@@ -58,7 +58,7 @@ public class Captcha {
/** /**
* Whether a photo selected by the user was in the "yes" set * Whether a photo selected by the user was in the "yes" set
*/ */
public boolean isCorrect(int idx) { public static boolean isCorrect(int idx) {
return idx >= 0 && idx < yesPhotos.length; return idx >= 0 && idx < yesPhotos.length;
} }
......
...@@ -11,8 +11,16 @@ ...@@ -11,8 +11,16 @@
<body> <body>
<h1>Tick all the beagles:</h1> <h1>Tick all the beagles:</h1>
<!-- TODO: This form probably has the wrong method... -->
<form action="captcha" > <form action="matches" method="POST">
@for(idx <- indexes) {
<div>
<input name="sent" value="@idx" type="hidden" />
<input name="beagle" value="@idx" type="checkbox" />
<img src="@model.Captcha.getPhoto(idx)" style="max-width: 400px; max-height: 200px;" />
</div>
}
<!-- <!--
TODO: For each index in the array, show a checkbox with the value of the index, and the relevant img TODO: For each index in the array, show a checkbox with the value of the index, and the relevant img
--> -->
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment