예제 #1
0
func TestDraw(t *testing.T) {
	game := core.NewGame()

	output := captureOutput(func() {
		draw(game)
	})

	shouldBeString := `/===========================\
|    0 |    0 |    0 |    0 |
|===========================|
|    0 |    0 |    2 |    0 |
|===========================|
|    0 |    0 |    0 |    0 |
|===========================|
|    0 |    0 |    0 |    0 |
|===========================|
| Next number: |      2     |
| Score: |             0    |
\===========================/
`

	if output != shouldBeString {
		t.Errorf("Expected things to be drawn as %v, but got %v.", output, shouldBeString)
	}
}
예제 #2
0
파일: base.go 프로젝트: brain-geek/fivez
func StartGameUI() {
	fmt.Println("Game goes here")

	game := core.NewGame()

	draw(game)

mainloop:
	for {

		loss, _ := game.HaveLost()

		if loss {
			fmt.Println("Thank you for playing!")
			fmt.Printf("Your score is %d.", game.GetScore())
			fmt.Println("")

			break mainloop
		}

		charCode, controlCode, _ := getChar()

		switch controlCode {
		case 0: //KeyEsc:
			if (charCode == 27 /* ESC */) || (charCode == 113 /* q */) || (charCode == 3 /* ctrl-c */) {
				fmt.Println("Thank you for playing!")
				break mainloop
			}
		case 37: //KeyArrowLeft:
			if game.Move(core.LEFT) == nil { // no errors means move was OK
				game.SpawnVertical()
			}
		case 39: //KeyArrowRight:
			if game.Move(core.RIGHT) == nil { // no errors means move was OK
				game.SpawnVertical()
			}
		case 38: //KeyArrowUp:
			if game.Move(core.UP) == nil { // no errors means move was OK
				game.SpawnVertical()
			}
		case 40: //KeyArrowDown:
			if game.Move(core.DOWN) == nil { // no errors means move was OK
				game.SpawnVertical()
			}
		default:
		}

		// Clearing screen
		fmt.Print("\033[H\033[2J")
		draw(game)
	}
}
예제 #3
0
파일: fivez.go 프로젝트: brain-geek/fivez
func handleRequest(conn net.Conn) {
	defer conn.Close()

	game := core.NewGame()
	conn.Write([]byte("To play - use commands 'up', 'down', 'left', 'right' to move.\r\n"))
	conn.Write([]byte("To quit - type 'quit' and press Enter.\r\n"))
	conn.Write(DrawField(game))

	for {
		buf := make([]byte, 1024)

		_, err := conn.Read(buf)

		if err == io.EOF {
			return
		}

		if err != nil {
			fmt.Println("Error reading:", err.Error())
		}

		command := strings.TrimSpace(string(buf))
		command = strings.Trim(command, "\x00")
		command = strings.Trim(command, "\n")
		command = strings.Trim(command, "\r")

		switch command {
		case "up":
			if game.Move(core.UP) == nil {
				game.SpawnVertical()
			} else {
				conn.Write([]byte("Move failed.\r\n"))
				conn.Write(DrawField(game))
			}
		case "down":
			if game.Move(core.DOWN) == nil {
				game.SpawnVertical()
			} else {
				conn.Write([]byte("Move failed.\r\n"))
				conn.Write(DrawField(game))
			}
		case "left":
			if game.Move(core.LEFT) == nil {
				game.SpawnVertical()
			} else {
				conn.Write([]byte("Move failed.\r\n"))
				conn.Write(DrawField(game))
			}
		case "right":
			if game.Move(core.RIGHT) == nil {
				game.SpawnVertical()
			} else {
				conn.Write([]byte("Move failed.\r\n"))
				conn.Write(DrawField(game))
			}
		case "quit":
			conn.Write([]byte(fmt.Sprintf("Your score is %d, re-connect to play again!\n", game.GetScore())))
			conn.Close()
			return
		default:
			conn.Write([]byte("Unknown command. Please use 'up', 'down', 'left' or 'right' to make move or 'quit' to stop game.\n\r"))
		}

		conn.Write(DrawField(game))

		loss, _ := game.HaveLost()

		if loss {
			conn.Write([]byte(fmt.Sprintf("Game has ended. Your score is %d, re-connect to play again!\n", game.GetScore())))
			conn.Close()
		}
	}
}