TIC-TAC-TOE in RUST

In this blog, we will code for tic-tac-toe program in rust

JSP
3 min readAug 20, 2024

Before moving straight into coding, let me share a fact that “Tic-Tac-Toe” can be found in Egypt with remnants (refers to small remaining parts or traces of something that was once larger or more significant) of 3x3 boards on roofing tiles, around 1300 BCE.

There are exactly 46,080 unique possible games in tic-tac-toe. Out of these, 91 unique games end in a win for the first player, 44 end in a win for the second player, and the rest end in a draw. Even a 3x3 board has these much of possibilities, then chess contains 10¹²⁰ and Go contains 10¹⁷² possible game scenarios.

pixel art of tic tac toe game screen in retro styled monitor — DALL E 3

Creating the project

To create a project in rust, open the terminal and move to your desired directory and enter the below cargo to create a project

cargo new tic-tac-toe

This command creates the following files and directories. The main.rs file where we will type the code which can be found under src/ folder.

tic-tac-toe/
|---> src/
| |---> main.rs
|---> .gitignore
|---> Cargo.lock
|---> Cargo.toml

Start coding

In the following coding snippets, I used very basic implementation of program and I tried to explain the program at my best. So let’s dive into the 100 lines of rusty code.

Rust programming language, starts executing the code from the main function in the main.rs file. The entire game is encircled in this function.

Main functionality — play_game()

The play_game() function displays the board and gets the player input and then it checks whether the player wins or not. If the player wins, it ends the game or else if all the possible place is marked then the game automatically ends.

Display Board

The display_board() function contains only print statement that prints the # board shape and elements in the board : Vector<String>

Get user input

The player provides the input in String data type

Checking player made valid mark

This function checks if the player won and does player made valid mark

Condition for checking player win the game

This function checks whether the player is won or not. If the player is won, then it return the bool to the plot_player() function and it returns to the play_game() function, thus printing the winner.

End of the game

The end_game()function will be much easier.

fn end_game(){
println!("Game ended");
}

Execute the code

To compile the code, enter the following cargo command

cargo build

To execute the code, enter the following cargo command

cargo run

Now the game starts

This will be the final output of the Tic Tac Tor program

Outro 👋

Thus in this blog, we created a Tic Tac Toe program for two players mode using Rust programming language. In the upcoming blog posts, I try to implement the Min Max algorithm for playing with an intelligent system. Until then if you found this blog helpful, please share it with your friends. If there is any suggestion on this blog content, you can also contact with me on X.

--

--

JSP

Whatever I find interesting, I tried to explain it here 🤗