Skip to content

Commit

Permalink
fix tabs to spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
danicat committed Apr 11, 2019
1 parent fe6fb30 commit 2781392
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 182 deletions.
82 changes: 41 additions & 41 deletions step01/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ All that comes together as the code below:

```go
func loadMaze() error {
f, err := os.Open("maze01.txt")
if err != nil {
return err
}
f, err := os.Open("maze01.txt")
if err != nil {
return err
}
defer f.Close()

scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
maze = append(maze, line)
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
maze = append(maze, line)
}

return nil
return nil
}

var maze []string
Expand Down Expand Up @@ -87,12 +87,12 @@ In the case above, Go automatically infers the type for both `f` and `err` varia
When a function returns an error it is a common patter to check the error immediately afterwards:

```go
f, err := os.Open("maze01.txt")
if err != nil {
f, err := os.Open("maze01.txt")
if err != nil {
// do something with err
log.Printf("...")
return
}
}
```

Note: It is a good practice to keep the "happy path" aligned to the left, and the sad path to the right (ie: terminating the function early).
Expand Down Expand Up @@ -129,11 +129,11 @@ func loadMaze() error {
The next part of the code just reads the file line by line and append it to the maze slice:

```go
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
maze = append(maze, line)
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
maze = append(maze, line)
}
```

A scanner is a very convenient way to read a file. `scanner.Scan()` will return true while there is something to be read from the file, and `scanner.Text()` will return the next line of input.
Expand All @@ -149,9 +149,9 @@ One way to do that is to iterate over each entry in the `maze` slice and print i
```go

func printScreen() {
for _, line := range maze {
fmt.Println(line)
}
for _, line := range maze {
fmt.Println(line)
}
}
```

Expand All @@ -175,33 +175,33 @@ Now that we have both a `loadMaze` and a `printScreen` function, we should updat

```go
func main() {
// initialize game

// load resources
err := loadMaze()
if err != nil {
log.Printf("Error loading maze: %v\n", err)
return
}

// game loop
for {
// update screen
// initialize game

// load resources
err := loadMaze()
if err != nil {
log.Printf("Error loading maze: %v\n", err)
return
}

// game loop
for {
// update screen
printScreen()

// process input
// process input

// process movement
// process movement

// process collisions
// process collisions

// check game over
// check game over

// Temp: break infinite loop
break
// Temp: break infinite loop
break

// repeat
}
// repeat
}
}
```

Expand Down
68 changes: 34 additions & 34 deletions step02/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ Here is the definition of our `init`:

```go
func init() {
cbTerm := exec.Command("/bin/stty", "cbreak", "-echo")
cbTerm.Stdin = os.Stdin
cbTerm := exec.Command("/bin/stty", "cbreak", "-echo")
cbTerm.Stdin = os.Stdin

err := cbTerm.Run()
if err != nil {
log.Fatalf("Unable to activate cbreak mode terminal: %v\n", err)
}
err := cbTerm.Run()
if err != nil {
log.Fatalf("Unable to activate cbreak mode terminal: %v\n", err)
}
}
```

Expand All @@ -69,24 +69,24 @@ Restoring the cooked mode is a pretty straightfoward process. It is the same as

```go
func cleanup() {
cookedTerm := exec.Command("/bin/stty", "-cbreak", "echo")
cookedTerm.Stdin = os.Stdin
cookedTerm := exec.Command("/bin/stty", "-cbreak", "echo")
cookedTerm.Stdin = os.Stdin

err := cookedTerm.Run()
if err != nil {
log.Fatalf("Unable to activate cooked mode terminal: %v\n", err)
}
err := cookedTerm.Run()
if err != nil {
log.Fatalf("Unable to activate cooked mode terminal: %v\n", err)
}
}
```

Note that this `cleanup` function doesn't have a special meaning in Go like the `init` one, so we must explicitly call it in our `main` function. We can either call it at the end of the function or use the `defer` statement as shown below:

```go
func main() {
// initialize game
defer cleanup()
// initialize game
defer cleanup()

// load resources
// load resources
// ...
```
Expand All @@ -98,18 +98,18 @@ The `os.Stdin.Read` returns two values: the number of bytes read and an error va
```go
func readInput() (string, error) {
buffer := make([]byte, 100)
buffer := make([]byte, 100)

cnt, err := os.Stdin.Read(buffer)
if err != nil {
return "", err
}
cnt, err := os.Stdin.Read(buffer)
if err != nil {
return "", err
}

if cnt == 1 && buffer[0] == 0x1b {
return "ESC", nil
}
if cnt == 1 && buffer[0] == 0x1b {
return "ESC", nil
}

return "", nil
return "", nil
}
```
Expand All @@ -135,16 +135,16 @@ Now it's time to update the game loop to have the `readInput` function called ev
// process input
input, err := readInput()
if err != nil {
log.Printf("Error reading input: %v", err)
break
log.Printf("Error reading input: %v", err)
break
}
```
Finally, we can get rid of that permanent `break` statement and start testing for the "ESC" key press.
```go
if input == "ESC" {
break
break
}
```
Expand All @@ -154,12 +154,12 @@ Since we now have a proper game loop, we need to clear the screen after each loo
```go
func clearScreen() {
fmt.Printf("\x1b[2J")
moveCursor(0, 0)
fmt.Printf("\x1b[2J")
moveCursor(0, 0)
}

func moveCursor(row, col int) {
fmt.Printf("\x1b[%d;%df", row+1, col+1)
fmt.Printf("\x1b[%d;%df", row+1, col+1)
}
```
Expand All @@ -171,10 +171,10 @@ We will update the printScreen function to call clearScreen before printing, so
```go
func printScreen() {
clearScreen()
for _, line := range maze {
fmt.Println(line)
}
clearScreen()
for _, line := range maze {
fmt.Println(line)
}
}
```
Expand Down
Loading

0 comments on commit 2781392

Please sign in to comment.