Telnet mini-game "lifeline" for ultra-novice viewing

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. In learning Golang these days in play a mobile phone game called "Lifeline", think very interesting, think can use Golang to do a small embryonic form out. By the way, learn about the operation of Golang on MySQL. First, enter the MySQL operation ' #首先创建一个数据库CREATE DATABASE ' test ' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; #表结构是这样, like ' Tree ', when you make a choice, find the next event based on left_event_id create TABLE ' events ' (' event_id ' INT () not NULL PRIMARY KEY auto_increment, ' Eve Nt_detail ' varchar (+) NULL, ' left_event_id ' int (ten), ' right_event_id ' int (ten) ', ' left_option ' varchar, ' right_ Option ' VARCHAR ' #先插入几条数据试试看INSERT into events SET event_id=1,event_detail= "Here is the ECUST219 number, call command, please reply, over. \ n This is ECUST219, call command ... Oh, my God, someone has finally responded to me, is it the command? "Left_event_id=2,right_event_id=3,left_option=," Yes, what happened? "Right_option=" No, what's going on? INSERT into events SET event_id=2,event_detail= "Oh, my God." command, I'm site-768 now, the expedition is all ... In addition to me, all the sacrifices, hope to send a rescue as soon as possible. ", left_event_id=4,right_event_id=5,left_option=" no Rescue ", right_option=" sorry "INSERT into events SET Event_id=3,event_ Detail= "Oh, my God. Don't Hang up! Please, don't break the signal, you're the only one who's been responding to me in the last few days. "Left_event_id=6,right_event_id=7,left_option=," what happened? ", Right_opTion= "What's wrong with you?" "'" haha, casually wrote something in it. Then start Golang code with a total of no more than 100 lines of "package main//Import Packages imported (" Bufio "" Database/sql "" FMT "" Net "_" github.com/go-sql-driver/ MySQL ") var (event_id int//Event Idevent_detail string//event content left_event_id INT//Left dial hand event idright_event_id int//Right child event Idleft_ Option string//Left Dial Hand event option Right_option string//Right child event option is actually a or b) Func Checkerr (err error) {if err! = Nil {panic (err)}}/* will get the information Output to Terminal */func Echo (c net. Conn, DB *sql. DB, id int) {row: = db. Queryrow ("SELECT * from Events WHERE event_id=?", id) Err: = row. Scan (&event_id, &event_detail, &left_event_id, &right_event_id, &left_option, &right_option ) If Err = = sql. errnorows {fmt. fprintf (C, "Game Over") C.close () return}fmt. fprintf (c, "\n\033[1;37m%s\n\n\033[0;31ma)%s\n\nb)%s\n", Event_detail, Left_option, right_option)}/* get terminal input and process */ Func handleconn (db *sql. DB, c net. Conn) {event_id = 1echo (c, DB, event_id) Input: = Bufio. Newscanner (c) for input. Scan () {switch input. Text () {case ' a ': event_id = Left_event_idecho (c, DB, event_id) case "B": Event_id = Right_event_idecho (c, DB, event_id) case "Exit": C.close () Returndefault:continue}}}func main () {db, err: = SQL. Open ("MySQL", "root:root@tcp (127.0.0.1:3306)/test?charset=utf8") defer db. Close () Checkerr (ERR)//echo (db, event_id) listener, err: = Net. Listen ("TCP", "localhost:8888") Checkerr (Err) for {conn, err: = Listener. Accept () Checkerr (err) Go handleconn (DB, conn)} "", Miserable! [A.jpg] (https://static.studygolang.com/171201/07156b2c57ab9e6eb1759dcdf3d17cca.jpg) 470 reads  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.