{h="\n";w=s=>c.write(s+h);if(o" /> {h="\n";w=s=>c.write(s+h);if(o" /> {h="\n";w=s=>c.write(s+h);if(o"/>

2 player tictactoe-hosting TCP server in 640 bytes

Overview

tictactinytoe

2 player tictactoe-hosting TCP server in 640 bytes:

F=_=>{x=o=z=0;t=1};F();require("net").createServer(c=>{h="\n";w=s=>c.write(s+h);if(o)return w("Game in progress");c.m={};c.w=w;z++?(o=c,o.W=x.w,x.W=w):x=c;G=e=>{for(l="",i=1;i<=9;i++)l+=i in x.m?"X":o&&i in o.m?"O":i,l+=i%3?"|":h;e.w(l)};G(c);c.on("end",_=>c.W("Player left")).on("data",d=>{if(t^c==x)return;v=parseInt(d);if(isNaN(v)||v<1||v>9||v in{...x.m,...o.m})return c.w("Invalid move");t=c.m[v]=1-t;G(x);o&&G(o);f=i=>i<24?"123147159369789753258456".slice(i,i+3).split("").every(j=>j in c.m)||f(i+3):0;if(f(0))throw c.w("You won"),c.W("You lost");if(z++>9)throw r="Draw",c.w(r),c.W(r)})}).listen(9191);process.on("uncaughtException",F)

Tested with node v14.16.1

node -e 'F=_=>{x=o=z=0;t=1};F();require("net").createServer(c=>{h="\n";w=s=>c.write(s+h);if(o)return w("Game in progress");c.m={};c.w=w;z++?(o=c,o.W=x.w,x.W=w):x=c;G=e=>{for(l="",i=1;i<=9;i++)l+=i in x.m?"X":o&&i in o.m?"O":i,l+=i%3?"|":h;e.w(l)};G(c);c.on("end",_=>c.W("Player left")).on("data",d=>{if(t^c==x)return;v=parseInt(d);if(isNaN(v)||v<1||v>9||v in{...x.m,...o.m})return c.w("Invalid move");t=c.m[v]=1-t;G(x);o&&G(o);f=i=>i<24?"123147159369789753258456".slice(i,i+3).split("").every(j=>j in c.m)||f(i+3):0;if(f(0))throw c.w("You won"),c.W("You lost");if(z++>9)throw r="Draw",c.w(r),c.W(r)})}).listen(9191);process.on("uncaughtException",F)'
  • Non-minified code is in tictactinytoe.js
  • Use netcat to connect to the server and play
  • Both players must Ctrl-C and reconnect for a new game
netcat localhost 9191  # first player: X
netcat localhost 9191  # second player: O
Docker
docker run --rm -it node:14-alpine node -e 'F=_=>{x=o=z=0;t=1};F();require("net").createServer(c=>{h="\n";w=s=>c.write(s+h);if(o)return w("Game in progress");c.m={};c.w=w;z++?(o=c,o.W=x.w,x.W=w):x=c;G=e=>{for(l="",i=1;i<=9;i++)l+=i in x.m?"X":o&&i in o.m?"O":i,l+=i%3?"|":h;e.w(l)};G(c);c.on("end",_=>c.W("Player left")).on("data",d=>{if(t^c==x)return;v=parseInt(d);if(isNaN(v)||v<1||v>9||v in{...x.m,...o.m})return c.w("Invalid move");t=c.m[v]=1-t;G(x);o&&G(o);f=i=>i<24?"123147159369789753258456".slice(i,i+3).split("").every(j=>j in c.m)||f(i+3):0;if(f(0))throw c.w("You won"),c.W("You lost");if(z++>9)throw r="Draw",c.w(r),c.W(r)})}).listen(9191);process.on("uncaughtException",F)'
  • Use docker inspect <container> | grep -m 1 '"IPAddress":' to find IP for netcat
  • To exit the server use docker stop <container>
You might also like...

A super tiny Javascript library to make DOM elements draggable and movable. ~500 bytes and no dependencies.

dragmove.js A super tiny Javascript library to make DOM elements draggable and movable. Has touch screen support. Zero dependencies and 500 bytes Gzip

Dec 29, 2022

👌A useful zero-dependencies, less than 434 Bytes (gzipped), pure JavaScript & CSS solution for drop an annoying pop-ups confirming the submission of form in your web apps.

👌A useful zero-dependencies, less than 434 Bytes (gzipped), pure JavaScript & CSS solution for drop an annoying pop-ups confirming the submission of form in your web apps.

Throw out pop-ups confirming the submission of form! A useful zero-dependencies, less than 434 Bytes (gzipped), pure JavaScript & CSS solution for dro

Aug 24, 2022

Monolithic repo for api server, image server, web server

Onsecondary Market Deployed at https://market.onsecondary.com Monolithic repo for api server, image server, web server TODO -use a script to cull expi

Jan 11, 2022

A Discord Bot that connects to your AzerothCore server so you / users can manage the server / character

A Discord Bot that connects to your AzerothCore server so you / users can manage the server / character. Made for AzerothCore / azerothcore-tools

Sep 24, 2022

Deploying Fake Back-End Server & DataBase Using JSON-SERVER, GitHub, and Heroku

Deploying Fake Back-End Server & DataBase Using JSON-SERVER, GitHub, and Heroku

Deploying Fake Back-End Server & DataBase Using JSON-SERVER, GitHub, and Heroku. In this article, we will create and host a fake server that we can de

Sep 5, 2022

simple PWA catalogue with vanilla javascript and json-server as a fake server

simple PWA catalogue with vanilla javascript and json-server as a fake server

simple PWA catalogue with vanilla javascript and json-server as a fake server

Mar 12, 2022

Adds full-text search to Community Solid Server. Powered by atomic-server

Solid Search for Community Solid Server This is an extension / plugin for the Community Solid Server. It adds full-text search to the Community Solid

Jun 6, 2022

An OAuth2 Authorization Server,Based on Spring Authorization Server

🚀 id-server 一个基于Spring Authorization Server的开源的授权服务器。 概念 一些概念 OAuth2Client 客户端指的是OAuth2 Client,但又不单单是一个OAuth2 Client,连id server本身都是一个客户端。 role 角色必须依附

Dec 30, 2022

WPPConnect/WA-JS API SERVER is a small api server to provide url preview for @wppconnect/wa-js library

WPPConnect/WA-JS API SERVER WPPConnect/WA-JS API SERVER is a small api server to provide url preview for @wppconnect/wa-js library Our online channels

Aug 11, 2022
Comments
  • feat: alternate turns

    feat: alternate turns

    depends on: https://github.com/shivamMg/tictactinytoe/pull/1

    I thought about always allowing the winner to take the first turn, but that puts the initial starter at an advantage so this is probably a better idea

    opened by kiprasmel 0
  • feat: add scores

    feat: add scores

    depends on: https://github.com/shivamMg/tictactinytoe/pull/1

    refactored to use c.o as connection.other instead of keeping track of separate write methods and scores, since using references to keep scores avoids the need to update it in 2 places, which automatically avoids bugs

    opened by kiprasmel 0
  • feat: auto restart

    feat: auto restart

    thanks for this, it was fun to play with:D

    I added auto restart because it seemed a little annoying for both clients to reconnect after each game. But you don't have to merge this - it can stay as a reference for the curious (I assume you'd like to keep the byte size small:D)

    opened by kiprasmel 1
Owner
Shivam Mamgain
programmer;
Shivam Mamgain
Inter Process Communication Module for node supporting Unix sockets, TCP, TLS, and UDP. Giving lightning speed on Linux, Mac, and Windows. Neural Networking in Node.JS

Inter Process Communication Module for node supporting Unix sockets, TCP, TLS, and UDP. Giving lightning speed on Linux, Mac, and Windows. Neural Networking in Node.JS

Node IPC 43 Dec 9, 2022
Create Route53 HealthChecks to monitor TCP, HTTP, HTTPS endpoints, CloudWatch Alarms and other Route53 HealthChecks

AWS CDK Route53 HealthCheck Create Route53 HealthChecks to monitor TCP, HTTP, HTTPS endpoints, to monitor CloudWatch Alarms and to monitor other Route

Pepperize 7 Dec 15, 2022
Short sample and instructions for configuring a Flutter Web application to deploy-on-push to Firebase Hosting

Short sample and instructions for configuring a Flutter Web application to deploy-on-push to Firebase Hosting

Kevin Moore 18 Nov 17, 2022
Like Obsidian Publish but for self-hosting. Plugin integrations for dataview, admonition, and more.

Obsidian Export Obsidian Publish is great but lacks support for many of the plugins we Obsidian addicts have grown accustomed to — in particular Datav

null 12 Nov 28, 2022
Image hosting based on Cloudflare R2. Supports PicGo.

Cloudflare R2 ImageBed English | 简体中文 CF-R2-ImageBed is a image hosting service based on Cloudflare R2 object storage. PicGo supported. Cloudflare R2

null 4 Oct 4, 2022
the music metadata you love, with IPFS hosting on nft.storage :rainbow:

Music NFT IPFS Metadata Standard for musicians pioneered by Catalog & Mint Songs. Compatible with contracts created by: Catalog Sound.xyz Manifold - W

sweetman.eth 12 Oct 17, 2022
Official repository hosting the Runtipi App Store. Submit apps and updates here.

Tipi App Store ⛺️ This is the official repository for the Tipi App Store. It contains all the apps that are available for download on Tipi. Apps avail

Nicolas Meienberger 59 Jan 3, 2023
Essential Audio Player JS is a simple, clean and minimal JavaScript / HTML5 / CSS web audio player.

Essential Audio Player JS is a simple, clean and minimal JavaScript / HTML5 / CSS web audio player. No unnecessary controls, just a button and a track

null 32 Dec 14, 2022
🟢 Music player app with a modern homepage, fully-fledged music player, search, lyrics, song exploration features, search, popular music around you, worldwide top charts, and much more.

Music-player-app see the project here. 1. Key Features 2. Technologies I've used Key Features: ?? Fully responsive clean UI. ?? Entirely mobile respo

suraj ✨ 3 Nov 16, 2022
A tiny (108 bytes), secure, URL-friendly, unique string ID generator for JavaScript

Nano ID English | Русский | 简体中文 | Bahasa Indonesia A tiny, secure, URL-friendly, unique string ID generator for JavaScript. “An amazing level of sens

Andrey Sitnik 19.6k Jan 8, 2023