GuessGame
Версия от 07:58, 26 сентября 2020; pw>Безуглов Сергей
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
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);
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; }