Skip to content

Commit 74cbcc0

Browse files
committed
jump over occupied intersection if shift is pressed
1 parent e878ba4 commit 74cbcc0

File tree

2 files changed

+59
-23
lines changed

2 files changed

+59
-23
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The Game of Go written in Go
1515
* `q` - quit
1616
* `` - save game
1717
* `p` - place stone
18+
* if `shift` is pressed the navigation will jump to the next empty intersection
1819
* these can be changed in the `config.json` file
1920
* only supports 9x9 boards
2021
* the GUI is terminal-based

main.go

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package main
22

33
import (
4-
"fmt"
5-
"log"
6-
74
"encoding/json"
8-
"os"
5+
"fmt"
96
"github.com/RubikNube/GoInGo/cmd/game"
107
"github.com/jroimartin/gocui"
8+
"log"
9+
"os"
10+
"unicode"
1111
)
1212

1313
type Config struct {
@@ -46,7 +46,7 @@ func layout(g *gocui.Gui) error {
4646
return err
4747
}
4848
v.Wrap = false
49-
fmt.Fprintf(v, "Move (%s/%s/%s/%s), %s to place stone, %s to quit",keybindings["moveLeft"], keybindings["moveDown"], keybindings["moveUp"], keybindings["moveRight"], keybindings["placeStone"], keybindings["quit"])
49+
fmt.Fprintf(v, "Move (%s/%s/%s/%s), %s to place stone, %s to quit", keybindings["moveLeft"], keybindings["moveDown"], keybindings["moveUp"], keybindings["moveRight"], keybindings["placeStone"], keybindings["quit"])
5050
}
5151
// Always redraw board
5252
if v, err := g.View("board"); err == nil {
@@ -56,21 +56,41 @@ func layout(g *gocui.Gui) error {
5656
return nil
5757
}
5858

59-
func moveCursor(dRow, dCol int) func(*gocui.Gui, *gocui.View) error {
59+
func moveCursor(dRow, dCol int, jumpOverOccupied bool) func(*gocui.Gui, *gocui.View) error {
6060
return func(g *gocui.Gui, v *gocui.View) error {
61-
cursorRow += dRow
62-
cursorCol += dCol
63-
if cursorRow < 0 {
64-
cursorRow = 0
65-
}
66-
if cursorRow > 8 {
67-
cursorRow = 8
68-
}
69-
if cursorCol < 0 {
70-
cursorCol = 0
71-
}
72-
if cursorCol > 8 {
73-
cursorCol = 8
61+
nextRow, nextCol := cursorRow, cursorCol
62+
for {
63+
nextRow += dRow
64+
nextCol += dCol
65+
if nextRow < 0 {
66+
nextRow = 0
67+
break
68+
}
69+
if nextRow > 8 {
70+
nextRow = 8
71+
break
72+
}
73+
if nextCol < 0 {
74+
nextCol = 0
75+
break
76+
}
77+
if nextCol > 8 {
78+
nextCol = 8
79+
break
80+
}
81+
if jumpOverOccupied {
82+
if gui.Grid[nextRow][nextCol] == game.Empty {
83+
cursorRow, cursorCol = nextRow, nextCol
84+
break
85+
}
86+
// If we hit the edge and still not empty, stop
87+
if (dRow != 0 && (nextRow == 0 || nextRow == 8)) || (dCol != 0 && (nextCol == 0 || nextCol == 8)) {
88+
break
89+
}
90+
} else {
91+
cursorRow, cursorCol = nextRow, nextCol
92+
break
93+
}
7494
}
7595
return nil
7696
}
@@ -128,18 +148,33 @@ func main() {
128148
placeKey = []rune(placeStoneKey)[0]
129149
}
130150

131-
if err := g.SetKeybinding("", moveLeftKey, gocui.ModNone, moveCursor(0, -1)); err != nil {
151+
// Lowercase: move regardless of occupation
152+
if err := g.SetKeybinding("", moveLeftKey, gocui.ModNone, moveCursor(0, -1, false)); err != nil {
132153
log.Panicln(err)
133154
}
134-
if err := g.SetKeybinding("", moveRightKey, gocui.ModNone, moveCursor(0, 1)); err != nil {
155+
if err := g.SetKeybinding("", moveRightKey, gocui.ModNone, moveCursor(0, 1, false)); err != nil {
135156
log.Panicln(err)
136157
}
137-
if err := g.SetKeybinding("", moveUpKey, gocui.ModNone, moveCursor(-1, 0)); err != nil {
158+
if err := g.SetKeybinding("", moveUpKey, gocui.ModNone, moveCursor(-1, 0, false)); err != nil {
138159
log.Panicln(err)
139160
}
140-
if err := g.SetKeybinding("", moveDownKey, gocui.ModNone, moveCursor(1, 0)); err != nil {
161+
if err := g.SetKeybinding("", moveDownKey, gocui.ModNone, moveCursor(1, 0, false)); err != nil {
141162
log.Panicln(err)
142163
}
164+
// Uppercase: jump over occupied intersections (Shift+key)
165+
if err := g.SetKeybinding("", rune(unicode.ToUpper(moveLeftKey)), gocui.ModNone, moveCursor(0, -1, true)); err != nil {
166+
log.Panicln(err)
167+
}
168+
if err := g.SetKeybinding("", rune(unicode.ToUpper(moveRightKey)), gocui.ModNone, moveCursor(0, 1, true)); err != nil {
169+
log.Panicln(err)
170+
}
171+
if err := g.SetKeybinding("", rune(unicode.ToUpper(moveUpKey)), gocui.ModNone, moveCursor(-1, 0, true)); err != nil {
172+
log.Panicln(err)
173+
}
174+
if err := g.SetKeybinding("", rune(unicode.ToUpper(moveDownKey)), gocui.ModNone, moveCursor(1, 0, true)); err != nil {
175+
log.Panicln(err)
176+
}
177+
143178
if err := g.SetKeybinding("", placeKey, gocui.ModNone, placeStone); err != nil {
144179
log.Panicln(err)
145180
}

0 commit comments

Comments
 (0)