Rock Paper Scissors AI in GO

Abish Pius
3 min readMar 7, 2024
Gopher GOes no where

To begin, let’s set up our environment and define the basic structures and functions needed for our game. We’ll have two players: the user and the CPU. Each player will have a name, a set of moves they’ve made, and their current choice for the next move. We’ll also define the moves: Rock, Paper, and Scissors.

// Define constants for moves
const (
Rock = 1
Paper = 2
Scissors = 3
)

// Define a struct for a move
type Move struct {
value int
count int
}
// Define a struct for a player
type Player struct {
name string
moves []Move
choice int
wins int
losses int
draws int
}

Implementing the Game Logic

With our basic setup in place, let’s implement the core logic of the game. We’ll prompt the user to make a move, generate a move for the CPU based on a simple AI strategy, compare the moves, and determine the winner.

func main() {
// Initialize players
user := Player{name: "User"}
cpu := Player{name: "CPU"}
rand.Seed(time.Now().UnixNano())

// Main game loop
for {
// Prompt user for move
fmt.Println("\nChoose your move:")
fmt.Println("1. Rock")
fmt.Println("2. Paper")
fmt.Println("3. Scissors")
fmt.Println("0. Exit")
var userChoice int
fmt.Scanf("%d", &userChoice)…

--

--

Abish Pius

Data Science Professional, Python Enthusiast, turned LLM Engineer