Task: Spoof game

C: #include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define TRUE 1
#define FALSE 0
#define ESC 27
#define TEST TRUE  

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);  
                printf("%c[2K", ESC);  
                printf("\r\n");        
            }
            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;
}




Go: package main

import (
    "bufio"
    "fmt"
    "log"
    "math/rand"
    "os"
    "strconv"
    "time"
)

const (
    esc  = "\033"
    test = true 
)

var scanner = bufio.NewScanner(os.Stdin)

func indexOf(s []int, el int) int {
    for i, v := range s {
        if v == el {
            return i
        }
    }
    return -1
}

func getNumber(prompt string, min, max int, showMinMax bool) (int, error) {
    for {
        fmt.Print(prompt)
        if showMinMax {
            fmt.Printf(" from %d to %d : ", min, max)
        } else {
            fmt.Printf(" : ")
        }
        scanner.Scan()
        if scerr := scanner.Err(); scerr != nil {
            return 0, scerr
        }
        input, err := strconv.Atoi(scanner.Text())
        if err == nil && input >= min && input <= max {
            fmt.Println()
            return input, nil
        }
    }
}

func check(err error, text string) {
    if err != nil {
        log.Fatalln(err, text)
    }
}

func main() {
    rand.Seed(time.Now().UnixNano())
    players, err := getNumber("Number of players", 2, 9, true)
    check(err, "when getting players")
    coins, err2 := getNumber("Number of coins per player", 3, 6, true)
    check(err2, "when getting coins")
    remaining := make([]int, players)
    for i := range remaining {
        remaining[i] = i + 1
    }
    first := 1 + rand.Intn(players)
    fmt.Println("The number of coins in your hand will be randomly determined for")
    fmt.Println("each round and displayed to you. However, when you press ENTER")
    fmt.Println("it will be erased so that the other players, who should look")
    fmt.Println("away until it's their turn, won't see it. When asked to guess")
    fmt.Println("the total, the computer won't allow a 'bum guess'.")
    for round := 1; ; round++ {
        fmt.Printf("\nROUND %d:\n\n", round)
        n := first
        hands := make([]int, players+1)
        guesses := make([]int, players+1)
        for i := range guesses {
            guesses[i] = -1
        }
        for {
            fmt.Printf("  PLAYER %d:\n", n)
            fmt.Println("    Please come to the computer and press ENTER")
            hands[n] = rand.Intn(coins + 1)
            fmt.Print("      <There are ", hands[n], " coin(s) in your hand>")
            scanner.Scan()
            check(scanner.Err(), "when pressing ENTER")
            if !test {
                fmt.Print(esc, "[1A") 
                fmt.Print(esc, "[2K") 
                fmt.Println("\r")     
            } else {
                fmt.Println()
            }
            for {
                min := hands[n]
                max := (len(remaining)-1)*coins + hands[n]
                guess, err3 := getNumber("    Guess the total", min, max, false)
                check(err3, "when guessing the total")
                if indexOf(guesses, guess) == -1 {
                    guesses[n] = guess
                    break
                }
                fmt.Println("    Already guessed by another player, try again")
            }
            index := indexOf(remaining, n)
            if index < len(remaining)-1 {
                n = remaining[index+1]
            } else {
                n = remaining[0]
            }
            if n == first {
                break
            }
        }
        total := 0
        for _, hand := range hands {
            total += hand
        }
        fmt.Println("  Total coins held =", total)
        eliminated := false
        for _, v := range remaining {
            if guesses[v] == total {
                fmt.Println("  PLAYER", v, "guessed correctly and is eliminated")
                r := indexOf(remaining, v)
                remaining = append(remaining[:r], remaining[r+1:]...)
                eliminated = true
                break
            }
        }
        if !eliminated {
            fmt.Println("  No players guessed correctly in this round")
        } else if len(remaining) == 1 {
            fmt.Println("\nPLAYER", remaining[0], "buys the drinks!")
            return
        }
        index2 := indexOf(remaining, n)
        if index2 < len(remaining)-1 {
            first = remaining[index2+1]
        } else {
            first = remaining[0]
        }
    }
}