Go-snake - Go CLI implementation of Snake game, using channels

go-snake 🐍

Go CLI implementation of Snake game, using channels.
NB: this code works only with ANSI terminals.

Demo

Demo
Resize the terminal window to change the "difficulty".

Fruit Spawn Algorithm

To prevent the fruit from spawning over the snake, we first get a pair of random indexes f.x and f.y and we check if that location is already occupied by the snake. If so, we save those indexes and iterate over the columns (f.y) and rows (f.x) of the board, starting from f.y+1 and f.x+1. At each cycle we check if the new location is still occupied by the snake:

  • If not we've found our fruit coordinates;
  • otherwise we increase f.x by one (or f.y, for the external loop). When the index reaches the border (BL or BH), we reset it to 1, so it can make a complete cycle, and stop when they get to f.x or f.y (remember we started from f.x+1 and f.y+1).

This way we got a random starting point for our fruit spawn location and, in case it's not valid, we iteratively check for free coordinates.

f.x = rand.Intn(BL-2) + 1
f.y = rand.Intn(BH-2) + 1

if b.xy[f.x][f.y] == HEAD || b.xy[f.x][f.y] == BODY {
  if f.x == 1 || f.x == BL-1 {
    f.x = BL / 2
  }
  if f.y == 1 || f.y == BH-1 {
    f.y = BH / 2
  }
  found := false
  j := f.y
  i := f.x
  for f.y += 1; f.y != j; f.y++ {
    for f.x += 1; f.x != i; f.x++ {
      if b.xy[f.x][f.y] != HEAD && b.xy[f.x][f.y] != BODY && b.xy[f.x][f.y] != BORDER {
        found = true
        break
      }
      if f.x == BL-1 {
        f.x = 1
      }
    }
    if found {
      break
    }
    if f.y == BH-1 {
      f.y = 1
    }
  }
}

References

Owner
Michele Righi
Bachelor's Degree in Software Engineering. University of Bologna
Michele Righi
Similar Resources

News-parser-cli - Simple CLI which allows you to receive news depending on the parameters passed to it

News-parser-cli - Simple CLI which allows you to receive news depending on the parameters passed to it

news-parser-cli Simple CLI which allows you to receive news depending on the par

Jan 4, 2022

Go-api-cli - Small CLI to fetch data from an API sync and async

Async API Cli CLI to fetch data on "todos" from a given API in a number of ways.

Jan 13, 2022

Syno-cli - Synology unofficial API CLI and library

Synology CLI Unofficial wrapper over Synology API in Go. Focus on administrative

Jan 6, 2023

Nebula Diagnosis CLI Tool is an information diagnosis cli tool for the nebula service and the node to which the service belongs.

Nebula Diagnosis CLI Tool is an information diagnosis cli tool for the nebula service and the node to which the service belongs.

Jan 12, 2022

bcrypt-cli is the CLI tool for hashing passwords with bcrypt.

bcrypt-cli bcrypt-cli is the CLI tool for hashing passwords with bcrypt. Install go install github.com/ryicoh/bcrypt-cli Usage It can be used like bas

Jan 9, 2023

Gobby-cli - CLI application to debug gobby applications

go(bby) Interactive debugging tool for gobby applications Usage Coming soonβ„’ Ins

Feb 8, 2022

GTDF-CLI - The official CLI tool to operate with Getting Things Done Framework

GTDF-CLI - The official CLI tool to operate with Getting Things Done Framework

This is the official CLI tool to operate with Getting Things Done Framework. How

Feb 14, 2022

Command-line tool for listening log file of game named as

Path of Exile Trade Notifier Command-line tool for listening log file of game named as "Path of Exile" and looking for buy message and send it to Tele

Apr 15, 2022

Go-wordle - Terminal based wordle game

Go-wordle - Terminal based wordle game

Feb 25, 2022
A simple CLI based rock-paper-scissors game created in GO with interactive shell prompt.

rock-paper-scissors A simple CLI (Command Line Interface) based rock-paper-scissors game with interactive shell prompt. Language Download Grab a binar

Oct 9, 2022
CLI to run a docker image with R. CLI built using cobra library in go.
CLI  to run a docker image with R. CLI built using cobra library in go.

BlueBeak Installation Guide Task 1: Building the CLI The directory structure looks like Fastest process: 1)cd into bbtools 2)cd into bbtools/bin 3)I h

Dec 20, 2021
Go-file-downloader-ftctl - A file downloader cli built using golang. Makes use of cobra for building the cli and go concurrent feature to download files.

ftctl This is a file downloader cli written in Golang which uses the concurrent feature of go to download files. The cli is built using cobra. How to

Jan 2, 2022
A go language implementation of a CLI, based on input directory files

A go language implementation of a CLI, based on input directory files, using tree output of all file names, based on input database links, dynamic reading of database table information, based on input swagger files to automate the generation of the RESTFUL API.

Aug 23, 2022
Elegant CLI wrapper for kubeseal CLI

Overview This is a wrapper CLI ofkubeseal CLI, specifically the raw mode. If you just need to encrypt your secret on RAW mode, this CLI will be the ea

Jan 8, 2022
A wrapper of aliyun-cli subcommand alidns, run aliyun-cli in Declarative mode.

aliyun-dns A wrapper of aliyun-cli subcommand alidns, run aliyun-cli in Declarative mode. Installation Install aliyun-cli. Usage $ aliyun-dns -h A wra

Dec 21, 2021
Symfony-cli - The Symfony CLI tool For Golang

Symfony CLI Install To install Symfony CLI, please download the appropriate vers

Dec 28, 2022
Cli-algorithm - A cli program with A&DS in go!

cli-algorithm Objectives The objective of this cli is to implement 4 basic algorithms to sort arrays been Merge Sort Insertion Sort Bubble Sort Quick

Jan 2, 2022
Nebulant-cli - Nebulant's CLI
Nebulant-cli - Nebulant's CLI

Nebulant CLI Website: https://nebulant.io Documentation: https://nebulant.io/docs.html The Nebulant CLI tool is a single binary that can be used as a

Jan 11, 2022