Skip to content

Instantly share code, notes, and snippets.

@tormaroe
Last active August 29, 2015 14:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tormaroe/b4706b6f93f09d267e6d to your computer and use it in GitHub Desktop.
A simple game of skill, implemented in Racket and C#
#lang racket
;; Author: Torbjørn Marø
;; Inspired by Peter Sessions REVERSE game
;; as published in 'BASIC Computer Games' (1978)
;; http://www.atariarchives.org/basicgames/showpage.php?page=135
(displayln "Welcome to REVERSE -- A Game of Skill! ")
(newline)
(displayln "To win, all you have to do is arrange a list ")
(displayln "of numbers (1 through 9) in numberical order ")
(displayln "from left to right. To move, you tell me how ")
(displayln "many numbers (counted from the left) to reverse. ")
(newline)
(displayln "No doubt you will like this game, but if you want ")
(displayln "to quit, reverse 0 (zero). ")
(newline)
(define (slice list index count)
(take (drop list index) count))
(define (reverse-slice list index count)
(append (slice list 0 index)
(reverse (slice list index count))
(slice list
(+ index count)
(- (length list) index count))))
(define (new-game-list)
(shuffle (range 1 10)))
(define (prompt-choice)
(display "How many shall I reverse? ")
(let ([input (read)])
(if (and (integer? input)
(>= input 0)
(< input 10))
input
(begin
(displayln "Please answer between 0 and 9!")
(prompt-choice)))))
(define done?
((curry equal?) '(1 2 3 4 5 6 7 8 9)))
(define (print-game list)
(newline)
(for ([n list])
(printf " ~a" n))
(newline)
(newline))
(displayln "Here we go ... the list is: ")
(define (run-game moves game)
(let ([choice (prompt-choice)])
(if (zero? choice)
(displayln "O.K. Bye Bye!")
(let ([game (reverse-slice game 0 choice)])
(print-game game)
(if (done? game)
(printf "You won it in ~a moves!" moves)
(run-game (add1 moves) game))))))
(let ([game (new-game-list)])
(print-game game)
(run-game 1 game))
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication3
{
/**
* This is basically the same thing implemented in C#
* as a reference. Both implementations will appear
* in a post on my blog.
**/
class Program
{
static List<int> NewGameList()
{
return Enumerable
.Range(1, 9)
.OrderBy( _ => Guid.NewGuid() )
.ToList();
}
static int PromptChoice()
{
Console.Write("Hom many shall I reverse? ");
int result;
if (int.TryParse(Console.ReadLine(), out result)
&& result >= 0 && result < 10)
{
return result;
}
Console.WriteLine("Answer between 0 and 9, please try again!");
return PromptChoice();
}
static bool Done(List<int> list)
{
for (int i = 0; i < list.Count; i++)
if (list[i] != i + 1)
return false;
return list.Count == 9;
}
static void PrintGame(List<int> list)
{
Console.WriteLine();
foreach (var item in list)
Console.Write(" " + item);
Console.WriteLine("\n");
}
static void RunGame(int moves, List<int> game)
{
int choice = PromptChoice();
if (choice == 0)
{
Console.WriteLine("O.K. Bye Bye!");
return;
}
game.Reverse(0, choice);
PrintGame(game);
if (Done(game))
{
Console.WriteLine("You won it in {0} moves!", moves);
return;
}
RunGame(moves + 1, game);
}
static void Main(string[] args)
{
Console.WriteLine(
"Welcome to REVERSE -- A Game of Skill!\n\n" +
"To win, all you have to do is arrange a list\n" +
"of numbers (1 through 9) in numberical order\n" +
"from left to right. To move, you tell me how\n" +
"many numbers (counted from the left) to reverse.\n\n" +
"No doubt you will like this game, but if you want\n" +
"to quit, reverse 0 (zero).\n\n" +
"Here we go ... the list is:");
var game = NewGameList();
PrintGame(game);
RunGame(1, game);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment