GuessGame: различия между версиями
Перейти к навигации
Перейти к поиску
pw>Безуглов Сергей |
pw>Безуглов Сергей |
||
Строка 42: | Строка 42: | ||
</pre> | </pre> | ||
− | == Javascript == | + | == Javascript 1 == |
=== index.html === | === index.html === | ||
<pre> | <pre> | ||
Строка 90: | Строка 90: | ||
alert('Я угадал число = ' + guess); | alert('Я угадал число = ' + guess); | ||
+ | </pre> | ||
+ | |||
+ | == Javascript 2 == | ||
+ | === index.html === | ||
+ | <pre> | ||
+ | <!DOCTYPE html> | ||
+ | <html lang="en"> | ||
+ | <head> | ||
+ | <meta charset="UTF-8"> | ||
+ | <title>Угадай</title> | ||
+ | |||
+ | <script type="application/javascript" src="script.js"></script> | ||
+ | </head> | ||
+ | <body> | ||
+ | <h1>Угадаю число</h1> | ||
+ | <div> | ||
+ | Моя догадка = | ||
+ | <input type="text" id="guess" readonly> | ||
+ | </div> | ||
+ | <div> | ||
+ | <p>Ваше число больше, меньше или равно догадке?</p> | ||
+ | <button type="button" id="more">Больше</button> | ||
+ | <button type="button" id="less">Меньше</button> | ||
+ | <button type="button" id="equal">Равно</button> | ||
+ | </div> | ||
+ | |||
+ | <div style="font-family: 'Comic Sans MS'; font-size: 4em; color: #ff0000;" id="result"></div> | ||
+ | </body> | ||
+ | </html> | ||
+ | </pre> | ||
+ | |||
+ | === script.js === | ||
+ | <pre> | ||
+ | window.onload = function() { | ||
+ | function CalculateAndSetGuess() { | ||
+ | guess = Math.trunc((lowerBound + upperBound) / 2); | ||
+ | guessInput.value = guess; | ||
+ | } | ||
+ | |||
+ | |||
+ | var lowerBound = 1, upperBound = 1000; | ||
+ | var gameOver; | ||
+ | var guess; | ||
+ | |||
+ | var guessInput = document.getElementById("guess"); | ||
+ | |||
+ | var MoreButton = document.getElementById("more"); | ||
+ | MoreButton.addEventListener("click", function() { | ||
+ | if (!gameOver) { | ||
+ | lowerBound = guess + 1; | ||
+ | CalculateAndSetGuess(); | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | var LessButton = document.getElementById("less"); | ||
+ | LessButton.addEventListener("click", function() { | ||
+ | if (!gameOver) { | ||
+ | upperBound = guess - 1; | ||
+ | CalculateAndSetGuess(); | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | var EqualButton = document.getElementById("equal"); | ||
+ | EqualButton.addEventListener("click", function() { | ||
+ | if (!gameOver) { | ||
+ | gameOver = true; | ||
+ | document.getElementById("result").innerHTML = "Ваше число = " + guess; | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | alert("Задумайте число от 1 до 1000."); | ||
+ | |||
+ | CalculateAndSetGuess(); | ||
+ | } | ||
</pre> | </pre> | ||
Версия 13:13, 28 сентября 2020
Pascal.ABC
Program GuessGame; var lowerBound, upperBound : integer; gameOver : boolean; guess : integer; userAnswer : integer; Begin gameOver := false; Writeln('Задумайте число от 1 до 1000'); lowerBound := 1; upperBound := 1000; Repeat guess := Round((lowerBound + upperBound) / 2); Writeln('Мой вариант = ' + guess); Writeln('Твоё число больше (1), меньше (2) или точно такое(0)?'); Read(userAnswer); if (userAnswer = 1) then begin lowerBound := guess + 1; end; if (userAnswer = 2) then begin upperBound := guess - 1; end; if (userAnswer = 0) then begin gameOver := true; end; until gameOver; Writeln('Я угадал число = ' + guess); end.
Javascript 1
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello</title> <script type="application/javascript" src="script.js"></script> </head> <body> </body> </html>
script.js
var lowerBound, upperBound; var gameOver ; var guess; var userAnswer; gameOver = false; alert('Задумайте число от 1 до 1000'); lowerBound = 1; upperBound = 1000; do { guess = Math.trunc((lowerBound + upperBound) / 2); userAnswer = prompt('Мой вариант = ' + guess + '. Твоё число больше (1), меньше (2) или точно такое(0)?'); if (userAnswer === '1') { lowerBound = guess + 1; } if (userAnswer === '2') { upperBound = guess - 1; } if (userAnswer === '0') { gameOver = true; } } while(!gameOver); alert('Я угадал число = ' + guess);
Javascript 2
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Угадай</title> <script type="application/javascript" src="script.js"></script> </head> <body> <h1>Угадаю число</h1> <div> Моя догадка = <input type="text" id="guess" readonly> </div> <div> <p>Ваше число больше, меньше или равно догадке?</p> <button type="button" id="more">Больше</button> <button type="button" id="less">Меньше</button> <button type="button" id="equal">Равно</button> </div> <div style="font-family: 'Comic Sans MS'; font-size: 4em; color: #ff0000;" id="result"></div> </body> </html>
script.js
window.onload = function() { function CalculateAndSetGuess() { guess = Math.trunc((lowerBound + upperBound) / 2); guessInput.value = guess; } var lowerBound = 1, upperBound = 1000; var gameOver; var guess; var guessInput = document.getElementById("guess"); var MoreButton = document.getElementById("more"); MoreButton.addEventListener("click", function() { if (!gameOver) { lowerBound = guess + 1; CalculateAndSetGuess(); } }); var LessButton = document.getElementById("less"); LessButton.addEventListener("click", function() { if (!gameOver) { upperBound = guess - 1; CalculateAndSetGuess(); } }); var EqualButton = document.getElementById("equal"); EqualButton.addEventListener("click", function() { if (!gameOver) { gameOver = true; document.getElementById("result").innerHTML = "Ваше число = " + guess; } }); alert("Задумайте число от 1 до 1000."); CalculateAndSetGuess(); }
C
#include <math.h> #include <stdbool.h> #include <stdio.h> int main() { int guess; int userAnswer; bool gameOver = false; printf("Think of a number from 1 to 1000\n"); int lowerBound = 1; int upperBound = 1000; do { guess = round((lowerBound + upperBound) / 2); printf("My guess is = %d. Is your number greater(1), less(2) or equal(0) to mine?\n", guess); scanf_s("%d", &userAnswer); if (userAnswer == 1) { lowerBound = guess + 1; } if (userAnswer == 2) { upperBound = guess - 1; } if (userAnswer == 0) { gameOver = true; } } while (!gameOver); printf("Your number is %d.\n", guess); return 0; }