Beispiel #1
0
func serveGrid(rw http.ResponseWriter, r *http.Request) {
	defer func() {
		// Basic error handling
		if panicErr := recover(); panicErr != nil {
			err := panicErr.(error)
			log.Print(err)
			rw.WriteHeader(http.StatusInternalServerError)
			fmt.Println(err.Error())
			rw.Write([]byte(err.Error()))
		}
	}()

	ID, IAPlaysFirst, err := parseURLPath(r.URL.Path)

	if err != nil {
		log.Print(err)
		rw.WriteHeader(http.StatusNotFound)
		rw.Write([]byte("Not found"))
		return
	}

	templateData := web.PrepareData(ID)

	if !templateData.IsOver && (!templateData.FirstTurn || IAPlaysFirst) {
		templateData.PlayCPUTurn()
	}

	err = web.Render(rw, templateData)

	if err != nil {
		panic(err)
	}
}
Beispiel #2
0
func TestRender_FullGrid_Draw(t *testing.T) {
	buffer := &bytes.Buffer{}

	templateData := web.PrepareData(502766)

	err := web.Render(buffer, templateData)

	assert.Nil(t, err)

	assert.Equal(t, "\n<html>\n<head>\n    <title>Tic Tac Toe - Human vs CPU</title>\n\n    <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css\">\n    <link href=\"assets/main.css\" rel=\"stylesheet\"/>\n</head>\n<body>\n    <h1>Tic Tac Toe</h1>\n    <p class=\"well well-sm text-center\" >\n        \n            <strong>Game over</strong> : the winner is <a class=\"btn btn-player-nobody\" disabled=\"disabled\">nobody</a>\n        \n    </p>\n    <br/>\n    <div id=\"grid\">\n        \n<div class=\"grid\">\n    \n        \n            \n                \n                    <a class=\"btn btn-player-X\" disabled=\"disabled\">X</a>\n                \n\n        \n            \n                \n                    <a class=\"btn btn-player-X\" disabled=\"disabled\">X</a>\n                \n\n        \n            \n                \n                    <a class=\"btn btn-player-O\" disabled=\"disabled\">O</a>\n                \n\n        \n     <br/>\n    \n        \n            \n                \n                    <a class=\"btn btn-player-O\" disabled=\"disabled\">O</a>\n                \n\n        \n            \n                \n                    <a class=\"btn btn-player-O\" disabled=\"disabled\">O</a>\n                \n\n        \n            \n                \n                    <a class=\"btn btn-player-X\" disabled=\"disabled\">X</a>\n                \n\n        \n     <br/>\n    \n        \n            \n                \n                    <a class=\"btn btn-player-X\" disabled=\"disabled\">X</a>\n                \n\n        \n            \n                \n                    <a class=\"btn btn-player-O\" disabled=\"disabled\">O</a>\n                \n\n        \n            \n                \n                    <a class=\"btn btn-player-X\" disabled=\"disabled\">X</a>\n                \n\n        \n     <br/>\n    \n</div>\n\n    </div>\n    <br/>\n    <p class=\"text-center\"><a class=\"btn btn-success btn-lg\" href=\"./\">New game (you start)</a>&nbsp;<a class=\"btn btn-success btn-lg\" href=\"./cpu.html\">New game (CPU starts)</a></p>\n</body>\n</html>\n", buffer.String())
}
Beispiel #3
0
func TestRender_EmptyGrid(t *testing.T) {
	buffer := &bytes.Buffer{}

	templateData := web.PrepareData(0)

	err := web.Render(buffer, templateData)

	assert.Nil(t, err)

	assert.Equal(t, "\n<html>\n<head>\n    <title>Tic Tac Toe - Human vs CPU</title>\n\n    <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css\">\n    <link href=\"assets/main.css\" rel=\"stylesheet\"/>\n</head>\n<body>\n    <h1>Tic Tac Toe</h1>\n    <p class=\"well well-sm text-center\" >\n        \n            You play as <a class=\"btn btn-player-O\" disabled=\"disabled\">O</a>\n        \n    </p>\n    <br/>\n    <div id=\"grid\">\n        \n<div class=\"grid\">\n    \n        \n            \n                \n                    \n                        <a class=\"btn btn-default\" href=\"./3.html\"></a>\n                    \n                \n\n        \n            \n                \n                    \n                        <a class=\"btn btn-default\" href=\"./129.html\"></a>\n                    \n                \n\n        \n            \n                \n                    \n                        <a class=\"btn btn-default\" href=\"./8193.html\"></a>\n                    \n                \n\n        \n     <br/>\n    \n        \n            \n                \n                    \n                        <a class=\"btn btn-default\" href=\"./9.html\"></a>\n                    \n                \n\n        \n            \n                \n                    \n                        <a class=\"btn btn-default\" href=\"./513.html\"></a>\n                    \n                \n\n        \n            \n                \n                    \n                        <a class=\"btn btn-default\" href=\"./32769.html\"></a>\n                    \n                \n\n        \n     <br/>\n    \n        \n            \n                \n                    \n                        <a class=\"btn btn-default\" href=\"./33.html\"></a>\n                    \n                \n\n        \n            \n                \n                    \n                        <a class=\"btn btn-default\" href=\"./2049.html\"></a>\n                    \n                \n\n        \n            \n                \n                    \n                        <a class=\"btn btn-default\" href=\"./131073.html\"></a>\n                    \n                \n\n        \n     <br/>\n    \n</div>\n\n    </div>\n    <br/>\n    <p class=\"text-center\"><a class=\"btn btn-success btn-lg\" href=\"./\">New game (you start)</a>&nbsp;<a class=\"btn btn-success btn-lg\" href=\"./cpu.html\">New game (CPU starts)</a></p>\n</body>\n</html>\n", buffer.String())
}