Spoof game: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added C)
m (→‎{{header|Kotlin}}: Minor layout adjustments.)
Line 199: Line 199:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
This program has a TEST mode. If you set it to true, the number of coins allocated to each player for each round won't be erased after he/she presses ENTER, which allows you to check it is working out the total for the round correctly.
This program has a TEST mode. If you set it to true, the number of coins allocated to each player for each round won't be erased after he/she presses ENTER, which allows you to check it is working out the total for the round correctly.
<lang scala>//Version 1.2.40
<lang scala>// Version 1.2.40


import java.util.Random
import java.util.Random
Line 284: Line 284:
val index2 = remaining.indexOf(n)
val index2 = remaining.indexOf(n)
first = if (index2 < remaining.lastIndex) remaining[index2 + 1]
first = if (index2 < remaining.lastIndex) remaining[index2 + 1]
else remaining[0]
else remaining[0]
round++
round++
}
}

Revision as of 19:56, 23 April 2018

Spoof game is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.


Create Spoof game. See details: Spoof game


C

Translation of: Kotlin

<lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <time.h>
  1. define TRUE 1
  2. define FALSE 0
  3. define ESC 27
  4. define TEST TRUE /* set to 'false' to erase each player's coins */

typedef int bool;

int get_number(const char *prompt, int min, int max, bool show_mm) {

   int n;
   char *line = NULL;
   size_t len = 0;
   ssize_t read;
   fflush(stdin);
   do {
       printf("%s", prompt);
       if (show_mm)
           printf(" from %d to %d : ", min, max);
       else
           printf(" : ");
       read = getline(&line, &len, stdin);
       if (read < 2) continue;
       n = atoi(line);
   }
   while (n < min || n > max);
   printf("\n");
   return n;

}

int compare_int(const void *a, const void* b) {

   int i = *(int *)a;
   int j = *(int *)b;
   return i - j;

}

int main() {

   int i, j, n, players, coins, first, round = 1, rem_size;
   int min, max, guess, index, index2, total;
   int remaining[9], hands[10], guesses[10];
   bool found, eliminated;
   char c;
   players = get_number("Number of players", 2, 9, TRUE);
   coins = get_number("Number of coins per player", 3, 6, TRUE);
   for (i = 0; i < 9; ++i) remaining[i] = i + 1;
   rem_size = players;
   srand(time(NULL));
   first = 1 + rand() % players;
   printf("The number of coins in your hand will be randomly determined for");
   printf("\neach round and displayed to you. However, when you press ENTER");
   printf("\nit will be erased so that the other players, who should look");
   printf("\naway until it's their turn, won't see it. When asked to guess");
   printf("\nthe total, the computer won't allow a 'bum guess'.\n");
   while(TRUE) {
       printf("\nROUND %d:\n", round);
       n = first;
       for (i = 0; i < 10; ++i) {
           hands[i] = 0; guesses[i] = -1;
       }
       do {
           printf("  PLAYER %d:\n", n);
           printf("    Please come to the computer and press ENTER\n");
           hands[n] = rand() % (coins + 1);
           printf("      <There are %d coin(s) in your hand>", hands[n]);
           while (getchar() != '\n');
           if (!TEST) {
               printf("%c[1A", ESC);  // move cursor up one line
               printf("%c[2K", ESC);  // erase line
               printf("\r\n");        // move cursor to beginning of line
           }
           else printf("\n");
           while (TRUE) {
               min = hands[n];
               max = (rem_size - 1) * coins + hands[n];
               guess = get_number("    Guess the total", min, max, FALSE);
               found = FALSE;
               for (i = 1; i < 10; ++i) {
                   if (guess == guesses[i]) {
                       found = TRUE;
                       break;
                   }
               }
               if (!found) {
                   guesses[n] = guess;
                   break;
               }
               printf("    Already guessed by another player, try again\n");
           }
           index = -1;
           for (i = 0; i < rem_size; ++i) {
               if (remaining[i] == n) {
                   index = i;
                   break;
               }
           }
           if (index < rem_size - 1)
               n = remaining[index + 1];
           else
               n = remaining[0];
       }
       while (n != first);
       total = 0;
       for (i = 1; i < 10; ++i) total += hands[i];
       printf("  Total coins held = %d\n", total);
       eliminated = FALSE;
       for (i = 0; i < rem_size; ++i) {
           j = remaining[i];
           if (guesses[j] == total) {
               printf("  PLAYER %d guessed correctly and is eliminated\n", j);
               remaining[i] = 10;
               rem_size--;
               qsort(remaining, players, sizeof(int), compare_int);
               eliminated = TRUE;
               break;
           }
       }
       if (!eliminated)
           printf("  No players guessed correctly in this round\n");
       else if (rem_size == 1) {
           printf("\nPLAYER %d buys the drinks!\n", remaining[0]);
           break;
       }
       index2 = -1;
       for (i = 0; i < rem_size; ++i) {
           if (remaining[i] == first) {
               index2 = i;
               break;
           }
       }
       if (index2 < rem_size - 1)
           first = remaining[index2 + 1];
       else
           first = remaining[0];
       round++;
   }
   return 0;

}</lang>

Output:

Sample game (TEST == true):

Number of players from 2 to 9 : 2

Number of coins per player from 3 to 6 : 4

The number of coins in your hand will be randomly determined for
each round and displayed to you. However, when you press ENTER
it will be erased so that the other players, who should look
away until it's their turn, won't see it. When asked to guess
the total, the computer won't allow a 'bum guess'.

ROUND 1:
  PLAYER 1:
    Please come to the computer and press ENTER
      <There are 4 coin(s) in your hand>

    Guess the total : 6

  PLAYER 2:
    Please come to the computer and press ENTER
      <There are 0 coin(s) in your hand>

    Guess the total : 3

  Total coins held = 4
  No players guessed correctly in this round

ROUND 2:
  PLAYER 2:
    Please come to the computer and press ENTER
      <There are 4 coin(s) in your hand>

    Guess the total : 5

  PLAYER 1:
    Please come to the computer and press ENTER
      <There are 3 coin(s) in your hand>

    Guess the total : 7

  Total coins held = 7
  PLAYER 1 guessed correctly and is eliminated

PLAYER 2 buys the drinks!

Kotlin

This program has a TEST mode. If you set it to true, the number of coins allocated to each player for each round won't be erased after he/she presses ENTER, which allows you to check it is working out the total for the round correctly. <lang scala>// Version 1.2.40

import java.util.Random

const val ESC = '\u001B' const val TEST = true // set to 'false' to erase each player's coins

fun getNumber(prompt: String, min: Int, max: Int, showMinMax: Boolean) : Int {

   var input: Int?
   do {
       print(prompt)
       if (showMinMax)
           print(" from $min to $max : ")
       else
           print(" : ")
       input = readLine()!!.toIntOrNull()
   }
   while (input == null || input !in min..max)
   println()
   return input

}

fun main(args: Array<String>) {

   val rand = Random()
   val players = getNumber("Number of players", 2, 9, true)
   val coins = getNumber("Number of coins per player", 3, 6, true)
   var remaining = MutableList(players) { it + 1 }
   var first = 1 + rand.nextInt(players)
   var round = 1
   println("The number of coins in your hand will be randomly determined for")
   println("each round and displayed to you. However, when you press ENTER")
   println("it will be erased so that the other players, who should look")
   println("away until it's their turn, won't see it. When asked to guess")
   println("the total, the computer won't allow a 'bum guess'.")
   while(true) {
       println("\nROUND $round:\n")
       var n = first
       val hands = IntArray(players + 1)
       val guesses = IntArray(players + 1) { -1 }
       do {
           println("  PLAYER $n:")
           println("    Please come to the computer and press ENTER")
           hands[n] = rand.nextInt(coins + 1)
           print("      <There are ${hands[n]} coin(s) in your hand>")
           do {} while (readLine() == null)
           if (!TEST) {
               print("${ESC}[1A")  // move cursor up one line
               print("${ESC}[2K")  // erase line
               println("\r")       // move cursor to beginning of line
           }
           else println()
           while (true) {
               val min = hands[n]
               val max = (remaining.size - 1) * coins + hands[n]
               val guess = getNumber("    Guess the total", min, max, false)
               if (guess !in guesses) {
                   guesses[n] = guess
                   break
               }
               println("    Already guessed by another player, try again")
           }
           val index = remaining.indexOf(n)
           n = if (index < remaining.lastIndex) remaining[index + 1]
               else remaining[0]
       }
       while (n != first)
       val total = hands.sum()
       println("  Total coins held = $total")
       var eliminated = false
       for (i in remaining) {
           if (guesses[i] == total) {
               println("  PLAYER $i guessed correctly and is eliminated")
               remaining.remove(i)
               eliminated = true
               break
           }
       }
       if (!eliminated)
           println("  No players guessed correctly in this round")
       else if (remaining.size == 1) {
           println("\nPLAYER ${remaining[0]} buys the drinks!")
           return
       }
       val index2 = remaining.indexOf(n)
       first = if (index2 < remaining.lastIndex) remaining[index2 + 1]
               else remaining[0]
       round++
   }

}</lang>

Output:

Sample game. Note that lines enclosed in angle brackets will be erased after the player presses ENTER if TEST = false.

Number of players from 2 to 9 : 3

Number of coins per player from 3 to 6 : 3

The number of coins in your hand will be randomly determined for
each round and displayed to you. However, when you press ENTER
it will be erased so that the other players, who should look
away until it's their turn, won't see it. When asked to guess
the total, the computer won't allow a 'bum guess'.

ROUND 1:

  PLAYER 3:
    Please come to the computer and press ENTER
      <There are 2 coin(s) in your hand>

    Guess the total : 6

  PLAYER 1:
    Please come to the computer and press ENTER
      <There are 3 coin(s) in your hand>

    Guess the total : 7

  PLAYER 2:
    Please come to the computer and press ENTER
      <There are 3 coin(s) in your hand>

    Guess the total : 8

  Total coins held = 8
  PLAYER 2 guessed correctly and is eliminated

ROUND 2:

  PLAYER 1:
    Please come to the computer and press ENTER
      <There are 2 coin(s) in your hand>

    Guess the total : 4

  PLAYER 3:
    Please come to the computer and press ENTER
      <There are 0 coin(s) in your hand>

    Guess the total : 3

  Total coins held = 2
  No players guessed correctly in this round

ROUND 3:

  PLAYER 3:
    Please come to the computer and press ENTER
      <There are 3 coin(s) in your hand>

    Guess the total : 4

  PLAYER 1:
    Please come to the computer and press ENTER
      <There are 0 coin(s) in your hand>

    Guess the total : 3

  Total coins held = 3
  PLAYER 1 guessed correctly and is eliminated

PLAYER 3 buys the drinks!

Ring

<lang ring>

  1. Project : Spoof game
  2. Date  : 2018/04/22
  3. Author : Gal Zsolt (~ CalmoSoft ~)
  4. Email  : <calmosoft@gmail.com>

spoof = 0:6 see "How many games you want? " give games

for n = 1 to games

    while true
            mypot = random(3)
            myguess = random(6)
            if mypot + 3 >= myguess
               loop
            else
               exit
            ok
    end
    see "I have put my pot and guess" + nl
    while true
            see "Your pot? "
            give yourpot
            see "Your guess? "
            give yourguess
            if ascii(yourpot) < 48 or ascii(yourpot) > 54 or ascii(yourguess) < 48 or ascii(yourguess) > 54
               loop
            ok
            if yourpot + 3 >= yourguess
               exit
            else
               see "Bad input! Try again" + nl
               loop
            ok
    end
    see "My put is: " + mypot + nl
    see "My guess is: " + myguess + nl
    pot = mypot + yourpot
    if myguess = pot and yourguess = pot
       see "Draw!" + nl
    elseif myguess = pot
       see "I won!" + nl
    elseif yourguess = pot
       see "You won!" + nl
    else
       see "No winner!" + nl
    ok

next </lang> Output:

How many games you want? 4
I have put my pot and guess
Your pot? 1
Your guess? 3
My put is: 2
My guess is: 6
You won!
I have put my pot and guess
Your pot? 1
Your guess? 4
My put is: 2
My guess is: 5
No winner!
I have put my pot and guess
Your pot? 3
Your guess? 6
My put is: 3
My guess is: 6
Draw!
I have put my pot and guess
Your pot? 2
Your guess? 4
My put is: 0
My guess is: 5
No winner!

zkl

Translation of: Ring

<lang zkl>do(getNum("How many games you want? ")){

  reg mypot,yourpot,myguess,yourguess;
  do{ mypot,myguess = (0).random(4),(0).random(7);  // 0..3, 0..6
     }while(mypot + 3 <= myguess);
  println("I have put my pot and guess.");
  while(True){
     yourpot  =getNum("Your pot? ");
     yourguess=getNum("Your guess? ");
     if(((0<=yourpot<=6) and (0<=yourguess<=6)) and 
        yourpot + 3 >= yourguess) break;
     println("Bad input! Try again");
  }
  pot:=mypot + yourpot;
  println("My pot is: ",mypot, ".  My guess is: ",myguess, ".  Pot is ",pot);
  if(myguess==pot==yourguess) println("Draw!\n");
  else if(myguess==pot)       println("I won!\n");
  else if(yourguess==pot)     println("You won!\n");
  else 		       println("No winner!\n");

} fcn getNum(msg){

  try{ return(ask(msg).strip().toInt()) }
  catch{ println("ack"); return(self.fcn(msg)) }  // tail recursion

}</lang>

Output:
How many games you want? 4
I have put my pot and guess.
Your pot? 2
Your guess? 3
My pot is: 1.  My guess is: 0.  Pot is 3
You won!

I have put my pot and guess.
Your pot? 1
Your guess? 0
My pot is: 2.  My guess is: 3.  Pot is 3
I won!

I have put my pot and guess.
Your pot? 4
Your guess? 5
My pot is: 1.  My guess is: 1.  Pot is 5
You won!

I have put my pot and guess.
Your pot? 2
Your guess? 1
My pot is: 3.  My guess is: 2.  Pot is 5
No winner!