Page 1 of 1

Number Flip - Game Design Challenge

PostPosted: Sun Jun 07, 2015 8:41 am
by Turn
Create a game in which the user will flip/reverse numbers around until they get them in ascending order.
Use the digits 1-9 once each as the list of numbers: i.e, [9, 4, 8, 5, 1, 2, 7, 3, 6]
The amount of digits flipped will be based on the number the user supplies, below are some examples:
list: [9, 4, 8, 5, 1, 2, 7, 3, 6] -> user input: 7 -> new list: [ 7, 2, 1, 5, 8, 4, 9, 3, 6]
list: [9, 4, 8, 5, 1, 2, 7, 3, 6] -> user input: 4 -> new list: [5, 8, 4, 9, 1, 2, 7, 3, 6]
The user gets a score based on how many flips it takes them to set the numbers in ascending order.
The final ascended list of course should be: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Ensure that your program does not start in ascending order, ever.

Re: Number Flip - Game Design Challenge

PostPosted: Sun Jun 07, 2015 9:43 am
by dusseldork
Since this was pretty simple I tried to additionally do it as short as I could. I don't know whether using C# made this shorter (because of how many libraries it has) or longer (because of how verbose it is compared to other languages). Maybe we need GolfScript.NET. Either way, here is my submission:

Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
    static void Main(string[] args)
    {
        var w = false;
        var i = new[]{1,2,3,4,5,6,7,8,9};
        var g = (int[])i.Clone();
        var r = new Random();
        var p = 0;
        for(var c=1;c<i.Length;c++)
            if (r.NextDouble() < 0.5||c==1)
            {
                var t = i[0];
                i[0]=i[c];
                i[c]=t;
            }

        while (!w)
        {
            Console.WriteLine();
            foreach (var b in i)
                Console.Write(b + " ");
            Console.WriteLine();
            int c = (int)Console.ReadKey().KeyChar - 48;
            var n = i.ToList().IndexOf(c);
            if (n >= 0) { i = i.Take(n + 1).Reverse().Concat(i.Skip(n + 1)).ToArray(); p++; }
            w = Enumerable.SequenceEqual(i,g);
        }
        Console.WriteLine("\nComplete in "+p+" turns.");
        Console.ReadLine();
    }
}


EDIT: I just noticed that the OP assumed that the input was the number of characters to swap, I interpreted it as the character to swap to (so [5, 3, 2, 4, 1] input 1 -> [1, 3, 2, 3, 5]). It would honestly be pretty trivial to change this if it's important :D

Re: Number Flip - Game Design Challenge

PostPosted: Sun Jun 07, 2015 10:20 am
by boriz666
Made my solution in c its not as compact as the C# solution,
have to make buffer overflow checks on input and reversing the array also
and some more stuff :D

The score keeping is crude, so bear with me!!

The game board is shuffled in the start.


Code: Select all

// Compile with: gcc -o numberflip numberflip.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#define OK       0
#define NO_INPUT 1
#define TOO_LONG 2

static int getLine (char *prmpt, char *buff, size_t sz);
void calculateGameState(int shiftNr,int* gameState, size_t sz);
void printGameState(int* gameState, size_t sz);
int isSolved(int* gameState) ;
void shuffleGameState(int* gameState);

int maxScore = 1000;
int myScore  = 0;

int main() {

    int rc;
    myScore = maxScore;

    int gameState[9] = {9, 4, 8, 5, 1, 2, 7, 3, 6};
    char buff[2];
    int myMove = 1; // default move is a no move


    shuffleGameState(gameState);

    printGameState(gameState,9);


    while (!isSolved(gameState)) {

      rc = getLine ("Enter amount of digits to shift, q to quit [2-9]: ", buff, sizeof(buff));
      if (rc == NO_INPUT) {
          // Extra NL since my system doesn't output that on EOF.
          printf ("\nWrong input\n");
          myMove = 1;       
      }
      else if (rc == TOO_LONG) {
          printf ("Input too long [%s]\n", buff);
          myMove = 1;
      }
      else if ( buff[0] == 'Q' || buff[0] == 'q') {
          printf("\nAre you giving in so quickly???\n\n");
          return 0;
      }
      else if ( (buff[0] - '0') < 2 || (buff[0] - '0') > 9 ) {
          printf ("Please enter digit from 2-9\n");
          myMove = 1;
      }
      else {
          myMove = buff[0] - '0';
      }
      calculateGameState(myMove,gameState,sizeof(gameState));
      printGameState(gameState,9);

      if ( isSolved(gameState) ) {
        printf("\n********* CONGRATULATIONS, YOU WIN!!! ****************\n");
        printf("YOUR SCORE IS: %d\n",myScore);
        return 0;
      }
    }

    return 0;

}



void shuffleGameState(int* gameState) {

   size_t n = 9;

   struct timeval tv;
   gettimeofday(&tv, NULL);
   int usec = tv.tv_usec;
   srand48(usec);

   size_t i;
   for (i = n - 1; i > 0; i--) {
     printf("Shuffeling!\n");
     size_t j = (unsigned int) (drand48()*(i+1));
     int t = gameState[j];
     gameState[j] = gameState[i];
     gameState[i] = t;
   }
}

int isSolved(int* gameState) {

  int solvedState[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  int i = 0;
  int j = 0;
  int solved = 0;

  for (i=0; i<9;i++) {
    if ( solvedState[i] != gameState[i]) return 0;
  }
 
  return 1;
}


void printGameState(int* gameState, size_t sz) {
   int i = 0;
   printf("Current Gamestate: ");
   
   for (i=0;i<sz;i++) {
    printf("%d ",gameState[i]); 
   }
   printf("\n");
}


void calculateGameState(int shiftNr,int* gameState, size_t sz) {

   if ( shiftNr < 2)  return;
   int shifted[shiftNr];

   int pos = 0;
   int i  = 0;

   for (i=shiftNr-1;i>=0;i--) {
     shifted[pos] = gameState[i];
     pos++;
   }

   for (i=0;i<shiftNr;i++) {
    gameState[i]=shifted[i];
   }

   myScore--;

}


static int getLine (char *prmpt, char *buff, size_t sz) {
    int ch, extra;

    if (prmpt != NULL) {
        printf ("%s", prmpt);
        fflush (stdout);
    }
    if (fgets (buff, sz, stdin) == NULL)
        return NO_INPUT;

    if (buff[strlen(buff)-1] != '\n') {
        extra = 0;
        while (((ch = getchar()) != '\n') && (ch != EOF))
            extra = 1;
        return (extra == 1) ? TOO_LONG : OK;
    }

    buff[strlen(buff)-1] = '\0';
    return OK;
}



Re: Number Flip - Game Design Challenge

PostPosted: Wed Jun 17, 2015 2:44 am
by Turn
dusseldork wrote:EDIT: I just noticed that the OP assumed that the input was the number of characters to swap, I interpreted it as the character to swap to (so [5, 3, 2, 4, 1] input 1 -> [1, 3, 2, 3, 5]). It would honestly be pretty trivial to change this if it's important :D


Yes, it's important :D

Boriz, again you did a fantastic job