| Crates.io | reversi_client_rust |
| lib.rs | reversi_client_rust |
| version | 0.2.0 |
| created_at | 2025-02-08 06:07:28.650928+00 |
| updated_at | 2025-02-14 22:22:58.539061+00 |
| description | A client for the Reversi game server used in BYU CS 470 |
| homepage | https://github.com/commanderjcc/reversi_client_rust |
| repository | https://github.com/commanderjcc/reversi_client_rust |
| max_upload_size | |
| id | 1547803 |
| size | 68,960 |
This is a Rust library for a Reversi game client, specifically designed for BYU CS 470. It includes the ReversiClient struct, the GameState struct, and the ReversiStrategy trait, along with methods to manage game state and make moves.
Note, that this library is not a complete implementation of the Reversi game. It is designed to be used as a starting point for building a Reversi agent.
No support is provided or guaranteed by the TAs for anything here. Only use this if you know Rust, want to learn Rust, and are a strong programmer already. Pull requests are welcome.
use reversi_client::{ReversiClient, RandomStrategy, ReversiError};
fn main() -> Result<(), ReversiError> {
let server_address = "127.0.0.1"; // Replace with your server address
let player_number = 2; // Set the player number (1 or 2)
let strategy = RandomStrategy; // Replace with your own strategy
let mut client = ReversiClient::connect(server_address, player_number, strategy)?;
client.run()?;
Ok(())
}
The client struct manages the connection to the server, game state, and applies the given strategy whenever a move is needed.
pub struct ReversiClient<S: ReversiStrategy> {
stream: TcpStream,
player_number: i8,
strategy: S,
game_minutes: f32,
board: [[i8; 8]; 8],
}
| Field Name | Type | Description |
|---|
stream | TcpStream | The TCP stream used to communicate with the server.
player_number | i8 | The player number (1 or 2).
strategy | ReversiStrategy | The strategy to use for choosing moves.
game_minutes | f32 | The number of minutes the player has to make their moves. This should be updated by the server on each turn.
board | [[i8; 8]; 8] | The current board state. Each cell is either 0 (empty), 1, or 2.
ReversiClient::connect() - Connects to the server and initializes the game state, returns a client struct.
| Parameter Name | Type | Description |
|---|
server_address:| &str | The ip address of the server, without the port. Port is assumed ot be 3333 + player_number.
player_number:| i8 | The player number (1 or 2).
strategy: | ReversiStrategy | The strategy to use for choosing moves.
ReversiClient::run() - Starts the game loop, handling incoming messages and making moves according to the strategy.
ReversiClient::get_valid_moves() - Returns a vector of valid moves for a given player and board state.
| Parameter Name | Type | Description |
|---|
board | &[[i8; 8]; 8] | The current board state. Each cell is either 0 (empty), 1, or 2. Passed so that you can get valid moves for any player or position.
player | i8 | The number of the player that is making the move.
This trait defines the strategy for choosing moves in the game. You can implement your own strategy by creating a struct that implements this trait.
pub trait ReversiStrategy {
fn choose_move(&self, valid_moves: &[(i8, i8)]) -> (i8, i8);
}
You can use the &self parameter of the choose_move() method to access the fields of your client and calculate the best move based on your own strategy.
For example consider the following strategy that chooses a random valid move:
pub struct RandomStrategy;
impl ReversiStrategy for RandomStrategy {
fn choose_move(&self, valid_moves: &[(i8, i8)]) -> (i8, i8) {
let mut rng = rand::thread_rng();
valid_moves[rng.gen_range(0..valid_moves.len())]
}
}