// Intended to run as a goroutine. // Keeps track of the state of a single game, recieving and sending moves through the appropriate channel. func game() { board := &engine.Board{Turn: 1} board.SetUpPieces() url := fmt.Sprintf("http://localhost%s", PORT) cmd := exec.Command("open", url) if _, err := cmd.Output(); err != nil { panic(err) } rand.Seed(time.Now().UTC().UnixNano()) for { select { case oppmove := <-incmoves: for _, p := range board.Board { if p.Position.X == oppmove.Begin.X && p.Position.Y == oppmove.Begin.Y { oppmove.Piece = p.Name break } } board.ForceMove(oppmove) if LOG { fmt.Println(oppmove.ToString()) board.PrintBoard() } var mymove *engine.Move if moves, ok := search.Book[board.ToFen()]; ok { mymove = stringToMove(moves[rand.Intn(len(moves))]) } else { if m := search.AlphaBeta(board, 4, search.BLACKWIN, search.WHITEWIN); m != nil { mymove = m } else { quit <- 1 break } } board.ForceMove(mymove) outmoves <- mymove if LOG { fmt.Println(mymove.ToString()) board.PrintBoard() } case <-quit: board.SetUpPieces() board.Turn = 1 } } }