You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
4.8 KiB

function run() {
const terminal = document.getElementById("console");
const runner = new BrythonRunner({
stdout: {
write(content) {
terminal.innerHTML += content;
terminal.scrollTop = terminal.scrollHeight;
},
flush() {}
},
stderr: {
write(content) {
terminal.innerHTML += content;
terminal.scrollTop = terminal.scrollHeight;
},
flush() {}
},
stdin: {
async readline() {
terminal.innerHTML += "\n";
terminal.scrollTop = terminal.scrollHeight;
var userInput = prompt();
return userInput;
},
flush() {}
}
});
var code = editor.getValue();
runner.runCode(code);
setTimeout(() => {
runner.stopRunning();
}, 10*1000);
}
function run_init() {
if (document.getElementById("console") != '') {
document.getElementById("console").innerHTML = '';
}
run();
}
var editor = ace.edit("editor");
editor.setTheme("ace/theme/vibrant_ink");
editor.getSession().setMode("ace/mode/python");
editor.setFontSize("16px");
editor.container.style.height = "250px"
editor.setOptions({
enableLiveAutocompletion: true,
copyWithEmptySelection: true,
showGutter: true,
useWrapMode: true, // wrap text to view
indentedSoftWrap: false,
});
//Function that execute given code and return the result in a given element by id
function exec(code, id) {
const terminal = document.getElementById("console");
const runner = new BrythonRunner({
stdout: {
write(content) {
if (id == "code"){
retourCode = content;
}
if (id == "solution"){
retourSolution = content;
}
},
flush() {}
},
stderr: {
write(content) {
if (id == "solution"){
retourSolution = "ERROR";
}
terminal.innerHTML += content;
terminal.scrollTop = terminal.scrollHeight;
},
flush() {}
},
stdin: {
async readline() {
terminal.innerHTML += "\n";
terminal.scrollTop = terminal.scrollHeight;
var userInput = prompt();
return userInput;
},
flush() {}
}
});
runner.runCode(code)
setTimeout(() => {
runner.stopRunning();
}, 10*1000);
}
//~ Function that check if the code in the editor as the same result as the solution.
function check(){
if (retourSolution == "ERROR") {
result.innerHTML = "Il semblerait qu'il y a une erreur dans ton code :/";
result.classList.remove('hidden');
}
else if (retourSolution == retourCode) {
result.innerHTML = "Bien joué";
result.classList.remove('hidden');
}
else {
result.innerHTML = "Mauvaise réponse";
result.classList.remove('hidden');
}
}
//~ Function that test the user code
async function submit(){
var test = editor.getValue()+`\n
import random as r
def binaire2unitaireVerif(x):
rep=0
pre=x[0]
unitaire=""
for i in x:
if(pre==i):
rep+=1
else:
if(pre=="1"):
unitaire=unitaire+"0 "
else:
unitaire=unitaire+"00 "
for j in range(rep):
unitaire=unitaire+"0"
unitaire=unitaire+" "
rep=1
pre=i
if(pre=="1"):
unitaire=unitaire+"0 "
else:
unitaire=unitaire+"00 "
for i in range(rep):
unitaire=unitaire+"0"
return unitaire
def testChuckNorris(x):
l="0000000"
if(binaire2unitaire(l)!="00 0000000"):
return False
l="1111111"
if(binaire2unitaire(l)!="0 0000000"):
return False
l="1101001"
if(binaire2unitaire(l)!="0 00 00 0 0 0 00 00 0 0"):
return False
l=""
for i in range(x):
for j in range(r.randint(1,10)):
l=l+str(r.randint(0,1))
if(binaire2unitaire(l)!=binaire2unitaireVerif(l)):
return False
l=""
return True
print(testChuckNorris(5))
`;
exec("print ('True')", "code");
exec(test, "solution");
await new Promise(r => setTimeout(r, 1500));
check();
}
function aide(){
if(document.getElementById("textAide").textContent == ""){
document.getElementById("textAide").textContent = "Bonne chance :)";
}
else{
document.getElementById("textAide").textContent = "";
}
}
function rappel(){
if(document.getElementById("textRappel").textContent == ""){
document.getElementById("textRappel").textContent = `
Dans le codage Chuck Norris, le '0' est codé '00' et le '1' est codé '0'. Puis un espace, puis ensuite autant de '0' que le message contient de '0' ou '1' successifs.
Par exemple, la suite "10000111" sera traduite de la façon suivante : "0 0 00 0000 0 000
`
}
else{
document.getElementById("textRappel").textContent = "";
}
}