There is a game I like to play. It happens on a grid of finite size (but it's wrapped, like a sphere). On that grid, a random (integer-only) point is selected. Then, I, the user, am prompted for a coordinate input. If my input matches the random point exactly, I am told that I have won. Otherwise, I am told the point-wise distance between my input and the random point. For example, if I guessed (2,2)
and the random point was at (4,3)
, then the distance would be sqrt[(3-2)^2 + (4-2)^2] = sqrt[5]
.
The game continues until the player arrives at the correct location of the point.
Objective Create a functional version of the game described above. You must create a full program to do so. Here's what your program should do:
- Request two inputs: the height and the width of the board. The origin is at the top-left of the board. These inputs will not exceed
1024
. - Select a random point on that board; this will be the point to be guessed.
- Accept input simulating a turn. The input will be either a space-separated pair of integers or two separate integer inputs. In response to this input, the program will do one of two things:
- If the input matches the random point selected, output a message signalling the user's victory. I would suggest "You won!".
- Otherwise, output the distance between the user's input point and the random point.
- Once the user has achieved victory, display the number of turns the user took. The program then exits.
Bonuses
Bonuses are applied in the order that they appear in this list
- -150 bytes if your program takes an input integer
D
that describes the dimension in which the game takes place. E.g., ifD = 3
, then you create a random point of3
integers, take3
integer inputs, and output the distance between those points. - -50% (or +50% if
score < 0
)if you provide a graphical representation of the board (ASCII or Picture) that shows where the user has previously guessed on the grid of given dimensions and the turn counter. (If you go for the first bonus, then this bonus only applies to the2D
and1D
modes. If you add a 3D graphical output, you get an additional -50%.) - -60 bytes if you can provide a gamemode (selected by an input in the beginning; that is, when given
0
, perform the regular gamemode; when given1
, perform this gamemode) in which the point moves 1 unit in a random orthogonal direction per turn
More on wrapping
Wrapping occurs only when, in the third bonus, the moving point moves across any of the boundaries; in this case, the moving point is warped to the respective point, like so:
... ...
..R (move right) R..
... ...
This wrapping behaviour does not effect the user's guess, asides from the fact that point has changed direction.
Leaderboard
The Stack Snippet at the bottom of this post generates the catalogue from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.
To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:
# Language Name, N bytes
where N
is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:
# Ruby, <s>104</s> <s>101</s> 96 bytes
If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:
# Perl, 43 + 2 (-p flag) = 45 bytes
You can also make the language name a link which will then show up in the snippet:
# [><>](http://esolangs.org/wiki/Fish), 121 bytes
<style>body{text-align:left!important}#answer-list{padding:10px;width:290px;float:left}#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}</style><script src=https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js></script><link rel=stylesheet type=text/css href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"><div id=language-list><h2>Shortest Solution by Language</h2><table class=language-list><thead><tr><td>Language<td>User<td>Score<tbody id=languages></table></div><div id=answer-list><h2>Leaderboard</h2><table class=answer-list><thead><tr><td><td>Author<td>Language<td>Size<tbody id=answers></table></div><table style=display:none><tbody id=answer-template><tr><td>{{PLACE}}<td>{{NAME}}<td>{{LANGUAGE}}<td>{{SIZE}}<td><a href={{LINK}}>Link</a></table><table style=display:none><tbody id=language-template><tr><td>{{LANGUAGE}}<td>{{NAME}}<td>{{SIZE}}<td><a href={{LINK}}>Link</a></table><script>function answersUrl(e){return"//api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"//api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var a=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(a="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var r=a.match(SCORE_REG);r?e.push({user:getAuthorName(s),size:+r[2],language:r[1],link:s.share_link}):console.log(a)}),e.sort(function(e,s){var a=e.size,r=s.size;return a-r});var s={},a=1,r=null,n=1;e.forEach(function(e){e.size!=r&&(n=a),r=e.size,++a;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;o=jQuery("<a>"+o+"</a>").text(),s[o]=s[o]||{lang:e.language,lang_raw:o,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang_raw>s.lang_raw?1:e.lang_raw<s.lang_raw?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var QUESTION_ID=62598,ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",OVERRIDE_USER=8478,answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?([-\d]+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;</script>
10x10
, the random point is(9,4)
, and I guess(2,2)
, is the distancesqrt(13)
orsqrt(53)
? (Note for future: if you're doing anything weird, don't include randomness because it makes it almost impossible to supply test cases). 2. In the third bonus, should the distance be computed and output before or after the point moves? \$\endgroup\$ – Peter Taylor Nov 2 '15 at 20:19